From 4a99ba147b4765d3b6eb6252b9c6fe85c45002a2 Mon Sep 17 00:00:00 2001 From: Sanjay Pujare Date: Sun, 24 Jul 2022 14:42:55 +0530 Subject: [PATCH 1/2] gcp-observability: add custom tags to metrics and traces using Stackdriver config-builders --- .../gcp/observability/GcpObservability.java | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/gcp-observability/src/main/java/io/grpc/gcp/observability/GcpObservability.java b/gcp-observability/src/main/java/io/grpc/gcp/observability/GcpObservability.java index 1eb243a7940..411ba2f2e21 100644 --- a/gcp-observability/src/main/java/io/grpc/gcp/observability/GcpObservability.java +++ b/gcp-observability/src/main/java/io/grpc/gcp/observability/GcpObservability.java @@ -39,10 +39,15 @@ import io.opencensus.exporter.stats.stackdriver.StackdriverStatsExporter; import io.opencensus.exporter.trace.stackdriver.StackdriverTraceConfiguration; import io.opencensus.exporter.trace.stackdriver.StackdriverTraceExporter; +import io.opencensus.metrics.LabelKey; +import io.opencensus.metrics.LabelValue; +import io.opencensus.trace.AttributeValue; import io.opencensus.trace.Tracing; import io.opencensus.trace.config.TraceConfig; import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -78,7 +83,8 @@ public static synchronized GcpObservability grpcInit() throws IOException { instance = grpcInit(sink, observabilityConfig, new InternalLoggingChannelInterceptor.FactoryImpl(helper, configFilterHelper), new InternalLoggingServerInterceptor.FactoryImpl(helper, configFilterHelper)); - instance.registerStackDriverExporter(observabilityConfig.getDestinationProjectId()); + instance.registerStackDriverExporter(observabilityConfig.getDestinationProjectId(), + observabilityConfig.getCustomTags()); } return instance; } @@ -139,7 +145,8 @@ private void setProducer( clientInterceptors, serverInterceptors, tracerFactories); } - private void registerStackDriverExporter(String projectId) throws IOException { + private void registerStackDriverExporter(String projectId, Map customTags) + throws IOException { if (config.isEnableCloudMonitoring()) { RpcViews.registerAllGrpcViews(); StackdriverStatsConfiguration.Builder statsConfigurationBuilder = @@ -147,6 +154,14 @@ private void registerStackDriverExporter(String projectId) throws IOException { if (projectId != null) { statsConfigurationBuilder.setProjectId(projectId); } + if (customTags != null) { + Map constantLabels = new HashMap<>(); + for (Map.Entry mapEntry : customTags.entrySet()) { + constantLabels.put(LabelKey.create(mapEntry.getKey(), mapEntry.getKey()), + LabelValue.create(mapEntry.getValue())); + } + statsConfigurationBuilder.setConstantLabels(constantLabels); + } StackdriverStatsExporter.createAndRegister(statsConfigurationBuilder.build()); metricsEnabled = true; } @@ -160,6 +175,14 @@ private void registerStackDriverExporter(String projectId) throws IOException { if (projectId != null) { traceConfigurationBuilder.setProjectId(projectId); } + if (customTags != null) { + Map fixedAttributes = new HashMap<>(); + for (Map.Entry mapEntry : customTags.entrySet()) { + fixedAttributes.put(mapEntry.getKey(), + AttributeValue.stringAttributeValue(mapEntry.getValue())); + } + traceConfigurationBuilder.setFixedAttributes(fixedAttributes); + } StackdriverTraceExporter.createAndRegister(traceConfigurationBuilder.build()); tracesEnabled = true; } From 79935899d3c03576a3bd46ad7318b08321c3242c Mon Sep 17 00:00:00 2001 From: Sanjay Pujare Date: Tue, 26 Jul 2022 11:26:26 +0530 Subject: [PATCH 2/2] address review comments --- .../gcp/observability/GcpObservability.java | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/gcp-observability/src/main/java/io/grpc/gcp/observability/GcpObservability.java b/gcp-observability/src/main/java/io/grpc/gcp/observability/GcpObservability.java index 411ba2f2e21..d5a608d83b0 100644 --- a/gcp-observability/src/main/java/io/grpc/gcp/observability/GcpObservability.java +++ b/gcp-observability/src/main/java/io/grpc/gcp/observability/GcpObservability.java @@ -46,10 +46,10 @@ import io.opencensus.trace.config.TraceConfig; import java.io.IOException; import java.util.ArrayList; -import java.util.HashMap; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.stream.Collectors; /** The main class for gRPC Google Cloud Platform Observability features. */ @ExperimentalApi("https://github.com/grpc/grpc-java/issues/8869") @@ -155,11 +155,9 @@ private void registerStackDriverExporter(String projectId, Map c statsConfigurationBuilder.setProjectId(projectId); } if (customTags != null) { - Map constantLabels = new HashMap<>(); - for (Map.Entry mapEntry : customTags.entrySet()) { - constantLabels.put(LabelKey.create(mapEntry.getKey(), mapEntry.getKey()), - LabelValue.create(mapEntry.getValue())); - } + Map constantLabels = customTags.entrySet().stream() + .collect(Collectors.toMap(e -> LabelKey.create(e.getKey(), e.getKey()), + e -> LabelValue.create(e.getValue()))); statsConfigurationBuilder.setConstantLabels(constantLabels); } StackdriverStatsExporter.createAndRegister(statsConfigurationBuilder.build()); @@ -176,11 +174,9 @@ private void registerStackDriverExporter(String projectId, Map c traceConfigurationBuilder.setProjectId(projectId); } if (customTags != null) { - Map fixedAttributes = new HashMap<>(); - for (Map.Entry mapEntry : customTags.entrySet()) { - fixedAttributes.put(mapEntry.getKey(), - AttributeValue.stringAttributeValue(mapEntry.getValue())); - } + Map fixedAttributes = customTags.entrySet().stream() + .collect(Collectors.toMap(e -> e.getKey(), + e -> AttributeValue.stringAttributeValue(e.getValue()))); traceConfigurationBuilder.setFixedAttributes(fixedAttributes); } StackdriverTraceExporter.createAndRegister(traceConfigurationBuilder.build());