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

[jaeger/apache-http-instrumentation] TracingRequestInterceptor gets span from http request context vs tracer scope manager #378

Merged
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -10,12 +10,14 @@ dependencies {
testCompile group: 'org.mock-server', name: 'mockserver-netty', version: '3.11'
testCompile group: 'junit', name: 'junit', version: junitVersion

testCompile group: 'org.hamcrest', name: 'hamcrest-all', version: '1.3'
// Mockito 2.12 -> 2.8 due to PowerMock/Mockito dependency issues
testCompile group: 'org.mockito', name: 'mockito-core', version: '2.8.0'
testCompile group: 'org.powermock', name: 'powermock-api-mockito2', version: '1.7.0RC2'
testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: powermockitoVersion
testCompile group: 'org.powermock', name: 'powermock-core', version: powermockitoVersion
testCompile group: 'org.powermock', name: 'powermock-module-junit4-rule', version: powermockitoVersion
testCompile group: 'uk.org.lidalia', name: 'slf4j-test', version: '1.2.0'

signature 'org.codehaus.mojo.signature:java16:1.1@signature'
}
@@ -14,7 +14,6 @@

package com.uber.jaeger.httpclient;

import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.Tracer;
import java.io.IOException;
@@ -50,11 +49,11 @@ public void process(HttpRequest httpRequest, HttpContext httpContext)
try {
spanCreationInterceptor.process(httpRequest, httpContext);

Scope currentScope = tracer.scopeManager().active();
if (currentScope != null) {
onSpanStarted(currentScope.span(), httpRequest, httpContext);
Span clientSpan = (Span) httpContext.getAttribute(Constants.CURRENT_SPAN_CONTEXT_KEY);
if (clientSpan != null) {
onSpanStarted(clientSpan, httpRequest, httpContext);
} else {
log.warn("Current scope is null; possibly failed to start client tracing span.");
log.warn("Current client span not found in http context.");
}

spanInjectionInterceptor.process(httpRequest, httpContext);
@@ -14,44 +14,127 @@

package com.uber.jaeger.httpclient;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static java.util.Arrays.asList;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.when;
import static uk.org.lidalia.slf4jtest.LoggingEvent.error;
import static uk.org.lidalia.slf4jtest.LoggingEvent.warn;

import io.opentracing.ScopeManager;
import io.opentracing.Span;
import io.opentracing.Tracer;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.protocol.HttpContext;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import uk.org.lidalia.slf4jtest.TestLogger;
import uk.org.lidalia.slf4jtest.TestLoggerFactory;

@RunWith(PowerMockRunner.class)
@PrepareForTest(TracingRequestInterceptor.class)
public class TracingRequestInterceptorTest {

TestLogger logger = TestLoggerFactory.getTestLogger(TracingRequestInterceptor.class);

@Test
public void testProcessNullScope()
throws Exception {
public void testProcessNoErrors() throws IOException, HttpException {
ScopeManager mockScopeManager = Mockito.mock(ScopeManager.class);
when(mockScopeManager.active()).thenReturn(null);

Tracer mockTracer = Mockito.mock(Tracer.class);
when(mockTracer.scopeManager()).thenReturn(mockScopeManager);

HttpRequestInterceptor interceptor = new TracingRequestInterceptor(mockTracer);
PowerMockito.spy(interceptor);

HttpRequest mockRequest = Mockito.mock(HttpRequest.class);
HttpContext mockContext = Mockito.mock(HttpContext.class);

Span mockSpan = Mockito.mock(Span.class);

// make sure that mockContext does not return a null span
when(mockContext.getAttribute(Constants.CURRENT_SPAN_CONTEXT_KEY)).thenReturn(mockSpan);

interceptor.process(mockRequest, mockContext);
PowerMockito.verifyPrivate(interceptor, times(0))
.invoke("onSpanStarted", any(Span.class), mockRequest, mockContext);

assertThat(
logger.getLoggingEvents(),
is(new ArrayList<>())
);
}

@Test
public void testProcessNoClientSpanInContext()
throws IOException, HttpException {
ScopeManager mockScopeManager = Mockito.mock(ScopeManager.class);
when(mockScopeManager.active()).thenReturn(null);

Tracer mockTracer = Mockito.mock(Tracer.class);
when(mockTracer.scopeManager()).thenReturn(mockScopeManager);

HttpRequestInterceptor interceptor = new TracingRequestInterceptor(mockTracer);

HttpRequest mockRequest = Mockito.mock(HttpRequest.class);
HttpContext mockContext = Mockito.mock(HttpContext.class);

// make sure that mockContext returns a null span
when(mockContext.getAttribute(Constants.CURRENT_SPAN_CONTEXT_KEY)).thenReturn(null);

interceptor.process(mockRequest, mockContext);

assertThat(
logger.getLoggingEvents(),
is(asList(warn("Current client span not found in http context.")))
);
}

@Test
public void testProcessSpanCreationException()
throws NoSuchFieldException, IllegalAccessException, IOException, HttpException {
ScopeManager mockScopeManager = Mockito.mock(ScopeManager.class);
when(mockScopeManager.active()).thenReturn(null);

Tracer mockTracer = Mockito.mock(Tracer.class);
when(mockTracer.scopeManager()).thenReturn(mockScopeManager);

HttpRequest mockRequest = Mockito.mock(HttpRequest.class);
HttpContext mockContext = Mockito.mock(HttpContext.class);

SpanCreationRequestInterceptor mockSpanCreationInterceptor = Mockito
.mock(SpanCreationRequestInterceptor.class);

Exception spanCreationException = new IOException("Failed to create span");
doThrow(spanCreationException)
.when(mockSpanCreationInterceptor)
.process(mockRequest, mockContext);

HttpRequestInterceptor interceptor = new TracingRequestInterceptor(mockTracer);
Field spanCreationInterceptorField = interceptor
.getClass()
.getDeclaredField("spanCreationInterceptor");
spanCreationInterceptorField.setAccessible(true);
spanCreationInterceptorField.set(interceptor, mockSpanCreationInterceptor);

interceptor.process(mockRequest, mockContext);

assertThat(
logger.getLoggingEvents(),
is(asList(error(spanCreationException, "Could not start client tracing span.")))
);
}

@After
public void clearLoggers() {
TestLoggerFactory.clear();
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.