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

Fix JDK http client propagation of non-sampled traces #3736

Merged
merged 3 commits into from
Aug 2, 2021
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 @@ -13,7 +13,6 @@

import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge;
import java.net.http.HttpHeaders;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
Expand All @@ -40,9 +39,7 @@ public static class HeadersAdvice {

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void methodExit(@Advice.Return(readOnly = false) HttpHeaders headers) {
if (Java8BytecodeBridge.currentSpan().isRecording()) {
headers = tracer().inject(headers);
}
headers = tracer().inject(headers);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like I just happened to pick an unlucky client. ;-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or a lucky one, to find this bug 💯

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ abstract class HttpClientTest<REQUEST> extends InstrumentationSpecification {
method << BODY_METHODS
}

def "basic GET request with not sampled parent"() {
expect:
junitTest.successfulRequestWithNotSampledParent()
}

def "should suppress nested CLIENT span if already under parent CLIENT span (#method)"() {
assumeTrue(testWithClientParent())
expect:
Expand All @@ -271,7 +276,6 @@ abstract class HttpClientTest<REQUEST> extends InstrumentationSpecification {
method << BODY_METHODS
}


//FIXME: add tests for POST with large/chunked data

def "trace request with callback and parent"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,10 @@ public <T, E extends Throwable> T runWithServerSpan(
String spanName, ThrowingSupplier<T, E> callback) throws E {
return testInstrumenters.runWithServerSpan(spanName, callback);
}

@Override
public <T, E extends Throwable> T runWithNonRecordingSpan(ThrowingSupplier<T, E> callback)
throws E {
return testInstrumenters.runWithNonRecordingSpan(callback);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,7 @@ default <E extends Throwable> void runWithServerSpan(
*/
<T, E extends Throwable> T runWithServerSpan(String spanName, ThrowingSupplier<T, E> callback)
throws E;

/** Runs the provided {@code callback} inside the scope of a non-recording span. */
<T, E extends Throwable> T runWithNonRecordingSpan(ThrowingSupplier<T, E> callback) throws E;
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ public <T, E extends Throwable> T runWithServerSpan(
return testInstrumenters.runWithServerSpan(spanName, callback);
}

@Override
public <T, E extends Throwable> T runWithNonRecordingSpan(ThrowingSupplier<T, E> callback)
throws E {
return testInstrumenters.runWithNonRecordingSpan(callback);
}

private static class FlushTrackingSpanProcessor implements SpanProcessor {
@Override
public void onStart(Context parentContext, ReadWriteSpan span) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
package io.opentelemetry.instrumentation.testing;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.TraceFlags;
import io.opentelemetry.api.trace.TraceState;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
Expand Down Expand Up @@ -58,6 +62,19 @@ <T, E extends Throwable> T runWithServerSpan(String spanName, ThrowingSupplier<T
return runWithInstrumenter(spanName, testServerInstrumenter, callback);
}

/** Runs the provided {@code callback} inside the scope of a non-recording span. */
<T, E extends Throwable> T runWithNonRecordingSpan(ThrowingSupplier<T, E> callback) throws E {
SpanContext spanContext =
SpanContext.create(
"12345678123456781234567812345678",
"1234567812345678",
TraceFlags.getDefault(),
TraceState.getDefault());
try (Scope ignored = Span.wrap(spanContext).makeCurrent()) {
return callback.get();
}
}

private static <T, E extends Throwable> T runWithInstrumenter(
String spanName, Instrumenter<String, Void> instrumenter, ThrowingSupplier<T, E> callback)
throws E {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,20 @@ void successfulRequestWithParent(String method) {
});
}

@Test
void successfulRequestWithNotSampledParent() throws InterruptedException {
String method = "GET";
URI uri = resolveAddress("/success");
int responseCode = testing.runWithNonRecordingSpan(() -> doRequest(method, uri));

assertThat(responseCode).isEqualTo(200);

// sleep to ensure no spans are emitted
Thread.sleep(200);

assertThat(testing.traces()).isEmpty();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this verify that the proper headers are being sent? Would you expect the server to generate a trace if it wasn't?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ya 👍

}

@ParameterizedTest
@ValueSource(strings = {"PUT", "POST"})
void shouldSuppressNestedClientSpanIfAlreadyUnderParentClientSpan(String method) {
Expand Down