Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set default values for monitored resource #2809

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could we use something to detect grpc vs http even if this isn't set up for http yet? would make this reusable in the future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIU, resource detector is platform dependent on https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/processor/resourcedetectionprocessor/README.md#gcp-metadata

API type would need to be captured client side / added when introduced into the java storage client.

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
Loading