Skip to content

Commit

Permalink
gcp-o11y: add default custom tag for metrics exporter
Browse files Browse the repository at this point in the history
This PR adds a default custom tag for metrics, irrespective of custom
tags being present in the observability configuration. 

OpenCensus by default adds a custom tag
[opencenus_task](https://docs.google.com/document/d/1sWC-XD277cM0PXxAhzJKY2X0Uj2W7bVoSv-jvnA0N8Q/edit?resourcekey=0-l-wqh1fctxZXHCUrvZv2BQ#heading=h.xy85j580eik0)
for metrics which gets overriden if custom tags are set.

The unique custom tag is required to ensure the uniqueness of the
Timeseries. The format of the default custom tag is:
`java-{PID}@{HOSTNAME}`, if `{PID}` is not available a random number
will be used.
  • Loading branch information
DNVindhya authored and ejona86 committed Mar 23, 2023
1 parent fefa2d9 commit 9f26b7d
Showing 1 changed file with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@
import io.opencensus.trace.Tracing;
import io.opencensus.trace.config.TraceConfig;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
Expand All @@ -64,10 +69,13 @@ public final class GcpObservability implements AutoCloseable {

private static final Logger logger = Logger.getLogger(GcpObservability.class.getName());
private static final int METRICS_EXPORT_INTERVAL = 30;

static final String DEFAULT_METRIC_CUSTOM_TAG_KEY = "opencensus_task";
@VisibleForTesting
static final ImmutableSet<String> SERVICES_TO_EXCLUDE = ImmutableSet.of(
"google.logging.v2.LoggingServiceV2", "google.monitoring.v3.MetricService",
"google.devtools.cloudtrace.v2.TraceService");

private static GcpObservability instance = null;
private final Sink sink;
private final ObservabilityConfig config;
Expand Down Expand Up @@ -199,12 +207,17 @@ void registerStackDriverExporter(String projectId, Map<String, String> customTag
if (projectId != null) {
statsConfigurationBuilder.setProjectId(projectId);
}
Map<LabelKey, LabelValue> constantLabels = new HashMap<>();
constantLabels.put(
LabelKey.create(DEFAULT_METRIC_CUSTOM_TAG_KEY, DEFAULT_METRIC_CUSTOM_TAG_KEY),
LabelValue.create(generateDefaultMetricTagValue()));
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);
for (Map.Entry<String, String> mapEntry : customTags.entrySet()) {
constantLabels.putIfAbsent(LabelKey.create(mapEntry.getKey(), mapEntry.getKey()),
LabelValue.create(mapEntry.getValue()));
}
}
statsConfigurationBuilder.setConstantLabels(constantLabels);
statsConfigurationBuilder.setExportInterval(Duration.create(METRICS_EXPORT_INTERVAL, 0));
StackdriverStatsExporter.createAndRegister(statsConfigurationBuilder.build());
}
Expand All @@ -228,6 +241,20 @@ void registerStackDriverExporter(String projectId, Map<String, String> customTag
}
}

private static String generateDefaultMetricTagValue() {
final String jvmName = ManagementFactory.getRuntimeMXBean().getName();
if (jvmName.indexOf('@') < 1) {
String hostname = "localhost";
try {
hostname = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
logger.log(Level.INFO, "Unable to get the hostname.", e);
}
return "java-" + new SecureRandom().nextInt() + "@" + hostname;
}
return "java-" + jvmName;
}

private GcpObservability(
Sink sink,
ObservabilityConfig config) {
Expand Down

0 comments on commit 9f26b7d

Please sign in to comment.