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 1 commit
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,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;

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,23 @@ 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 = new HashMap<>();
for (Map.Entry<String, String> mapEntry : customTags.entrySet()) {
constantLabels.put(LabelKey.create(mapEntry.getKey(), mapEntry.getKey()),
LabelValue.create(mapEntry.getValue()));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a nit, but for a mundane map translation, something like this might be more succinct:

Map<LabelKey, LabelValue> constantLabels = customTags.entrySet().stream()
    .collect(Collectors.toMap(k -> LabelKey.create(k, k), v -> LabelValue.create(v)));

Same for your other map translation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs to be this: do you agree?

       Map<LabelKey, LabelValue> constantLabels = customTags.entrySet().stream()
                .collect(Collectors.toMap(k -> LabelKey.create(k.getKey(), k.getKey()),
                   v -> LabelValue.create(v.getValue())));

because k and v are Map.Entry (and not Map.Key and Map.Value)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes - I did say "something like this" ;-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, PTAL

statsConfigurationBuilder.setConstantLabels(constantLabels);
}
StackdriverStatsExporter.createAndRegister(statsConfigurationBuilder.build());
metricsEnabled = true;
}
Expand All @@ -160,6 +175,14 @@ private void registerStackDriverExporter(String projectId) throws IOException {
if (projectId != null) {
traceConfigurationBuilder.setProjectId(projectId);
}
if (customTags != null) {
Map<String, AttributeValue> fixedAttributes = new HashMap<>();
for (Map.Entry<String, String> mapEntry : customTags.entrySet()) {
fixedAttributes.put(mapEntry.getKey(),
AttributeValue.stringAttributeValue(mapEntry.getValue()));
}
traceConfigurationBuilder.setFixedAttributes(fixedAttributes);
}
StackdriverTraceExporter.createAndRegister(traceConfigurationBuilder.build());
tracesEnabled = true;
}
Expand Down