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

gcp-observability: add custom tags to metrics and traces using Stackdriver config-builders #9407

Merged
merged 2 commits into from Jul 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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