xref: /aosp_15_r20/external/federated-compute/fcp/tracing/tracing_context_utils.h (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 #ifndef FCP_TRACING_TRACING_CONTEXT_UTILS_H_
16*14675a02SAndroid Build Coastguard Worker #define FCP_TRACING_TRACING_CONTEXT_UTILS_H_
17*14675a02SAndroid Build Coastguard Worker 
18*14675a02SAndroid Build Coastguard Worker #include <string>
19*14675a02SAndroid Build Coastguard Worker 
20*14675a02SAndroid Build Coastguard Worker #include "google/protobuf/descriptor.h"
21*14675a02SAndroid Build Coastguard Worker #include "google/protobuf/message.h"
22*14675a02SAndroid Build Coastguard Worker #include "fcp/base/monitoring.h"
23*14675a02SAndroid Build Coastguard Worker #include "fcp/tracing/tracing_traits.h"
24*14675a02SAndroid Build Coastguard Worker #include "flatbuffers/flatbuffers.h"
25*14675a02SAndroid Build Coastguard Worker 
26*14675a02SAndroid Build Coastguard Worker namespace fcp::tracing_internal {
27*14675a02SAndroid Build Coastguard Worker 
28*14675a02SAndroid Build Coastguard Worker constexpr char kContextFieldName[] = "tracing_context";
29*14675a02SAndroid Build Coastguard Worker constexpr char kContextWrongTypeMessage[] =
30*14675a02SAndroid Build Coastguard Worker     "Type of tracing_context field should be bytes or string";
31*14675a02SAndroid Build Coastguard Worker 
32*14675a02SAndroid Build Coastguard Worker // Given a proto message, checks to see if it has a tracing_context field and
33*14675a02SAndroid Build Coastguard Worker // if so, serializes the provided `context` message and sets the field contents
34*14675a02SAndroid Build Coastguard Worker // to the serialized context.
35*14675a02SAndroid Build Coastguard Worker // If no tracing_context field is present on `message` or it is of a type other
36*14675a02SAndroid Build Coastguard Worker // than string or bytes, this will be a no-op.
37*14675a02SAndroid Build Coastguard Worker void SetTracingContextOnMessage(const google::protobuf::Message& context,
38*14675a02SAndroid Build Coastguard Worker                                 google::protobuf::Message& message);
39*14675a02SAndroid Build Coastguard Worker 
40*14675a02SAndroid Build Coastguard Worker // Given a proto `message` which may have a field tracing_context which contains
41*14675a02SAndroid Build Coastguard Worker // a serialized tracing context proto of type ContextT, uses reflection to
42*14675a02SAndroid Build Coastguard Worker // extract and parse the serialized proto to return a ContextT.
43*14675a02SAndroid Build Coastguard Worker // If the proto `message` does not have a tracing_context field, or it is empty,
44*14675a02SAndroid Build Coastguard Worker // returns the default value of ContextT.
45*14675a02SAndroid Build Coastguard Worker // If the tracing_context field is of a type other than string or bytes, this
46*14675a02SAndroid Build Coastguard Worker // will fail.
47*14675a02SAndroid Build Coastguard Worker template <class ContextT>
GetContextFromMessage(const google::protobuf::Message & message)48*14675a02SAndroid Build Coastguard Worker ContextT GetContextFromMessage(const google::protobuf::Message& message) {
49*14675a02SAndroid Build Coastguard Worker   const google::protobuf::FieldDescriptor* field_descriptor =
50*14675a02SAndroid Build Coastguard Worker       message.GetDescriptor()->FindFieldByName(kContextFieldName);
51*14675a02SAndroid Build Coastguard Worker   ContextT context;
52*14675a02SAndroid Build Coastguard Worker   if (field_descriptor == nullptr) {
53*14675a02SAndroid Build Coastguard Worker     return context;
54*14675a02SAndroid Build Coastguard Worker   }
55*14675a02SAndroid Build Coastguard Worker   FCP_CHECK(field_descriptor->type() == google::protobuf::FieldDescriptor::TYPE_BYTES ||
56*14675a02SAndroid Build Coastguard Worker             field_descriptor->type() == google::protobuf::FieldDescriptor::TYPE_STRING)
57*14675a02SAndroid Build Coastguard Worker       << kContextWrongTypeMessage;
58*14675a02SAndroid Build Coastguard Worker   std::string serialized_context =
59*14675a02SAndroid Build Coastguard Worker       message.GetReflection()->GetString(message, field_descriptor);
60*14675a02SAndroid Build Coastguard Worker   if (serialized_context.empty()) {
61*14675a02SAndroid Build Coastguard Worker     return context;
62*14675a02SAndroid Build Coastguard Worker   }
63*14675a02SAndroid Build Coastguard Worker   FCP_CHECK(context.ParseFromString(serialized_context));
64*14675a02SAndroid Build Coastguard Worker 
65*14675a02SAndroid Build Coastguard Worker   return context;
66*14675a02SAndroid Build Coastguard Worker }
67*14675a02SAndroid Build Coastguard Worker 
68*14675a02SAndroid Build Coastguard Worker }  // namespace fcp::tracing_internal
69*14675a02SAndroid Build Coastguard Worker 
70*14675a02SAndroid Build Coastguard Worker #endif  // FCP_TRACING_TRACING_CONTEXT_UTILS_H_
71