Skip to content

Commit

Permalink
Support overriding ikey, cloud role name and cloud role instance per …
Browse files Browse the repository at this point in the history
…telemetry (#1630)
  • Loading branch information
trask authored Apr 21, 2021
1 parent 3c4e315 commit 82d5bd2
Show file tree
Hide file tree
Showing 14 changed files with 302 additions and 188 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.Collections;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import io.opentelemetry.instrumentation.api.aisdk.MicrometerUtil;
import io.opentelemetry.instrumentation.api.aisdk.MicrometerUtil.MicrometerUtilDelegate;
Expand All @@ -46,57 +45,62 @@ public static void setDelegate(final BytecodeUtilDelegate delegate) {
MicrometerUtil.setDelegate(new MicrometerUtilDelegate() {
@Override
public void trackMetric(String name, double value, Integer count, Double min, Double max, Map<String, String> properties) {
delegate.trackMetric(name, value, count, min, max, null, properties, Collections.emptyMap());
delegate.trackMetric(name, value, count, min, max, null, properties, Collections.emptyMap(), null);
}
});
}
}

public static void trackEvent(String name, Map<String, String> properties, Map<String, String> tags, Map<String, Double> metrics) {
public static void trackEvent(String name, Map<String, String> properties, Map<String, String> tags,
Map<String, Double> metrics, String instrumentationKey) {
if (delegate != null) {
delegate.trackEvent(name, properties, tags, metrics);
delegate.trackEvent(name, properties, tags, metrics, instrumentationKey);
}
}

public static void trackMetric(String name, double value, Integer count, Double min, Double max, Double stdDev,
Map<String, String> properties, Map<String, String> tags) {
Map<String, String> properties, Map<String, String> tags, String instrumentationKey) {
if (delegate != null) {
delegate.trackMetric(name, value, count, min, max, stdDev, properties, tags);
delegate.trackMetric(name, value, count, min, max, stdDev, properties, tags, instrumentationKey);
}
}

public static void trackDependency(String name, String id, String resultCode, Long totalMillis, boolean success,
String commandName, String type, String target, Map<String, String> properties,
Map<String, String> tags, Map<String, Double> metrics) {
Map<String, String> tags, Map<String, Double> metrics, String instrumentationKey) {
if (delegate != null) {
delegate.trackDependency(name, id, resultCode, totalMillis, success, commandName, type, target, properties,
tags, metrics);
tags, metrics, instrumentationKey);
}
}

public static void trackPageView(String name, URI uri, long totalMillis, Map<String, String> properties, Map<String, String> tags,
Map<String, Double> metrics) {
Map<String, Double> metrics, String instrumentationKey) {
if (delegate != null) {
delegate.trackPageView(name, uri, totalMillis, properties, tags, metrics);
delegate.trackPageView(name, uri, totalMillis, properties, tags, metrics, instrumentationKey);
}
}

