Skip to content

Commit

Permalink
Update OpenTelemetry Bridge to latest version
Browse files Browse the repository at this point in the history
Remove some of the interfaces from the wrapped objects to stop encouraging casting to our interfaces as a way to integrate.
Add instrumentation to sync our context with the new otel context.  Due to implementation differences, there are still some edge cases where they can get out of sync.
  • Loading branch information
tylerbenson committed Dec 3, 2020
1 parent 1fef18f commit 10aee28
Show file tree
Hide file tree
Showing 18 changed files with 513 additions and 511 deletions.
29 changes: 14 additions & 15 deletions dd-java-agent/instrumentation/opentelemetry/opentelemetry.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,36 @@ ext {
}

// Made this a variable so we can easily update to latest releases.
def otelVersion = '0.3.0'
def otelVersion = '0.11.0'

muzzle {
pass {
module = 'opentelemetry-api'
group = 'io.opentelemetry'
versions = "[$otelVersion,0.8.0)"
versions = "[$otelVersion,]"
assertInverse = true
// I have no idea why gradle doesn't respect the version range above
// but I also have no time to figure it out for an instrumentation
// that is experimental
skipVersions = ['0.2.2', '0.2.3', '0.9.0']
}
}

apply from: "$rootDir/gradle/java.gradle"

apply plugin: 'org.unbroken-dome.test-sets'

testSets {
latestDepTest {
dirName = 'test'
}
}
// Can't have a latest test until a new version is released.
//apply plugin: 'org.unbroken-dome.test-sets'
//
//testSets {
// latestDepTest {
// dirName = 'test'
// }
//}

