diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/OpenTelemetryBootstrappingUtils.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/OpenTelemetryBootstrappingUtils.java index fcf1440b4..fd24ad0b1 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/OpenTelemetryBootstrappingUtils.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/OpenTelemetryBootstrappingUtils.java @@ -31,6 +31,7 @@ import io.grpc.opentelemetry.GrpcOpenTelemetry; import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.api.internal.StringUtils; import io.opentelemetry.contrib.gcp.resource.GCPResourceProvider; import io.opentelemetry.sdk.OpenTelemetrySdk; @@ -196,24 +197,36 @@ static SdkMeterProvider createMeterProvider( shouldSuppressExceptions ? new PermissionDeniedSingleReportMetricsExporter(cloudMonitoringExporter) : cloudMonitoringExporter; + AttributesBuilder attributesBuilder = + Attributes.builder() + .put("gcp.resource_type", "storage.googleapis.com/Client") + .put("project_id", projectIdToUse) + .put("instance_id", UUID.randomUUID().toString()) + .put("api", "grpc"); + String detectedLocation = detectedAttributes.get(AttributeKey.stringKey("cloud.region")); + if (detectedLocation != null) { + attributesBuilder.put("location", detectedLocation); + } else { + attributesBuilder.put("location", "global"); + } + String detectedCloudPlatform = detectedAttributes.get(AttributeKey.stringKey("cloud.platform")); + if (detectedCloudPlatform != null) { + attributesBuilder.put("cloud_platform", detectedCloudPlatform); + } else { + attributesBuilder.put("cloud_platform", "unknown"); + } + String detectedHostId = detectedAttributes.get(AttributeKey.stringKey("host.id")); + if (detectedHostId != null) { + attributesBuilder.put("host_id", detectedHostId); + } else { + attributesBuilder.put("host_id", "unknown"); + } providerBuilder .registerMetricReader( PeriodicMetricReader.builder(exporter) .setInterval(java.time.Duration.ofSeconds(60)) .build()) - .setResource( - Resource.create( - Attributes.builder() - .put("gcp.resource_type", "storage.googleapis.com/Client") - .put("location", detectedAttributes.get(AttributeKey.stringKey("cloud.region"))) - .put("project_id", projectIdToUse) - .put( - "cloud_platform", - detectedAttributes.get(AttributeKey.stringKey("cloud.platform"))) - .put("host_id", detectedAttributes.get(AttributeKey.stringKey("host.id"))) - .put("instance_id", UUID.randomUUID().toString()) - .put("api", "grpc") - .build())); + .setResource(Resource.create(attributesBuilder.build())); addHistogramView( providerBuilder, latencyHistogramBoundaries(), "grpc/client/attempt/duration", "s"); diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/ITGrpcMetricsTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/ITGrpcMetricsTest.java index f1a406ca6..4c82054ff 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/ITGrpcMetricsTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/ITGrpcMetricsTest.java @@ -50,9 +50,15 @@ public void testGrpcMetrics() { // What the project ID will be will depend on the environment, so we just make sure it's present // and not null/empty - assertThat(result.contains("project_id")); assertThat(result).doesNotContain("project_id=\"\""); assertThat(result).doesNotContain("project_id=null"); + assertThat(result).contains("project_id"); + assertThat(result).contains("host_id"); + assertThat(result).contains("cloud_platform"); + assertThat(result).contains("location"); + assertThat(result).contains("instance_id"); + assertThat(result).contains("gcp.resource_type"); + assertThat(result).contains("api"); // This is the check for the Seconds histogram boundary. We can't practically check for every // boundary,