| 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 |
#include <grpc/support/port_platform.h> |
| 18 |
|
| 19 |
#include "src/cpp/ext/gcp/observability_config.h" |
| 20 |
|
| 21 |
#include <stddef.h> |
| 22 |
|
| 23 |
#include <algorithm> |
| 24 |
#include <utility> |
| 25 |
|
| 26 |
#include "absl/status/status.h" |
| 27 |
#include "absl/strings/match.h" |
| 28 |
#include "absl/strings/str_cat.h" |
| 29 |
#include "absl/strings/str_split.h" |
| 30 |
#include "absl/strings/string_view.h" |
| 31 |
#include "absl/types/optional.h" |
| 32 |
|
| 33 |
#include <grpc/slice.h> |
| 34 |
#include <grpc/status.h> |
| 35 |
|
| 36 |
#include "src/core/lib/gprpp/env.h" |
| 37 |
#include "src/core/lib/gprpp/status_helper.h" |
| 38 |
#include "src/core/lib/gprpp/validation_errors.h" |
| 39 |
#include "src/core/lib/iomgr/error.h" |
| 40 |
#include "src/core/lib/iomgr/load_file.h" |
| 41 |
#include "src/core/lib/json/json.h" |
| 42 |
#include "src/core/lib/slice/slice_internal.h" |
| 43 |
#include "src/core/lib/transport/error_utils.h" |
| 44 |
|
| 45 |
namespace grpc { |
| 46 |
namespace internal { |
| 47 |
|
| 48 |
namespace { |
| 49 |
|
| 50 |
// Loads the contents of the file pointed by env var |
| 51 |
// GRPC_OBSERVABILITY_CONFIG_FILE. If unset, falls back to the contents of |
| 52 |
// GRPC_OBSERVABILITY_CONFIG. |
| 53 |
absl::StatusOr<std::string> GetGcpObservabilityConfigContents() { |
| 54 |
// First, try GRPC_OBSERVABILITY_CONFIG_FILE |
| 55 |
std::string contents_str; |
| 56 |
auto path = grpc_core::GetEnv("GRPC_OBSERVABILITY_CONFIG_FILE"); |
| 57 |
if (path.has_value()) { |
| 58 |
grpc_slice contents; |
| 59 |
grpc_error_handle error = |
| 60 |
grpc_load_file(path->c_str(), /*add_null_terminator=*/true, &contents); |
| 61 |
if (!error.ok()) { |
| 62 |
return grpc_error_to_absl_status( |
| 63 |
grpc_error_set_int(error, grpc_core::StatusIntProperty::kRpcStatus, |
| 64 |
GRPC_STATUS_FAILED_PRECONDITION)); |
| 65 |
} |
| 66 |
std::string contents_str(grpc_core::StringViewFromSlice(contents)); |
| 67 |
grpc_slice_unref(contents); |
| 68 |
return std::move(contents_str); |
| 69 |
} |
| 70 |
// Next, try GRPC_OBSERVABILITY_CONFIG env var. |
| 71 |
auto env_config = grpc_core::GetEnv("GRPC_OBSERVABILITY_CONFIG"); |
| 72 |
if (env_config.has_value()) { |
| 73 |
return std::move(*env_config); |
| 74 |
} |
| 75 |
// No observability config found. |
| 76 |
return absl::FailedPreconditionError( |
| 77 |
"Environment variables GRPC_OBSERVABILITY_CONFIG_FILE or " |
| 78 |
"GRPC_OBSERVABILITY_CONFIG " |
| 79 |
"not defined"); |
| 80 |
} |
| 81 |
|
| 82 |
// Tries to get the GCP Project ID from environment variables, or returns an |
| 83 |
// empty string if not found. |
| 84 |
std::string GetProjectIdFromGcpEnvVar() { |
| 85 |
// First check GCP_PROEJCT |
| 86 |
absl::optional<std::string> project_id = grpc_core::GetEnv("GCP_PROJECT"); |
| 87 |
if (project_id.has_value() && !project_id->empty()) { |
| 88 |
return project_id.value(); |
| 89 |
} |
| 90 |
// Next, try GCLOUD_PROJECT |
| 91 |
project_id = grpc_core::GetEnv("GCLOUD_PROJECT"); |
| 92 |
if (project_id.has_value() && !project_id->empty()) { |
| 93 |
return project_id.value(); |
| 94 |
} |
| 95 |
// Lastly, try GOOGLE_CLOUD_PROJECT |
| 96 |
project_id = grpc_core::GetEnv("GOOGLE_CLOUD_PROJECT"); |
| 97 |
if (project_id.has_value() && !project_id->empty()) { |
| 98 |
return project_id.value(); |
| 99 |
} |
| 100 |
return ""; |
| 101 |
} |
| 102 |
|
| 103 |
} // namespace |
| 104 |
|
| 105 |
// |
| 106 |
// GcpObservabilityConfig::CloudLogging::RpcEventConfiguration |
| 107 |
// |
| 108 |
|
| 109 |
const grpc_core::JsonLoaderInterface* |
| 110 |
GcpObservabilityConfig::CloudLogging::RpcEventConfiguration::JsonLoader( |
| 111 |
const grpc_core::JsonArgs&) { |
| 112 |
static const auto* loader = |
| 113 |
grpc_core::JsonObjectLoader<RpcEventConfiguration>() |
| 114 |
.OptionalField("methods", &RpcEventConfiguration::qualified_methods) |
| 115 |
.OptionalField("exclude", &RpcEventConfiguration::exclude) |
| 116 |
.OptionalField("max_metadata_bytes", |
| 117 |
&RpcEventConfiguration::max_metadata_bytes) |
| 118 |
.OptionalField("max_message_bytes", |
| 119 |
&RpcEventConfiguration::max_message_bytes) |
| 120 |
.Finish(); |
| 121 |
return loader; |
| 122 |
} |
| 123 |
|
| 124 |
void GcpObservabilityConfig::CloudLogging::RpcEventConfiguration::JsonPostLoad( |
| 125 |
const grpc_core::Json& /* json */, const grpc_core::JsonArgs& /* args */, |
| 126 |
grpc_core::ValidationErrors* errors) { |
| 127 |
grpc_core::ValidationErrors::ScopedField methods_field(errors, ".methods"); |
| 128 |
parsed_methods.reserve(qualified_methods.size()); |
| 129 |
for (size_t i = 0; i < qualified_methods.size(); ++i) { |
| 130 |
grpc_core::ValidationErrors::ScopedField methods_index( |
| 131 |
errors, absl::StrCat("[", i, "]")); |
| 132 |
std::vector<absl::string_view> parts = |
| 133 |
absl::StrSplit(qualified_methods[i], '/', absl::SkipEmpty()); |
| 134 |
if (parts.size() > 2) { |
| 135 |
errors->AddError("methods[] can have at most a single '/'"); |
| 136 |
continue; |
| 137 |
} else if (parts.empty()) { |
| 138 |
errors->AddError("Empty configuration"); |
| 139 |
continue; |
| 140 |
} else if (parts.size() == 1) { |
| 141 |
if (parts[0] != "*") { |
| 142 |
errors->AddError("Illegal methods[] configuration"); |
| 143 |
continue; |
| 144 |
} |
| 145 |
if (exclude) { |
| 146 |
errors->AddError( |
| 147 |
"Wildcard match '*' not allowed when 'exclude' is set"); |
| 148 |
continue; |
| 149 |
} |
| 150 |
parsed_methods.push_back(ParsedMethod{parts[0], ""}); |
| 151 |
} else { |
| 152 |
// parts.size() == 2 |
| 153 |
if (absl::StrContains(parts[0], '*')) { |
| 154 |
errors->AddError("Configuration of type '*/method' not allowed"); |
| 155 |
continue; |
| 156 |
} |
| 157 |
if (absl::StrContains(parts[1], '*') && parts[1].size() != 1) { |
| 158 |
errors->AddError("Wildcard specified for method in incorrect manner"); |
| 159 |
continue; |
| 160 |
} |
| 161 |
parsed_methods.push_back(ParsedMethod{parts[0], parts[1]}); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
absl::StatusOr<GcpObservabilityConfig> GcpObservabilityConfig::ReadFromEnv() { |
| 167 |
auto config_contents = GetGcpObservabilityConfigContents(); |
| 168 |
if (!config_contents.ok()) { |
| 169 |
return config_contents.status(); |
| 170 |
} |
| 171 |
auto config_json = grpc_core::Json::Parse(*config_contents); |
| 172 |
if (!config_json.ok()) { |
| 173 |
return config_json.status(); |
| 174 |
} |
| 175 |
auto config = grpc_core::LoadFromJson<GcpObservabilityConfig>(*config_json); |
| 176 |
if (!config.ok()) { |
| 177 |
return config.status(); |
| 178 |
} |
| 179 |
if (config->project_id.empty()) { |
| 180 |
// Get project ID from GCP environment variables since project ID was not |
| 181 |
// set it in the GCP observability config. |
| 182 |
config->project_id = GetProjectIdFromGcpEnvVar(); |
| 183 |
if (config->project_id.empty()) { |
| 184 |
// Could not find project ID from GCP environment variables either. |
| 185 |
return absl::FailedPreconditionError("GCP Project ID not found."); |
| 186 |
} |
| 187 |
} |
| 188 |
return config; |
| 189 |
} |
| 190 |
|
| 191 |
} // namespace internal |
| 192 |
} // namespace grpc |