|
31 | 31 | import io.grpc.opentelemetry.GrpcOpenTelemetry;
|
32 | 32 | import io.opentelemetry.api.common.AttributeKey;
|
33 | 33 | import io.opentelemetry.api.common.Attributes;
|
| 34 | +import io.opentelemetry.api.common.AttributesBuilder; |
34 | 35 | import io.opentelemetry.api.internal.StringUtils;
|
35 | 36 | import io.opentelemetry.contrib.gcp.resource.GCPResourceProvider;
|
36 | 37 | import io.opentelemetry.sdk.OpenTelemetrySdk;
|
@@ -196,24 +197,36 @@ static SdkMeterProvider createMeterProvider(
|
196 | 197 | shouldSuppressExceptions
|
197 | 198 | ? new PermissionDeniedSingleReportMetricsExporter(cloudMonitoringExporter)
|
198 | 199 | : 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 | + } |
199 | 224 | providerBuilder
|
200 | 225 | .registerMetricReader(
|
201 | 226 | PeriodicMetricReader.builder(exporter)
|
202 | 227 | .setInterval(java.time.Duration.ofSeconds(60))
|
203 | 228 | .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())); |
217 | 230 |
|
218 | 231 | addHistogramView(
|
219 | 232 | providerBuilder, latencyHistogramBoundaries(), "grpc/client/attempt/duration", "s");
|
|
0 commit comments