GCP Observability: Fix env var names (#32066)

pull/31700/head
Yash Tibrewal 2 years ago committed by GitHub
parent 175c2c946a
commit d5c1dc1667
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      src/cpp/ext/gcp/observability_config.cc
  2. 6
      src/cpp/ext/gcp/observability_config.h
  3. 32
      test/cpp/ext/gcp/observability_config_test.cc
  4. 4
      test/cpp/ext/gcp/observability_test.cc

@ -48,12 +48,12 @@ namespace internal {
namespace {
// Loads the contents of the file pointed by env var
// GRPC_OBSERVABILITY_CONFIG_FILE. If unset, falls back to the contents of
// GRPC_OBSERVABILITY_CONFIG.
// GRPC_GCP_OBSERVABILITY_CONFIG_FILE. If unset, falls back to the contents of
// GRPC_GCP_OBSERVABILITY_CONFIG.
absl::StatusOr<std::string> GetGcpObservabilityConfigContents() {
// First, try GRPC_OBSERVABILITY_CONFIG_FILE
// First, try GRPC_GCP_OBSERVABILITY_CONFIG_FILE
std::string contents_str;
auto path = grpc_core::GetEnv("GRPC_OBSERVABILITY_CONFIG_FILE");
auto path = grpc_core::GetEnv("GRPC_GCP_OBSERVABILITY_CONFIG_FILE");
if (path.has_value()) {
grpc_slice contents;
grpc_error_handle error =
@ -67,15 +67,15 @@ absl::StatusOr<std::string> GetGcpObservabilityConfigContents() {
grpc_slice_unref(contents);
return std::move(contents_str);
}
// Next, try GRPC_OBSERVABILITY_CONFIG env var.
auto env_config = grpc_core::GetEnv("GRPC_OBSERVABILITY_CONFIG");
// Next, try GRPC_GCP_OBSERVABILITY_CONFIG env var.
auto env_config = grpc_core::GetEnv("GRPC_GCP_OBSERVABILITY_CONFIG");
if (env_config.has_value()) {
return std::move(*env_config);
}
// No observability config found.
return absl::FailedPreconditionError(
"Environment variables GRPC_OBSERVABILITY_CONFIG_FILE or "
"GRPC_OBSERVABILITY_CONFIG "
"Environment variables GRPC_GCP_OBSERVABILITY_CONFIG_FILE or "
"GRPC_GCP_OBSERVABILITY_CONFIG "
"not defined");
}

@ -116,9 +116,9 @@ struct GcpObservabilityConfig {
}
// Tries to load the contents of GcpObservabilityConfig from the file located
// by the value of environment variable `GRPC_OBSERVABILITY_CONFIG_FILE`. If
// `GRPC_OBSERVABILITY_CONFIG_FILE` is unset, falls back to
// `GRPC_OBSERVABILITY_CONFIG`.
// by the value of environment variable `GRPC_GCP_OBSERVABILITY_CONFIG_FILE`.
// If `GRPC_GCP_OBSERVABILITY_CONFIG_FILE` is unset, falls back to
// `GRPC_GCP_OBSERVABILITY_CONFIG`.
static absl::StatusOr<GcpObservabilityConfig> ReadFromEnv();
};

@ -247,13 +247,13 @@ TEST(GcpEnvParsingTest, NoEnvironmentVariableSet) {
auto config = GcpObservabilityConfig::ReadFromEnv();
EXPECT_EQ(config.status(),
absl::FailedPreconditionError(
"Environment variables GRPC_OBSERVABILITY_CONFIG_FILE or "
"GRPC_OBSERVABILITY_CONFIG "
"Environment variables GRPC_GCP_OBSERVABILITY_CONFIG_FILE or "
"GRPC_GCP_OBSERVABILITY_CONFIG "
"not defined"));
}
TEST(GcpEnvParsingTest, ConfigFileDoesNotExist) {
grpc_core::SetEnv("GRPC_OBSERVABILITY_CONFIG_FILE",
grpc_core::SetEnv("GRPC_GCP_OBSERVABILITY_CONFIG_FILE",
"/tmp/gcp_observability_config_does_not_exist");
auto config = GcpObservabilityConfig::ReadFromEnv();
@ -261,22 +261,22 @@ TEST(GcpEnvParsingTest, ConfigFileDoesNotExist) {
EXPECT_EQ(config.status(),
absl::FailedPreconditionError("Failed to load file"));
grpc_core::UnsetEnv("GRPC_OBSERVABILITY_CONFIG_FILE");
grpc_core::UnsetEnv("GRPC_GCP_OBSERVABILITY_CONFIG_FILE");
}
TEST(GcpEnvParsingTest, ProjectIdNotSet) {
grpc_core::SetEnv("GRPC_OBSERVABILITY_CONFIG", "{}");
grpc_core::SetEnv("GRPC_GCP_OBSERVABILITY_CONFIG", "{}");
auto config = GcpObservabilityConfig::ReadFromEnv();
EXPECT_EQ(config.status(),
absl::FailedPreconditionError("GCP Project ID not found."));
grpc_core::UnsetEnv("GRPC_OBSERVABILITY_CONFIG");
grpc_core::UnsetEnv("GRPC_GCP_OBSERVABILITY_CONFIG");
grpc_core::CoreConfiguration::Reset();
}
TEST(GcpEnvParsingTest, ProjectIdFromGcpProjectEnvVar) {
grpc_core::SetEnv("GRPC_OBSERVABILITY_CONFIG", "{}");
grpc_core::SetEnv("GRPC_GCP_OBSERVABILITY_CONFIG", "{}");
grpc_core::SetEnv("GCP_PROJECT", "gcp_project");
auto config = GcpObservabilityConfig::ReadFromEnv();
@ -284,12 +284,12 @@ TEST(GcpEnvParsingTest, ProjectIdFromGcpProjectEnvVar) {
EXPECT_EQ(config->project_id, "gcp_project");
grpc_core::UnsetEnv("GCP_PROJECT");
grpc_core::UnsetEnv("GRPC_OBSERVABILITY_CONFIG");
grpc_core::UnsetEnv("GRPC_GCP_OBSERVABILITY_CONFIG");
grpc_core::CoreConfiguration::Reset();
}
TEST(GcpEnvParsingTest, ProjectIdFromGcloudProjectEnvVar) {
grpc_core::SetEnv("GRPC_OBSERVABILITY_CONFIG", "{}");
grpc_core::SetEnv("GRPC_GCP_OBSERVABILITY_CONFIG", "{}");
grpc_core::SetEnv("GCLOUD_PROJECT", "gcloud_project");
auto config = GcpObservabilityConfig::ReadFromEnv();
@ -297,12 +297,12 @@ TEST(GcpEnvParsingTest, ProjectIdFromGcloudProjectEnvVar) {
EXPECT_EQ(config->project_id, "gcloud_project");
grpc_core::UnsetEnv("GCLOUD_PROJECT");
grpc_core::UnsetEnv("GRPC_OBSERVABILITY_CONFIG");
grpc_core::UnsetEnv("GRPC_GCP_OBSERVABILITY_CONFIG");
grpc_core::CoreConfiguration::Reset();
}
TEST(GcpEnvParsingTest, ProjectIdFromGoogleCloudProjectEnvVar) {
grpc_core::SetEnv("GRPC_OBSERVABILITY_CONFIG", "{}");
grpc_core::SetEnv("GRPC_GCP_OBSERVABILITY_CONFIG", "{}");
grpc_core::SetEnv("GOOGLE_CLOUD_PROJECT", "google_cloud_project");
auto config = GcpObservabilityConfig::ReadFromEnv();
@ -310,7 +310,7 @@ TEST(GcpEnvParsingTest, ProjectIdFromGoogleCloudProjectEnvVar) {
EXPECT_EQ(config->project_id, "google_cloud_project");
grpc_core::UnsetEnv("GOOGLE_CLOUD_PROJECT");
grpc_core::UnsetEnv("GRPC_OBSERVABILITY_CONFIG");
grpc_core::UnsetEnv("GRPC_GCP_OBSERVABILITY_CONFIG");
grpc_core::CoreConfiguration::Reset();
}
@ -352,13 +352,13 @@ class EnvParsingTest : public ::testing::TestWithParam<EnvParsingTestType> {
~EnvParsingTest() override {
if (GetParam().config_source() == EnvParsingTestType::ConfigSource::kFile) {
if (tmp_file_name != nullptr) {
grpc_core::UnsetEnv("GRPC_OBSERVABILITY_CONFIG_FILE");
grpc_core::UnsetEnv("GRPC_GCP_OBSERVABILITY_CONFIG_FILE");
remove(tmp_file_name);
gpr_free(tmp_file_name);
}
} else if (GetParam().config_source() ==
EnvParsingTestType::ConfigSource::kEnvVar) {
grpc_core::UnsetEnv("GRPC_OBSERVABILITY_CONFIG");
grpc_core::UnsetEnv("GRPC_GCP_OBSERVABILITY_CONFIG");
}
}
@ -369,10 +369,10 @@ class EnvParsingTest : public ::testing::TestWithParam<EnvParsingTestType> {
gpr_tmpfile("gcp_observability_config", &tmp_file_name);
fputs(json, tmp_config_file);
fclose(tmp_config_file);
grpc_core::SetEnv("GRPC_OBSERVABILITY_CONFIG_FILE", tmp_file_name);
grpc_core::SetEnv("GRPC_GCP_OBSERVABILITY_CONFIG_FILE", tmp_file_name);
} else if (GetParam().config_source() ==
EnvParsingTestType::ConfigSource::kEnvVar) {
grpc_core::SetEnv("GRPC_OBSERVABILITY_CONFIG", json);
grpc_core::SetEnv("GRPC_GCP_OBSERVABILITY_CONFIG", json);
}
}

@ -28,8 +28,8 @@ TEST(GcpObservabilityTest, RegistrationTest) {
auto status = grpc::experimental::GcpObservabilityInit();
EXPECT_EQ(status,
absl::FailedPreconditionError(
"Environment variables GRPC_OBSERVABILITY_CONFIG_FILE or "
"GRPC_OBSERVABILITY_CONFIG "
"Environment variables GRPC_GCP_OBSERVABILITY_CONFIG_FILE or "
"GRPC_GCP_OBSERVABILITY_CONFIG "
"not defined"));
grpc_core::CoreConfiguration::Reset();

Loading…
Cancel
Save