forked from open-telemetry/opentelemetry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZipkinSpanExporterEndToEndHttpTest.java
283 lines (250 loc) · 11.1 KB
/
ZipkinSpanExporterEndToEndHttpTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.exporter.zipkin;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import static org.mockito.Mockito.mock;
import com.linecorp.armeria.client.WebClient;
import com.linecorp.armeria.common.AggregatedHttpResponse;
import com.linecorp.armeria.common.HttpData;
import com.linecorp.armeria.common.HttpStatus;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.MeterProvider;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.TraceFlags;
import io.opentelemetry.api.trace.TraceState;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader;
import io.opentelemetry.sdk.testing.trace.TestSpanData;
import io.opentelemetry.sdk.trace.IdGenerator;
import io.opentelemetry.sdk.trace.data.EventData;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.data.StatusData;
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import zipkin2.Endpoint;
import zipkin2.Span;
import zipkin2.codec.Encoding;
import zipkin2.codec.SpanBytesDecoder;
import zipkin2.codec.SpanBytesEncoder;
import zipkin2.reporter.okhttp3.OkHttpSender;
@Testcontainers(disabledWithoutDocker = true)
class ZipkinSpanExporterEndToEndHttpTest {
private static final WebClient client = WebClient.of();
private static final int ZIPKIN_API_PORT = 9411;
private static final String SPAN_ID = "9cc1e3049173be09";
private static final String PARENT_SPAN_ID = "8b03ab423da481c5";
private static final String SPAN_NAME = "Recv.helloworld.Greeter.SayHello";
private static final long START_EPOCH_NANOS = 1505855794_194009601L;
private static final long END_EPOCH_NANOS = 1505855799_465726528L;
private static final long RECEIVED_TIMESTAMP_NANOS = 1505855799_433901068L;
private static final long SENT_TIMESTAMP_NANOS = 1505855799_459486280L;
private static final Attributes attributes = Attributes.empty();
private static final List<EventData> annotations =
Collections.unmodifiableList(
Arrays.asList(
EventData.create(RECEIVED_TIMESTAMP_NANOS, "RECEIVED", Attributes.empty()),
EventData.create(SENT_TIMESTAMP_NANOS, "SENT", Attributes.empty())));
private static final String ENDPOINT_V1_SPANS = "/api/v1/spans";
private static final String ENDPOINT_V2_SPANS = "/api/v2/spans";
private static final String SERVICE_NAME = "myService";
private static final Attributes SEEN_ATTRIBUTES =
Attributes.of(AttributeKey.stringKey("type"), "span");
private static final Attributes EXPORTED_SUCCESS_ATTRIBUTES =
SEEN_ATTRIBUTES.toBuilder().put(AttributeKey.booleanKey("success"), true).build();
private static final Attributes EXPORTED_FAILED_ATTRIBUTES =
SEEN_ATTRIBUTES.toBuilder().put(AttributeKey.booleanKey("success"), false).build();
@Container
public static GenericContainer<?> zipkinContainer =
new GenericContainer<>("ghcr.io/openzipkin/zipkin:2.23")
.withExposedPorts(ZIPKIN_API_PORT)
.waitingFor(Wait.forHttp("/health").forPort(ZIPKIN_API_PORT));
private final InMemoryMetricReader sdkMeterReader = InMemoryMetricReader.create();
private final SdkMeterProvider sdkMeterProvider =
SdkMeterProvider.builder().registerMetricReader(sdkMeterReader).build();
private static final InetAddress localIp = mock(InetAddress.class);
@AfterEach
void tearDown() {
sdkMeterProvider.close();
}
@Test
void testExportWithDefaultEncoding() {
ZipkinSpanExporter exporter =
ZipkinSpanExporter.builder()
.setEndpoint(zipkinUrl(ENDPOINT_V2_SPANS))
.setMeterProvider(sdkMeterProvider)
.setLocalIpAddressSupplier(() -> localIp)
.build();
exportAndVerify(exporter);
exporter.close();
verifyMetrics(sdkMeterReader, "http-json", EXPORTED_SUCCESS_ATTRIBUTES);
}
@Test
void testExportAsProtobuf() {
ZipkinSpanExporter exporter =
buildZipkinExporter(
zipkinUrl(ENDPOINT_V2_SPANS),
Encoding.PROTO3,
SpanBytesEncoder.PROTO3,
sdkMeterProvider);
exportAndVerify(exporter);
exporter.close();
verifyMetrics(sdkMeterReader, "http", EXPORTED_SUCCESS_ATTRIBUTES);
}
@Test
void testExportAsThrift() {
@SuppressWarnings("deprecation") // we have to use the deprecated thrift encoding to test it
ZipkinSpanExporter exporter =
buildZipkinExporter(
zipkinUrl(ENDPOINT_V1_SPANS),
Encoding.THRIFT,
SpanBytesEncoder.THRIFT,
sdkMeterProvider);
exportAndVerify(exporter);
exporter.close();
verifyMetrics(sdkMeterReader, "http", EXPORTED_SUCCESS_ATTRIBUTES);
}
@Test
void testExportAsJsonV1() {
ZipkinSpanExporter exporter =
buildZipkinExporter(
zipkinUrl(ENDPOINT_V1_SPANS),
Encoding.JSON,
SpanBytesEncoder.JSON_V1,
sdkMeterProvider);
exportAndVerify(exporter);
exporter.close();
verifyMetrics(sdkMeterReader, "http-json", EXPORTED_SUCCESS_ATTRIBUTES);
}
@Test
void testExportFailedAsWrongEncoderUsed() {
ZipkinSpanExporter exporter =
buildZipkinExporter(
zipkinUrl(ENDPOINT_V2_SPANS), Encoding.JSON, SpanBytesEncoder.PROTO3, sdkMeterProvider);
String traceId = IdGenerator.random().generateTraceId();
SpanData spanData = buildStandardSpan(traceId).build();
CompletableResultCode resultCode = exporter.export(Collections.singleton(spanData));
assertThat(resultCode.isSuccess()).isFalse();
List<Span> zipkinSpans = getTrace(traceId);
assertThat(zipkinSpans).isEmpty();
exporter.close();
verifyMetrics(sdkMeterReader, "http-json", EXPORTED_FAILED_ATTRIBUTES);
}
private static ZipkinSpanExporter buildZipkinExporter(
String endpoint, Encoding encoding, SpanBytesEncoder encoder, MeterProvider meterProvider) {
return ZipkinSpanExporter.builder()
.setSender(OkHttpSender.newBuilder().endpoint(endpoint).encoding(encoding).build())
.setEncoder(encoder)
.setMeterProvider(meterProvider)
.setLocalIpAddressSupplier(() -> localIp)
.build();
}
/**
* Exports a span, verify that it was received by Zipkin, and check that the span stored by Zipkin
* matches what was sent.
*/
private static void exportAndVerify(ZipkinSpanExporter zipkinSpanExporter) {
String traceId = IdGenerator.random().generateTraceId();
SpanData spanData = buildStandardSpan(traceId).build();
CompletableResultCode resultCode = zipkinSpanExporter.export(Collections.singleton(spanData));
resultCode.join(10, TimeUnit.SECONDS);
assertThat(resultCode.isSuccess()).isTrue();
List<Span> zipkinSpans = getTrace(traceId);
assertThat(zipkinSpans).isNotNull();
assertThat(zipkinSpans.size()).isEqualTo(1);
assertThat(zipkinSpans.get(0)).isEqualTo(buildZipkinSpan(localIp, traceId));
}
private static TestSpanData.Builder buildStandardSpan(String traceId) {
return TestSpanData.builder()
.setSpanContext(
SpanContext.create(traceId, SPAN_ID, TraceFlags.getSampled(), TraceState.getDefault()))
.setParentSpanContext(
SpanContext.create(
traceId, PARENT_SPAN_ID, TraceFlags.getDefault(), TraceState.getDefault()))
.setStatus(StatusData.ok())
.setKind(SpanKind.SERVER)
.setName(SPAN_NAME)
.setStartEpochNanos(START_EPOCH_NANOS)
.setAttributes(attributes)
.setTotalAttributeCount(attributes.size())
.setTotalRecordedEvents(annotations.size())
.setEvents(annotations)
.setLinks(Collections.emptyList())
.setEndEpochNanos(END_EPOCH_NANOS)
.setHasEnded(true)
.setResource(Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, SERVICE_NAME)));
}
private static Span buildZipkinSpan(InetAddress localAddress, String traceId) {
return Span.newBuilder()
.traceId(traceId)
.parentId(PARENT_SPAN_ID)
.id(SPAN_ID)
.kind(Span.Kind.SERVER)
.name(SPAN_NAME)
.timestamp(START_EPOCH_NANOS / 1000)
.duration((END_EPOCH_NANOS / 1000) - (START_EPOCH_NANOS / 1000))
.localEndpoint(Endpoint.newBuilder().serviceName(SERVICE_NAME).ip(localAddress).build())
.addAnnotation(RECEIVED_TIMESTAMP_NANOS / 1000, "\"RECEIVED\":{}")
.addAnnotation(SENT_TIMESTAMP_NANOS / 1000, "\"SENT\":{}")
.putTag(OtelToZipkinSpanTransformer.OTEL_STATUS_CODE, "OK")
.build();
}
private static List<Span> getTrace(String traceId) {
AggregatedHttpResponse response =
client.get(zipkinUrl("/api/v2/trace/" + traceId)).aggregate().join();
if (response.status().equals(HttpStatus.NOT_FOUND)) {
return Collections.emptyList();
}
try (HttpData content = response.content()) {
return SpanBytesDecoder.JSON_V2.decodeList(content.array());
}
}
private static String zipkinUrl(String endpoint) {
return "http://localhost:" + zipkinContainer.getMappedPort(ZIPKIN_API_PORT) + endpoint;
}
private static void verifyMetrics(
InMemoryMetricReader sdkMeterReader, String transportName, Attributes exportedAttributes) {
assertThat(sdkMeterReader.collectAllMetrics())
.allSatisfy(
metric ->
assertThat(metric)
.hasInstrumentationScope(
InstrumentationScopeInfo.create(
"io.opentelemetry.exporters.zipkin-" + transportName)))
.satisfiesExactlyInAnyOrder(
metric ->
assertThat(metric)
.hasName("zipkin.exporter.seen")
.hasLongSumSatisfying(
sum ->
sum.isMonotonic()
.isCumulative()
.hasPointsSatisfying(
point -> point.hasAttributes(SEEN_ATTRIBUTES).hasValue(1))),
metric ->
assertThat(metric)
.hasName("zipkin.exporter.exported")
.hasLongSumSatisfying(
sum ->
sum.isMonotonic()
.isCumulative()
.hasPointsSatisfying(
point -> point.hasAttributes(exportedAttributes).hasValue(1))));
}
}