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

okhttp: run our interceptor before other interceptors #6997

Merged
merged 1 commit into from
Oct 27, 2022
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 @@ -66,7 +66,10 @@ public Interceptor newInterceptor() {
* @return a {@link Call.Factory} for creating new {@link Call} instances.
*/
public Call.Factory newCallFactory(OkHttpClient baseClient) {
OkHttpClient tracingClient = baseClient.newBuilder().addInterceptor(newInterceptor()).build();
OkHttpClient.Builder builder = baseClient.newBuilder();
// add our interceptor before other interceptors
builder.interceptors().add(0, newInterceptor());
OkHttpClient tracingClient = builder.build();
return new TracingCallFactory(tracingClient);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@
package io.opentelemetry.instrumentation.okhttp.v3_0;

import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest;
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
Expand All @@ -28,6 +32,7 @@
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.internal.http.HttpMethod;
import org.junit.jupiter.api.Test;

public abstract class AbstractOkHttp3Test extends AbstractHttpClientTest<Request> {

Expand Down Expand Up @@ -124,4 +129,62 @@ protected void configure(HttpClientTestOptions options) {
return attributes;
});
}

private static class TestInterceptor implements Interceptor {

@Override
public Response intercept(Chain chain) throws IOException {
// make copy of the request
Request request = chain.request().newBuilder().build();

return chain.proceed(request);
}
}

@Test
void requestWithCustomInterceptor() throws Throwable {
String method = "GET";
URI uri = resolveAddress("/success");

RequestResult result = new RequestResult(() -> testing.runWithSpan("child", () -> {}));
OkHttpClient.Builder builder = getClientBuilder(false);
builder.addInterceptor(new TestInterceptor());
Call.Factory testClient = createCallFactory(builder);

testing.runWithSpan(
"parent",
() -> {
Request request = buildRequest(method, uri, Collections.emptyMap());
testClient
.newCall(request)
.enqueue(
new Callback() {
@Override
public void onFailure(Call call, IOException e) {
result.complete(e);
}

@Override
public void onResponse(Call call, Response response) {
try (ResponseBody ignored = response.body()) {
result.complete(response.code());
}
}
});
});

assertThat(result.get()).isEqualTo(200);

testing.waitAndAssertTraces(
trace -> {
trace.hasSpansSatisfyingExactly(
span -> span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(),
span -> span.hasName("HTTP GET").hasKind(SpanKind.CLIENT).hasParent(trace.getSpan(0)),
span ->
span.hasName("test-http-server")
.hasKind(SpanKind.SERVER)
.hasParent(trace.getSpan(1)),
span -> span.hasName("child").hasKind(SpanKind.INTERNAL).hasParent(trace.getSpan(0)));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ protected final Duration readTimeout() {
return READ_TIMEOUT;
}

private InstrumentationTestRunner testing;
protected InstrumentationTestRunner testing;
private HttpClientTestServer server;

private final HttpClientTestOptions options = new HttpClientTestOptions();
Expand Down