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..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 @@ -39,12 +39,17 @@ 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.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") @@ -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,12 @@ private void registerStackDriverExporter(String projectId) throws IOException { if (projectId != null) { statsConfigurationBuilder.setProjectId(projectId); } + if (customTags != null) { + 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()); metricsEnabled = true; } @@ -160,6 +173,12 @@ private void registerStackDriverExporter(String projectId) throws IOException { if (projectId != null) { traceConfigurationBuilder.setProjectId(projectId); } + if (customTags != null) { + Map fixedAttributes = customTags.entrySet().stream() + .collect(Collectors.toMap(e -> e.getKey(), + e -> AttributeValue.stringAttributeValue(e.getValue()))); + traceConfigurationBuilder.setFixedAttributes(fixedAttributes); + } StackdriverTraceExporter.createAndRegister(traceConfigurationBuilder.build()); tracesEnabled = true; }