Skip to content

Commit 27829a4

Browse files
authored
fix: set default values for monitored resource (#2809)
* fix: set default values for monitored resource * use variable...
1 parent 6186781 commit 27829a4

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

google-cloud-storage/src/main/java/com/google/cloud/storage/OpenTelemetryBootstrappingUtils.java

+26-13
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import io.grpc.opentelemetry.GrpcOpenTelemetry;
3232
import io.opentelemetry.api.common.AttributeKey;
3333
import io.opentelemetry.api.common.Attributes;
34+
import io.opentelemetry.api.common.AttributesBuilder;
3435
import io.opentelemetry.api.internal.StringUtils;
3536
import io.opentelemetry.contrib.gcp.resource.GCPResourceProvider;
3637
import io.opentelemetry.sdk.OpenTelemetrySdk;
@@ -196,24 +197,36 @@ static SdkMeterProvider createMeterProvider(
196197
shouldSuppressExceptions
197198
? new PermissionDeniedSingleReportMetricsExporter(cloudMonitoringExporter)
198199
: cloudMonitoringExporter;
200+
AttributesBuilder attributesBuilder =
201+
Attributes.builder()
202+
.put("gcp.resource_type", "storage.googleapis.com/Client")
203+
.put("project_id", projectIdToUse)
204+
.put("instance_id", UUID.randomUUID().toString())
205+
.put("api", "grpc");
206+
String detectedLocation = detectedAttributes.get(AttributeKey.stringKey("cloud.region"));
207+
if (detectedLocation != null) {
208+
attributesBuilder.put("location", detectedLocation);
209+
} else {
210+
attributesBuilder.put("location", "global");
211+
}
212+
String detectedCloudPlatform = detectedAttributes.get(AttributeKey.stringKey("cloud.platform"));
213+
if (detectedCloudPlatform != null) {
214+
attributesBuilder.put("cloud_platform", detectedCloudPlatform);
215+
} else {
216+
attributesBuilder.put("cloud_platform", "unknown");
217+
}
218+
String detectedHostId = detectedAttributes.get(AttributeKey.stringKey("host.id"));
219+
if (detectedHostId != null) {
220+
attributesBuilder.put("host_id", detectedHostId);
221+
} else {
222+
attributesBuilder.put("host_id", "unknown");
223+
}
199224
providerBuilder
200225
.registerMetricReader(
201226
PeriodicMetricReader.builder(exporter)
202227
.setInterval(java.time.Duration.ofSeconds(60))
203228
.build())
204-
.setResource(
205-
Resource.create(
206-
Attributes.builder()
207-
.put("gcp.resource_type", "storage.googleapis.com/Client")
208-
.put("location", detectedAttributes.get(AttributeKey.stringKey("cloud.region")))
209-
.put("project_id", projectIdToUse)
210-
.put(
211-
"cloud_platform",
212-
detectedAttributes.get(AttributeKey.stringKey("cloud.platform")))
213-
.put("host_id", detectedAttributes.get(AttributeKey.stringKey("host.id")))
214-
.put("instance_id", UUID.randomUUID().toString())
215-
.put("api", "grpc")
216-
.build()));
229+
.setResource(Resource.create(attributesBuilder.build()));
217230

218231
addHistogramView(
219232
providerBuilder, latencyHistogramBoundaries(), "grpc/client/attempt/duration", "s");

google-cloud-storage/src/test/java/com/google/cloud/storage/ITGrpcMetricsTest.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,15 @@ public void testGrpcMetrics() {
5050

5151
// What the project ID will be will depend on the environment, so we just make sure it's present
5252
// and not null/empty
53-
assertThat(result.contains("project_id"));
5453
assertThat(result).doesNotContain("project_id=\"\"");
5554
assertThat(result).doesNotContain("project_id=null");
55+
assertThat(result).contains("project_id");
56+
assertThat(result).contains("host_id");
57+
assertThat(result).contains("cloud_platform");
58+
assertThat(result).contains("location");
59+
assertThat(result).contains("instance_id");
60+
assertThat(result).contains("gcp.resource_type");
61+
assertThat(result).contains("api");
5662

5763
// This is the check for the Seconds histogram boundary. We can't practically check for every
5864
// boundary,

0 commit comments

Comments
 (0)