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