|
9 | 9 | import static io.opentelemetry.exporter.zipkin.ZipkinTestUtil.zipkinSpanBuilder; |
10 | 10 | import static org.assertj.core.api.Assertions.assertThat; |
11 | 11 | import static org.assertj.core.api.Assertions.assertThatThrownBy; |
12 | | -import static org.junit.jupiter.api.Assertions.assertFalse; |
13 | 12 | import static org.junit.jupiter.api.Assertions.assertTrue; |
14 | 13 | import static org.mockito.Mockito.doThrow; |
15 | 14 | import static org.mockito.Mockito.verify; |
|
26 | 25 | import java.net.InetAddress; |
27 | 26 | import java.time.Duration; |
28 | 27 | import java.util.Collections; |
| 28 | +import java.util.List; |
29 | 29 | import java.util.concurrent.TimeUnit; |
30 | 30 | import java.util.concurrent.atomic.AtomicBoolean; |
31 | 31 | import org.junit.jupiter.api.Test; |
|
38 | 38 | import zipkin2.reporter.BytesMessageSender; |
39 | 39 | import zipkin2.reporter.Encoding; |
40 | 40 | import zipkin2.reporter.SpanBytesEncoder; |
41 | | -import zipkin2.reporter.okhttp3.OkHttpSender; |
42 | 41 |
|
43 | 42 | @ExtendWith(MockitoExtension.class) |
44 | 43 | class ZipkinSpanExporterTest { |
@@ -253,25 +252,52 @@ void stringRepresentation() { |
253 | 252 |
|
254 | 253 | @Test |
255 | 254 | void testSuppressInstrumentation() { |
256 | | - AtomicBoolean suppressInstrumentation = |
257 | | - new AtomicBoolean(InstrumentationUtil.shouldSuppressInstrumentation(Context.current())); |
258 | | - |
259 | | - assertFalse(suppressInstrumentation.get()); |
260 | | - |
261 | | - InstrumentationUtil.suppressInstrumentation( |
262 | | - () -> { |
263 | | - try (BytesMessageSender sender = |
264 | | - OkHttpSender.newBuilder() |
265 | | - .endpoint("https://localhost") |
266 | | - .encoding(Encoding.PROTO3) |
267 | | - .build()) { |
268 | | - sender.send(Collections.singletonList(new byte[0])); |
269 | | - } catch (IOException e) { |
270 | | - // it always goes here |
271 | | - suppressInstrumentation.set( |
272 | | - InstrumentationUtil.shouldSuppressInstrumentation(Context.current())); |
273 | | - } |
274 | | - }); |
275 | | - assertTrue(suppressInstrumentation.get()); |
| 255 | + TestSpanData testSpanData = spanBuilder().build(); |
| 256 | + |
| 257 | + SuppressCatchingSender suppressCatchingSender = new SuppressCatchingSender(Encoding.JSON); |
| 258 | + ZipkinSpanExporter zipkinSpanExporter = |
| 259 | + new ZipkinSpanExporter( |
| 260 | + new ZipkinSpanExporterBuilder(), |
| 261 | + mockEncoder, |
| 262 | + suppressCatchingSender, |
| 263 | + MeterProvider::noop, |
| 264 | + mockTransformer); |
| 265 | + |
| 266 | + byte[] someBytes = new byte[0]; |
| 267 | + Span zipkinSpan = |
| 268 | + zipkinSpanBuilder(Span.Kind.SERVER, localIp) |
| 269 | + .putTag(OtelToZipkinSpanTransformer.OTEL_STATUS_CODE, "OK") |
| 270 | + .build(); |
| 271 | + when(mockTransformer.generateSpan(testSpanData)).thenReturn(zipkinSpan); |
| 272 | + when(mockEncoder.encode(zipkinSpan)).thenReturn(someBytes); |
| 273 | + |
| 274 | + zipkinSpanExporter.export(Collections.singleton(testSpanData)); |
| 275 | + |
| 276 | + assertTrue(suppressCatchingSender.sent.get()); |
| 277 | + assertTrue(suppressCatchingSender.suppressed.get()); |
| 278 | + } |
| 279 | + |
| 280 | + static class SuppressCatchingSender extends BytesMessageSender.Base { |
| 281 | + |
| 282 | + final AtomicBoolean sent = new AtomicBoolean(); |
| 283 | + final AtomicBoolean suppressed = new AtomicBoolean(); |
| 284 | + |
| 285 | + protected SuppressCatchingSender(Encoding encoding) { |
| 286 | + super(encoding); |
| 287 | + } |
| 288 | + |
| 289 | + @Override |
| 290 | + public int messageMaxBytes() { |
| 291 | + return 1024; |
| 292 | + } |
| 293 | + |
| 294 | + @Override |
| 295 | + public void send(List<byte[]> list) throws IOException { |
| 296 | + sent.set(true); |
| 297 | + suppressed.set(InstrumentationUtil.shouldSuppressInstrumentation(Context.current())); |
| 298 | + } |
| 299 | + |
| 300 | + @Override |
| 301 | + public void close() throws IOException {} |
276 | 302 | } |
277 | 303 | } |
0 commit comments