Skip to content
Open
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
@@ -0,0 +1,39 @@
package io.opentracing.contrib.okhttp3;

import java.util.regex.Pattern;

public class SkipPatternUtil {
private static Pattern SKIP_PATTERN = null;

private static final Pattern EMPTY_SKIP_PATTERN = Pattern.compile("emptyPartter");

public static final String SKIP_PATTERN_PROPERTY_KEY = "opentracing.okhttp.skipPattern";
public static boolean isTraced(String url) {
if (SKIP_PATTERN == null) {
String okhhtpSkipPattern = System.getProperty(SKIP_PATTERN_PROPERTY_KEY);
if (okhhtpSkipPattern != null && okhhtpSkipPattern.length() > 0) {
Pattern skipPatter = Pattern.compile(okhhtpSkipPattern);
// dubbo check
if (SKIP_PATTERN == null) {
synchronized (SkipPatternUtil.class) {
SKIP_PATTERN = skipPatter;
}
}
} else {
// dubbo check
if (SKIP_PATTERN == null) {
synchronized (SkipPatternUtil.class) {
SKIP_PATTERN = EMPTY_SKIP_PATTERN;
}
}
}
}
// dono't need to use equals
if (EMPTY_SKIP_PATTERN == SKIP_PATTERN) {
return true;
}
// skip URLs matching skip pattern
// e.g. pattern is defined as '/health|/status' then URL 'http://localhost:5000/context/health' won't be traced
return !SKIP_PATTERN.matcher(url).find();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public TracingCallFactory(OkHttpClient okHttpClient, Tracer tracer, List<OkHttpC

@Override
public Call newCall(final Request request) {
if (request.url() != null) {
String url = request.url().url().toString();
if (url != null && !SkipPatternUtil.isTraced(url)) {
return okHttpClient.newBuilder().build().newCall(request);
}
}
final Span span = tracer.buildSpan(request.method())
.withTag(Tags.COMPONENT.getKey(), COMPONENT_NAME)
.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;
import java.util.concurrent.Executors;
import java.util.logging.Logger;
import java.util.regex.Pattern;

import io.opentracing.Tracer;
import io.opentracing.tag.Tags;
Expand All @@ -34,7 +35,6 @@ public class TracingInterceptor implements Interceptor {
private Tracer tracer;
private List<OkHttpClientSpanDecorator> decorators;


/**
* Create tracing interceptor. Interceptor has to be added to {@link OkHttpClient.Builder#addInterceptor(Interceptor)}
* and {@link OkHttpClient.Builder#addNetworkInterceptor(Interceptor)}.
Expand Down Expand Up @@ -75,6 +75,13 @@ public static OkHttpClient addTracing(OkHttpClient.Builder builder,
public Response intercept(Chain chain) throws IOException {
Response response = null;

if (chain.request().url() != null) {
String url = chain.request().url().url().toString();
if (url != null && !SkipPatternUtil.isTraced(url)) {
return chain.proceed(chain.request().newBuilder().build());
}
}

// application interceptor?
if (chain.connection() == null) {
Span span = tracer.buildSpan(chain.request().method())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,23 @@ public void testFollowRedirectsTrue() throws IOException {
Assert.assertEquals("localhost", networkSpan.tags().get(Tags.PEER_HOSTNAME.getKey()));
}

@Test
public void testSkipTracing() throws IOException {

{
mockWebServer.enqueue(new MockResponse()
.setResponseCode(200));
client.newCall(new Request.Builder()
.url(mockWebServer.url("/healthabc?foo1"))
.build())
.execute();
}

List<MockSpan> mockSpans = mockTracer.finishedSpans();
Assert.assertEquals(0, mockSpans.size());

}

protected void assertLocalSpan(List<MockSpan> mockSpans) {
MockSpan localSpan = mockSpans.get(mockSpans.size() - 1);
Assert.assertNotNull(localSpan.tags().get(Tags.COMPONENT.getKey()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
*/
public class TracingCallFactoryTest extends AbstractOkHttpTest {

static {
System.setProperty(SkipPatternUtil.SKIP_PATTERN_PROPERTY_KEY, "/health*");
}

public TracingCallFactoryTest() {
super(new TracingCallFactory(new OkHttpClient(), AbstractOkHttpTest.mockTracer));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
*/
public class TracingInterceptorTest extends AbstractOkHttpTest {

static {
System.setProperty(SkipPatternUtil.SKIP_PATTERN_PROPERTY_KEY, "/health*");
}

public TracingInterceptorTest() {
super(TracingInterceptor.addTracing(new OkHttpClient.Builder(), AbstractOkHttpTest.mockTracer));
}
Expand Down