From 8a0de81dc083ed6f676741a60931a0d60280224c Mon Sep 17 00:00:00 2001 From: Gauri Kalra Date: Tue, 1 Dec 2026 05:58:20 +0000 Subject: [PATCH] fix(storage): propagate client options to the gRPC metrics exporter --- google/cloud/storage/grpc_plugin.h | 27 + .../storage/internal/grpc/enable_metrics.cc | 27 +- .../storage/internal/grpc/enable_metrics.h | 19 + .../internal/grpc/metrics_exporter_impl.cc | 23 +- .../internal/grpc/metrics_exporter_impl.h | 19 + .../grpc/metrics_exporter_impl_test.cc | 1313 ++++++++++++++++- .../internal/grpc/metrics_exporter_options.cc | 206 +++ .../internal/grpc/metrics_exporter_options.h | 37 + .../grpc/metrics_exporter_options_test.cc | 353 +++++ 9 files changed, 2007 insertions(+), 17 deletions(-) diff --git a/google/cloud/storage/grpc_plugin.h b/google/cloud/storage/grpc_plugin.h index d6960f14ba897..86c80f54d4e88 100644 --- a/google/cloud/storage/grpc_plugin.h +++ b/google/cloud/storage/grpc_plugin.h @@ -65,6 +65,33 @@ GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN * CPU (a single RPC every few minutes) or memory (a few KiB to batch the * telemetry). * + * The exporter connects to Cloud Monitoring using the client's credentials, CA + * certificates file, and proxy configuration. Telemetry is exported only when + * the client's endpoint host belongs to the client's universe domain, the + * client does not use `google::cloud::MakeInsecureCredentials()`, + * `google::cloud::MakeApiKeyCredentials()`, or + * `google::cloud::MakeGDCHServiceAccountCredentials()`, and any credentials + * file's universe domain matches the client's universe domain. A credentials + * file without a `universe_domain` field defaults to `googleapis.com`; export + * is disabled if the file's universe domain cannot be determined. Credential + * types without a file (such as access tokens, metadata-server credentials, or + * `google::cloud::GrpcCredentialOption`) skip the universe-domain check. These + * requirements apply to `EnableGrpcMetricsOption`, `EnableOTelMetricsOption`, + * and `EnableOTelDebugMetricsOption`; setting any of those options to `true` + * does not override them. + * + * Clients with the same `google::cloud::AuthorityOption` (by default, + * `storage.` followed by the universe domain) share one Cloud Monitoring + * exporter for the life of the process. The first qualifying client creates + * the exporter, and subsequent clients sharing that authority reuse it without + * re-checking these requirements. + * + * Monitored resource attributes from the host (project, zone, and instance) + * are used when the client authenticates through the host's metadata server. + * For other credential types, the monitoring project comes from + * `google::cloud::storage::ProjectIdOption` or the credentials, and metrics + * export is skipped when neither specifies a project. + * * [gRFC/66]: https://github.com/grpc/proposal/blob/master/A66-otel-stats.md * [gRFC/78]: * https://github.com/grpc/proposal/blob/master/A78-grpc-metrics-wrr-pf-xds.md diff --git a/google/cloud/storage/internal/grpc/enable_metrics.cc b/google/cloud/storage/internal/grpc/enable_metrics.cc index 9ebf0699ff829..92fa0adc81c6c 100644 --- a/google/cloud/storage/internal/grpc/enable_metrics.cc +++ b/google/cloud/storage/internal/grpc/enable_metrics.cc @@ -19,6 +19,9 @@ #include "google/cloud/storage/grpc_plugin.h" #include "google/cloud/storage/internal/grpc/default_options.h" #include "google/cloud/storage/internal/grpc/metrics_exporter_impl.h" +#include "google/cloud/storage/internal/grpc/metrics_exporter_options.h" +#include +#include namespace google { namespace cloud { @@ -26,12 +29,26 @@ namespace storage_internal { GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN void EnableGrpcMetrics(Options const& storage_options) { - auto options = DefaultOptionsGrpc(storage_options); - // Avoid running the resource detector if all metrics options are disabled. + EnableGrpcMetrics(storage_options, + [] { return otel::MakeResourceDetector()->Detect(); }); +} + +void EnableGrpcMetrics( + Options const& storage_options, + std::function const& + detect_resource) { + Options const options = DefaultOptionsGrpc(storage_options); + // Avoid querying the metadata server for GCP resource attributes when metrics + // export is disabled or does not use host resource attributes. if (!AnyMetricsEnabled(options)) return; - auto detector = otel::MakeResourceDetector(); - auto resources = detector->Detect(); - auto config = MakeMeterProviderConfig(resources, options); + MetricsExportMode const mode = DetectMetricsExportMode(options); + if (mode == MetricsExportMode::kDisabled) return; + opentelemetry::sdk::resource::Resource const resource = + mode == MetricsExportMode::kWithHostResource + ? detect_resource() + : opentelemetry::sdk::resource::Resource::GetEmpty(); + std::optional config = + MakeMeterProviderConfig(resource, options, mode); if (!config) return; EnableGrpcMetricsImpl(std::move(*config)); } diff --git a/google/cloud/storage/internal/grpc/enable_metrics.h b/google/cloud/storage/internal/grpc/enable_metrics.h index 70b641285f40f..9f42a6615c60a 100644 --- a/google/cloud/storage/internal/grpc/enable_metrics.h +++ b/google/cloud/storage/internal/grpc/enable_metrics.h @@ -17,6 +17,10 @@ #include "google/cloud/options.h" #include "google/cloud/version.h" +#ifdef GOOGLE_CLOUD_CPP_STORAGE_WITH_OTEL_METRICS +#include +#include +#endif // GOOGLE_CLOUD_CPP_STORAGE_WITH_OTEL_METRICS namespace google { namespace cloud { @@ -25,6 +29,21 @@ GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN void EnableGrpcMetrics(Options const& storage_options); +#ifdef GOOGLE_CLOUD_CPP_STORAGE_WITH_OTEL_METRICS +/** + * Overload of `EnableGrpcMetrics` that takes an explicit resource detector for + * testing. + * + * Invokes @p detect_resource only when `DetectMetricsExportMode()` returns + * `MetricsExportMode::kWithHostResource`, avoiding an unnecessary metadata + * server query otherwise. + */ +void EnableGrpcMetrics( + Options const& storage_options, + std::function const& + detect_resource); +#endif // GOOGLE_CLOUD_CPP_STORAGE_WITH_OTEL_METRICS + GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END } // namespace storage_internal } // namespace cloud diff --git a/google/cloud/storage/internal/grpc/metrics_exporter_impl.cc b/google/cloud/storage/internal/grpc/metrics_exporter_impl.cc index 1bba437f90a30..29ef2590319df 100644 --- a/google/cloud/storage/internal/grpc/metrics_exporter_impl.cc +++ b/google/cloud/storage/internal/grpc/metrics_exporter_impl.cc @@ -159,15 +159,28 @@ bool AnyMetricsEnabled(Options const& options) { std::optional MakeMeterProviderConfig( opentelemetry::sdk::resource::Resource const& resource, Options const& options) { - if (!AnyMetricsEnabled(options)) { - return std::nullopt; - } + if (!AnyMetricsEnabled(options)) return std::nullopt; + return MakeMeterProviderConfig(resource, options, + DetectMetricsExportMode(options)); +} + +std::optional MakeMeterProviderConfig( + opentelemetry::sdk::resource::Resource const& resource, + Options const& options, MetricsExportMode mode) { + if (!AnyMetricsEnabled(options)) return std::nullopt; + if (mode == MetricsExportMode::kDisabled) return std::nullopt; bool const enable_grpc = options.get(); - auto project = MonitoringProject(resource, options); + // Attach host resource attributes only when the client authenticates via the + // host's metadata server; otherwise use default client resource labels. + opentelemetry::sdk::resource::Resource const& client_resource = + mode == MetricsExportMode::kWithHostResource + ? resource + : opentelemetry::sdk::resource::Resource::GetEmpty(); + std::optional project = MonitoringProject(client_resource, options); if (!project) return std::nullopt; - auto exporter_options = MetricsExporterOptions(*project, resource); + Options exporter_options = MetricsExporterOptions(*project, client_resource); if (options.has()) { exporter_options.set( options.get()); diff --git a/google/cloud/storage/internal/grpc/metrics_exporter_impl.h b/google/cloud/storage/internal/grpc/metrics_exporter_impl.h index 95a4bab4edb67..e33746016f61b 100644 --- a/google/cloud/storage/internal/grpc/metrics_exporter_impl.h +++ b/google/cloud/storage/internal/grpc/metrics_exporter_impl.h @@ -17,6 +17,7 @@ #ifdef GOOGLE_CLOUD_CPP_STORAGE_WITH_OTEL_METRICS +#include "google/cloud/storage/internal/grpc/metrics_exporter_options.h" #include "google/cloud/options.h" #include "google/cloud/project.h" #include "google/cloud/version.h" @@ -46,10 +47,28 @@ struct ExporterConfig { /// Returns true if any gRPC or OpenTelemetry storage metrics option is enabled. bool AnyMetricsEnabled(Options const& options); +/** + * Returns the exporter configuration for the resolved @p options, or + * `std::nullopt` when metrics export is disabled or no monitoring project can + * be determined. + * + * Uses the host @p resource for the monitoring project and resource labels only + * when `DetectMetricsExportMode()` returns + * `MetricsExportMode::kWithHostResource`. + */ std::optional MakeMeterProviderConfig( opentelemetry::sdk::resource::Resource const& resource, Options const& options); +/** + * Overload of `MakeMeterProviderConfig` that accepts a precomputed + * `MetricsExportMode` so callers that already inspected @p options do not call + * `DetectMetricsExportMode()` a second time. + */ +std::optional MakeMeterProviderConfig( + opentelemetry::sdk::resource::Resource const& resource, + Options const& options, MetricsExportMode mode); + void EnableGrpcMetricsImpl(ExporterConfig config); /// Test-only overload allowing callers to intercept gRPC plugin registration. diff --git a/google/cloud/storage/internal/grpc/metrics_exporter_impl_test.cc b/google/cloud/storage/internal/grpc/metrics_exporter_impl_test.cc index 9982cf2f7b6ce..ea7e00b6040fa 100644 --- a/google/cloud/storage/internal/grpc/metrics_exporter_impl_test.cc +++ b/google/cloud/storage/internal/grpc/metrics_exporter_impl_test.cc @@ -19,12 +19,26 @@ #include "google/cloud/opentelemetry/monitoring_exporter.h" #include "google/cloud/storage/grpc_plugin.h" #include "google/cloud/storage/internal/grpc/default_options.h" +#include "google/cloud/storage/internal/grpc/enable_metrics.h" #include "google/cloud/storage/options.h" +#include "google/cloud/storage/testing/constants.h" +#include "google/cloud/storage/testing/temp_file.h" #include "google/cloud/common_options.h" #include "google/cloud/credentials.h" +#include "google/cloud/grpc_options.h" +#include "google/cloud/internal/credentials_impl.h" +#include "google/cloud/internal/detect_gcp.h" +#include "google/cloud/internal/filesystem.h" +#include "google/cloud/internal/unified_grpc_credentials.h" +#include "google/cloud/status.h" #include "google/cloud/testing_util/chrono_output.h" #include "google/cloud/testing_util/scoped_environment.h" +#include "google/cloud/testing_util/scoped_log.h" +#include "google/cloud/universe_domain_options.h" +#include "google/api/monitored_resource.pb.h" #include +#include +#include #include #include #include @@ -43,9 +57,16 @@ GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN namespace { using ::google::cloud::testing_util::ScopedEnvironment; +using ::google::cloud::testing_util::ScopedLog; +using ::testing::AllOf; +using ::testing::Contains; +using ::testing::Each; using ::testing::Eq; +using ::testing::HasSubstr; using ::testing::IsNull; +using ::testing::Not; using ::testing::NotNull; +using ::testing::Pair; auto EmptyResource() { return opentelemetry::sdk::resource::Resource::Create({}); @@ -56,6 +77,12 @@ auto FullResource() { {{"cloud.provider", "gcp"}, {"cloud.account.id", "project-id-resource"}}); } +std::shared_ptr AccessTokenCredentials() { + return MakeAccessTokenCredentials( + "test-only-invalid", + std::chrono::system_clock::now() + std::chrono::minutes(15)); +} + auto TestOptions(Options opts = {}) { // In CI builds these environment variables may be set and affect the behavior // in ways we do not need to (re)test here. @@ -64,10 +91,14 @@ auto TestOptions(Options opts = {}) { ScopedEnvironment("GCP_STORAGE_CPP_ENABLE_OTEL_METRICS", std::nullopt); auto debug_env = ScopedEnvironment( "GCP_STORAGE_CPP_ENABLE_OTEL_DEBUG_METRICS", std::nullopt); + ScopedEnvironment const emulator_env( + "CLOUD_STORAGE_EXPERIMENTAL_GRPC_TESTBENCH_ENDPOINT", std::nullopt); + ScopedEnvironment const universe_env("GOOGLE_CLOUD_UNIVERSE_DOMAIN", + std::nullopt); + // Use the credentials of the host. Only for those does the resource passed + // to `MakeMeterProviderConfig()` supply the monitoring project. opts.set(true) - .set(MakeAccessTokenCredentials( - "unused", - std::chrono::system_clock::now() + std::chrono::minutes(15))); + .set(MakeComputeEngineCredentials()); return DefaultOptionsGrpc(std::move(opts)); } @@ -125,7 +156,11 @@ TEST(GrpcMetricsExporter, OptionCombinationsGateConfigAndGrpcPlugin) { } TEST(GrpcMetricsExporter, DisabledBecauseNoProject) { - auto config = MakeMeterProviderConfig(EmptyResource(), TestOptions()); + // Access token credentials name no project. With the host's credentials the + // lookup would continue with the metadata server. + std::optional const config = MakeMeterProviderConfig( + EmptyResource(), + TestOptions().set(AccessTokenCredentials())); EXPECT_FALSE(config.has_value()); } @@ -227,7 +262,8 @@ TEST(MakeMeterProviderConfigTest, NoExcludedLabels) { options.set(true); options.set("test-project"); - auto config = MakeMeterProviderConfig(resource, options); + std::optional const config = + MakeMeterProviderConfig(resource, TestOptions(options)); ASSERT_TRUE(config.has_value()); EXPECT_FALSE(config->exporter_options @@ -245,7 +281,8 @@ TEST(MakeMeterProviderConfigTest, WithExcludedLabels) { options.set( excluded_labels); - auto config = MakeMeterProviderConfig(resource, options); + std::optional const config = + MakeMeterProviderConfig(resource, TestOptions(options)); ASSERT_TRUE(config.has_value()); EXPECT_TRUE(config->exporter_options @@ -266,7 +303,8 @@ TEST(MakeMeterProviderConfigTest, EmptyExcludedLabels) { options.set( std::set{}); - auto config = MakeMeterProviderConfig(resource, options); + std::optional const config = + MakeMeterProviderConfig(resource, TestOptions(options)); ASSERT_TRUE(config.has_value()); EXPECT_TRUE(config->exporter_options @@ -406,6 +444,1267 @@ TEST(GrpcMetricsExporter, OTelOnlyProviderReusedByGrpcEnable) { EXPECT_EQ(plugin_registrations, 1); } +/// A path where no file exists, for tests about a missing credentials file. +std::string MissingFile() { + return internal::PathAppend(::testing::TempDir(), + "grpc-metrics-exporter-test-missing-file.json"); +} + +/// The resource the detector reports on a Compute Engine instance. +opentelemetry::sdk::resource::Resource HostResource() { + return opentelemetry::sdk::resource::Resource::Create({ + {"cloud.provider", "gcp"}, + {"cloud.account.id", "project-id-resource"}, + {"cloud.platform", "gcp_compute_engine"}, + {"cloud.availability_zone", "us-central1-c"}, + {"host.id", "test-host-id"}, + }); +} + +/// The labels of the monitored resource the exporter attaches to its time +/// series. +google::protobuf::Map MonitoredResourceLabels( + ExporterConfig const& config) { + return config.exporter_options.get().labels(); +} + +/// The options of a client that enables the exporter explicitly, so a result +/// does not depend on the gRPC version the test is built with. +Options EnabledOptions() { + return Options{}.set(true); +} + +/// As `EnabledOptions()`, with a monitoring project that depends neither on +/// the host nor on the credentials. +Options EnabledOptionsWithProject() { + return EnabledOptions().set("project-id-option"); +} + +/// A service account key file for the project `project-id-credentials`. Like +/// key files created before the field existed, it has no `universe_domain`. +nlohmann::json ServiceAccountKey() { + return nlohmann::json{ + {"type", "service_account"}, + {"project_id", "project-id-credentials"}, + {"private_key_id", "test-only-key-id"}, + {"private_key", google::cloud::storage::testing::kWellFormattedKey}, + {"client_email", "sa@invalid-test-only-project.iam.gserviceaccount.com"}, + {"client_id", "invalid-test-only-client-id"}, + {"auth_uri", "https://accounts.google.com/o/oauth2/auth"}, + {"token_uri", "https://accounts.google.com/o/oauth2/token"}, + {"auth_provider_x509_cert_url", + "https://www.googleapis.com/oauth2/v1/certs"}, + {"client_x509_cert_url", + "https://www.googleapis.com/robot/v1/metadata/x509/" + "foo-email%40foo-project.iam.gserviceaccount.com"}, + }; +} + +/// The minimum an external account (Workload Identity Federation) file needs +/// to be accepted. The subject token file is only read to fetch a token. +nlohmann::json ExternalAccountFile() { + return nlohmann::json{ + {"type", "external_account"}, + {"audience", + "//iam.googleapis.com/projects/123456/locations/global/" + "workloadIdentityPools/test-pool/providers/test-provider"}, + {"subject_token_type", "urn:ietf:params:oauth:token-type:jwt"}, + {"token_url", "https://sts.googleapis.com/v1/token"}, + {"credential_source", {{"file", "/var/run/secrets/test-subject-token"}}}, + }; +} + +/// The file `gcloud auth application-default login` writes. +nlohmann::json AuthorizedUserFile() { + return nlohmann::json{ + {"type", "authorized_user"}, + {"client_id", "test-client-id.apps.googleusercontent.com"}, + {"client_secret", "test-client-secret"}, + {"refresh_token", "test-refresh-token"}, + }; +} + +/// Returns the text of @p credentials_file with its `universe_domain` field +/// set to @p universe_domain. +std::string WithUniverseDomain(nlohmann::json credentials_file, + nlohmann::json const& universe_domain) { + credentials_file["universe_domain"] = universe_domain; + return credentials_file.dump(); +} + +/// Controls every process-wide input of `DefaultOptionsGrpc()` and of the +/// Application Default Credentials lookup, so a test depends only on the +/// options and variables it sets itself. Otherwise the results would change +/// with the gcloud credentials of whoever runs the test. +class GrpcMetricsExporterConfigTest : public ::testing::Test { + protected: + /// Makes a file with @p contents the Application Default Credentials. + void SetApplicationDefaultCredentials(std::string const& contents) { + adc_file_.emplace(contents); + adc_override_.emplace("GOOGLE_APPLICATION_CREDENTIALS", adc_file_->name()); + } + + /// Names a file that does not exist as the Application Default Credentials. + void SetMissingApplicationDefaultCredentials() { + adc_override_.emplace("GOOGLE_APPLICATION_CREDENTIALS", MissingFile()); + } + + private: + ScopedEnvironment project_{"GOOGLE_CLOUD_PROJECT", std::nullopt}; + ScopedEnvironment otel_metrics_{"GCP_STORAGE_CPP_ENABLE_OTEL_METRICS", + std::nullopt}; + ScopedEnvironment otel_debug_metrics_{ + "GCP_STORAGE_CPP_ENABLE_OTEL_DEBUG_METRICS", std::nullopt}; + ScopedEnvironment universe_domain_{"GOOGLE_CLOUD_UNIVERSE_DOMAIN", + std::nullopt}; + ScopedEnvironment emulator_{ + "CLOUD_STORAGE_EXPERIMENTAL_GRPC_TESTBENCH_ENDPOINT", std::nullopt}; + ScopedEnvironment preserve_credentials_{ + "GOOGLE_CLOUD_CPP_STORAGE_TESTING_PRESERVE_CREDENTIALS", std::nullopt}; + ScopedEnvironment interconnect_{ + "GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS_OVER_INTERCONNECT", std::nullopt}; + // Unless a test sets one, there is no Application Default Credentials file: + // the variable is unset and the gcloud location names a missing file. + ScopedEnvironment adc_{"GOOGLE_APPLICATION_CREDENTIALS", std::nullopt}; + ScopedEnvironment gcloud_adc_{"GOOGLE_GCLOUD_ADC_PATH_OVERRIDE", + MissingFile()}; + std::optional adc_file_; + std::optional adc_override_; +}; + +/// @test Verify `UnifiedCredentialsOption` is propagated to the Monitoring +/// connection options. +TEST_F(GrpcMetricsExporterConfigTest, UsesClientCredentials) { + std::shared_ptr const credentials = AccessTokenCredentials(); + std::optional const config = MakeMeterProviderConfig( + EmptyResource(), + DefaultOptionsGrpc( + EnabledOptionsWithProject().set( + credentials))); + ASSERT_TRUE(config.has_value()); + EXPECT_THAT( + config->exporter_connection_options.get(), + Eq(credentials)); +} + +/// @test Verify the default credentials populated by `DefaultOptionsGrpc()` +/// are forwarded to the Monitoring connection options. +TEST_F(GrpcMetricsExporterConfigTest, UsesClientDefaultCredentials) { + Options const options = DefaultOptionsGrpc(EnabledOptionsWithProject()); + std::optional const config = + MakeMeterProviderConfig(EmptyResource(), options); + ASSERT_TRUE(config.has_value()); + EXPECT_THAT( + config->exporter_connection_options.get(), + AllOf(NotNull(), Eq(options.get()))); +} + +/// @test Verify `GrpcCredentialOption` is forwarded without setting +/// `UnifiedCredentialsOption`, which would take precedence when creating the +/// Monitoring channel. +TEST_F(GrpcMetricsExporterConfigTest, UsesClientGrpcCredentials) { + std::shared_ptr const credentials = + grpc::InsecureChannelCredentials(); + std::optional const config = MakeMeterProviderConfig( + EmptyResource(), + DefaultOptionsGrpc( + EnabledOptionsWithProject().set(credentials))); + ASSERT_TRUE(config.has_value()); + Options const& exporter = config->exporter_connection_options; + EXPECT_THAT(exporter.get(), Eq(credentials)); + EXPECT_FALSE(exporter.has()); +} + +/// @test Verify both credential options are forwarded when both are set, so +/// the Monitoring connection resolves them with the same precedence as the +/// Storage connection. +TEST_F(GrpcMetricsExporterConfigTest, UsesBothCredentialOptions) { + std::shared_ptr const unified = AccessTokenCredentials(); + std::shared_ptr const channel = + grpc::InsecureChannelCredentials(); + std::optional const config = MakeMeterProviderConfig( + EmptyResource(), + DefaultOptionsGrpc(EnabledOptionsWithProject() + .set(unified) + .set(channel))); + ASSERT_TRUE(config.has_value()); + Options const& exporter = config->exporter_connection_options; + EXPECT_THAT(exporter.get(), Eq(unified)); + EXPECT_THAT(exporter.get(), Eq(channel)); +} + +/// @test Verify `CARootsFilePathOption` is forwarded to the Monitoring +/// connection options. +TEST_F(GrpcMetricsExporterConfigTest, UsesClientCARoots) { + std::optional const config = MakeMeterProviderConfig( + EmptyResource(), + DefaultOptionsGrpc( + EnabledOptionsWithProject() + .set(AccessTokenCredentials()) + .set("/test-only/ca-certificates.pem"))); + ASSERT_TRUE(config.has_value()); + EXPECT_EQ(config->exporter_connection_options.get(), + "/test-only/ca-certificates.pem"); +} + +/// @test Verify `ProxyOption` is forwarded to the Monitoring connection +/// options. +TEST_F(GrpcMetricsExporterConfigTest, UsesClientProxy) { + std::optional const config = MakeMeterProviderConfig( + EmptyResource(), + DefaultOptionsGrpc( + EnabledOptionsWithProject() + .set(AccessTokenCredentials()) + .set(ProxyConfig() + .set_hostname("proxy.example.com") + .set_port("3128")))); + ASSERT_TRUE(config.has_value()); + ProxyConfig const& proxy = + config->exporter_connection_options.get(); + EXPECT_EQ(proxy.hostname(), "proxy.example.com"); + EXPECT_EQ(proxy.port(), "3128"); +} + +/// @test Verify Storage-specific channel and per-request options (such as +/// `AuthorityOption`, `UserProjectOption`, and +/// `GrpcGoogleDefaultTlsOnlyOption`) are not copied into the Monitoring +/// connection options. +TEST_F(GrpcMetricsExporterConfigTest, ForwardsOnlyConnectionOptions) { + Options const options = DefaultOptionsGrpc( + EnabledOptionsWithProject() + .set( + "google-c2p:///storage-direct.googleapis.com?force-xds") + .set("storage-direct.googleapis.com") + .set("test-only-user-project") + .set({{"x-test-only-header", "value"}}) + .set({{"test-only-argument", "value"}}) + .set(3), + internal::MakeGcpDetector(), /*grpc_supports_force_xds=*/true); + ASSERT_TRUE(options.get()); + + std::optional const config = + MakeMeterProviderConfig(EmptyResource(), options); + ASSERT_TRUE(config.has_value()); + Options const& exporter = config->exporter_connection_options; + EXPECT_FALSE(exporter.has()); + EXPECT_FALSE(exporter.has()); + EXPECT_FALSE(exporter.has()); + EXPECT_FALSE(exporter.has()); + EXPECT_FALSE(exporter.has()); + EXPECT_FALSE(exporter.has()); + EXPECT_FALSE(exporter.has()); + EXPECT_FALSE(exporter.has()); + EXPECT_FALSE(exporter.has()); + EXPECT_FALSE(exporter.has()); +} + +/// @test Verify metrics export is disabled when the client uses +/// `MakeInsecureCredentials()` (for example against a local emulator), and +/// logs the reason. +TEST_F(GrpcMetricsExporterConfigTest, SkipsExportForInsecureCredentials) { + ScopedLog log; + std::optional const config = MakeMeterProviderConfig( + HostResource(), + DefaultOptionsGrpc( + EnabledOptionsWithProject().set( + MakeInsecureCredentials()))); + EXPECT_FALSE(config.has_value()); + EXPECT_THAT(log.ExtractLines(), + Contains(AllOf(HasSubstr("metrics export is disabled"), + HasSubstr("insecure credentials")))); +} + +/// @test Verify the emulator endpoint gets no exporter, even when the +/// application enables metrics explicitly. `DefaultOptionsGrpc()` points such +/// a client at the emulator with insecure credentials. +TEST_F(GrpcMetricsExporterConfigTest, SkipsExportForEmulatorEndpoint) { + ScopedEnvironment const emulator( + "CLOUD_STORAGE_EXPERIMENTAL_GRPC_TESTBENCH_ENDPOINT", "localhost:1234"); + std::optional const config = MakeMeterProviderConfig( + HostResource(), DefaultOptionsGrpc(EnabledOptionsWithProject())); + EXPECT_FALSE(config.has_value()); +} + +/// @test Verify `GOOGLE_CLOUD_CPP_METRIC_SERVICE_ENDPOINT` does not change +/// whether metrics are exported. +/// +/// The variable redirects the Monitoring connection of every client in the +/// process. It says nothing about the universe of one client's endpoint, so it +/// neither enables the exporter for an endpoint outside the universe domain +/// nor disables it for one inside. +TEST_F(GrpcMetricsExporterConfigTest, IgnoresMetricServiceEndpointVariable) { + ScopedEnvironment const metric_endpoint( + "GOOGLE_CLOUD_CPP_METRIC_SERVICE_ENDPOINT", "monitoring.example.com"); + Options const client_options = + EnabledOptionsWithProject().set( + AccessTokenCredentials()); + + std::optional const inside = MakeMeterProviderConfig( + EmptyResource(), DefaultOptionsGrpc(client_options)); + EXPECT_TRUE(inside.has_value()); + + std::optional const outside = MakeMeterProviderConfig( + EmptyResource(), + DefaultOptionsGrpc( + Options(client_options).set("storage.ud.net"))); + EXPECT_FALSE(outside.has_value()); +} + +/// @test Verify `GOOGLE_CLOUD_UNIVERSE_DOMAIN` selects the universe domain the +/// endpoint is compared with, as `UniverseDomainOption` does. The variable +/// applies to every client in the process, so an application-supplied endpoint +/// in the default universe is then outside the universe domain. +TEST_F(GrpcMetricsExporterConfigTest, UniverseDomainVariableSelectsUniverse) { + ScopedEnvironment const universe_domain("GOOGLE_CLOUD_UNIVERSE_DOMAIN", + "ud.net"); + Options const client_options = + EnabledOptionsWithProject().set( + AccessTokenCredentials()); + + std::optional const library_endpoint = + MakeMeterProviderConfig(EmptyResource(), + DefaultOptionsGrpc(client_options)); + ASSERT_TRUE(library_endpoint.has_value()); + EXPECT_EQ(library_endpoint->exporter_connection_options + .get(), + "ud.net"); + + std::optional const default_universe_endpoint = + MakeMeterProviderConfig( + EmptyResource(), + DefaultOptionsGrpc( + Options(client_options) + .set("storage.googleapis.com"))); + EXPECT_FALSE(default_universe_endpoint.has_value()); +} + +/// @test Verify an empty `GOOGLE_CLOUD_UNIVERSE_DOMAIN` is treated as unset, +/// so the default endpoint is still in the (default) universe domain. +TEST_F(GrpcMetricsExporterConfigTest, EmptyUniverseDomainVariableIsIgnored) { + ScopedEnvironment const universe_domain("GOOGLE_CLOUD_UNIVERSE_DOMAIN", ""); + std::optional const config = MakeMeterProviderConfig( + EmptyResource(), + DefaultOptionsGrpc( + EnabledOptionsWithProject().set( + AccessTokenCredentials()))); + ASSERT_TRUE(config.has_value()); + EXPECT_FALSE(config->exporter_connection_options + .has()); +} + +struct EndpointTestCase { + std::string test_name; + // An application-supplied `EndpointOption`. When unset, the library selects + // the endpoint. + std::optional endpoint; + // An application-supplied `UniverseDomainOption`. + std::optional universe_domain; + bool expected_export; +}; + +class GrpcMetricsExporterEndpointTest + : public GrpcMetricsExporterConfigTest, + public ::testing::WithParamInterface {}; + +/// @test Verify metrics export is enabled only when the client's endpoint +/// resolves to a subdomain of the configured universe domain +/// (case-insensitive), and disabled for custom hosts, IP literals, or +/// unsupported gRPC target schemes. +TEST_P(GrpcMetricsExporterEndpointTest, ExportsOnlyWithinUniverseDomain) { + EndpointTestCase const& test_case = GetParam(); + Options client_options = + EnabledOptionsWithProject().set( + AccessTokenCredentials()); + if (test_case.endpoint) { + client_options.set(*test_case.endpoint); + } + if (test_case.universe_domain) { + client_options.set( + *test_case.universe_domain); + } + std::optional const config = MakeMeterProviderConfig( + EmptyResource(), DefaultOptionsGrpc(client_options)); + EXPECT_EQ(config.has_value(), test_case.expected_export) + << "endpoint=" << test_case.endpoint.value_or("(library default)") + << ", universe_domain=" + << test_case.universe_domain.value_or("(not set)"); +} + +INSTANTIATE_TEST_SUITE_P( + GrpcMetricsExporter, GrpcMetricsExporterEndpointTest, + ::testing::Values( + // Endpoints the library selects. + EndpointTestCase{"LibraryDefault", std::nullopt, std::nullopt, true}, + EndpointTestCase{"LibraryDefaultWithUniverseDomain", std::nullopt, + "ud.net", true}, + // Application-supplied endpoints in the default universe. + EndpointTestCase{"CloudPath", "storage.googleapis.com", std::nullopt, + true}, + EndpointTestCase{"CloudPathWithPort", "storage.googleapis.com:443", + std::nullopt, true}, + EndpointTestCase{"MixedCase", "STORAGE.GoogleAPIs.com", std::nullopt, + true}, + EndpointTestCase{"DirectPath", "google-c2p:///storage.googleapis.com", + std::nullopt, true}, + EndpointTestCase{"DirectPathWithPort", + "google-c2p:///storage.googleapis.com:443", + std::nullopt, true}, + EndpointTestCase{"DirectPathExperimentalScheme", + "google-c2p-experimental:///storage.googleapis.com", + std::nullopt, true}, + EndpointTestCase{ + "Interconnect", + "google-c2p:///storage-direct.googleapis.com?force-xds", + std::nullopt, true}, + EndpointTestCase{ + "InterconnectPreprod", + "google-c2p:///storage-preprod-direct.sandbox.googleapis.com" + "?force-xds", + std::nullopt, true}, + EndpointTestCase{"PrivateServiceConnect", + "storage-foo.p.googleapis.com", std::nullopt, true}, + EndpointTestCase{"PrivateVip", "private.googleapis.com", std::nullopt, + true}, + EndpointTestCase{"RestrictedVip", "restricted.googleapis.com", + std::nullopt, true}, + // Application-supplied endpoints in another universe, with the + // matching universe domain. + EndpointTestCase{"UniverseDomain", "storage.ud.net", "ud.net", true}, + EndpointTestCase{"UniverseDomainMixedCase", "storage.ud.net", "UD.Net", + true}, + EndpointTestCase{"UniverseDomainDirectPath", + "google-c2p:///storage.ud.net", "ud.net", true}, + EndpointTestCase{"UniverseDomainPrivateVip", "private.ud.net", "ud.net", + true}, + // The endpoint and the universe domain name different universes. + EndpointTestCase{"OtherUniverse", "storage.ud.net", std::nullopt, + false}, + EndpointTestCase{"OtherUniverseDirectPath", + "google-c2p:///storage.ud.net", std::nullopt, false}, + EndpointTestCase{"DefaultUniverseWithUniverseDomain", + "storage.googleapis.com", "ud.net", false}, + EndpointTestCase{"DefaultUniversePrivateVipWithUniverseDomain", + "private.googleapis.com", "ud.net", false}, + EndpointTestCase{"DefaultUniversePrivateServiceConnectWithUniverseDoma" + "in", + "storage-foo.p.googleapis.com", "ud.net", false}, + // Non-subdomain host names. + EndpointTestCase{"SuffixWithoutDot", "storage.notgoogleapis.com", + std::nullopt, false}, + EndpointTestCase{"SuffixWithoutLabel", "examplegoogleapis.com", + std::nullopt, false}, + EndpointTestCase{"UniverseDomainItself", "googleapis.com", std::nullopt, + false}, + EndpointTestCase{"UniverseDomainAsPrefix", + "storage.googleapis.com.example.com", std::nullopt, + false}, + // Non-universe hosts. + EndpointTestCase{"Localhost", "localhost:8080", std::nullopt, false}, + EndpointTestCase{"Ipv4Address", "127.0.0.1:9000", std::nullopt, false}, + EndpointTestCase{"Ipv6Address", "[::1]:9000", std::nullopt, false}, + EndpointTestCase{"PrivateDnsName", "storage.corp.example:443", + std::nullopt, false}, + // Unsupported gRPC target formats and invalid DNS host names. + EndpointTestCase{"HttpsScheme", "https://storage.googleapis.com", + std::nullopt, false}, + EndpointTestCase{"DnsScheme", "dns:///storage.googleapis.com", + std::nullopt, false}, + EndpointTestCase{"UnixScheme", "unix:/tmp/test-only.sock", std::nullopt, + false}, + EndpointTestCase{"DirectPathWithAuthority", + "google-c2p://xds/storage.googleapis.com", + std::nullopt, false}, + EndpointTestCase{"UnderscoreInHost", "storage_1.googleapis.com", + std::nullopt, false}, + EndpointTestCase{"Path", "storage.googleapis.com/x", std::nullopt, + false}, + EndpointTestCase{"DirectPathPath", + "google-c2p:///storage.googleapis.com/x", std::nullopt, + false}, + EndpointTestCase{"DirectPathPortAndPath", + "google-c2p:///storage.googleapis.com:443/v1", + std::nullopt, false}, + EndpointTestCase{"Query", "storage.googleapis.com?x", std::nullopt, + false}, + EndpointTestCase{"DirectPathFragment", + "google-c2p:///storage.googleapis.com#x", std::nullopt, + false}, + EndpointTestCase{"TrailingDot", "storage.googleapis.com.", std::nullopt, + false}, + EndpointTestCase{"EmptyLabel", "storage..googleapis.com", std::nullopt, + false}, + EndpointTestCase{"NonNumericPort", "storage.googleapis.com:https", + std::nullopt, false}, + EndpointTestCase{"DirectPathNonNumericPort", + "google-c2p:///storage.googleapis.com:https", + std::nullopt, false}, + EndpointTestCase{"EmptyPort", "storage.googleapis.com:", std::nullopt, + false}, + EndpointTestCase{"DirectPathEmptyPort", + "google-c2p:///storage.googleapis.com:", std::nullopt, + false}, + EndpointTestCase{"DirectPathWithoutHost", "google-c2p:///", + std::nullopt, false}, + EndpointTestCase{"EmptyEndpoint", "", std::nullopt, false}, + // An empty universe domain names no universe. + EndpointTestCase{"EmptyUniverseDomain", std::nullopt, "", false}, + EndpointTestCase{"EmptyUniverseDomainWithEndpoint", + "storage.googleapis.com", "", false}), + [](::testing::TestParamInfo const& info) { + return info.param.test_name; + }); + +// The credentials the tables below are built from. Each credentials file +// comes in two forms: as the tooling wrote it, and stating a universe domain. +std::shared_ptr ServiceAccount() { + return MakeServiceAccountCredentials(ServiceAccountKey().dump()); +} + +std::shared_ptr ServiceAccountFor( + nlohmann::json const& universe_domain) { + return MakeServiceAccountCredentials( + WithUniverseDomain(ServiceAccountKey(), universe_domain)); +} + +std::shared_ptr ExternalAccount() { + return MakeExternalAccountCredentials(ExternalAccountFile().dump()); +} + +std::shared_ptr ExternalAccountFor( + nlohmann::json const& universe_domain) { + return MakeExternalAccountCredentials( + WithUniverseDomain(ExternalAccountFile(), universe_domain)); +} + +std::shared_ptr AuthorizedUser() { + return internal::MakeUserAccountCredentials(AuthorizedUserFile().dump()); +} + +std::shared_ptr AuthorizedUserFor( + nlohmann::json const& universe_domain) { + return internal::MakeUserAccountCredentials( + WithUniverseDomain(AuthorizedUserFile(), universe_domain)); +} + +std::shared_ptr ImpersonationFrom( + std::shared_ptr const& base) { + return MakeImpersonateServiceAccountCredentials( + base, "sa@invalid-test-only-project.iam.gserviceaccount.com"); +} + +Options Unified(std::shared_ptr const& credentials) { + return Options{}.set(credentials); +} + +struct HostResourceTestCase { + std::string test_name; + // The credential options the application sets; none for the library default. + Options credential_options; + // An application-supplied `storage::ProjectIdOption`. + std::optional project_option; + // The monitoring project. When unset, no exporter is expected. + std::optional expected_project; + // Whether the monitored resource names the host's zone, platform and id. + bool expected_host_labels; +}; + +class GrpcMetricsExporterHostResourceTest + : public GrpcMetricsExporterConfigTest, + public ::testing::WithParamInterface {}; + +/// @test Verify host resource attributes (project, zone, platform, and host +/// id) are used only when the client authenticates via the host's metadata +/// server; otherwise the project comes from `storage::ProjectIdOption` or the +/// credentials with default resource labels. +TEST_P(GrpcMetricsExporterHostResourceTest, + UsesHostResourceOnlyForMetadataCredentials) { + HostResourceTestCase const& test_case = GetParam(); + Options client_options = + internal::MergeOptions(test_case.credential_options, EnabledOptions()); + if (test_case.project_option) { + client_options.set(*test_case.project_option); + } + std::optional const config = MakeMeterProviderConfig( + HostResource(), DefaultOptionsGrpc(client_options)); + if (!test_case.expected_project) { + EXPECT_FALSE(config.has_value()); + return; + } + ASSERT_TRUE(config.has_value()); + EXPECT_EQ(config->project, Project(*test_case.expected_project)); + bool const host = test_case.expected_host_labels; + EXPECT_THAT( + MonitoredResourceLabels(*config), + AllOf(Contains(Pair("project_id", *test_case.expected_project)), + Contains(Pair("location", host ? "us-central1-c" : "global")), + Contains(Pair("cloud_platform", + host ? "gcp_compute_engine" : "unknown")), + Contains(Pair("host_id", host ? "test-host-id" : "unknown")))); +} + +INSTANTIATE_TEST_SUITE_P( + GrpcMetricsExporter, GrpcMetricsExporterHostResourceTest, + ::testing::Values( + // Credentials served by the host's metadata server. The host's + // project takes precedence over `storage::ProjectIdOption`. + HostResourceTestCase{"LibraryDefaultCredentials", Options{}, + std::nullopt, "project-id-resource", true}, + HostResourceTestCase{"LibraryDefaultCredentialsWithProjectOption", + Options{}, "project-id-option", + "project-id-resource", true}, + HostResourceTestCase{"ComputeEngineCredentials", + Unified(MakeComputeEngineCredentials()), + std::nullopt, "project-id-resource", true}, + HostResourceTestCase{ + "ImpersonationFromComputeEngineCredentials", + Unified(ImpersonationFrom(MakeComputeEngineCredentials())), + std::nullopt, "project-id-resource", true}, + // Credentials from anywhere else. + HostResourceTestCase{"AccessToken", Unified(AccessTokenCredentials()), + "project-id-option", "project-id-option", false}, + HostResourceTestCase{"AccessTokenWithoutProject", + Unified(AccessTokenCredentials()), std::nullopt, + std::nullopt, false}, + HostResourceTestCase{ + "ImpersonationFromAccessToken", + Unified(ImpersonationFrom(AccessTokenCredentials())), + "project-id-option", "project-id-option", false}, + HostResourceTestCase{"ServiceAccountKey", Unified(ServiceAccount()), + std::nullopt, "project-id-credentials", false}, + HostResourceTestCase{"ServiceAccountKeyWithProjectOption", + Unified(ServiceAccount()), "project-id-option", + "project-id-option", false}, + // gRPC channel credentials are opaque, so the library cannot tell + // whether they come from the host. + HostResourceTestCase{"GrpcCredentials", + Options{}.set( + grpc::InsecureChannelCredentials()), + "project-id-option", "project-id-option", false}, + HostResourceTestCase{"GrpcCredentialsWithoutProject", + Options{}.set( + grpc::InsecureChannelCredentials()), + std::nullopt, std::nullopt, false}), + [](::testing::TestParamInfo const& info) { + return info.param.test_name; + }); + +struct CredentialsUniverseTestCase { + std::string test_name; + // The `UnifiedCredentialsOption` of the client. + std::shared_ptr credentials; + // An application-supplied `UniverseDomainOption`. The client uses the + // default endpoint of that universe. + std::optional universe_domain; + bool expected_export; +}; + +class GrpcMetricsExporterCredentialsUniverseTest + : public GrpcMetricsExporterConfigTest, + public ::testing::WithParamInterface {}; + +/// @test Verify metrics export requires a matching `universe_domain` for +/// credential types that carry one (service account, external account, and +/// authorized user JSON), defaulting to `googleapis.com` when the field is +/// absent, and skips export when the JSON cannot be read or parsed. +TEST_P(GrpcMetricsExporterCredentialsUniverseTest, + ExportsOnlyWithCredentialsForUniverseDomain) { + CredentialsUniverseTestCase const& test_case = GetParam(); + Options client_options = + EnabledOptionsWithProject().set( + test_case.credentials); + if (test_case.universe_domain) { + client_options.set( + *test_case.universe_domain); + } + std::optional const config = MakeMeterProviderConfig( + EmptyResource(), DefaultOptionsGrpc(client_options)); + EXPECT_EQ(config.has_value(), test_case.expected_export); +} + +INSTANTIATE_TEST_SUITE_P( + GrpcMetricsExporter, GrpcMetricsExporterCredentialsUniverseTest, + ::testing::Values( + // Service account keys. + CredentialsUniverseTestCase{"ServiceAccountWithoutUniverse", + ServiceAccount(), std::nullopt, true}, + CredentialsUniverseTestCase{ + "ServiceAccountWithoutUniverseInOtherUniverse", ServiceAccount(), + "ud.net", false}, + CredentialsUniverseTestCase{"ServiceAccountForDefaultUniverse", + ServiceAccountFor("googleapis.com"), + std::nullopt, true}, + CredentialsUniverseTestCase{ + "ServiceAccountForDefaultUniverseInOtherUniverse", + ServiceAccountFor("googleapis.com"), "ud.net", false}, + CredentialsUniverseTestCase{"ServiceAccountForOtherUniverse", + ServiceAccountFor("ud.net"), "ud.net", + true}, + CredentialsUniverseTestCase{"ServiceAccountForOtherUniverseMixedCase", + ServiceAccountFor("UD.Net"), "ud.net", + true}, + CredentialsUniverseTestCase{ + "ServiceAccountForOtherUniverseInDefaultUniverse", + ServiceAccountFor("ud.net"), std::nullopt, false}, + CredentialsUniverseTestCase{"ServiceAccountEmptyUniverse", + ServiceAccountFor(""), std::nullopt, false}, + CredentialsUniverseTestCase{"ServiceAccountNonStringUniverse", + ServiceAccountFor(42), std::nullopt, false}, + CredentialsUniverseTestCase{"ServiceAccountNotJson", + MakeServiceAccountCredentials("not-json"), + std::nullopt, false}, + CredentialsUniverseTestCase{"ServiceAccountNotAJsonObject", + MakeServiceAccountCredentials("[]"), + std::nullopt, false}, + CredentialsUniverseTestCase{ + "ServiceAccountFileMissing", + MakeServiceAccountCredentialsFromFile(MissingFile()), std::nullopt, + false}, + // The other credential types that name their universe. + CredentialsUniverseTestCase{"ExternalAccountWithoutUniverse", + ExternalAccount(), std::nullopt, true}, + CredentialsUniverseTestCase{"ExternalAccountForOtherUniverse", + ExternalAccountFor("ud.net"), "ud.net", + true}, + CredentialsUniverseTestCase{ + "ExternalAccountForOtherUniverseInDefaultUniverse", + ExternalAccountFor("ud.net"), std::nullopt, false}, + CredentialsUniverseTestCase{"AuthorizedUserWithoutUniverse", + AuthorizedUser(), std::nullopt, true}, + CredentialsUniverseTestCase{ + "AuthorizedUserForOtherUniverseInDefaultUniverse", + AuthorizedUserFor("ud.net"), std::nullopt, false}, + // Impersonation, by the credentials it starts from. + CredentialsUniverseTestCase{ + "ImpersonationFromServiceAccountForOtherUniverse", + ImpersonationFrom(ServiceAccountFor("ud.net")), "ud.net", true}, + CredentialsUniverseTestCase{ + "ImpersonationFromServiceAccountForOtherUniverseInDefaultUniverse", + ImpersonationFrom(ServiceAccountFor("ud.net")), std::nullopt, + false}, + CredentialsUniverseTestCase{"ImpersonationWithoutBaseCredentials", + ImpersonationFrom(nullptr), std::nullopt, + false}, + // Credentials that carry no universe are not compared. + CredentialsUniverseTestCase{"AccessTokenInOtherUniverse", + AccessTokenCredentials(), "ud.net", true}, + CredentialsUniverseTestCase{"ComputeEngineInOtherUniverse", + MakeComputeEngineCredentials(), "ud.net", + true}, + CredentialsUniverseTestCase{ + "ImpersonationFromAccessTokenInOtherUniverse", + ImpersonationFrom(AccessTokenCredentials()), "ud.net", true}, + // Credentials not supported for Cloud Monitoring export. + CredentialsUniverseTestCase{"ApiKeyInDefaultUniverse", + MakeApiKeyCredentials("test-only-api-key"), + std::nullopt, false}, + CredentialsUniverseTestCase{"ApiKeyInOtherUniverse", + MakeApiKeyCredentials("test-only-api-key"), + "ud.net", false}, + CredentialsUniverseTestCase{ + "GdchServiceAccountInDefaultUniverse", + MakeGDCHServiceAccountCredentials( + "{}", "https://test-only-audience.example.com"), + std::nullopt, false}, + CredentialsUniverseTestCase{ + "GdchServiceAccountInOtherUniverse", + MakeGDCHServiceAccountCredentials( + "{}", "https://test-only-audience.example.com"), + "ud.net", false}, + // Credentials that failed to load are passed on like any other. Each + // export then fails with the stored error. + CredentialsUniverseTestCase{ + "ErrorCredentialsInOtherUniverse", + internal::MakeErrorCredentials(Status(StatusCode::kPermissionDenied, + "test-only")), + "ud.net", true}), + [](::testing::TestParamInfo const& info) { + return info.param.test_name; + }); + +/// @test Verify the universe domain of a service account key is also checked +/// when the application names the key file instead of passing its contents. +TEST_F(GrpcMetricsExporterConfigTest, ReadsUniverseDomainOfServiceAccountFile) { + storage::testing::TempFile const key_file( + WithUniverseDomain(ServiceAccountKey(), "ud.net")); + Options const client_options = + EnabledOptionsWithProject().set( + MakeServiceAccountCredentialsFromFile(key_file.name())); + + std::optional const same_universe = MakeMeterProviderConfig( + EmptyResource(), + DefaultOptionsGrpc(Options(client_options) + .set("ud.net"))); + EXPECT_TRUE(same_universe.has_value()); + + std::optional const default_universe = + MakeMeterProviderConfig(EmptyResource(), + DefaultOptionsGrpc(client_options)); + EXPECT_FALSE(default_universe.has_value()); +} + +struct DefaultCredentialsTestCase { + std::string test_name; + // The contents of the file `GOOGLE_APPLICATION_CREDENTIALS` names. When + // unset, the variable names a file that does not exist. + std::optional adc_contents; + // An application-supplied `UniverseDomainOption`. The client uses the + // default endpoint of that universe. + std::optional universe_domain; + // An application-supplied `storage::ProjectIdOption`. + std::optional project_option; + // The monitoring project. When unset, no exporter is expected. + std::optional expected_project; +}; + +class GrpcMetricsExporterDefaultCredentialsTest + : public GrpcMetricsExporterConfigTest, + public ::testing::WithParamInterface {}; + +/// @test Verify default credentials resolved from an Application Default +/// Credentials file use the file's project and `universe_domain` with default +/// resource labels, and disable export when the file cannot be used by gRPC's +/// Google Default credentials. +TEST_P(GrpcMetricsExporterDefaultCredentialsTest, + FollowsApplicationDefaultCredentialsFile) { + DefaultCredentialsTestCase const& test_case = GetParam(); + if (test_case.adc_contents) { + SetApplicationDefaultCredentials(*test_case.adc_contents); + } else { + SetMissingApplicationDefaultCredentials(); + } + Options client_options = EnabledOptions(); + if (test_case.universe_domain) { + client_options.set( + *test_case.universe_domain); + } + if (test_case.project_option) { + client_options.set(*test_case.project_option); + } + std::optional const config = MakeMeterProviderConfig( + HostResource(), DefaultOptionsGrpc(client_options)); + if (!test_case.expected_project) { + EXPECT_FALSE(config.has_value()); + return; + } + ASSERT_TRUE(config.has_value()); + EXPECT_EQ(config->project, Project(*test_case.expected_project)); + EXPECT_THAT(MonitoredResourceLabels(*config), + AllOf(Contains(Pair("location", "global")), + Contains(Pair("cloud_platform", "unknown")), + Contains(Pair("host_id", "unknown")))); +} + +INSTANTIATE_TEST_SUITE_P( + GrpcMetricsExporter, GrpcMetricsExporterDefaultCredentialsTest, + ::testing::Values( + DefaultCredentialsTestCase{"ServiceAccount", ServiceAccountKey().dump(), + std::nullopt, std::nullopt, + "project-id-credentials"}, + DefaultCredentialsTestCase{"ServiceAccountWithProjectOption", + ServiceAccountKey().dump(), std::nullopt, + "project-id-option", "project-id-option"}, + DefaultCredentialsTestCase{ + "ServiceAccountForOtherUniverse", + WithUniverseDomain(ServiceAccountKey(), "ud.net"), "ud.net", + std::nullopt, "project-id-credentials"}, + DefaultCredentialsTestCase{ + "ServiceAccountForOtherUniverseInDefaultUniverse", + WithUniverseDomain(ServiceAccountKey(), "ud.net"), std::nullopt, + "project-id-option", std::nullopt}, + DefaultCredentialsTestCase{ + "ServiceAccountWithoutUniverseInOtherUniverse", + ServiceAccountKey().dump(), "ud.net", "project-id-option", + std::nullopt}, + DefaultCredentialsTestCase{"AuthorizedUser", + AuthorizedUserFile().dump(), std::nullopt, + "project-id-option", "project-id-option"}, + // gcloud user credentials name no project, and the host's is not + // used for them. + DefaultCredentialsTestCase{"AuthorizedUserWithoutProject", + AuthorizedUserFile().dump(), std::nullopt, + std::nullopt, std::nullopt}, + DefaultCredentialsTestCase{ + "AuthorizedUserForOtherUniverseInDefaultUniverse", + WithUniverseDomain(AuthorizedUserFile(), "ud.net"), std::nullopt, + "project-id-option", std::nullopt}, + DefaultCredentialsTestCase{"ExternalAccount", + ExternalAccountFile().dump(), std::nullopt, + "project-id-option", "project-id-option"}, + DefaultCredentialsTestCase{ + "ExternalAccountForOtherUniverseInDefaultUniverse", + WithUniverseDomain(ExternalAccountFile(), "ud.net"), std::nullopt, + "project-id-option", std::nullopt}, + DefaultCredentialsTestCase{ + "UnsupportedType", + nlohmann::json{{"type", "impersonated_service_account"}}.dump(), + std::nullopt, "project-id-option", std::nullopt}, + DefaultCredentialsTestCase{"NotJson", "not-json", std::nullopt, + "project-id-option", std::nullopt}, + DefaultCredentialsTestCase{"MissingFile", std::nullopt, std::nullopt, + "project-id-option", std::nullopt}), + [](::testing::TestParamInfo const& info) { + return info.param.test_name; + }); + +/// @test Verify the log line for a skipped exporter names the host of the +/// endpoint and the universe domain it was compared with. +TEST_F(GrpcMetricsExporterConfigTest, LogsEndpointHostAndUniverseDomain) { + ScopedLog log; + std::optional const config = MakeMeterProviderConfig( + EmptyResource(), + DefaultOptionsGrpc( + EnabledOptionsWithProject() + .set(AccessTokenCredentials()) + .set("storage.ud.net:443"))); + EXPECT_FALSE(config.has_value()); + EXPECT_THAT(log.ExtractLines(), + Contains(AllOf(HasSubstr("metrics export is disabled"), + HasSubstr("endpoint_host=storage.ud.net,"), + HasSubstr("universe_domain=googleapis.com")))); +} + +/// @test Verify the disabled-export log line records `(unrecognized)` instead +/// of the raw endpoint string when the endpoint has no extractable host. +TEST_F(GrpcMetricsExporterConfigTest, LogsPlaceholderForUnrecognizedEndpoint) { + ScopedLog log; + std::optional const config = MakeMeterProviderConfig( + EmptyResource(), + DefaultOptionsGrpc( + EnabledOptionsWithProject() + .set(AccessTokenCredentials()) + .set("unix:/tmp/test-only-storage.sock"))); + EXPECT_FALSE(config.has_value()); + std::vector const lines = log.ExtractLines(); + EXPECT_THAT(lines, + Contains(AllOf(HasSubstr("metrics export is disabled"), + HasSubstr("endpoint_host=(unrecognized)")))); + EXPECT_THAT(lines, Each(Not(HasSubstr("test-only-storage.sock")))); +} + +/// @test Verify the log line for credentials of another universe names both +/// universe domains. +TEST_F(GrpcMetricsExporterConfigTest, LogsCredentialsUniverseDomain) { + ScopedLog log; + std::optional const config = MakeMeterProviderConfig( + EmptyResource(), + DefaultOptionsGrpc( + EnabledOptionsWithProject().set( + MakeServiceAccountCredentials( + WithUniverseDomain(ServiceAccountKey(), "ud.net"))))); + EXPECT_FALSE(config.has_value()); + EXPECT_THAT(log.ExtractLines(), + Contains(AllOf(HasSubstr("metrics export is disabled"), + HasSubstr("credentials_universe_domain=ud.net"), + HasSubstr("universe_domain=googleapis.com")))); +} + +/// @test Verify the log line says so when the universe domain of the +/// credentials cannot be determined. +TEST_F(GrpcMetricsExporterConfigTest, LogsUnknownCredentialsUniverseDomain) { + ScopedLog log; + std::optional const config = MakeMeterProviderConfig( + EmptyResource(), + DefaultOptionsGrpc( + EnabledOptionsWithProject().set( + MakeServiceAccountCredentials("not-json")))); + EXPECT_FALSE(config.has_value()); + EXPECT_THAT( + log.ExtractLines(), + Contains(AllOf(HasSubstr("metrics export is disabled"), + HasSubstr("cannot determine the universe domain of the " + "credentials")))); +} + +/// @test Verify the log line reports unsupported credentials when export is +/// disabled for API key or GDCH credentials. +TEST_F(GrpcMetricsExporterConfigTest, LogsUnsupportedCredentials) { + for (std::shared_ptr const& credentials : { + MakeApiKeyCredentials("test-only-api-key"), + MakeGDCHServiceAccountCredentials( + "{}", "https://test-only-audience.example.com"), + }) { + ScopedLog log; + std::optional const config = MakeMeterProviderConfig( + EmptyResource(), + DefaultOptionsGrpc( + EnabledOptionsWithProject().set( + credentials))); + EXPECT_FALSE(config.has_value()); + EXPECT_THAT(log.ExtractLines(), + Contains(AllOf(HasSubstr("metrics export is disabled"), + HasSubstr("unsupported credentials")))); + } +} + +/// @test Verify nothing is logged about a skipped exporter when the exporter +/// is created. +TEST_F(GrpcMetricsExporterConfigTest, DoesNotLogWhenExporting) { + ScopedLog log; + std::optional const config = MakeMeterProviderConfig( + EmptyResource(), + DefaultOptionsGrpc( + EnabledOptionsWithProject().set( + AccessTokenCredentials()))); + EXPECT_TRUE(config.has_value()); + EXPECT_THAT(log.ExtractLines(), + Each(Not(HasSubstr("metrics export is disabled")))); +} + +struct ResourceDetectionTestCase { + std::string test_name; + // The options the application sets on the client. + Options client_options; +}; + +class GrpcMetricsExporterResourceDetectionTest + : public GrpcMetricsExporterConfigTest, + public ::testing::WithParamInterface {}; + +/// @test Verify `EnableGrpcMetrics()` skips GCP resource detection when the +/// client's options disable metrics export. +TEST_P(GrpcMetricsExporterResourceDetectionTest, SkipsDetectionWithoutExport) { + int detections = 0; + EnableGrpcMetrics(GetParam().client_options, [&detections] { + ++detections; + return HostResource(); + }); + EXPECT_EQ(detections, 0); +} + +INSTANTIATE_TEST_SUITE_P( + GrpcMetricsExporter, GrpcMetricsExporterResourceDetectionTest, + ::testing::Values( + ResourceDetectionTestCase{ + "MetricsDisabled", + Unified(AccessTokenCredentials()) + .set(false)}, + ResourceDetectionTestCase{ + "InsecureCredentials", + Unified(MakeInsecureCredentials()) + .set(true)}, + ResourceDetectionTestCase{ + "EndpointOutsideUniverseDomain", + Unified(AccessTokenCredentials()) + .set(true) + .set("storage.ud.net")}, + ResourceDetectionTestCase{ + "CredentialsForOtherUniverse", + Unified(ServiceAccountFor("ud.net")) + .set(true)}, + ResourceDetectionTestCase{ + "OTelMetricsInsecureCredentials", + Unified(MakeInsecureCredentials()) + .set(false) + .set(true)}, + ResourceDetectionTestCase{ + "OTelMetricsEndpointOutsideUniverseDomain", + Unified(AccessTokenCredentials()) + .set(false) + .set(true) + .set("storage.ud.net")}, + ResourceDetectionTestCase{ + "ApiKeyCredentials", + Unified(MakeApiKeyCredentials("test-only-api-key")) + .set(true)}, + ResourceDetectionTestCase{ + "OTelDebugMetricsCredentialsForOtherUniverse", + Unified(ServiceAccountFor("ud.net")) + .set(false) + .set( + true)}), + [](::testing::TestParamInfo const& info) { + return info.param.test_name; + }); + +/// @test Verify the emulator endpoint disables export and skips resource +/// detection even when `GCP_STORAGE_CPP_ENABLE_OTEL_METRICS` and +/// `GCP_STORAGE_CPP_ENABLE_OTEL_DEBUG_METRICS` are enabled. +TEST_F(GrpcMetricsExporterConfigTest, + SkipsExportForEmulatorWithOTelMetricsEnv) { + ScopedEnvironment const emulator( + "CLOUD_STORAGE_EXPERIMENTAL_GRPC_TESTBENCH_ENDPOINT", "localhost:1234"); + ScopedEnvironment const otel_metrics("GCP_STORAGE_CPP_ENABLE_OTEL_METRICS", + "true"); + ScopedEnvironment const otel_debug_metrics( + "GCP_STORAGE_CPP_ENABLE_OTEL_DEBUG_METRICS", "true"); + Options const client_options = + Options{}.set("project-id-option"); + + int detections = 0; + EnableGrpcMetrics(client_options, [&detections] { + ++detections; + return HostResource(); + }); + EXPECT_EQ(detections, 0); + + std::optional const config = MakeMeterProviderConfig( + HostResource(), DefaultOptionsGrpc(client_options)); + EXPECT_FALSE(config.has_value()); +} + +/// @test Verify OTel-only metrics configurations (`EnableGrpcMetricsOption` +/// disabled with `EnableOTelMetricsOption` or `EnableOTelDebugMetricsOption` +/// enabled) forward client connection options, select non-host resource +/// attributes for key-file credentials, and enforce universe-domain matching. +TEST_F(GrpcMetricsExporterConfigTest, + OTelOnlyMetricsApplyClientOptionsAndResourceMode) { + std::shared_ptr const credentials = ServiceAccount(); + Options const base_options = + Options{} + .set(false) + .set(credentials) + .set("/test-only/ca-certificates.pem") + .set( + ProxyConfig().set_hostname("proxy.example.com").set_port("3128")); + + for (Options const& enabled : + {Options(base_options) + .set(true), + Options(base_options) + .set(true)}) { + std::optional const config = + MakeMeterProviderConfig(HostResource(), DefaultOptionsGrpc(enabled)); + ASSERT_TRUE(config.has_value()); + EXPECT_FALSE(config->enable_grpc_plugin); + EXPECT_EQ(config->project, Project("project-id-credentials")); + EXPECT_THAT(MonitoredResourceLabels(*config), + AllOf(Contains(Pair("project_id", "project-id-credentials")), + Contains(Pair("location", "global")), + Contains(Pair("cloud_platform", "unknown")), + Contains(Pair("host_id", "unknown")))); + Options const& exporter = config->exporter_connection_options; + EXPECT_THAT(exporter.get(), Eq(credentials)); + EXPECT_EQ(exporter.get(), + "/test-only/ca-certificates.pem"); + EXPECT_EQ(exporter.get().hostname(), "proxy.example.com"); + + EXPECT_FALSE(MakeMeterProviderConfig( + HostResource(), + DefaultOptionsGrpc(Options(enabled).set( + "storage.ud.net"))) + .has_value()); + EXPECT_FALSE( + MakeMeterProviderConfig( + HostResource(), + DefaultOptionsGrpc(Options(enabled).set( + ServiceAccountFor("ud.net")))) + .has_value()); + } +} + +/// @test Verify the resource is detected once for a client using metadata +/// credentials, and the exporter is created. The authority is unique to this +/// test because the registry of exporters is process-wide. +TEST_F(GrpcMetricsExporterConfigTest, DetectsResourceOnceWhenExporting) { + std::string const authority = "test-only-detects-resource.example.com"; + int detections = 0; + EnableGrpcMetrics( + EnabledOptionsWithProject() + .set(MakeComputeEngineCredentials()) + .set(authority), + [&detections] { + ++detections; + return HostResource(); + }); + ScopedProviderShutdown const provider_shutdown(FindMeterProvider(authority)); + EXPECT_EQ(detections, 1); + EXPECT_THAT(FindMeterProvider(authority), NotNull()); +} + +/// @test Verify `EnableGrpcMetrics()` skips GCP resource detection and still +/// creates an exporter when the client uses non-host credentials. +TEST_F(GrpcMetricsExporterConfigTest, + SkipsResourceDetectionForNonHostCredentials) { + std::string const authority = + "test-only-skips-detection-non-host.example.com"; + int detections = 0; + EnableGrpcMetrics(EnabledOptionsWithProject() + .set(AccessTokenCredentials()) + .set(authority), + [&detections] { + ++detections; + return HostResource(); + }); + ScopedProviderShutdown const provider_shutdown(FindMeterProvider(authority)); + EXPECT_EQ(detections, 0); + EXPECT_THAT(FindMeterProvider(authority), NotNull()); +} + +/// @test Verify clients sharing an authority reuse the first client's +/// `MeterProvider` even when later clients supply different credentials. +TEST_F(GrpcMetricsExporterConfigTest, + ReusesFirstExporterPerAuthorityAcrossCredentials) { + std::string const authority = + "test-only-reuses-provider-across-credentials.example.com"; + EnableGrpcMetrics(EnabledOptionsWithProject() + .set(AccessTokenCredentials()) + .set(authority), + [] { return HostResource(); }); + std::shared_ptr const first = + FindMeterProvider(authority); + ScopedProviderShutdown const provider_shutdown(first); + ASSERT_THAT(first, NotNull()); + + EnableGrpcMetrics(EnabledOptions() + .set(ServiceAccount()) + .set(authority), + [] { return HostResource(); }); + EXPECT_THAT(FindMeterProvider(authority), Eq(first)); +} + +/// @test Verify the `MakeMeterProviderConfig()` overload taking a precomputed +/// `MetricsExportMode` honors the supplied mode directly instead of +/// re-evaluating `DetectMetricsExportMode()`, while still enforcing +/// `AnyMetricsEnabled()`. +TEST_F(GrpcMetricsExporterConfigTest, + MakeMeterProviderConfigUsesPrecomputedMode) { + Options const host_options = DefaultOptionsGrpc( + EnabledOptionsWithProject().set( + MakeComputeEngineCredentials())); + + // Passing `kDisabled` suppresses export even when `host_options` would + // otherwise detect `kWithHostResource`. + EXPECT_FALSE(MakeMeterProviderConfig(HostResource(), host_options, + MetricsExportMode::kDisabled) + .has_value()); + + // Passing `kWithoutHostResource` omits host project and labels instead of + // re-detecting `kWithHostResource`. + std::optional const without_host = MakeMeterProviderConfig( + HostResource(), host_options, MetricsExportMode::kWithoutHostResource); + ASSERT_TRUE(without_host.has_value()); + EXPECT_EQ(without_host->project, Project("project-id-option")); + EXPECT_THAT(MonitoredResourceLabels(*without_host), + AllOf(Contains(Pair("project_id", "project-id-option")), + Contains(Pair("location", "global")), + Contains(Pair("cloud_platform", "unknown")), + Contains(Pair("host_id", "unknown")))); + + // Passing `kWithHostResource` attaches the host project and resource labels. + std::optional const with_host = MakeMeterProviderConfig( + HostResource(), host_options, MetricsExportMode::kWithHostResource); + ASSERT_TRUE(with_host.has_value()); + EXPECT_EQ(with_host->project, Project("project-id-resource")); + EXPECT_THAT(MonitoredResourceLabels(*with_host), + AllOf(Contains(Pair("project_id", "project-id-resource")), + Contains(Pair("location", "us-central1-c")), + Contains(Pair("cloud_platform", "gcp_compute_engine")), + Contains(Pair("host_id", "test-host-id")))); + + // Calling the overload when all metrics options are disabled still returns + // `std::nullopt`. + Options const disabled_options = DefaultOptionsGrpc( + Options{} + .set(false) + .set(false) + .set(false) + .set("project-id-option") + .set(MakeComputeEngineCredentials())); + EXPECT_FALSE(MakeMeterProviderConfig(HostResource(), disabled_options, + MetricsExportMode::kWithHostResource) + .has_value()); +} + } // namespace GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END } // namespace storage_internal diff --git a/google/cloud/storage/internal/grpc/metrics_exporter_options.cc b/google/cloud/storage/internal/grpc/metrics_exporter_options.cc index 584aef992104a..8291214670c3c 100644 --- a/google/cloud/storage/internal/grpc/metrics_exporter_options.cc +++ b/google/cloud/storage/internal/grpc/metrics_exporter_options.cc @@ -16,15 +16,32 @@ #include "google/cloud/storage/internal/grpc/metrics_exporter_options.h" #include "google/cloud/opentelemetry/monitoring_exporter.h" +#include "google/cloud/storage/internal/grpc/channel_telemetry.h" #include "google/cloud/storage/internal/metrics_constants.h" #include "google/cloud/common_options.h" +#include "google/cloud/credentials.h" +#include "google/cloud/grpc_options.h" +#include "google/cloud/internal/credentials_impl.h" #include "google/cloud/internal/invocation_id_generator.h" +#include "google/cloud/internal/oauth2_universe_domain.h" +#include "google/cloud/internal/unified_grpc_credentials.h" +#include "google/cloud/log.h" +#include "google/cloud/status_or.h" #include "google/cloud/universe_domain_options.h" +#include "absl/strings/ascii.h" #include "absl/strings/match.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/str_split.h" #include "google/api/monitored_resource.pb.h" +#include #include #include #include +#include +#include +#include +#include +#include #include #include #include @@ -42,6 +59,141 @@ auto ByName(opentelemetry::sdk::resource::ResourceAttributes const& attributes, return std::get(l->second); } +template +void Forward(Options const& from, Options& to) { + if (from.has