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 { 87 float sampling_rate = 0; 88 JsonLoaderGcpObservabilityConfig::CloudTrace89 static const grpc_core::JsonLoaderInterface* JsonLoader( 90 const grpc_core::JsonArgs&) { 91 static const auto* loader = 92 grpc_core::JsonObjectLoader<CloudTrace>() 93 .OptionalField("sampling_rate", &CloudTrace::sampling_rate) 94 .Finish(); 95 return loader; 96 } 97 }; 98 99 absl::optional<CloudLogging> cloud_logging; 100 absl::optional<CloudMonitoring> cloud_monitoring; 101 absl::optional<CloudTrace> cloud_trace; 102 std::string project_id; 103 std::map<std::string, std::string> labels; 104 JsonLoaderGcpObservabilityConfig105 static const grpc_core::JsonLoaderInterface* JsonLoader( 106 const grpc_core::JsonArgs&) { 107 static const auto* loader = 108 grpc_core::JsonObjectLoader<GcpObservabilityConfig>() 109 .OptionalField("cloud_logging", 110 &GcpObservabilityConfig::cloud_logging) 111 .OptionalField("cloud_monitoring", 112 &GcpObservabilityConfig::cloud_monitoring) 113 .OptionalField("cloud_trace", &GcpObservabilityConfig::cloud_trace) 114 .OptionalField("project_id", &GcpObservabilityConfig::project_id) 115 .OptionalField("labels", &GcpObservabilityConfig::labels) 116 .Finish(); 117 return loader; 118 } 119 120 // Tries to load the contents of GcpObservabilityConfig from the file located 121 // by the value of environment variable `GRPC_GCP_OBSERVABILITY_CONFIG_FILE`. 122 // If `GRPC_GCP_OBSERVABILITY_CONFIG_FILE` is unset, falls back to 123 // `GRPC_GCP_OBSERVABILITY_CONFIG`. 124 static absl::StatusOr<GcpObservabilityConfig> ReadFromEnv(); 125 }; 126 127 } // namespace internal 128 } // namespace grpc 129 130 #endif // GRPC_SRC_CPP_EXT_GCP_OBSERVABILITY_CONFIG_H 131