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

Cleanup httpurlconnection androidTests #503

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 @@ -29,8 +29,7 @@ public final class HttpUrlConnectionSingletons {
private static final Object lock = new Object();
private static OpenTelemetry openTelemetryInstance;

public static Instrumenter<URLConnection, Integer> createInstrumenter(
OpenTelemetry opentelemetry) {
public static Instrumenter<URLConnection, Integer> createInstrumenter() {

HttpUrlHttpAttributesGetter httpAttributesGetter = new HttpUrlHttpAttributesGetter();

Expand All @@ -53,7 +52,7 @@ public static Instrumenter<URLConnection, Integer> createInstrumenter(
httpAttributesGetter,
HttpUrlInstrumentationConfig.newPeerServiceResolver());

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

InstrumenterBuilder<URLConnection, Integer> builder =
Instrumenter.<URLConnection, Integer>builder(
Expand All @@ -79,7 +78,7 @@ public static Instrumenter<URLConnection, Integer> instrumenter() {
if (instrumenter == null) {
synchronized (lock) {
if (instrumenter == null) {
instrumenter = createInstrumenter(null);
instrumenter = createInstrumenter();
}
}
}
Expand All @@ -90,10 +89,5 @@ public static OpenTelemetry openTelemetryInstance() {
return openTelemetryInstance;
}

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

private HttpUrlConnectionSingletons() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ dependencies {
byteBuddy(project(":instrumentation:httpurlconnection:agent"))
implementation(project(":instrumentation:httpurlconnection:library"))
implementation(project(":test-common"))
androidTestImplementation(libs.assertj.core)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,56 @@ package io.opentelemetry.instrumentation.library.httpurlconnection
import io.opentelemetry.android.test.common.OpenTelemetryTestUtils
import io.opentelemetry.instrumentation.library.httpurlconnection.HttpUrlConnectionTestUtil.executeGet
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.Assert
import org.assertj.core.api.Assertions.assertThat
import org.junit.After
import org.junit.BeforeClass
import org.junit.Test
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit

class InstrumentationTest {
companion object {
private val inMemorySpanExporter: InMemorySpanExporter = InMemorySpanExporter.create()

@JvmStatic
@BeforeClass
fun setUpClass() {
OpenTelemetryTestUtils.setUpSpanExporter(inMemorySpanExporter)
}
}

@After
fun tearDown() {
inMemorySpanExporter.reset()
}

@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()
assertThat(inMemorySpanExporter.finishedSpanItems.size).isEqualTo(1)
}

@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()
assertThat(inMemorySpanExporter.finishedSpanItems.size).isEqualTo(1)
}

@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()
assertThat(inMemorySpanExporter.finishedSpanItems.size).isEqualTo(0)
}

@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()
assertThat(inMemorySpanExporter.finishedSpanItems.size).isEqualTo(1)
}

@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 All @@ -64,49 +66,34 @@ class InstrumentationTest {
executor.submit { executeGet("http://httpbin.org/headers") }

executor.shutdown()

// Wait for all tasks to finish execution or timeout
if (executor.awaitTermination(2, TimeUnit.SECONDS)) {
// if all tasks finish before timeout
Assert.assertEquals(4, inMemorySpanExporter.finishedSpanItems.size)
} else {
// if all tasks don't finish before timeout
Assert.fail(
"Test could not be completed as tasks did not complete within the 2s timeout period.",
)
}
} catch (e: InterruptedException) {
// print stack trace to decipher lines that threw InterruptedException as it can be
// possibly thrown by multiple calls above.
e.printStackTrace()
Assert.fail("Test could not be completed due to an interrupted exception.")
assertThat(executor.awaitTermination(2, TimeUnit.SECONDS))
.withFailMessage("Test could not be completed as tasks did not complete within the 2s timeout period.")
.isTrue()

assertThat(inMemorySpanExporter.finishedSpanItems.size).isEqualTo(4)
} finally {
if (!executor.isShutdown) {
executor.shutdownNow()
}
inMemorySpanExporter.shutdown()
}
}

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

parentSpan.makeCurrent().use {
executeGet("http://httpbin.org/get")
val spanDataList = inMemorySpanExporter.finishedSpanItems
if (spanDataList.isNotEmpty()) {
val currentSpanData = spanDataList[0]
Assert.assertEquals(
parentSpan.spanContext.traceId,
currentSpanData.traceId,
)
assertThat(currentSpanData.traceId).isEqualTo(parentSpan.spanContext.traceId)
}
}
parentSpan.end()

Assert.assertEquals(2, inMemorySpanExporter.finishedSpanItems.size)
inMemorySpanExporter.shutdown()
assertThat(inMemorySpanExporter.finishedSpanItems.size).isEqualTo(2)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,14 @@ object OpenTelemetryTestUtils {
}

@JvmStatic
fun setUpSpanExporter(spanExporter: SpanExporter): OpenTelemetry {
fun setUpSpanExporter(spanExporter: SpanExporter) {
openTelemetry =
OpenTelemetrySdk.builder()
.setTracerProvider(getSimpleTracerProvider(spanExporter))
.build()

// TODO: Remove the bottom two lines after making okhttp3 androidTests parallel too.
GlobalOpenTelemetry.resetForTest()
GlobalOpenTelemetry.set(openTelemetry)

return openTelemetry
}

private fun getSimpleTracerProvider(spanExporter: SpanExporter): SdkTracerProvider {
Expand Down