|
3 | 3 | * SPDX-License-Identifier: Apache-2.0 |
4 | 4 | */ |
5 | 5 |
|
6 | | -package io.opentelemetry.instrumentation.library.okhttp.v3_0; |
7 | | - |
8 | | -import static org.junit.Assert.assertEquals; |
9 | | - |
10 | | -import androidx.annotation.NonNull; |
11 | | -import io.opentelemetry.android.test.common.OpenTelemetryRumRule; |
12 | | -import io.opentelemetry.api.OpenTelemetry; |
13 | | -import io.opentelemetry.api.trace.Span; |
14 | | -import io.opentelemetry.api.trace.SpanContext; |
15 | | -import io.opentelemetry.context.Scope; |
16 | | -import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter; |
17 | | -import io.opentelemetry.sdk.OpenTelemetrySdk; |
18 | | -import io.opentelemetry.sdk.trace.SdkTracerProvider; |
19 | | -import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; |
20 | | -import java.io.IOException; |
21 | | -import java.util.concurrent.CountDownLatch; |
22 | | -import mockwebserver3.MockResponse; |
23 | | -import mockwebserver3.MockWebServer; |
24 | | -import okhttp3.Call; |
25 | | -import okhttp3.Callback; |
26 | | -import okhttp3.OkHttpClient; |
27 | | -import okhttp3.Request; |
28 | | -import okhttp3.Response; |
29 | | -import org.junit.After; |
30 | | -import org.junit.Before; |
31 | | -import org.junit.Rule; |
32 | | -import org.junit.Test; |
33 | | - |
34 | | -public class InstrumentationTest { |
35 | | - private MockWebServer server; |
36 | | - |
37 | | - @Rule public OpenTelemetryRumRule openTelemetryRumRule = new OpenTelemetryRumRule(); |
| 6 | +@file:Suppress("ktlint:standard:package-name") |
| 7 | + |
| 8 | +package io.opentelemetry.instrumentation.library.okhttp.v3_0 |
| 9 | + |
| 10 | +import io.opentelemetry.android.test.common.OpenTelemetryRumRule |
| 11 | +import io.opentelemetry.api.OpenTelemetry |
| 12 | +import io.opentelemetry.api.trace.Span |
| 13 | +import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter |
| 14 | +import io.opentelemetry.sdk.OpenTelemetrySdk |
| 15 | +import io.opentelemetry.sdk.trace.SdkTracerProvider |
| 16 | +import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor |
| 17 | +import mockwebserver3.MockResponse |
| 18 | +import mockwebserver3.MockWebServer |
| 19 | +import okhttp3.Call |
| 20 | +import okhttp3.Callback |
| 21 | +import okhttp3.Interceptor |
| 22 | +import okhttp3.OkHttpClient |
| 23 | +import okhttp3.Request |
| 24 | +import okhttp3.Response |
| 25 | +import org.junit.After |
| 26 | +import org.junit.Assert |
| 27 | +import org.junit.Before |
| 28 | +import org.junit.Rule |
| 29 | +import org.junit.Test |
| 30 | +import java.io.IOException |
| 31 | +import java.util.concurrent.CountDownLatch |
| 32 | + |
| 33 | +class InstrumentationTest { |
| 34 | + private lateinit var server: MockWebServer |
| 35 | + |
| 36 | + @get:Rule |
| 37 | + internal var openTelemetryRumRule: OpenTelemetryRumRule = OpenTelemetryRumRule() |
38 | 38 |
|
39 | 39 | @Before |
40 | | - public void setUp() throws IOException { |
41 | | - server = new MockWebServer(); |
42 | | - server.start(); |
| 40 | + @Throws(IOException::class) |
| 41 | + fun setUp() { |
| 42 | + server = MockWebServer() |
| 43 | + server.start() |
43 | 44 | } |
44 | 45 |
|
45 | 46 | @After |
46 | | - public void tearDown() throws IOException { |
47 | | - server.close(); |
| 47 | + @Throws(IOException::class) |
| 48 | + fun tearDown() { |
| 49 | + server.close() |
48 | 50 | } |
49 | 51 |
|
50 | 52 | @Test |
51 | | - public void okhttpTraces() throws IOException { |
52 | | - server.enqueue(new MockResponse.Builder().code(200).build()); |
53 | | - |
54 | | - Span span = openTelemetryRumRule.getSpan(); |
55 | | - |
56 | | - try (Scope ignored = span.makeCurrent()) { |
57 | | - OkHttpClient client = |
58 | | - new OkHttpClient.Builder() |
59 | | - .addInterceptor( |
60 | | - chain -> { |
61 | | - SpanContext currentSpan = Span.current().getSpanContext(); |
62 | | - assertEquals( |
63 | | - span.getSpanContext().getTraceId(), |
64 | | - currentSpan.getTraceId()); |
65 | | - return chain.proceed(chain.request()); |
66 | | - }) |
67 | | - .build(); |
68 | | - createCall(client, "/test/").execute().close(); |
| 53 | + @Throws(IOException::class) |
| 54 | + fun okhttpTraces() { |
| 55 | + server.enqueue(MockResponse.Builder().code(200).build()) |
| 56 | + |
| 57 | + val span = openTelemetryRumRule.getSpan() |
| 58 | + |
| 59 | + span.makeCurrent().use { ignored -> |
| 60 | + val client = |
| 61 | + OkHttpClient |
| 62 | + .Builder() |
| 63 | + .addInterceptor( |
| 64 | + Interceptor { chain: Interceptor.Chain? -> |
| 65 | + val currentSpan = Span.current().spanContext |
| 66 | + Assert.assertEquals( |
| 67 | + span.spanContext.traceId, |
| 68 | + currentSpan.traceId, |
| 69 | + ) |
| 70 | + chain!!.proceed(chain.request()) |
| 71 | + }, |
| 72 | + ).build() |
| 73 | + createCall(client, "/test/").execute().close() |
69 | 74 | } |
| 75 | + span.end() |
70 | 76 |
|
71 | | - span.end(); |
72 | | - |
73 | | - assertEquals(2, openTelemetryRumRule.inMemorySpanExporter.getFinishedSpanItems().size()); |
| 77 | + Assert.assertEquals( |
| 78 | + 2, |
| 79 | + openTelemetryRumRule.inMemorySpanExporter.finishedSpanItems.size |
| 80 | + .toLong(), |
| 81 | + ) |
74 | 82 | } |
75 | 83 |
|
76 | 84 | @Test |
77 | | - public void okhttpTraces_with_callback() throws InterruptedException { |
78 | | - CountDownLatch lock = new CountDownLatch(1); |
79 | | - Span span = openTelemetryRumRule.getSpan(); |
80 | | - |
81 | | - try (Scope ignored = span.makeCurrent()) { |
82 | | - server.enqueue(new MockResponse.Builder().code(200).build()); |
83 | | - |
84 | | - OkHttpClient client = |
85 | | - new OkHttpClient.Builder() |
86 | | - .addInterceptor( |
87 | | - chain -> { |
88 | | - SpanContext currentSpan = Span.current().getSpanContext(); |
89 | | - // Verify context propagation. |
90 | | - assertEquals( |
91 | | - span.getSpanContext().getTraceId(), |
92 | | - currentSpan.getTraceId()); |
93 | | - return chain.proceed(chain.request()); |
94 | | - }) |
95 | | - .build(); |
| 85 | + @Throws(InterruptedException::class) |
| 86 | + fun okhttpTraces_with_callback() { |
| 87 | + val lock = CountDownLatch(1) |
| 88 | + val span = openTelemetryRumRule.getSpan() |
| 89 | + |
| 90 | + span.makeCurrent().use { ignored -> |
| 91 | + server.enqueue(MockResponse.Builder().code(200).build()) |
| 92 | + val client = |
| 93 | + OkHttpClient |
| 94 | + .Builder() |
| 95 | + .addInterceptor( |
| 96 | + Interceptor { chain: Interceptor.Chain? -> |
| 97 | + val currentSpan = Span.current().spanContext |
| 98 | + // Verify context propagation. |
| 99 | + Assert.assertEquals( |
| 100 | + span.spanContext.traceId, |
| 101 | + currentSpan.traceId, |
| 102 | + ) |
| 103 | + chain!!.proceed(chain.request()) |
| 104 | + }, |
| 105 | + ).build() |
96 | 106 | createCall(client, "/test/") |
97 | | - .enqueue( |
98 | | - new Callback() { |
99 | | - @Override |
100 | | - public void onFailure(@NonNull Call call, @NonNull IOException e) {} |
101 | | - |
102 | | - @Override |
103 | | - public void onResponse( |
104 | | - @NonNull Call call, @NonNull Response response) { |
105 | | - // Verify that the original caller's context is the current one |
106 | | - // here. |
107 | | - assertEquals(span, Span.current()); |
108 | | - lock.countDown(); |
109 | | - } |
110 | | - }); |
| 107 | + .enqueue( |
| 108 | + object : Callback { |
| 109 | + override fun onFailure( |
| 110 | + call: Call, |
| 111 | + e: IOException, |
| 112 | + ) {} |
| 113 | + |
| 114 | + override fun onResponse( |
| 115 | + call: Call, |
| 116 | + response: Response, |
| 117 | + ) { |
| 118 | + // Verify that the original caller's context is the current one |
| 119 | + // here. |
| 120 | + Assert.assertEquals(span, Span.current()) |
| 121 | + lock.countDown() |
| 122 | + } |
| 123 | + }, |
| 124 | + ) |
111 | 125 | } |
112 | | - |
113 | | - lock.await(); |
114 | | - span.end(); |
115 | | - |
116 | | - assertEquals(2, openTelemetryRumRule.inMemorySpanExporter.getFinishedSpanItems().size()); |
| 126 | + lock.await() |
| 127 | + span.end() |
| 128 | + |
| 129 | + Assert.assertEquals( |
| 130 | + 2, |
| 131 | + openTelemetryRumRule.inMemorySpanExporter.finishedSpanItems.size |
| 132 | + .toLong(), |
| 133 | + ) |
117 | 134 | } |
118 | 135 |
|
119 | 136 | @Test |
120 | | - public void avoidCreatingSpansForInternalOkhttpRequests() throws InterruptedException { |
| 137 | + @Throws(InterruptedException::class) |
| 138 | + fun avoidCreatingSpansForInternalOkhttpRequests() { |
121 | 139 | // NOTE: For some reason this test always passes when running all the tests in this file at |
122 | 140 | // once, |
123 | 141 | // so it should be run isolated to actually get it to fail when it's expected to fail. |
124 | | - OtlpHttpSpanExporter exporter = |
125 | | - OtlpHttpSpanExporter.builder().setEndpoint(server.url("").toString()).build(); |
126 | | - OpenTelemetry openTelemetry = |
127 | | - OpenTelemetrySdk.builder() |
128 | | - .setTracerProvider( |
129 | | - SdkTracerProvider.builder() |
130 | | - .addSpanProcessor(SimpleSpanProcessor.create(exporter)) |
131 | | - .build()) |
132 | | - .build(); |
133 | | - |
134 | | - server.enqueue(new MockResponse.Builder().code(200).build()); |
| 142 | + val exporter = |
| 143 | + OtlpHttpSpanExporter.builder().setEndpoint(server.url("").toString()).build() |
| 144 | + val openTelemetry: OpenTelemetry = |
| 145 | + OpenTelemetrySdk |
| 146 | + .builder() |
| 147 | + .setTracerProvider( |
| 148 | + SdkTracerProvider |
| 149 | + .builder() |
| 150 | + .addSpanProcessor(SimpleSpanProcessor.create(exporter)) |
| 151 | + .build(), |
| 152 | + ).build() |
| 153 | + |
| 154 | + server.enqueue(MockResponse.Builder().code(200).build()) |
135 | 155 |
|
136 | 156 | // This span should trigger 1 export okhttp call, which is the only okhttp call expected |
137 | 157 | // for this test case. |
138 | 158 | openTelemetry |
139 | | - .tracerBuilder("Some Scope") |
140 | | - .build() |
141 | | - .spanBuilder("Some Span") |
142 | | - .startSpan() |
143 | | - .end(); |
| 159 | + .tracerBuilder("Some Scope") |
| 160 | + .build() |
| 161 | + .spanBuilder("Some Span") |
| 162 | + .startSpan() |
| 163 | + .end() |
144 | 164 |
|
145 | 165 | // Wait for unwanted extra okhttp requests. |
146 | | - int loop = 0; |
| 166 | + var loop = 0 |
147 | 167 | while (loop < 10) { |
148 | | - Thread.sleep(100); |
| 168 | + Thread.sleep(100) |
149 | 169 | // Stop waiting if we get at least one unwanted request. |
150 | | - if (server.getRequestCount() > 1) { |
151 | | - break; |
| 170 | + if (server.requestCount > 1) { |
| 171 | + break |
152 | 172 | } |
153 | | - loop++; |
| 173 | + loop++ |
154 | 174 | } |
155 | 175 |
|
156 | | - assertEquals(1, server.getRequestCount()); |
| 176 | + Assert.assertEquals(1, server.requestCount.toLong()) |
157 | 177 | } |
158 | 178 |
|
159 | | - private Call createCall(OkHttpClient client, String urlPath) { |
160 | | - Request request = new Request.Builder().url(server.url(urlPath)).build(); |
161 | | - return client.newCall(request); |
| 179 | + private fun createCall( |
| 180 | + client: OkHttpClient, |
| 181 | + urlPath: String, |
| 182 | + ): Call { |
| 183 | + val request = Request.Builder().url(server.url(urlPath)).build() |
| 184 | + return client.newCall(request) |
162 | 185 | } |
163 | 186 | } |
0 commit comments