Skip to content

Commit 6021aca

Browse files
committed
add test suppress instrumentation for zipkin
1 parent 300712b commit 6021aca

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/ZipkinSpanExporterTest.java

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@
99
import static io.opentelemetry.exporter.zipkin.ZipkinTestUtil.zipkinSpanBuilder;
1010
import static org.assertj.core.api.Assertions.assertThat;
1111
import static org.assertj.core.api.Assertions.assertThatThrownBy;
12+
import static org.junit.jupiter.api.Assertions.assertFalse;
13+
import static org.junit.jupiter.api.Assertions.assertTrue;
1214
import static org.mockito.Mockito.doThrow;
1315
import static org.mockito.Mockito.verify;
1416
import static org.mockito.Mockito.when;
1517

1618
import io.github.netmikey.logunit.api.LogCapturer;
19+
import io.opentelemetry.api.internal.InstrumentationUtil;
1720
import io.opentelemetry.api.metrics.MeterProvider;
21+
import io.opentelemetry.context.Context;
1822
import io.opentelemetry.internal.testing.slf4j.SuppressLogger;
1923
import io.opentelemetry.sdk.common.CompletableResultCode;
2024
import io.opentelemetry.sdk.testing.trace.TestSpanData;
@@ -23,6 +27,7 @@
2327
import java.time.Duration;
2428
import java.util.Collections;
2529
import java.util.concurrent.TimeUnit;
30+
import java.util.concurrent.atomic.AtomicBoolean;
2631
import org.junit.jupiter.api.Test;
2732
import org.junit.jupiter.api.extension.ExtendWith;
2833
import org.junit.jupiter.api.extension.RegisterExtension;
@@ -33,6 +38,7 @@
3338
import zipkin2.reporter.BytesMessageSender;
3439
import zipkin2.reporter.Encoding;
3540
import zipkin2.reporter.SpanBytesEncoder;
41+
import zipkin2.reporter.okhttp3.OkHttpSender;
3642

3743
@ExtendWith(MockitoExtension.class)
3844
class ZipkinSpanExporterTest {
@@ -119,18 +125,18 @@ void testShutdown() throws IOException {
119125
verify(mockSender).close();
120126
assertThat(logs.getEvents()).isEmpty();
121127
assertThat(
122-
exporter
123-
.export(Collections.singletonList(spanBuilder().build()))
124-
.join(10, TimeUnit.SECONDS)
125-
.isSuccess())
128+
exporter
129+
.export(Collections.singletonList(spanBuilder().build()))
130+
.join(10, TimeUnit.SECONDS)
131+
.isSuccess())
126132
.isFalse();
127133
assertThat(exporter.shutdown().isSuccess()).isTrue();
128134
logs.assertContains("Calling shutdown() multiple times.");
129135
}
130136

131137
@Test
132138
@SuppressWarnings({"PreferJavaTimeOverload", "deprecation"})
133-
// we have to use the deprecated setEncoder overload to test it
139+
// we have to use the deprecated setEncoder overload to test it
134140
void invalidConfig() {
135141
assertThatThrownBy(() -> ZipkinSpanExporter.builder().setReadTimeout(-1, TimeUnit.MILLISECONDS))
136142
.isInstanceOf(IllegalArgumentException.class)
@@ -157,7 +163,7 @@ void invalidConfig() {
157163
.hasMessage("sender");
158164

159165
assertThatThrownBy(
160-
() -> ZipkinSpanExporter.builder().setEncoder((zipkin2.codec.BytesEncoder<Span>) null))
166+
() -> ZipkinSpanExporter.builder().setEncoder((zipkin2.codec.BytesEncoder<Span>) null))
161167
.isInstanceOf(NullPointerException.class)
162168
.hasMessage("encoder");
163169

@@ -244,4 +250,28 @@ void stringRepresentation() {
244250
"ZipkinSpanExporter{endpoint=http://zipkin:9411/api/v2/spans, compressionEnabled=false, readTimeoutMillis=15000}");
245251
}
246252
}
253+
254+
@Test
255+
void testSuppressInstrumentation() {
256+
AtomicBoolean suppressInstrumentation = new AtomicBoolean(
257+
InstrumentationUtil.shouldSuppressInstrumentation(Context.current()));
258+
259+
assertFalse(suppressInstrumentation.get());
260+
261+
InstrumentationUtil.suppressInstrumentation(() ->
262+
{
263+
try (BytesMessageSender sender = OkHttpSender.newBuilder().endpoint("https://localhost")
264+
.encoding(Encoding.PROTO3)
265+
.build()) {
266+
sender.send(Collections.singletonList(new byte[0]));
267+
} catch (IOException e) {
268+
//it always goes here
269+
suppressInstrumentation.set(
270+
InstrumentationUtil.shouldSuppressInstrumentation(Context.current()));
271+
}
272+
}
273+
);
274+
assertTrue(suppressInstrumentation.get());
275+
}
276+
247277
}

0 commit comments

Comments
 (0)