Skip to content
Merged
Show file tree
Hide file tree
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 @@ -22,8 +22,10 @@

import java.net.URI;
import java.net.URL;
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 @@ -44,56 +46,63 @@ 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);
delegate.trackMetric(name, value, count, min, max, null, properties, Collections.emptyMap());
}
});
}
}

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

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

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, Double> metrics) {
Map<String, String> tags, Map<String, Double> metrics) {
if (delegate != null) {
delegate.trackDependency(name, id, resultCode, totalMillis, success, commandName, type, target, properties,
metrics);
tags, metrics);
}
}

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

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

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

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

public static void flush() {
if (delegate != null) {
delegate.flush();
}
}

Expand All @@ -111,25 +120,54 @@ public static long getTotalMilliseconds(long days, int hours, int minutes, int s
+ milliseconds;
}

// basically the same as SDK MapUtil.copy()
Comment thread
trask marked this conversation as resolved.
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()) {
return;
}

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

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

public interface BytecodeUtilDelegate {

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

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

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

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

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

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

void trackRequest(String id, String name, URL url, Date timestamp, long duration, String responseCode, boolean success);
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, Double> metrics);
void flush();

void logErrorOnce(Throwable t);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import java.util.concurrent.atomic.AtomicBoolean;

import com.google.common.base.Strings;
import com.microsoft.applicationinsights.agent.internal.Global;
import com.microsoft.applicationinsights.agent.bootstrap.BytecodeUtil.BytecodeUtilDelegate;
import com.microsoft.applicationinsights.agent.internal.Global;
import com.microsoft.applicationinsights.agent.internal.sampling.SamplingScoreGeneratorV2;
import com.microsoft.applicationinsights.internal.util.MapUtil;
import com.microsoft.applicationinsights.telemetry.Duration;
Expand All @@ -55,29 +55,28 @@
// supporting all properties of event, metric, remove dependency and page view telemetry
public class BytecodeUtilImpl implements BytecodeUtilDelegate {

private static final Tracer tracer = OpenTelemetry.getGlobalTracer("");

private static final Logger logger = LoggerFactory.getLogger(BytecodeUtilImpl.class);

private static final AtomicBoolean alreadyLoggedError = new AtomicBoolean();

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

if (Strings.isNullOrEmpty(name)) {
return;
}
EventTelemetry telemetry = new EventTelemetry(name);
MapUtil.copy(properties, telemetry.getContext().getProperties());
MapUtil.copy(metrics, telemetry.getMetrics());
telemetry.getProperties().putAll(properties);
Comment thread
trask marked this conversation as resolved.
telemetry.getContext().getTags().putAll(tags);
telemetry.getMetrics().putAll(metrics);

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) {
Double stdDev, Map<String, String> properties, Map<String, String> tags) {

if (Strings.isNullOrEmpty(name)) {
return;
Expand All @@ -89,15 +88,16 @@ public void trackMetric(String name, double value, Integer count, Double min, Do
telemetry.setMin(min);
telemetry.setMax(max);
telemetry.setStandardDeviation(stdDev);
MapUtil.copy(properties, telemetry.getProperties());
telemetry.getProperties().putAll(properties);
telemetry.getContext().getTags().putAll(tags);

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, Double> metrics) {
Map<String, String> properties, Map<String, String> tags, Map<String, Double> metrics) {

if (Strings.isNullOrEmpty(name)) {
return;
Expand All @@ -113,15 +113,16 @@ public void trackDependency(String name, String id, String resultCode, @Nullable
telemetry.setCommandName(commandName);
telemetry.setType(type);
telemetry.setTarget(target);
MapUtil.copy(properties, telemetry.getProperties());
MapUtil.copy(metrics, telemetry.getMetrics());
telemetry.getProperties().putAll(properties);
telemetry.getContext().getTags().putAll(tags);
telemetry.getMetrics().putAll(metrics);

track(telemetry);
}

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

if (Strings.isNullOrEmpty(name)) {
return;
Expand All @@ -130,14 +131,15 @@ public void trackPageView(String name, URI uri, long totalMillis, Map<String, St
telemetry.setName(name);
telemetry.setUrl(uri);
telemetry.setDuration(totalMillis);
MapUtil.copy(properties, telemetry.getProperties());
MapUtil.copy(metrics, telemetry.getMetrics());
telemetry.getProperties().putAll(properties);
telemetry.getContext().getTags().putAll(tags);
telemetry.getMetrics().putAll(metrics);

track(telemetry);
}

@Override
public void trackTrace(String message, int severityLevel, Map<String, String> properties) {
public void trackTrace(String message, int severityLevel, Map<String, String> properties, Map<String, String> tags) {
if (Strings.isNullOrEmpty(message)) {
return;
}
Expand All @@ -147,13 +149,15 @@ public void trackTrace(String message, int severityLevel, Map<String, String> pr
if (severityLevel != -1) {
telemetry.setSeverityLevel(getSeverityLevel(severityLevel));
}
MapUtil.copy(properties, telemetry.getProperties());
telemetry.getProperties().putAll(properties);
telemetry.getContext().getTags().putAll(tags);

track(telemetry);
}

@Override
public void trackRequest(String id, String name, URL url, Date timestamp, long duration, String responseCode, boolean success) {
public void trackRequest(String id, String name, URL url, Date timestamp, @Nullable Long duration, String responseCode, boolean success,
Map<String, String> properties, Map<String, String> tags) {
if (Strings.isNullOrEmpty(name)) {
return;
}
Expand All @@ -165,24 +169,30 @@ public void trackRequest(String id, String name, URL url, Date timestamp, long d
telemetry.setUrl(url);
}
telemetry.setTimestamp(timestamp);
telemetry.setDuration(new Duration(duration));
if (duration != null) {
telemetry.setDuration(new Duration(duration));
}
telemetry.setResponseCode(responseCode);
telemetry.setSuccess(success);
telemetry.getProperties().putAll(properties);
telemetry.getContext().getTags().putAll(tags);

track(telemetry);
}

@Override
public void trackException(Exception exception, Map<String, String> properties, Map<String, Double> metrics) {
public void trackException(Exception exception, Map<String, String> properties, Map<String, String> tags,
Map<String, Double> metrics) {
if (exception == null) {
return;
}

ExceptionTelemetry telemetry = new ExceptionTelemetry();
telemetry.setException(exception);
telemetry.setSeverityLevel(SeverityLevel.Error);
MapUtil.copy(properties, telemetry.getProperties());
MapUtil.copy(metrics, telemetry.getMetrics());
telemetry.getProperties().putAll(properties);
telemetry.getContext().getTags().putAll(tags);
telemetry.getMetrics().putAll(metrics);

track(telemetry);
}
Expand All @@ -196,6 +206,12 @@ private SeverityLevel getSeverityLevel(int value) {
return null;
}

@Override
public void flush() {
// this is not null because sdk instrumentation is not added until Global.setTelemetryClient() is called
checkNotNull(Global.getTelemetryClient()).flush();
}

@Override
public void logErrorOnce(Throwable t) {
if (!alreadyLoggedError.getAndSet(true)) {
Expand Down
Loading