xref: /aosp_15_r20/external/federated-compute/fcp/tracing/tracing_context_utils.cc (revision 14675a029014e728ec732f129a32e299b2da0601)
1*14675a02SAndroid Build Coastguard Worker // Copyright 2020 Google LLC
2*14675a02SAndroid Build Coastguard Worker //
3*14675a02SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*14675a02SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*14675a02SAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*14675a02SAndroid Build Coastguard Worker //
7*14675a02SAndroid Build Coastguard Worker //      http://www.apache.org/licenses/LICENSE-2.0
8*14675a02SAndroid Build Coastguard Worker //
9*14675a02SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*14675a02SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*14675a02SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*14675a02SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*14675a02SAndroid Build Coastguard Worker // limitations under the License.
14*14675a02SAndroid Build Coastguard Worker 
15*14675a02SAndroid Build Coastguard Worker #include "fcp/tracing/tracing_context_utils.h"
16*14675a02SAndroid Build Coastguard Worker 
17*14675a02SAndroid Build Coastguard Worker #include <optional>
18*14675a02SAndroid Build Coastguard Worker 
19*14675a02SAndroid Build Coastguard Worker #include "google/protobuf/descriptor.h"
20*14675a02SAndroid Build Coastguard Worker #include "google/protobuf/message.h"
21*14675a02SAndroid Build Coastguard Worker #include "fcp/base/monitoring.h"
22*14675a02SAndroid Build Coastguard Worker #include "flatbuffers/flatbuffers.h"
23*14675a02SAndroid Build Coastguard Worker 
24*14675a02SAndroid Build Coastguard Worker namespace fcp::tracing_internal {
25*14675a02SAndroid Build Coastguard Worker 
SetTracingContextOnMessage(const google::protobuf::Message & context,google::protobuf::Message & message)26*14675a02SAndroid Build Coastguard Worker void SetTracingContextOnMessage(const google::protobuf::Message& context,
27*14675a02SAndroid Build Coastguard Worker                                 google::protobuf::Message& message) {
28*14675a02SAndroid Build Coastguard Worker   const google::protobuf::FieldDescriptor* field_descriptor =
29*14675a02SAndroid Build Coastguard Worker       message.GetDescriptor()->FindFieldByName(kContextFieldName);
30*14675a02SAndroid Build Coastguard Worker   if (field_descriptor == nullptr) {
31*14675a02SAndroid Build Coastguard Worker     return;
32*14675a02SAndroid Build Coastguard Worker   }
33*14675a02SAndroid Build Coastguard Worker   FCP_CHECK(field_descriptor->type() == google::protobuf::FieldDescriptor::TYPE_BYTES ||
34*14675a02SAndroid Build Coastguard Worker             field_descriptor->type() == google::protobuf::FieldDescriptor::TYPE_STRING)
35*14675a02SAndroid Build Coastguard Worker       << kContextWrongTypeMessage;
36*14675a02SAndroid Build Coastguard Worker   message.GetReflection()->SetString(&message, field_descriptor,
37*14675a02SAndroid Build Coastguard Worker                                      context.SerializeAsString());
38*14675a02SAndroid Build Coastguard Worker }
39*14675a02SAndroid Build Coastguard Worker 
40*14675a02SAndroid Build Coastguard Worker }  // namespace fcp::tracing_internal
41