dependencies {
compileOnly group: 'io.opentelemetry', name: 'opentelemetry-api', version: otelVersion
main_java8CompileOnly group: 'io.opentelemetry', name: 'opentelemetry-api', version: otelVersion

compileOnly group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.2'
compileOnly group: 'com.google.auto.value', name: 'auto-value-annotations', version: '1.6.6'
main_java8CompileOnly group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.2'
main_java8CompileOnly group: 'com.google.auto.value', name: 'auto-value-annotations', version: '1.6.6'

testCompile group: 'io.opentelemetry', name: 'opentelemetry-api', version: otelVersion
latestDepTestCompile group: 'io.opentelemetry', name: 'opentelemetry-api', version: '0.7+'
// latestDepTestCompile group: 'io.opentelemetry', name: 'opentelemetry-api', version: '0.7+'
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package datadog.trace.instrumentation.opentelemetry;

import static datadog.trace.agent.tooling.bytebuddy.matcher.DDElementMatchers.implementsInterface;
import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.returns;
import static net.bytebuddy.matcher.ElementMatchers.takesNoArguments;

import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.trace.TracerProvider;
import java.util.HashMap;
import datadog.trace.bootstrap.ContextStore;
import datadog.trace.bootstrap.InstrumentationContext;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import io.opentelemetry.api.OpenTelemetryBuilder;
import io.opentelemetry.api.trace.SpanContext;
import java.util.Map;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
Expand All @@ -30,58 +34,47 @@ protected boolean defaultEnabled() {

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return named("io.opentelemetry.OpenTelemetry");
return implementsInterface(named("io.opentelemetry.api.OpenTelemetryBuilder"));
}

@Override
public String[] helperClassNames() {
return new String[] {
packageName + ".OtelScope",
packageName + ".OtelSpan",
packageName + ".OtelSpan$1", // switch statement
packageName + ".OtelSpanContext",
packageName + ".OtelTracer",
packageName + ".OtelTracer$1", // switch statement
packageName + ".OtelTracerProvider",
packageName + ".OtelTracer$SpanBuilder",
packageName + ".OtelTracer$OtelSpanBuilder",
packageName + ".OtelContextPropagators",
packageName + ".OtelContextPropagators$1", // switch statement
packageName + ".OtelContextPropagators$OtelHttpTextFormat",
packageName + ".OtelContextPropagators$OtelTextMapPropagator",
packageName + ".OtelContextPropagators$OtelSetter",
packageName + ".OtelContextPropagators$OtelGetter",
packageName + ".TypeConverter",
packageName + ".TypeConverter$OtelSpanContext"
};
}

@Override
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() {
final Map<ElementMatcher<? super MethodDescription>, String> transformers = new HashMap<>();
transformers.put(
named("getTracerProvider").and(returns(named("io.opentelemetry.trace.TracerProvider"))),
OpenTelemetryInstrumentation.class.getName() + "$TracerProviderAdvice");
transformers.put(
named("getPropagators")
.and(returns(named("io.opentelemetry.context.propagation.ContextPropagators"))),
OpenTelemetryInstrumentation.class.getName() + "$ContextPropagatorsAdvice");
return transformers;
return singletonMap(
named("build").and(takesNoArguments()),
OpenTelemetryInstrumentation.class.getName() + "$BuilderAdvice");
}

public static class TracerProviderAdvice {
@Advice.OnMethodExit(suppress = Throwable.class)
public static void returnProvider(@Advice.Return(readOnly = false) TracerProvider result) {
result = OtelTracerProvider.INSTANCE;
}
@Override
public Map<String, String> contextStore() {
return singletonMap(
"io.opentelemetry.api.trace.SpanContext", AgentSpan.Context.class.getName());
}

public static class ContextPropagatorsAdvice {
@Advice.OnMethodExit(suppress = Throwable.class)
public static void returnProvider(@Advice.Return(readOnly = false) ContextPropagators result) {
result = OtelContextPropagators.INSTANCE;
}

// Muzzle doesn't detect the advice method's argument type, so we have to help it a bit.
public static void muzzleCheck(final ContextPropagators propagators) {
propagators.getHttpTextFormat();
public static class BuilderAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void beforeBuild(@Advice.This OpenTelemetryBuilder builder) {
ContextStore<SpanContext, AgentSpan.Context> spanContextStore =
InstrumentationContext.get(SpanContext.class, AgentSpan.Context.class);
builder.setTracerProvider(new OtelTracerProvider(spanContextStore));
builder.setPropagators(new OtelContextPropagators(spanContextStore));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package datadog.trace.instrumentation.opentelemetry.context;

import static datadog.trace.agent.tooling.bytebuddy.matcher.DDElementMatchers.implementsInterface;
import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.returns;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;

import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;
import java.util.Map;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;

/**
* This is experimental instrumentation and should only be enabled for evaluation/testing purposes.
*/
@AutoService(Instrumenter.class)
public class OpenTelemetryContextInstrumentation extends Instrumenter.Default {
public OpenTelemetryContextInstrumentation() {
super("opentelemetry-beta");
}

@Override
protected boolean defaultEnabled() {
return false;
}

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return implementsInterface(named("io.opentelemetry.context.ContextStorage"));
}

@Override
public String[] helperClassNames() {
return new String[] {
"datadog.trace.instrumentation.opentelemetry.OtelSpan",
"datadog.trace.instrumentation.opentelemetry.TypeConverter",
"datadog.trace.instrumentation.opentelemetry.TypeConverter$OtelSpanContext",
packageName + ".WrappedScope",
};
}

@Override
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() {
return singletonMap(
named("attach")
.and(takesArgument(0, named("io.opentelemetry.context.Context")))
.and(returns(named("io.opentelemetry.context.Scope"))),
packageName + ".ContextStorageAdvice");
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
package datadog.trace.instrumentation.opentelemetry;

import datadog.trace.bootstrap.ContextStore;
import datadog.trace.bootstrap.instrumentation.api.AgentPropagation;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import io.grpc.Context;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.context.propagation.HttpTextFormat;
import io.opentelemetry.trace.DefaultSpan;
import io.opentelemetry.trace.Span;
import io.opentelemetry.trace.TracingContextUtils;
import java.util.Arrays;
import io.opentelemetry.context.propagation.TextMapPropagator;
import java.util.List;

public class OtelContextPropagators implements ContextPropagators {
public static final OtelContextPropagators INSTANCE = new OtelContextPropagators();
public final class OtelContextPropagators implements ContextPropagators {

private OtelContextPropagators() {}
private final OtelTextMapPropagator propagator;

public OtelContextPropagators(ContextStore<SpanContext, AgentSpan.Context> spanContextStore) {
propagator = new OtelTextMapPropagator(spanContextStore);
}

@Override
public HttpTextFormat getHttpTextFormat() {
return OtelHttpTextFormat.INSTANCE;
public TextMapPropagator getTextMapPropagator() {
return propagator;
}

private static class OtelHttpTextFormat implements HttpTextFormat {
private static final OtelHttpTextFormat INSTANCE = new OtelHttpTextFormat();
private static class OtelTextMapPropagator implements TextMapPropagator {

private final AgentTracer.TracerAPI tracer = AgentTracer.get();
private final TypeConverter converter = new TypeConverter();
private final TypeConverter converter;

private OtelTextMapPropagator(ContextStore<SpanContext, AgentSpan.Context> spanContextStore) {
converter = new TypeConverter(spanContextStore);
}

@Override
public List<String> fields() {
Expand All @@ -35,8 +40,8 @@ public List<String> fields() {

@Override
public <C> void inject(final Context context, final C carrier, final Setter<C> setter) {
final Span span = TracingContextUtils.getSpanWithoutDefault(context);
if (span == null || !span.getContext().isValid()) {
final Span span = Span.fromContextOrNull(context);
if (span == null || !span.getSpanContext().isValid()) {
return;
}
tracer.inject(converter.toAgentSpan(span), carrier, new OtelSetter<>(setter));
Expand All @@ -45,15 +50,14 @@ public <C> void inject(final Context context, final C carrier, final Setter<C> s
@Override
public <C> Context extract(final Context context, final C carrier, final Getter<C> getter) {
final AgentSpan.Context agentContext = tracer.extract(carrier, new OtelGetter<>(getter));
return TracingContextUtils.withSpan(
DefaultSpan.create(converter.toSpanContext(agentContext)), context);
return Span.wrap(converter.toSpanContext(agentContext)).storeInContext(context);
}
}

private static class OtelSetter<C> implements AgentPropagation.Setter<C> {
private final HttpTextFormat.Setter<C> setter;
private final TextMapPropagator.Setter<C> setter;

private OtelSetter(final HttpTextFormat.Setter<C> setter) {
private OtelSetter(final TextMapPropagator.Setter<C> setter) {
this.setter = setter;
}

Expand All @@ -64,43 +68,15 @@ public void set(final C carrier, final String key, final String value) {
}

private static class OtelGetter<C> implements AgentPropagation.ContextVisitor<C> {
private static final String DD_TRACE_ID_KEY = "x-datadog-trace-id";
private static final String DD_SPAN_ID_KEY = "x-datadog-parent-id";
private static final String DD_SAMPLING_PRIORITY_KEY = "x-datadog-sampling-priority";
private static final String DD_ORIGIN_KEY = "x-datadog-origin";

private static final String B3_TRACE_ID_KEY = "X-B3-TraceId";
private static final String B3_SPAN_ID_KEY = "X-B3-SpanId";
private static final String B3_SAMPLING_PRIORITY_KEY = "X-B3-Sampled";

private static final String HAYSTACK_TRACE_ID_KEY = "Trace-ID";
private static final String HAYSTACK_SPAN_ID_KEY = "Span-ID";
private static final String HAYSTACK_PARENT_ID_KEY = "Parent_ID";

private static final List<String> KEYS =
Arrays.asList(
DD_TRACE_ID_KEY,
DD_SPAN_ID_KEY,
DD_SAMPLING_PRIORITY_KEY,
DD_ORIGIN_KEY,
B3_TRACE_ID_KEY,
B3_SPAN_ID_KEY,
B3_SAMPLING_PRIORITY_KEY,
HAYSTACK_TRACE_ID_KEY,
HAYSTACK_SPAN_ID_KEY,
HAYSTACK_PARENT_ID_KEY);

private final HttpTextFormat.Getter<C> getter;

private OtelGetter(final HttpTextFormat.Getter<C> getter) {
private final TextMapPropagator.Getter<C> getter;

private OtelGetter(final TextMapPropagator.Getter<C> getter) {
this.getter = getter;
}

@Override
public void forEachKey(C carrier, AgentPropagation.KeyClassifier classifier) {
// TODO: Otel doesn't expose the keys, so we have to rely on hard coded keys.
// https://github.com/open-telemetry/opentelemetry-specification/issues/433
for (String key : KEYS) {
for (String key : getter.keys(carrier)) {
if (!classifier.accept(key, getter.get(carrier, key))) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,20 @@
package datadog.trace.instrumentation.opentelemetry;

import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.context.TraceScope;
import io.opentelemetry.context.Scope;

public class OtelScope implements Scope, TraceScope {
public final class OtelScope implements Scope {
private final AgentScope delegate;

OtelScope(final AgentScope delegate) {
this.delegate = delegate;
}

@Override
public Continuation capture() {
return delegate.capture();
}

@Override
public Continuation captureConcurrent() {
return delegate.captureConcurrent();
}

@Override
public void close() {
delegate.close();
}

@Override
public boolean isAsyncPropagating() {
return delegate.isAsyncPropagating();
}

@Override
public void setAsyncPropagation(final boolean value) {
delegate.setAsyncPropagation(value);
}

public AgentScope getDelegate() {
return delegate;
}
Expand Down
Loading

0 comments on commit 10aee28

Please sign in to comment.