xref: /aosp_15_r20/external/grpc-grpc/src/cpp/ext/gcp/observability_config.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 // Copyright 2022 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef GRPC_SRC_CPP_EXT_GCP_OBSERVABILITY_CONFIG_H
18 #define GRPC_SRC_CPP_EXT_GCP_OBSERVABILITY_CONFIG_H
19 
20 #include <grpc/support/port_platform.h>
21 
22 #include <stdint.h>
23 
24 #include <map>
25 #include <string>
26 #include <vector>
27 
28 #include "absl/status/statusor.h"
29 #include "absl/strings/string_view.h"
30 #include "absl/types/optional.h"
31 
32 #include "src/core/lib/gprpp/validation_errors.h"
33 #include "src/core/lib/json/json.h"
34 #include "src/core/lib/json/json_args.h"
35 #include "src/core/lib/json/json_object_loader.h"
36 
37 namespace grpc {
38 namespace internal {
39 
40 struct GcpObservabilityConfig {
41   struct CloudLogging {
42     struct RpcEventConfiguration {
43       struct ParsedMethod {
44         absl::string_view service;  // backed by methods
45         absl::string_view method;   // backed by methods
46       };
47       std::vector<std::string> qualified_methods;
48       std::vector<ParsedMethod> parsed_methods;
49       bool exclude = false;
50       uint32_t max_metadata_bytes = 0;
51       uint32_t max_message_bytes = 0;
52 
53       static const grpc_core::JsonLoaderInterface* JsonLoader(
54           const grpc_core::JsonArgs&);
55 
56       void JsonPostLoad(const grpc_core::Json& json,
57                         const grpc_core::JsonArgs& args,
58                         grpc_core::ValidationErrors* errors);
59     };
60 
61     std::vector<RpcEventConfiguration> client_rpc_events;
62     std::vector<RpcEventConfiguration> server_rpc_events;
63 
JsonLoaderGcpObservabilityConfig::CloudLogging64     static const grpc_core::JsonLoaderInterface* JsonLoader(
65         const grpc_core::JsonArgs&) {
66       static const auto* loader =
67           grpc_core::JsonObjectLoader<CloudLogging>()
68               .OptionalField("client_rpc_events",
69                              &CloudLogging::client_rpc_events)
70               .OptionalField("server_rpc_events",
71                              &CloudLogging::server_rpc_events)
72               .Finish();
73       return loader;
74     }
75   };
76 
77   struct CloudMonitoring {
JsonLoaderGcpObservabilityConfig::CloudMonitoring78     static const grpc_core::JsonLoaderInterface* JsonLoader(
79         const grpc_core::JsonArgs&) {
80       static const auto* loader =
81           grpc_core::JsonObjectLoader<CloudMonitoring>().Finish();
82       return loader;
83     }
84   };
85 
86   struct CloudTrace {
CloudTraceGcpObservabilityConfig::CloudTrace87     CloudTrace() : sampling_rate(0) {}
88     float sampling_rate;
89 
JsonLoaderGcpObservabilityConfig::CloudTrace90     static const grpc_core::JsonLoaderInterface* JsonLoader(
91         const grpc_core::JsonArgs&) {
92       static const auto* loader =
93           grpc_core::JsonObjectLoader<CloudTrace>()
94               .OptionalField("sampling_rate", &CloudTrace::sampling_rate)
95               .Finish();
96       return loader;
97     }
98   };
99 
100   absl::optional<CloudLogging> cloud_logging;
101   absl::optional<CloudMonitoring> cloud_monitoring;
102   absl::optional<CloudTrace> cloud_trace;
103   std::string project_id;
104   std::map<std::string, std::string> labels;
105 
JsonLoaderGcpObservabilityConfig106   static const grpc_core::JsonLoaderInterface* JsonLoader(
107       const grpc_core::JsonArgs&) {
108     static const auto* loader =
109         grpc_core::JsonObjectLoader<GcpObservabilityConfig>()
110             .OptionalField("cloud_logging",
111                            &GcpObservabilityConfig::cloud_logging)
112             .OptionalField("cloud_monitoring",
113                            &GcpObservabilityConfig::cloud_monitoring)
114             .OptionalField("cloud_trace", &GcpObservabilityConfig::cloud_trace)
115             .OptionalField("project_id", &GcpObservabilityConfig::project_id)
116             .OptionalField("labels", &GcpObservabilityConfig::labels)
117             .Finish();
118     return loader;
119   }
120 
121   // Tries to load the contents of GcpObservabilityConfig from the file located
122   // by the value of environment variable `GRPC_GCP_OBSERVABILITY_CONFIG_FILE`.
123   // If `GRPC_GCP_OBSERVABILITY_CONFIG_FILE` is unset, falls back to
124   // `GRPC_GCP_OBSERVABILITY_CONFIG`.
125   static absl::StatusOr<GcpObservabilityConfig> ReadFromEnv();
126 };
127 
128 }  // namespace internal
129 }  // namespace grpc
130 
131 #endif  // GRPC_SRC_CPP_EXT_GCP_OBSERVABILITY_CONFIG_H
132