|
45 | 45 | import co.elastic.apm.agent.impl.sampling.ConstantSampler;
|
46 | 46 | import co.elastic.apm.agent.impl.sampling.Sampler;
|
47 | 47 | import co.elastic.apm.agent.impl.stacktrace.StacktraceConfiguration;
|
| 48 | +import co.elastic.apm.agent.impl.transaction.AbstractSpan; |
48 | 49 | import co.elastic.apm.agent.impl.transaction.Id;
|
49 | 50 | import co.elastic.apm.agent.impl.transaction.OTelSpanKind;
|
50 | 51 | import co.elastic.apm.agent.impl.transaction.Span;
|
|
77 | 78 | import java.util.Map;
|
78 | 79 | import java.util.Objects;
|
79 | 80 | import java.util.concurrent.Future;
|
| 81 | +import java.util.function.Function; |
80 | 82 |
|
81 | 83 | import static org.assertj.core.api.Assertions.assertThat;
|
82 | 84 | import static org.assertj.core.api.SoftAssertions.assertSoftly;
|
@@ -1345,30 +1347,39 @@ void multiValueHeaders(boolean supportsMulti) {
|
1345 | 1347 |
|
1346 | 1348 | @Test
|
1347 | 1349 | void testOTelSpanSerialization() {
|
1348 |
| - Span span = new Span(MockTracer.create()).withName("span name"); |
1349 |
| - assertThat(span.getOtelKind()) |
| 1350 | + Span span = new Span(MockTracer.create()).withName("otel span"); |
| 1351 | + testOTelSpanSerialization(span, s -> readJsonString(serializer.toJsonString(s))); |
| 1352 | + |
| 1353 | + Transaction transaction = new Transaction(MockTracer.create()).withName("otel span"); |
| 1354 | + testOTelSpanSerialization(transaction, t -> readJsonString(serializer.toJsonString(t))); |
| 1355 | + } |
| 1356 | + |
| 1357 | + private <T extends AbstractSpan<T>> void testOTelSpanSerialization(T context, Function<T, JsonNode> toJson) { |
| 1358 | + |
| 1359 | + assertThat(context.getOtelKind()) |
1350 | 1360 | .describedAs("otel span kind should not be set by default")
|
1351 | 1361 | .isNull();
|
1352 | 1362 |
|
1353 |
| - JsonNode spanJson = readJsonString(serializer.toJsonString(span)); |
1354 |
| - assertThat(spanJson.get("name").asText()).isEqualTo("span name"); |
| 1363 | + JsonNode spanJson = toJson.apply(context ); |
| 1364 | + |
| 1365 | + assertThat(spanJson.get("name").asText()).isEqualTo("otel span"); |
1355 | 1366 | assertThat(spanJson.get("otel")).isNull();
|
1356 | 1367 |
|
1357 | 1368 | for (OTelSpanKind kind : OTelSpanKind.values()) {
|
1358 |
| - span.withOtelKind(kind); |
1359 |
| - spanJson = readJsonString(serializer.toJsonString(span)); |
| 1369 | + context.withOtelKind(kind); |
| 1370 | + spanJson = toJson.apply(context); |
1360 | 1371 |
|
1361 | 1372 | JsonNode otelJson = spanJson.get("otel");
|
1362 | 1373 | assertThat(otelJson).isNotNull();
|
1363 | 1374 | assertThat(otelJson.get("span_kind").asText()).isEqualTo(kind.name());
|
1364 | 1375 | }
|
1365 | 1376 |
|
1366 | 1377 | // with custom otel attributes
|
1367 |
| - span.getOtelAttributes().put("attribute.string", "hello"); |
1368 |
| - span.getOtelAttributes().put("attribute.long", 123L); |
1369 |
| - span.getOtelAttributes().put("attribute.boolean", false); |
1370 |
| - span.getOtelAttributes().put("attribute.float", 0.42f); |
1371 |
| - spanJson = readJsonString(serializer.toJsonString(span)); |
| 1378 | + context.getOtelAttributes().put("attribute.string", "hello"); |
| 1379 | + context.getOtelAttributes().put("attribute.long", 123L); |
| 1380 | + context.getOtelAttributes().put("attribute.boolean", false); |
| 1381 | + context.getOtelAttributes().put("attribute.float", 0.42f); |
| 1382 | + spanJson = toJson.apply(context); |
1372 | 1383 | JsonNode otelJson = spanJson.get("otel");
|
1373 | 1384 | assertThat(otelJson).isNotNull();
|
1374 | 1385 | JsonNode otelAttributes = otelJson.get("attributes");
|
|
0 commit comments