|
15 | 15 |
|
16 | 16 | package software.amazon.opentelemetry.javaagent.providers; |
17 | 17 |
|
18 | | -import static org.mockito.Mockito.mock; |
19 | | -import static org.mockito.Mockito.when; |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 20 | +import static org.mockito.Mockito.*; |
20 | 21 |
|
21 | 22 | import io.opentelemetry.api.common.AttributeKey; |
22 | 23 | import io.opentelemetry.api.common.Attributes; |
23 | 24 | import io.opentelemetry.api.trace.*; |
24 | | -import io.opentelemetry.sdk.OpenTelemetrySdk; |
25 | 25 | import io.opentelemetry.sdk.common.InstrumentationScopeInfo; |
26 | 26 | import io.opentelemetry.sdk.resources.Resource; |
27 | | -import io.opentelemetry.sdk.trace.ReadableSpan; |
28 | 27 | import io.opentelemetry.sdk.trace.data.SpanData; |
29 | 28 | import io.opentelemetry.sdk.trace.data.StatusData; |
30 | | -import java.util.Arrays; |
31 | 29 | import java.util.Collections; |
32 | 30 | import org.junit.jupiter.api.Test; |
33 | 31 |
|
34 | 32 | public class UdpExporterTest { |
35 | | - private static final boolean CONTAINS_ATTRIBUTES = true; |
36 | 33 |
|
37 | 34 | @Test |
38 | | - public void testExporter() { // TODO: only for testing. remove. |
39 | | - // Tracer tracer = GlobalOpenTelemetry.getTracer("My application"); |
40 | | - Tracer tracer = OpenTelemetrySdk.builder().build().getTracer("hello"); |
41 | | - |
42 | | - Span mySpan = tracer.spanBuilder("DoTheLoop_3").startSpan(); |
43 | | - int numIterations = 5; |
44 | | - mySpan.setAttribute("NumIterations", numIterations); |
45 | | - mySpan.setAttribute(AttributeKey.stringArrayKey("foo"), Arrays.asList("bar1", "bar2")); |
46 | | - try (var scope = mySpan.makeCurrent()) { |
47 | | - for (int i = 1; i <= numIterations; i++) { |
48 | | - System.out.println("i = " + i); |
49 | | - } |
50 | | - } finally { |
51 | | - mySpan.end(); |
52 | | - } |
53 | | - |
| 35 | + public void testUdpExporterWithDefaults() { |
54 | 36 | OtlpUdpSpanExporter exporter = new OtlpUdpSpanExporterBuilder().build(); |
| 37 | + UdpSender sender = exporter.getSender(); |
| 38 | + assertThat(sender.getEndpoint().getHostName()) |
| 39 | + .isEqualTo("localhost"); // getHostName implicitly converts 127.0.0.1 to localhost |
| 40 | + assertThat(sender.getEndpoint().getPort()).isEqualTo(2000); |
| 41 | + assertThat(exporter.isSampled()).isTrue(); |
| 42 | + } |
55 | 43 |
|
56 | | - ReadableSpan readableSpan = (ReadableSpan) mySpan; |
57 | | - SpanData spanData = readableSpan.toSpanData(); |
| 44 | + @Test |
| 45 | + public void testUdpExporterWithCustomEndpointAndSample() { |
| 46 | + OtlpUdpSpanExporter exporter = |
| 47 | + new OtlpUdpSpanExporterBuilder().setEndpoint("somehost:1000").setSampled(false).build(); |
| 48 | + UdpSender sender = exporter.getSender(); |
| 49 | + assertThat(sender.getEndpoint().getHostName()).isEqualTo("somehost"); |
| 50 | + assertThat(sender.getEndpoint().getPort()).isEqualTo(1000); |
| 51 | + assertThat(exporter.isSampled()).isFalse(); |
| 52 | + } |
58 | 53 |
|
59 | | - exporter.export(Collections.singletonList(spanData)); |
| 54 | + @Test |
| 55 | + public void testUdpExporterWithInvalidEndpoint() { |
| 56 | + assertThatThrownBy( |
| 57 | + () -> { |
| 58 | + new OtlpUdpSpanExporterBuilder().setEndpoint("invalidhost"); |
| 59 | + }) |
| 60 | + .isInstanceOf(IllegalArgumentException.class) |
| 61 | + .hasMessage("Invalid endpoint, must be a valid URL: invalidhost"); |
60 | 62 | } |
61 | 63 |
|
62 | 64 | @Test |
63 | | - public void testUdpExporter() { |
64 | | - // Add your test logic here |
65 | | - OtlpUdpSpanExporter exporter = new OtlpUdpSpanExporterBuilder().build(); |
| 65 | + public void testExport() { |
| 66 | + UdpSender senderMock = mock(UdpSender.class); |
| 67 | + |
| 68 | + // mock SpanData |
| 69 | + SpanData spanData = buildSpanDataMock(); |
| 70 | + |
| 71 | + OtlpUdpSpanExporter exporter = new OtlpUdpSpanExporterBuilder().setSender(senderMock).build(); |
| 72 | + exporter.export(Collections.singletonList(spanData)); |
| 73 | + |
| 74 | + // assert that the senderMock.send is called once |
| 75 | + verify(senderMock, times(1)).send(any(byte[].class)); |
| 76 | + } |
| 77 | + |
| 78 | + private static SpanData buildSpanDataMock() { |
| 79 | + SpanData mockSpanData = mock(SpanData.class); |
| 80 | + |
| 81 | + Attributes spanAttributes = |
| 82 | + Attributes.of(AttributeKey.stringKey("original key"), "original value"); |
| 83 | + when(mockSpanData.getAttributes()).thenReturn(spanAttributes); |
| 84 | + when(mockSpanData.getTotalAttributeCount()).thenReturn(spanAttributes.size()); |
| 85 | + when(mockSpanData.getKind()).thenReturn(SpanKind.SERVER); |
66 | 86 |
|
67 | | - // build test span |
68 | 87 | SpanContext parentSpanContextMock = mock(SpanContext.class); |
69 | | - Attributes spanAttributes = buildSpanAttributes(CONTAINS_ATTRIBUTES); |
70 | | - SpanData spanDataMock = buildSpanDataMock(spanAttributes); |
71 | | - when(spanDataMock.getParentSpanContext()).thenReturn(parentSpanContextMock); |
| 88 | + when(mockSpanData.getParentSpanContext()).thenReturn(parentSpanContextMock); |
| 89 | + |
72 | 90 | SpanContext spanContextMock = mock(SpanContext.class); |
73 | 91 | when(spanContextMock.isValid()).thenReturn(true); |
74 | | - when(spanDataMock.getSpanContext()).thenReturn(spanContextMock); |
| 92 | + when(mockSpanData.getSpanContext()).thenReturn(spanContextMock); |
| 93 | + |
75 | 94 | TraceState traceState = TraceState.builder().build(); |
76 | 95 | when(spanContextMock.getTraceState()).thenReturn(traceState); |
77 | | - StatusData statusData = StatusData.unset(); |
78 | | - when(spanDataMock.getStatus()).thenReturn(statusData); |
79 | | - when(spanDataMock.getInstrumentationScopeInfo()) |
| 96 | + |
| 97 | + when(mockSpanData.getStatus()).thenReturn(StatusData.unset()); |
| 98 | + when(mockSpanData.getInstrumentationScopeInfo()) |
80 | 99 | .thenReturn(InstrumentationScopeInfo.create("Dummy Scope")); |
81 | 100 |
|
82 | 101 | Resource testResource = Resource.empty(); |
83 | | - when(spanDataMock.getResource()).thenReturn(testResource); |
84 | | - |
85 | | - exporter.export(Collections.singletonList(spanDataMock)); |
86 | | - } |
87 | | - |
88 | | - private static Attributes buildSpanAttributes(boolean containsAttribute) { |
89 | | - if (containsAttribute) { |
90 | | - return Attributes.of(AttributeKey.stringKey("original key"), "original value"); |
91 | | - } else { |
92 | | - return Attributes.empty(); |
93 | | - } |
94 | | - } |
| 102 | + when(mockSpanData.getResource()).thenReturn(testResource); |
95 | 103 |
|
96 | | - private static SpanData buildSpanDataMock(Attributes spanAttributes) { |
97 | | - // Configure spanData |
98 | | - SpanData mockSpanData = mock(SpanData.class); |
99 | | - when(mockSpanData.getAttributes()).thenReturn(spanAttributes); |
100 | | - when(mockSpanData.getTotalAttributeCount()).thenReturn(spanAttributes.size()); |
101 | | - when(mockSpanData.getKind()).thenReturn(SpanKind.SERVER); |
102 | | - when(mockSpanData.getParentSpanContext()).thenReturn(null); |
103 | 104 | return mockSpanData; |
104 | 105 | } |
105 | 106 | } |
0 commit comments