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

Update HttpUrlConnection androidTests to make them compatible for parallel running #500

Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -23,11 +23,17 @@

public final class HttpUrlConnectionSingletons {

private static volatile Instrumenter<URLConnection, Integer> instrumenter;
private static final String INSTRUMENTATION_NAME =
"io.opentelemetry.android.http-url-connection";
private static final Object lock = new Object();

private static OpenTelemetry openTelemetryInstance;
private static volatile Instrumenter<URLConnection, Integer> instrumenter;
private static final Object lock = new Object();

// ThreadLocal instances for parallel test execution.
private static ThreadLocal<OpenTelemetry> testOpentelemetryInstance = new ThreadLocal<>();
private static ThreadLocal<Instrumenter<URLConnection, Integer>> testInstrumenter =
new ThreadLocal<>();

public static Instrumenter<URLConnection, Integer> createInstrumenter(
OpenTelemetry opentelemetry) {
Expand All @@ -53,11 +59,9 @@ public static Instrumenter<URLConnection, Integer> createInstrumenter(
httpAttributesGetter,
HttpUrlInstrumentationConfig.newPeerServiceResolver());

openTelemetryInstance = (opentelemetry == null) ? GlobalOpenTelemetry.get() : opentelemetry;

InstrumenterBuilder<URLConnection, Integer> builder =
Instrumenter.<URLConnection, Integer>builder(
openTelemetryInstance,
opentelemetry,
INSTRUMENTATION_NAME,
httpSpanNameExtractorBuilder.build())
.setSpanStatusExtractor(
Expand All @@ -76,23 +80,41 @@ public static Instrumenter<URLConnection, Integer> createInstrumenter(
}

public static Instrumenter<URLConnection, Integer> instrumenter() {
if (instrumenter == null) {
synchronized (lock) {
if (instrumenter == null) {
instrumenter = createInstrumenter(null);
Instrumenter<URLConnection, Integer> testInstrumenter =
HttpUrlConnectionSingletons.testInstrumenter.get();
if (testInstrumenter != null) {
return testInstrumenter;
} else {
if (instrumenter == null) {
synchronized (lock) {
if (instrumenter == null) {
openTelemetryInstance = GlobalOpenTelemetry.get();
instrumenter = createInstrumenter(openTelemetryInstance);
}
}
}
return instrumenter;
}
return instrumenter;
}

public static OpenTelemetry openTelemetryInstance() {
return openTelemetryInstance;
OpenTelemetry testOpenTelemetry = testOpentelemetryInstance.get();
if (testOpenTelemetry != null) {
return testOpenTelemetry;
} else {
return openTelemetryInstance;
}
Comment on lines +102 to +106
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: always prefer uninverting the if and omit the redundant else.

}

// Used for setting the instrumenter for testing purposes only.
public static void setInstrumenterForTesting(OpenTelemetry opentelemetry) {
instrumenter = createInstrumenter(opentelemetry);
public static void setThreadLocalInstrumenterForTesting(OpenTelemetry opentelemetry) {
testOpentelemetryInstance.set(opentelemetry);
testInstrumenter.set(createInstrumenter(opentelemetry));
}

public static void removeThreadLocalInstrumenterForTesting() {
testOpentelemetryInstance.remove();
testInstrumenter.remove();
}

private HttpUrlConnectionSingletons() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,56 @@ import io.opentelemetry.instrumentation.library.httpurlconnection.HttpUrlConnect
import io.opentelemetry.instrumentation.library.httpurlconnection.HttpUrlConnectionTestUtil.post
import io.opentelemetry.instrumentation.library.httpurlconnection.internal.HttpUrlConnectionSingletons
import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter
import org.junit.After
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit

class InstrumentationTest {
private lateinit var inMemorySpanExporter: InMemorySpanExporter
private lateinit var openTelemetryTestUtils: OpenTelemetryTestUtils

@Before
fun setUp() {
inMemorySpanExporter = InMemorySpanExporter.create()
openTelemetryTestUtils = OpenTelemetryTestUtils()
HttpUrlConnectionSingletons.setThreadLocalInstrumenterForTesting(openTelemetryTestUtils.setUpSpanExporter(inMemorySpanExporter))
}

@After
fun tearDown() {
inMemorySpanExporter.shutdown()
HttpUrlConnectionSingletons.removeThreadLocalInstrumenterForTesting()
}

@Test
fun testHttpUrlConnectionGetRequest_ShouldBeTraced() {
val inMemorySpanExporter = InMemorySpanExporter.create()
HttpUrlConnectionSingletons.setInstrumenterForTesting(OpenTelemetryTestUtils.setUpSpanExporter(inMemorySpanExporter))
executeGet("http://httpbin.org/get")
Assert.assertEquals(1, inMemorySpanExporter.finishedSpanItems.size)
inMemorySpanExporter.shutdown()
}

@Test
fun testHttpUrlConnectionPostRequest_ShouldBeTraced() {
val inMemorySpanExporter = InMemorySpanExporter.create()
HttpUrlConnectionSingletons.setInstrumenterForTesting(OpenTelemetryTestUtils.setUpSpanExporter(inMemorySpanExporter))
post("http://httpbin.org/post")
Assert.assertEquals(1, inMemorySpanExporter.finishedSpanItems.size)
inMemorySpanExporter.shutdown()
}

@Test
fun testHttpUrlConnectionGetRequest_WhenNoStreamFetchedAndNoDisconnectCalled_ShouldNotBeTraced() {
val inMemorySpanExporter = InMemorySpanExporter.create()
HttpUrlConnectionSingletons.setInstrumenterForTesting(OpenTelemetryTestUtils.setUpSpanExporter(inMemorySpanExporter))
executeGet("http://httpbin.org/get", false, false)
Assert.assertEquals(0, inMemorySpanExporter.finishedSpanItems.size)
inMemorySpanExporter.shutdown()
}

@Test
fun testHttpUrlConnectionGetRequest_WhenNoStreamFetchedButDisconnectCalled_ShouldBeTraced() {
val inMemorySpanExporter = InMemorySpanExporter.create()
HttpUrlConnectionSingletons.setInstrumenterForTesting(OpenTelemetryTestUtils.setUpSpanExporter(inMemorySpanExporter))
executeGet("http://httpbin.org/get", false)
Assert.assertEquals(1, inMemorySpanExporter.finishedSpanItems.size)
inMemorySpanExporter.shutdown()
}

@Test
fun testHttpUrlConnectionGetRequest_WhenFourConcurrentRequestsAreMade_AllShouldBeTraced() {
val inMemorySpanExporter = InMemorySpanExporter.create()
HttpUrlConnectionSingletons.setInstrumenterForTesting(OpenTelemetryTestUtils.setUpSpanExporter(inMemorySpanExporter))
val executor = Executors.newFixedThreadPool(4)
try {
executor.submit { executeGet("http://httpbin.org/get") }
Expand Down Expand Up @@ -83,15 +87,12 @@ class InstrumentationTest {
if (!executor.isShutdown) {
executor.shutdownNow()
}
inMemorySpanExporter.shutdown()
}
}

@Test
fun testHttpUrlConnectionRequest_ContextPropagationHappensAsExpected() {
val inMemorySpanExporter = InMemorySpanExporter.create()
HttpUrlConnectionSingletons.setInstrumenterForTesting(OpenTelemetryTestUtils.setUpSpanExporter(inMemorySpanExporter))
val parentSpan = OpenTelemetryTestUtils.getSpan()
val parentSpan = openTelemetryTestUtils.getSpan()

parentSpan.makeCurrent().use {
executeGet("http://httpbin.org/get")
Expand All @@ -107,6 +108,5 @@ class InstrumentationTest {
parentSpan.end()

Assert.assertEquals(2, inMemorySpanExporter.finishedSpanItems.size)
inMemorySpanExporter.shutdown()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
public class InstrumentationTest {
private MockWebServer server;
private static final InMemorySpanExporter inMemorySpanExporter = InMemorySpanExporter.create();
private static final OpenTelemetryTestUtils openTelemetryTestUtils =
new OpenTelemetryTestUtils();

@Before
public void setUp() throws IOException {
Expand All @@ -45,10 +47,10 @@ public void tearDown() throws IOException {

@Test
public void okhttpTraces() throws IOException {
OpenTelemetryTestUtils.setUpSpanExporter(inMemorySpanExporter);
openTelemetryTestUtils.setUpSpanExporter(inMemorySpanExporter);
server.enqueue(new MockResponse().setResponseCode(200));

Span span = OpenTelemetryTestUtils.getSpan();
Span span = openTelemetryTestUtils.getSpan();

try (Scope ignored = span.makeCurrent()) {
OkHttpClient client =
Expand All @@ -72,9 +74,9 @@ public void okhttpTraces() throws IOException {

@Test
public void okhttpTraces_with_callback() throws InterruptedException {
OpenTelemetryTestUtils.setUpSpanExporter(inMemorySpanExporter);
openTelemetryTestUtils.setUpSpanExporter(inMemorySpanExporter);
CountDownLatch lock = new CountDownLatch(1);
Span span = OpenTelemetryTestUtils.getSpan();
Span span = openTelemetryTestUtils.getSpan();

try (Scope ignored = span.makeCurrent()) {
server.enqueue(new MockResponse().setResponseCode(200));
Expand Down Expand Up @@ -121,13 +123,13 @@ public void avoidCreatingSpansForInternalOkhttpRequests() throws InterruptedExce
// so it should be run isolated to actually get it to fail when it's expected to fail.
OtlpHttpSpanExporter exporter =
OtlpHttpSpanExporter.builder().setEndpoint(server.url("").toString()).build();
OpenTelemetryTestUtils.setUpSpanExporter(exporter);
openTelemetryTestUtils.setUpSpanExporter(exporter);

server.enqueue(new MockResponse().setResponseCode(200));

// This span should trigger 1 export okhttp call, which is the only okhttp call expected
// for this test case.
OpenTelemetryTestUtils.getSpan().end();
openTelemetryTestUtils.getSpan().end();

// Wait for unwanted extra okhttp requests.
int loop = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ import io.opentelemetry.sdk.trace.SdkTracerProvider
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor
import io.opentelemetry.sdk.trace.export.SpanExporter

object OpenTelemetryTestUtils {
class OpenTelemetryTestUtils {
lateinit var openTelemetry: OpenTelemetry

@JvmStatic
fun getSpan(): Span {
return openTelemetry.getTracer("TestTracer").spanBuilder("A Span").startSpan()
}

@JvmStatic
fun setUpSpanExporter(spanExporter: SpanExporter): OpenTelemetry {
openTelemetry =
OpenTelemetrySdk.builder()
Expand Down