@@ -91,6 +91,10 @@ auto TestOptions(Options opts = {}) {
9191 ScopedEnvironment (" GCP_STORAGE_CPP_ENABLE_OTEL_METRICS" , std::nullopt );
9292 auto debug_env = ScopedEnvironment (
9393 " GCP_STORAGE_CPP_ENABLE_OTEL_DEBUG_METRICS" , std::nullopt );
94+ ScopedEnvironment const emulator_env (
95+ " CLOUD_STORAGE_EXPERIMENTAL_GRPC_TESTBENCH_ENDPOINT" , std::nullopt );
96+ ScopedEnvironment const universe_env (" GOOGLE_CLOUD_UNIVERSE_DOMAIN" ,
97+ std::nullopt );
9498 // Use the credentials of the host. Only for those does the resource passed
9599 // to `MakeMeterProviderConfig()` supply the monitoring project.
96100 opts.set <storage_experimental::EnableGrpcMetricsOption>(true )
@@ -1183,20 +1187,29 @@ INSTANTIATE_TEST_SUITE_P(
11831187 // Credentials that carry no universe are not compared.
11841188 CredentialsUniverseTestCase{" AccessTokenInOtherUniverse" ,
11851189 AccessTokenCredentials (), " ud.net" , true },
1186- CredentialsUniverseTestCase{" ApiKeyInOtherUniverse" ,
1187- MakeApiKeyCredentials (" test-only-api-key" ),
1188- " ud.net" , true },
11891190 CredentialsUniverseTestCase{" ComputeEngineInOtherUniverse" ,
11901191 MakeComputeEngineCredentials (), " ud.net" ,
11911192 true },
11921193 CredentialsUniverseTestCase{
11931194 " ImpersonationFromAccessTokenInOtherUniverse" ,
11941195 ImpersonationFrom (AccessTokenCredentials ()), " ud.net" , true },
1196+ // Credentials not supported for Cloud Monitoring export.
1197+ CredentialsUniverseTestCase{" ApiKeyInDefaultUniverse" ,
1198+ MakeApiKeyCredentials (" test-only-api-key" ),
1199+ std::nullopt , false },
1200+ CredentialsUniverseTestCase{" ApiKeyInOtherUniverse" ,
1201+ MakeApiKeyCredentials (" test-only-api-key" ),
1202+ " ud.net" , false },
1203+ CredentialsUniverseTestCase{
1204+ " GdchServiceAccountInDefaultUniverse" ,
1205+ MakeGDCHServiceAccountCredentials (
1206+ " {}" , " https://test-only-audience.example.com" ),
1207+ std::nullopt , false },
11951208 CredentialsUniverseTestCase{
11961209 " GdchServiceAccountInOtherUniverse" ,
11971210 MakeGDCHServiceAccountCredentials (
11981211 " {}" , " https://test-only-audience.example.com" ),
1199- " ud.net" , true },
1212+ " ud.net" , false },
12001213 // Credentials that failed to load are passed on like any other. Each
12011214 // export then fails with the stored error.
12021215 CredentialsUniverseTestCase{
@@ -1402,6 +1415,27 @@ TEST_F(GrpcMetricsExporterConfigTest, LogsUnknownCredentialsUniverseDomain) {
14021415 " credentials" ))));
14031416}
14041417
1418+ // / @test Verify the log line reports unsupported credentials when export is
1419+ // / disabled for API key or GDCH credentials.
1420+ TEST_F (GrpcMetricsExporterConfigTest, LogsUnsupportedCredentials) {
1421+ for (std::shared_ptr<Credentials> const & credentials : {
1422+ MakeApiKeyCredentials (" test-only-api-key" ),
1423+ MakeGDCHServiceAccountCredentials (
1424+ " {}" , " https://test-only-audience.example.com" ),
1425+ }) {
1426+ ScopedLog log;
1427+ std::optional<ExporterConfig> const config = MakeMeterProviderConfig (
1428+ EmptyResource (),
1429+ DefaultOptionsGrpc (
1430+ EnabledOptionsWithProject ().set <UnifiedCredentialsOption>(
1431+ credentials)));
1432+ EXPECT_FALSE (config.has_value ());
1433+ EXPECT_THAT (log.ExtractLines (),
1434+ Contains (AllOf (HasSubstr (" metrics export is disabled" ),
1435+ HasSubstr (" unsupported credentials" ))));
1436+ }
1437+ }
1438+
14051439// / @test Verify nothing is logged about a skipped exporter when the exporter
14061440// / is created.
14071441TEST_F (GrpcMetricsExporterConfigTest, DoesNotLogWhenExporting) {
@@ -1468,6 +1502,10 @@ INSTANTIATE_TEST_SUITE_P(
14681502 .set <storage_experimental::EnableGrpcMetricsOption>(false )
14691503 .set <storage_experimental::EnableOTelMetricsOption>(true )
14701504 .set <EndpointOption>(" storage.ud.net" )},
1505+ ResourceDetectionTestCase{
1506+ " ApiKeyCredentials" ,
1507+ Unified (MakeApiKeyCredentials (" test-only-api-key" ))
1508+ .set <storage_experimental::EnableGrpcMetricsOption>(true )},
14711509 ResourceDetectionTestCase{
14721510 " OTelDebugMetricsCredentialsForOtherUniverse" ,
14731511 Unified (ServiceAccountFor (" ud.net" ))
@@ -1554,12 +1592,32 @@ TEST_F(GrpcMetricsExporterConfigTest,
15541592 }
15551593}
15561594
1557- // / @test Verify the resource is detected once for a client that exports
1558- // / metrics , and the exporter is created. The authority is unique to this test
1559- // / because the registry of exporters is process-wide.
1595+ // / @test Verify the resource is detected once for a client using metadata
1596+ // / credentials , and the exporter is created. The authority is unique to this
1597+ // / test because the registry of exporters is process-wide.
15601598TEST_F (GrpcMetricsExporterConfigTest, DetectsResourceOnceWhenExporting) {
15611599 std::string const authority = " test-only-detects-resource.example.com" ;
15621600 int detections = 0 ;
1601+ EnableGrpcMetrics (
1602+ EnabledOptionsWithProject ()
1603+ .set <UnifiedCredentialsOption>(MakeComputeEngineCredentials ())
1604+ .set <AuthorityOption>(authority),
1605+ [&detections] {
1606+ ++detections;
1607+ return HostResource ();
1608+ });
1609+ ScopedProviderShutdown const provider_shutdown (FindMeterProvider (authority));
1610+ EXPECT_EQ (detections, 1 );
1611+ EXPECT_THAT (FindMeterProvider (authority), NotNull ());
1612+ }
1613+
1614+ // / @test Verify `EnableGrpcMetrics()` skips GCP resource detection and still
1615+ // / creates an exporter when the client uses non-host credentials.
1616+ TEST_F (GrpcMetricsExporterConfigTest,
1617+ SkipsResourceDetectionForNonHostCredentials) {
1618+ std::string const authority =
1619+ " test-only-skips-detection-non-host.example.com" ;
1620+ int detections = 0 ;
15631621 EnableGrpcMetrics (EnabledOptionsWithProject ()
15641622 .set <UnifiedCredentialsOption>(AccessTokenCredentials ())
15651623 .set <AuthorityOption>(authority),
@@ -1568,10 +1626,32 @@ TEST_F(GrpcMetricsExporterConfigTest, DetectsResourceOnceWhenExporting) {
15681626 return HostResource ();
15691627 });
15701628 ScopedProviderShutdown const provider_shutdown (FindMeterProvider (authority));
1571- EXPECT_EQ (detections, 1 );
1629+ EXPECT_EQ (detections, 0 );
15721630 EXPECT_THAT (FindMeterProvider (authority), NotNull ());
15731631}
15741632
1633+ // / @test Verify clients sharing an authority reuse the first client's
1634+ // / `MeterProvider` even when later clients supply different credentials.
1635+ TEST_F (GrpcMetricsExporterConfigTest,
1636+ ReusesFirstExporterPerAuthorityAcrossCredentials) {
1637+ std::string const authority =
1638+ " test-only-reuses-provider-across-credentials.example.com" ;
1639+ EnableGrpcMetrics (EnabledOptionsWithProject ()
1640+ .set <UnifiedCredentialsOption>(AccessTokenCredentials ())
1641+ .set <AuthorityOption>(authority),
1642+ [] { return HostResource (); });
1643+ std::shared_ptr<opentelemetry::metrics::MeterProvider> const first =
1644+ FindMeterProvider (authority);
1645+ ScopedProviderShutdown const provider_shutdown (first);
1646+ ASSERT_THAT (first, NotNull ());
1647+
1648+ EnableGrpcMetrics (EnabledOptions ()
1649+ .set <UnifiedCredentialsOption>(ServiceAccount ())
1650+ .set <AuthorityOption>(authority),
1651+ [] { return HostResource (); });
1652+ EXPECT_THAT (FindMeterProvider (authority), Eq (first));
1653+ }
1654+
15751655} // namespace
15761656GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
15771657} // namespace storage_internal
0 commit comments