Debian Codegraph @

src/cpp/ext/gcp/observability_config.h

raw
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_INTERNAL_CPP_EXT_GCP_OBSERVABILITY_GCP_OBSERVABILITY_CONFIG_H
18 #define GRPC_INTERNAL_CPP_EXT_GCP_OBSERVABILITY_GCP_OBSERVABILITY_CONFIG_H
19
20 #include <stdint.h>
21
22 #include <string>
23 #include <vector>
24
25 #include "absl/status/statusor.h"
26 #include "absl/strings/string_view.h"
27 #include "absl/types/optional.h"
28
29 #include "src/core/lib/gprpp/validation_errors.h"
30 #include "src/core/lib/json/json.h"
31 #include "src/core/lib/json/json_args.h"
32 #include "src/core/lib/json/json_object_loader.h"
33
34 namespace grpc {
35 namespace internal {
36
37 struct GcpObservabilityConfig {
38 struct CloudLogging {
39 struct RpcEventConfiguration {
40 struct ParsedMethod {
41 absl::string_view service; // backed by methods
42 absl::string_view method; // backed by methods
43 };
44 std::vector<std::string> qualified_methods;
45 std::vector<ParsedMethod> parsed_methods;
46 bool exclude = false;
47 uint32_t max_metadata_bytes = 0;
48 uint32_t max_message_bytes = 0;
49
50 static const grpc_core::JsonLoaderInterface* JsonLoader(
51 const grpc_core::JsonArgs&);
52
53 void JsonPostLoad(const grpc_core::Json& json,
54 const grpc_core::JsonArgs& args,
55 grpc_core::ValidationErrors* errors);
56 };
57
58 std::vector<RpcEventConfiguration> client_rpc_events;
59 std::vector<RpcEventConfiguration> server_rpc_events;
60
61 static const grpc_core::JsonLoaderInterface* JsonLoader(
62 const grpc_core::JsonArgs&) {
63 static const auto* loader =
64 grpc_core::JsonObjectLoader<CloudLogging>()
65 .OptionalField("client_rpc_events",
66 &CloudLogging::client_rpc_events)
67 .OptionalField("server_rpc_events",
68 &CloudLogging::server_rpc_events)
69 .Finish();
70 return loader;
71 }
72 };
73
74 struct CloudMonitoring {
75 static const grpc_core::JsonLoaderInterface* JsonLoader(
76 const grpc_core::JsonArgs&) {
77 static const auto* loader =
78 grpc_core::JsonObjectLoader<CloudMonitoring>().Finish();
79 return loader;
80 }
81 };
82
83 struct CloudTrace {
84 float sampling_rate = 0;
85
86 static const grpc_core::JsonLoaderInterface* JsonLoader(
87 const grpc_core::JsonArgs&) {
88 static const auto* loader =
89 grpc_core::JsonObjectLoader<CloudTrace>()
90 .OptionalField("sampling_rate", &CloudTrace::sampling_rate)
91 .Finish();
92 return loader;
93 }
94 };
95
96 absl::optional<CloudLogging> cloud_logging;
97 absl::optional<CloudMonitoring> cloud_monitoring;
98 absl::optional<CloudTrace> cloud_trace;
99 std::string project_id;
100
101 static const grpc_core::JsonLoaderInterface* JsonLoader(
102 const grpc_core::JsonArgs&) {
103 static const auto* loader =
104 grpc_core::JsonObjectLoader<GcpObservabilityConfig>()
105 .OptionalField("cloud_logging",
106 &GcpObservabilityConfig::cloud_logging)
107 .OptionalField("cloud_monitoring",
108 &GcpObservabilityConfig::cloud_monitoring)
109 .OptionalField("cloud_trace", &GcpObservabilityConfig::cloud_trace)
110 .OptionalField("project_id", &GcpObservabilityConfig::project_id)
111 .Finish();
112 return loader;
113 }
114
115 // Tries to load the contents of GcpObservabilityConfig from the file located
116 // by the value of environment variable `GRPC_OBSERVABILITY_CONFIG_FILE`. If
117 // `GRPC_OBSERVABILITY_CONFIG_FILE` is unset, falls back to
118 // `GRPC_OBSERVABILITY_CONFIG`.
119 static absl::StatusOr<GcpObservabilityConfig> ReadFromEnv();
120 };
121
122 } // namespace internal
123 } // namespace grpc
124
125 #endif // GRPC_INTERNAL_CPP_EXT_GCP_OBSERVABILITY_GCP_OBSERVABILITY_CONFIG_H