Skip to content

Commit

Permalink
fix: set default values for monitored resource (#2809)
Browse files Browse the repository at this point in the history
* fix: set default values for monitored resource

* use variable...
  • Loading branch information
frankyn authored Nov 5, 2024
1 parent 6186781 commit 27829a4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 27829a4

Please sign in to comment.