Skip to content

Commit

Permalink
gcp-observability: add custom tags to metrics and traces using Stackd…
Browse files Browse the repository at this point in the history
…river config-builders (#9407)
  • Loading branch information
sanjaypujare committed Jul 26, 2022
1 parent cc9505f commit e89d43d
Showing 1 changed file with 21 additions and 2 deletions.
Expand Up @@ -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")
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -139,14 +145,21 @@ private void setProducer(
clientInterceptors, serverInterceptors, tracerFactories);
}

private void registerStackDriverExporter(String projectId) throws IOException {
private void registerStackDriverExporter(String projectId, Map<String, String> customTags)
throws IOException {
if (config.isEnableCloudMonitoring()) {
RpcViews.registerAllGrpcViews();
StackdriverStatsConfiguration.Builder statsConfigurationBuilder =
StackdriverStatsConfiguration.builder();
if (projectId != null) {
statsConfigurationBuilder.setProjectId(projectId);
}
if (customTags != null) {
Map<LabelKey, LabelValue> 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;
}
Expand All @@ -160,6 +173,12 @@ private void registerStackDriverExporter(String projectId) throws IOException {
if (projectId != null) {
traceConfigurationBuilder.setProjectId(projectId);
}
if (customTags != null) {
Map<String, AttributeValue> fixedAttributes = customTags.entrySet().stream()
.collect(Collectors.toMap(e -> e.getKey(),
e -> AttributeValue.stringAttributeValue(e.getValue())));
traceConfigurationBuilder.setFixedAttributes(fixedAttributes);
}
StackdriverTraceExporter.createAndRegister(traceConfigurationBuilder.build());
tracesEnabled = true;
}
Expand Down

0 comments on commit e89d43d

Please sign in to comment.