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-o11y: add default custom tag for metrics exporter #9982

Merged
merged 2 commits into from
Mar 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

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