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