public static void trackTrace(String message, int severityLevel, Map<String, String> properties, Map<String, String> tags) {
public static void trackTrace(String message, int severityLevel, Map<String, String> properties, Map<String, String> tags,
String instrumentationKey) {
if (delegate != null) {
delegate.trackTrace(message, severityLevel, properties, tags);
delegate.trackTrace(message, severityLevel, properties, tags, instrumentationKey);
}
}

public static void trackRequest(String id, String name, URL url, Date timestamp, Long duration, String responseCode, boolean success,
String source, Map<String, String> properties, Map<String, String> tags, Map<String, Double> metrics) {
String source, Map<String, String> properties, Map<String, String> tags, Map<String, Double> metrics,
String instrumentationKey) {
if (delegate != null) {
delegate.trackRequest(id, name, url, timestamp, duration, responseCode, success, source, properties, tags, metrics);
delegate.trackRequest(id, name, url, timestamp, duration, responseCode, success, source, properties, tags,
metrics, instrumentationKey);
}
}

public static void trackException(Exception exception, Map<String, String> properties, Map<String, String> tags, Map<String, Double> metrics) {
public static void trackException(Exception exception, Map<String, String> properties, Map<String, String> tags,
Map<String, Double> metrics, String instrumentationKey) {
if (delegate != null) {
delegate.trackException(exception, properties, tags, metrics);
delegate.trackException(exception, properties, tags, metrics, instrumentationKey);
}
}

Expand All @@ -120,53 +124,55 @@ public static long getTotalMilliseconds(long days, int hours, int minutes, int s
+ milliseconds;
}

// basically the same as SDK MapUtil.copy()
public static void copy(Map<String, String> source, Map<String, String> target) {
if (target == null) {
throw new IllegalArgumentException("target must not be null");
}

if (source == null || source.isEmpty()) {
// originally from SDK MapUtil.copy()
public static void copy(Map<String, String> source, Map<String, String> target, String excludePrefix) {
if (source == null) {
return;
}

for (Map.Entry<String, String> entry : source.entrySet()) {
String key = entry.getKey();
if (key == null || key.isEmpty()) {
continue;
}

if (excludePrefix != null && key.startsWith(excludePrefix)) {
continue;
}
if (!target.containsKey(key)) {
if (target instanceof ConcurrentHashMap && entry.getValue() == null) {
continue;
} else {
target.put(key, entry.getValue());
String value = entry.getValue();
if (value != null) {
target.put(key, value);
}
}
}
}

public interface BytecodeUtilDelegate {

void trackEvent(String name, Map<String, String> properties, Map<String, String> tags, Map<String, Double> metrics);
void trackEvent(String name, Map<String, String> properties, Map<String, String> tags, Map<String, Double> metrics,
String instrumentationKey);

void trackMetric(String name, double value, Integer count, Double min, Double max,
Double stdDev, Map<String, String> properties, Map<String, String> tags);
Double stdDev, Map<String, String> properties, Map<String, String> tags,
String instrumentationKey);

void trackDependency(String name, String id, String resultCode, Long totalMillis,
boolean success, String commandName, String type, String target,
Map<String, String> properties, Map<String, String> tags, Map<String, Double> metrics);
Map<String, String> properties, Map<String, String> tags, Map<String, Double> metrics,
String instrumentationKey);

void trackPageView(String name, URI uri, long totalMillis, Map<String, String> properties, Map<String, String> tags,
Map<String, Double> metrics);
Map<String, Double> metrics, String instrumentationKey);

void trackTrace(String message, int severityLevel, Map<String, String> properties, Map<String, String> tags);
void trackTrace(String message, int severityLevel, Map<String, String> properties, Map<String, String> tags,
String instrumentationKey);

void trackRequest(String id, String name, URL url, Date timestamp, Long duration, String responseCode, boolean success,
String source, Map<String, String> properties, Map<String, String> tags, Map<String, Double> metrics);
String source, Map<String, String> properties, Map<String, String> tags, Map<String, Double> metrics,
String instrumentationKey);

// TODO also handle cases where ExceptionTelemetry parsedStack is used directly instead of indirectly through Exception
void trackException(Exception exception, Map<String, String> properties, Map<String, String> tags, Map<String, Double> metrics);
void trackException(Exception exception, Map<String, String> properties, Map<String, String> tags,
Map<String, Double> metrics, String instrumentationKey);

void flush();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ private static void start(Instrumentation instrumentation) {
SystemInformation.INSTANCE.getProcessId(),
formServiceProfilerConfig(config.preview.profiler),
configuration.getRoleInstance(),
// TODO this will not work with Azure Spring Cloud updating connection string at runtime
configuration.getInstrumentationKey(),
telemetryClient,
formApplicationInsightsUserAgent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public class BytecodeUtilImpl implements BytecodeUtilDelegate {
private static final AtomicBoolean alreadyLoggedError = new AtomicBoolean();

@Override
public void trackEvent(String name, Map<String, String> properties, Map<String, String> tags, Map<String, Double> metrics) {
public void trackEvent(String name, Map<String, String> properties, Map<String, String> tags,
Map<String, Double> metrics, String instrumentationKey) {

if (Strings.isNullOrEmpty(name)) {
return;
Expand All @@ -67,14 +68,15 @@ public void trackEvent(String name, Map<String, String> properties, Map<String,
telemetry.getProperties().putAll(properties);
telemetry.getContext().getTags().putAll(tags);
telemetry.getMetrics().putAll(metrics);
telemetry.getContext().setInstrumentationKey(instrumentationKey);

track(telemetry);
}

// TODO do not track if perf counter (?)
@Override
public void trackMetric(String name, double value, Integer count, Double min, Double max,
Double stdDev, Map<String, String> properties, Map<String, String> tags) {
public void trackMetric(String name, double value, Integer count, Double min, Double max, Double stdDev,
Map<String, String> properties, Map<String, String> tags, String instrumentationKey) {

if (Strings.isNullOrEmpty(name)) {
return;
Expand All @@ -88,14 +90,16 @@ public void trackMetric(String name, double value, Integer count, Double min, Do
telemetry.setStandardDeviation(stdDev);
telemetry.getProperties().putAll(properties);
telemetry.getContext().getTags().putAll(tags);
telemetry.getContext().setInstrumentationKey(instrumentationKey);

track(telemetry);
}

@Override
public void trackDependency(String name, String id, String resultCode, @Nullable Long totalMillis,
boolean success, String commandName, String type, String target,
Map<String, String> properties, Map<String, String> tags, Map<String, Double> metrics) {
Map<String, String> properties, Map<String, String> tags, Map<String, Double> metrics,
String instrumentationKey) {

if (Strings.isNullOrEmpty(name)) {
return;
Expand All @@ -114,13 +118,14 @@ public void trackDependency(String name, String id, String resultCode, @Nullable
telemetry.getProperties().putAll(properties);
telemetry.getContext().getTags().putAll(tags);
telemetry.getMetrics().putAll(metrics);
telemetry.getContext().setInstrumentationKey(instrumentationKey);

track(telemetry);
}

@Override
public void trackPageView(String name, URI uri, long totalMillis, Map<String, String> properties,
Map<String, String> tags, Map<String, Double> metrics) {
Map<String, String> tags, Map<String, Double> metrics, String instrumentationKey) {

if (Strings.isNullOrEmpty(name)) {
return;
Expand All @@ -132,12 +137,14 @@ public void trackPageView(String name, URI uri, long totalMillis, Map<String, St
telemetry.getProperties().putAll(properties);
telemetry.getContext().getTags().putAll(tags);
telemetry.getMetrics().putAll(metrics);
telemetry.getContext().setInstrumentationKey(instrumentationKey);

track(telemetry);
}

@Override
public void trackTrace(String message, int severityLevel, Map<String, String> properties, Map<String, String> tags) {
public void trackTrace(String message, int severityLevel, Map<String, String> properties, Map<String, String> tags,
String instrumentationKey) {
if (Strings.isNullOrEmpty(message)) {
return;
}
Expand All @@ -149,13 +156,15 @@ public void trackTrace(String message, int severityLevel, Map<String, String> pr
}
telemetry.getProperties().putAll(properties);
telemetry.getContext().getTags().putAll(tags);
telemetry.getContext().setInstrumentationKey(instrumentationKey);

track(telemetry);
}

@Override
public void trackRequest(String id, String name, URL url, Date timestamp, @Nullable Long duration, String responseCode, boolean success,
String source, Map<String, String> properties, Map<String, String> tags, Map<String, Double> metrics) {
String source, Map<String, String> properties, Map<String, String> tags, Map<String, Double> metrics,
String instrumentationKey) {
if (Strings.isNullOrEmpty(name)) {
return;
}
Expand All @@ -176,13 +185,14 @@ public void trackRequest(String id, String name, URL url, Date timestamp, @Nulla
telemetry.getProperties().putAll(properties);
telemetry.getContext().getTags().putAll(tags);
telemetry.getMetrics().putAll(metrics);
telemetry.getContext().setInstrumentationKey(instrumentationKey);

track(telemetry);
}

@Override
public void trackException(Exception exception, Map<String, String> properties, Map<String, String> tags,
Map<String, Double> metrics) {
Map<String, Double> metrics, String instrumentationKey) {
if (exception == null) {
return;
}
Expand All @@ -193,6 +203,7 @@ public void trackException(Exception exception, Map<String, String> properties,
telemetry.getProperties().putAll(properties);
telemetry.getContext().getTags().putAll(tags);
telemetry.getMetrics().putAll(metrics);
telemetry.getContext().setInstrumentationKey(instrumentationKey);

track(telemetry);
}
Expand Down
Loading

0 comments on commit 82d5bd2

Please sign in to comment.