From 5855a2481dacf8dab7bb9dcfff8eac8b2b5bec52 Mon Sep 17 00:00:00 2001 From: Jonatan Ivanov Date: Thu, 10 Nov 2022 13:44:19 -0800 Subject: [PATCH 1/4] Populate Zipkin remoteEndpoint fixes gh-4932 --- .../zipkin/OtelToZipkinSpanTransformer.java | 26 ++++- .../OtelToZipkinSpanTransformerTest.java | 104 +++++++++++++++++- 2 files changed, 122 insertions(+), 8 deletions(-) diff --git a/exporters/zipkin/src/main/java/io/opentelemetry/exporter/zipkin/OtelToZipkinSpanTransformer.java b/exporters/zipkin/src/main/java/io/opentelemetry/exporter/zipkin/OtelToZipkinSpanTransformer.java index acb76c242ab..f0d8077ebaa 100644 --- a/exporters/zipkin/src/main/java/io/opentelemetry/exporter/zipkin/OtelToZipkinSpanTransformer.java +++ b/exporters/zipkin/src/main/java/io/opentelemetry/exporter/zipkin/OtelToZipkinSpanTransformer.java @@ -6,6 +6,9 @@ package io.opentelemetry.exporter.zipkin; import static io.opentelemetry.api.common.AttributeKey.stringKey; +import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NET_PEER_PORT; +import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NET_SOCK_PEER_ADDR; +import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.PEER_SERVICE; import static java.util.concurrent.TimeUnit.NANOSECONDS; import io.opentelemetry.api.common.AttributeKey; @@ -72,8 +75,6 @@ private OtelToZipkinSpanTransformer(Supplier ipAddressSupplier) { * @return a new Zipkin Span */ Span generateSpan(SpanData spanData) { - Endpoint endpoint = getEndpoint(spanData); - long startTimestamp = toEpochMicros(spanData.getStartEpochNanos()); long endTimestamp = toEpochMicros(spanData.getEndEpochNanos()); @@ -85,7 +86,8 @@ Span generateSpan(SpanData spanData) { .name(spanData.getName()) .timestamp(toEpochMicros(spanData.getStartEpochNanos())) .duration(Math.max(1, endTimestamp - startTimestamp)) - .localEndpoint(endpoint); + .localEndpoint(getLocalEndpoint(spanData)) + .remoteEndpoint(getRemoteEndpoint(spanData)); if (spanData.getParentSpanContext().isValid()) { spanBuilder.parentId(spanData.getParentSpanId()); @@ -140,7 +142,7 @@ private static String nullToEmpty(String value) { return value != null ? value : ""; } - private Endpoint getEndpoint(SpanData spanData) { + private Endpoint getLocalEndpoint(SpanData spanData) { Attributes resourceAttributes = spanData.getResource().getAttributes(); Endpoint.Builder endpoint = Endpoint.newBuilder(); @@ -158,6 +160,22 @@ private Endpoint getEndpoint(SpanData spanData) { return endpoint.build(); } + private static Endpoint getRemoteEndpoint(SpanData spanData) { + // TODO: Implement fallback mechanism: + // https://opentelemetry.io/docs/reference/specification/trace/sdk_exporters/zipkin/#otlp---zipkin + Attributes attributes = spanData.getAttributes(); + + Endpoint.Builder endpoint = Endpoint.newBuilder(); + endpoint.serviceName(attributes.get(PEER_SERVICE)); + endpoint.ip(attributes.get(NET_SOCK_PEER_ADDR)); + Long port = attributes.get(NET_PEER_PORT); + if (port != null) { + endpoint.port(port.intValue()); + } + + return endpoint.build(); + } + @Nullable private static Span.Kind toSpanKind(SpanData spanData) { switch (spanData.getKind()) { diff --git a/exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/OtelToZipkinSpanTransformerTest.java b/exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/OtelToZipkinSpanTransformerTest.java index b47b23e5d2d..bded0b78a2d 100644 --- a/exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/OtelToZipkinSpanTransformerTest.java +++ b/exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/OtelToZipkinSpanTransformerTest.java @@ -16,6 +16,9 @@ import static io.opentelemetry.exporter.zipkin.ZipkinTestUtil.spanBuilder; import static io.opentelemetry.exporter.zipkin.ZipkinTestUtil.zipkinSpan; import static io.opentelemetry.exporter.zipkin.ZipkinTestUtil.zipkinSpanBuilder; +import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NET_PEER_PORT; +import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NET_SOCK_PEER_ADDR; +import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.PEER_SERVICE; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -135,11 +138,11 @@ void generateSpan_ResourceServiceNameMapping() { Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, "super-zipkin-service")); SpanData data = spanBuilder().setResource(resource).build(); - Endpoint expectedEndpoint = + Endpoint expectedLocalEndpoint = Endpoint.newBuilder().serviceName("super-zipkin-service").ip(localIp).build(); Span expectedZipkinSpan = zipkinSpan(Span.Kind.SERVER, localIp).toBuilder() - .localEndpoint(expectedEndpoint) + .localEndpoint(expectedLocalEndpoint) .putTag(OtelToZipkinSpanTransformer.OTEL_STATUS_CODE, "OK") .build(); assertThat(transformer.generateSpan(data)).isEqualTo(expectedZipkinSpan); @@ -149,19 +152,112 @@ void generateSpan_ResourceServiceNameMapping() { void generateSpan_defaultResourceServiceName() { SpanData data = spanBuilder().setResource(Resource.empty()).build(); - Endpoint expectedEndpoint = + Endpoint expectedLocalEndpoint = Endpoint.newBuilder() .serviceName(Resource.getDefault().getAttribute(ResourceAttributes.SERVICE_NAME)) .ip(localIp) .build(); Span expectedZipkinSpan = zipkinSpan(Span.Kind.SERVER, localIp).toBuilder() - .localEndpoint(expectedEndpoint) + .localEndpoint(expectedLocalEndpoint) .putTag(OtelToZipkinSpanTransformer.OTEL_STATUS_CODE, "OK") .build(); assertThat(transformer.generateSpan(data)).isEqualTo(expectedZipkinSpan); } + @Test + void generateSpan_RemoteEndpointMapping() { + Attributes attributes = + Attributes.builder() + .put(PEER_SERVICE, "remote-test-service") + .put(NET_SOCK_PEER_ADDR, "8.8.8.8") + .put(NET_PEER_PORT, 42L) + .build(); + + SpanData spanData = + spanBuilder().setResource(Resource.empty()).setAttributes(attributes).build(); + + Endpoint expectedLocalEndpoint = + Endpoint.newBuilder() + .serviceName(Resource.getDefault().getAttribute(ResourceAttributes.SERVICE_NAME)) + .ip(localIp) + .build(); + + Endpoint expectedRemoteEndpoint = + Endpoint.newBuilder().serviceName("remote-test-service").ip("8.8.8.8").port(42).build(); + + Span expectedSpan = + zipkinSpan(Span.Kind.SERVER, localIp).toBuilder() + .localEndpoint(expectedLocalEndpoint) + .remoteEndpoint(expectedRemoteEndpoint) + .putTag(PEER_SERVICE.getKey(), "remote-test-service") + .putTag(NET_SOCK_PEER_ADDR.getKey(), "8.8.8.8") + .putTag(NET_PEER_PORT.getKey(), "42") + .putTag(OtelToZipkinSpanTransformer.OTEL_STATUS_CODE, "OK") + .build(); + + assertThat(transformer.generateSpan(spanData)).isEqualTo(expectedSpan); + } + + @Test + void generateSpan_RemoteEndpointMappingWhenPortIsMissing() { + Attributes attributes = + Attributes.builder() + .put(PEER_SERVICE, "remote-test-service") + .put(NET_SOCK_PEER_ADDR, "8.8.8.8") + .build(); + + SpanData spanData = + spanBuilder().setResource(Resource.empty()).setAttributes(attributes).build(); + + Endpoint expectedLocalEndpoint = + Endpoint.newBuilder() + .serviceName(Resource.getDefault().getAttribute(ResourceAttributes.SERVICE_NAME)) + .ip(localIp) + .build(); + + Endpoint expectedRemoteEndpoint = + Endpoint.newBuilder().serviceName("remote-test-service").ip("8.8.8.8").build(); + + Span expectedSpan = + zipkinSpan(Span.Kind.SERVER, localIp).toBuilder() + .localEndpoint(expectedLocalEndpoint) + .remoteEndpoint(expectedRemoteEndpoint) + .putTag(PEER_SERVICE.getKey(), "remote-test-service") + .putTag(NET_SOCK_PEER_ADDR.getKey(), "8.8.8.8") + .putTag(OtelToZipkinSpanTransformer.OTEL_STATUS_CODE, "OK") + .build(); + + assertThat(transformer.generateSpan(spanData)).isEqualTo(expectedSpan); + } + + @Test + void generateSpan_RemoteEndpointMappingWhenIpAndPortAreMissing() { + Attributes attributes = Attributes.builder().put(PEER_SERVICE, "remote-test-service").build(); + + SpanData spanData = + spanBuilder().setResource(Resource.empty()).setAttributes(attributes).build(); + + Endpoint expectedLocalEndpoint = + Endpoint.newBuilder() + .serviceName(Resource.getDefault().getAttribute(ResourceAttributes.SERVICE_NAME)) + .ip(localIp) + .build(); + + Endpoint expectedRemoteEndpoint = + Endpoint.newBuilder().serviceName("remote-test-service").build(); + + Span expectedSpan = + zipkinSpan(Span.Kind.SERVER, localIp).toBuilder() + .localEndpoint(expectedLocalEndpoint) + .remoteEndpoint(expectedRemoteEndpoint) + .putTag(PEER_SERVICE.getKey(), "remote-test-service") + .putTag(OtelToZipkinSpanTransformer.OTEL_STATUS_CODE, "OK") + .build(); + + assertThat(transformer.generateSpan(spanData)).isEqualTo(expectedSpan); + } + @Test void generateSpan_WithAttributes() { Attributes attributes = From cddd6a67a699f3b901c7e74fb6f0c4c98561cbf6 Mon Sep 17 00:00:00 2001 From: Jonatan Ivanov Date: Tue, 15 Nov 2022 11:07:58 -0800 Subject: [PATCH 2/4] Add conditions for creating zipkin remote endpoint --- .../zipkin/OtelToZipkinSpanTransformer.java | 31 ++++--- .../OtelToZipkinSpanTransformerTest.java | 88 ++++++++++++++++++- 2 files changed, 105 insertions(+), 14 deletions(-) diff --git a/exporters/zipkin/src/main/java/io/opentelemetry/exporter/zipkin/OtelToZipkinSpanTransformer.java b/exporters/zipkin/src/main/java/io/opentelemetry/exporter/zipkin/OtelToZipkinSpanTransformer.java index f0d8077ebaa..8175315851d 100644 --- a/exporters/zipkin/src/main/java/io/opentelemetry/exporter/zipkin/OtelToZipkinSpanTransformer.java +++ b/exporters/zipkin/src/main/java/io/opentelemetry/exporter/zipkin/OtelToZipkinSpanTransformer.java @@ -14,6 +14,7 @@ import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.AttributeType; import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.api.trace.SpanKind; import io.opentelemetry.api.trace.StatusCode; import io.opentelemetry.sdk.common.InstrumentationScopeInfo; import io.opentelemetry.sdk.resources.Resource; @@ -160,20 +161,28 @@ private Endpoint getLocalEndpoint(SpanData spanData) { return endpoint.build(); } + @Nullable private static Endpoint getRemoteEndpoint(SpanData spanData) { - // TODO: Implement fallback mechanism: - // https://opentelemetry.io/docs/reference/specification/trace/sdk_exporters/zipkin/#otlp---zipkin - Attributes attributes = spanData.getAttributes(); - - Endpoint.Builder endpoint = Endpoint.newBuilder(); - endpoint.serviceName(attributes.get(PEER_SERVICE)); - endpoint.ip(attributes.get(NET_SOCK_PEER_ADDR)); - Long port = attributes.get(NET_PEER_PORT); - if (port != null) { - endpoint.port(port.intValue()); + if (spanData.getKind() == SpanKind.CLIENT || spanData.getKind() == SpanKind.PRODUCER) { + // TODO: Implement fallback mechanism: + // https://opentelemetry.io/docs/reference/specification/trace/sdk_exporters/zipkin/#otlp---zipkin + Attributes attributes = spanData.getAttributes(); + String serviceName = attributes.get(PEER_SERVICE); + + if (serviceName != null) { + Endpoint.Builder endpoint = Endpoint.newBuilder(); + endpoint.serviceName(serviceName); + endpoint.ip(attributes.get(NET_SOCK_PEER_ADDR)); + Long port = attributes.get(NET_PEER_PORT); + if (port != null) { + endpoint.port(port.intValue()); + } + + return endpoint.build(); + } } - return endpoint.build(); + return null; } @Nullable diff --git a/exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/OtelToZipkinSpanTransformerTest.java b/exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/OtelToZipkinSpanTransformerTest.java index bded0b78a2d..dfa55e85aed 100644 --- a/exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/OtelToZipkinSpanTransformerTest.java +++ b/exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/OtelToZipkinSpanTransformerTest.java @@ -175,7 +175,11 @@ void generateSpan_RemoteEndpointMapping() { .build(); SpanData spanData = - spanBuilder().setResource(Resource.empty()).setAttributes(attributes).build(); + spanBuilder() + .setKind(SpanKind.CLIENT) + .setResource(Resource.empty()) + .setAttributes(attributes) + .build(); Endpoint expectedLocalEndpoint = Endpoint.newBuilder() @@ -188,6 +192,7 @@ void generateSpan_RemoteEndpointMapping() { Span expectedSpan = zipkinSpan(Span.Kind.SERVER, localIp).toBuilder() + .kind(Span.Kind.CLIENT) .localEndpoint(expectedLocalEndpoint) .remoteEndpoint(expectedRemoteEndpoint) .putTag(PEER_SERVICE.getKey(), "remote-test-service") @@ -199,6 +204,73 @@ void generateSpan_RemoteEndpointMapping() { assertThat(transformer.generateSpan(spanData)).isEqualTo(expectedSpan); } + @Test + void generateSpan_RemoteEndpointMappingWhenKindIsNotClientOrProducer() { + Attributes attributes = + Attributes.builder() + .put(PEER_SERVICE, "remote-test-service") + .put(NET_SOCK_PEER_ADDR, "8.8.8.8") + .put(NET_PEER_PORT, 42L) + .build(); + + SpanData spanData = + spanBuilder() + .setKind(SpanKind.SERVER) + .setResource(Resource.empty()) + .setAttributes(attributes) + .build(); + + Endpoint expectedLocalEndpoint = + Endpoint.newBuilder() + .serviceName(Resource.getDefault().getAttribute(ResourceAttributes.SERVICE_NAME)) + .ip(localIp) + .build(); + + Span expectedSpan = + zipkinSpan(Span.Kind.SERVER, localIp).toBuilder() + .kind(Span.Kind.SERVER) + .localEndpoint(expectedLocalEndpoint) + .remoteEndpoint(null) + .putTag(PEER_SERVICE.getKey(), "remote-test-service") + .putTag(NET_SOCK_PEER_ADDR.getKey(), "8.8.8.8") + .putTag(NET_PEER_PORT.getKey(), "42") + .putTag(OtelToZipkinSpanTransformer.OTEL_STATUS_CODE, "OK") + .build(); + + assertThat(transformer.generateSpan(spanData)).isEqualTo(expectedSpan); + } + + @Test + void generateSpan_RemoteEndpointMappingWhenServiceNameIsMissing() { + Attributes attributes = + Attributes.builder().put(NET_SOCK_PEER_ADDR, "8.8.8.8").put(NET_PEER_PORT, 42L).build(); + + SpanData spanData = + spanBuilder() + .setKind(SpanKind.CLIENT) + .setResource(Resource.empty()) + .setAttributes(attributes) + .build(); + + Endpoint expectedLocalEndpoint = + Endpoint.newBuilder() + .serviceName(Resource.getDefault().getAttribute(ResourceAttributes.SERVICE_NAME)) + .ip(localIp) + .build(); + + Span expectedSpan = + zipkinSpan(Span.Kind.SERVER, localIp).toBuilder() + .kind(Span.Kind.CLIENT) + .localEndpoint(expectedLocalEndpoint) + .remoteEndpoint(null) + .putTag(NET_SOCK_PEER_ADDR.getKey(), "8.8.8.8") + .putTag(NET_PEER_PORT.getKey(), "42") + .putTag(OtelToZipkinSpanTransformer.OTEL_STATUS_CODE, "OK") + .build(); + + assertThat(transformer.generateSpan(spanData)).isEqualTo(expectedSpan); + } + @Test void generateSpan_RemoteEndpointMappingWhenPortIsMissing() { Attributes attributes = @@ -208,7 +280,11 @@ void generateSpan_RemoteEndpointMappingWhenPortIsMissing() { .build(); SpanData spanData = - spanBuilder().setResource(Resource.empty()).setAttributes(attributes).build(); + spanBuilder() + .setKind(SpanKind.PRODUCER) + .setResource(Resource.empty()) + .setAttributes(attributes) + .build(); Endpoint expectedLocalEndpoint = Endpoint.newBuilder() @@ -221,6 +297,7 @@ void generateSpan_RemoteEndpointMappingWhenPortIsMissing() { Span expectedSpan = zipkinSpan(Span.Kind.SERVER, localIp).toBuilder() + .kind(Span.Kind.PRODUCER) .localEndpoint(expectedLocalEndpoint) .remoteEndpoint(expectedRemoteEndpoint) .putTag(PEER_SERVICE.getKey(), "remote-test-service") @@ -236,7 +313,11 @@ void generateSpan_RemoteEndpointMappingWhenIpAndPortAreMissing() { Attributes attributes = Attributes.builder().put(PEER_SERVICE, "remote-test-service").build(); SpanData spanData = - spanBuilder().setResource(Resource.empty()).setAttributes(attributes).build(); + spanBuilder() + .setKind(SpanKind.CLIENT) + .setResource(Resource.empty()) + .setAttributes(attributes) + .build(); Endpoint expectedLocalEndpoint = Endpoint.newBuilder() @@ -249,6 +330,7 @@ void generateSpan_RemoteEndpointMappingWhenIpAndPortAreMissing() { Span expectedSpan = zipkinSpan(Span.Kind.SERVER, localIp).toBuilder() + .kind(Span.Kind.CLIENT) .localEndpoint(expectedLocalEndpoint) .remoteEndpoint(expectedRemoteEndpoint) .putTag(PEER_SERVICE.getKey(), "remote-test-service") From 9eb08c2ee96f49b58c5278e3d28b53eaaf79714c Mon Sep 17 00:00:00 2001 From: Jonatan Ivanov Date: Wed, 16 Nov 2022 12:12:03 -0800 Subject: [PATCH 3/4] Parameterize remote endpoint tests with span kind --- .../OtelToZipkinSpanTransformerTest.java | 62 +++++++++++-------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/OtelToZipkinSpanTransformerTest.java b/exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/OtelToZipkinSpanTransformerTest.java index dfa55e85aed..144617fb3d5 100644 --- a/exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/OtelToZipkinSpanTransformerTest.java +++ b/exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/OtelToZipkinSpanTransformerTest.java @@ -36,6 +36,8 @@ import java.util.Collections; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; import zipkin2.Endpoint; import zipkin2.Span; @@ -165,8 +167,11 @@ void generateSpan_defaultResourceServiceName() { assertThat(transformer.generateSpan(data)).isEqualTo(expectedZipkinSpan); } - @Test - void generateSpan_RemoteEndpointMapping() { + @ParameterizedTest + @EnumSource( + value = SpanKind.class, + names = {"CLIENT", "PRODUCER"}) + void generateSpan_RemoteEndpointMapping(SpanKind spanKind) { Attributes attributes = Attributes.builder() .put(PEER_SERVICE, "remote-test-service") @@ -176,7 +181,7 @@ void generateSpan_RemoteEndpointMapping() { SpanData spanData = spanBuilder() - .setKind(SpanKind.CLIENT) + .setKind(spanKind) .setResource(Resource.empty()) .setAttributes(attributes) .build(); @@ -191,8 +196,7 @@ void generateSpan_RemoteEndpointMapping() { Endpoint.newBuilder().serviceName("remote-test-service").ip("8.8.8.8").port(42).build(); Span expectedSpan = - zipkinSpan(Span.Kind.SERVER, localIp).toBuilder() - .kind(Span.Kind.CLIENT) + zipkinSpan(Span.Kind.valueOf(spanKind.name()), localIp).toBuilder() .localEndpoint(expectedLocalEndpoint) .remoteEndpoint(expectedRemoteEndpoint) .putTag(PEER_SERVICE.getKey(), "remote-test-service") @@ -204,8 +208,11 @@ void generateSpan_RemoteEndpointMapping() { assertThat(transformer.generateSpan(spanData)).isEqualTo(expectedSpan); } - @Test - void generateSpan_RemoteEndpointMappingWhenKindIsNotClientOrProducer() { + @ParameterizedTest + @EnumSource( + value = SpanKind.class, + names = {"SERVER", "CONSUMER"}) + void generateSpan_RemoteEndpointMappingWhenKindIsNotClientOrProducer(SpanKind spanKind) { Attributes attributes = Attributes.builder() .put(PEER_SERVICE, "remote-test-service") @@ -215,7 +222,7 @@ void generateSpan_RemoteEndpointMappingWhenKindIsNotClientOrProducer() { SpanData spanData = spanBuilder() - .setKind(SpanKind.SERVER) + .setKind(spanKind) .setResource(Resource.empty()) .setAttributes(attributes) .build(); @@ -227,8 +234,7 @@ void generateSpan_RemoteEndpointMappingWhenKindIsNotClientOrProducer() { .build(); Span expectedSpan = - zipkinSpan(Span.Kind.SERVER, localIp).toBuilder() - .kind(Span.Kind.SERVER) + zipkinSpan(Span.Kind.valueOf(spanKind.name()), localIp).toBuilder() .localEndpoint(expectedLocalEndpoint) .remoteEndpoint(null) .putTag(PEER_SERVICE.getKey(), "remote-test-service") @@ -240,14 +246,17 @@ void generateSpan_RemoteEndpointMappingWhenKindIsNotClientOrProducer() { assertThat(transformer.generateSpan(spanData)).isEqualTo(expectedSpan); } - @Test - void generateSpan_RemoteEndpointMappingWhenServiceNameIsMissing() { + @ParameterizedTest + @EnumSource( + value = SpanKind.class, + names = {"CLIENT", "PRODUCER"}) + void generateSpan_RemoteEndpointMappingWhenServiceNameIsMissing(SpanKind spanKind) { Attributes attributes = Attributes.builder().put(NET_SOCK_PEER_ADDR, "8.8.8.8").put(NET_PEER_PORT, 42L).build(); SpanData spanData = spanBuilder() - .setKind(SpanKind.CLIENT) + .setKind(spanKind) .setResource(Resource.empty()) .setAttributes(attributes) .build(); @@ -259,8 +268,7 @@ void generateSpan_RemoteEndpointMappingWhenServiceNameIsMissing() { .build(); Span expectedSpan = - zipkinSpan(Span.Kind.SERVER, localIp).toBuilder() - .kind(Span.Kind.CLIENT) + zipkinSpan(Span.Kind.valueOf(spanKind.name()), localIp).toBuilder() .localEndpoint(expectedLocalEndpoint) .remoteEndpoint(null) .putTag(NET_SOCK_PEER_ADDR.getKey(), "8.8.8.8") @@ -271,8 +279,11 @@ void generateSpan_RemoteEndpointMappingWhenServiceNameIsMissing() { assertThat(transformer.generateSpan(spanData)).isEqualTo(expectedSpan); } - @Test - void generateSpan_RemoteEndpointMappingWhenPortIsMissing() { + @ParameterizedTest + @EnumSource( + value = SpanKind.class, + names = {"CLIENT", "PRODUCER"}) + void generateSpan_RemoteEndpointMappingWhenPortIsMissing(SpanKind spanKind) { Attributes attributes = Attributes.builder() .put(PEER_SERVICE, "remote-test-service") @@ -281,7 +292,7 @@ void generateSpan_RemoteEndpointMappingWhenPortIsMissing() { SpanData spanData = spanBuilder() - .setKind(SpanKind.PRODUCER) + .setKind(spanKind) .setResource(Resource.empty()) .setAttributes(attributes) .build(); @@ -296,8 +307,7 @@ void generateSpan_RemoteEndpointMappingWhenPortIsMissing() { Endpoint.newBuilder().serviceName("remote-test-service").ip("8.8.8.8").build(); Span expectedSpan = - zipkinSpan(Span.Kind.SERVER, localIp).toBuilder() - .kind(Span.Kind.PRODUCER) + zipkinSpan(Span.Kind.valueOf(spanKind.name()), localIp).toBuilder() .localEndpoint(expectedLocalEndpoint) .remoteEndpoint(expectedRemoteEndpoint) .putTag(PEER_SERVICE.getKey(), "remote-test-service") @@ -308,13 +318,16 @@ void generateSpan_RemoteEndpointMappingWhenPortIsMissing() { assertThat(transformer.generateSpan(spanData)).isEqualTo(expectedSpan); } - @Test - void generateSpan_RemoteEndpointMappingWhenIpAndPortAreMissing() { + @ParameterizedTest + @EnumSource( + value = SpanKind.class, + names = {"CLIENT", "PRODUCER"}) + void generateSpan_RemoteEndpointMappingWhenIpAndPortAreMissing(SpanKind spanKind) { Attributes attributes = Attributes.builder().put(PEER_SERVICE, "remote-test-service").build(); SpanData spanData = spanBuilder() - .setKind(SpanKind.CLIENT) + .setKind(spanKind) .setResource(Resource.empty()) .setAttributes(attributes) .build(); @@ -329,8 +342,7 @@ void generateSpan_RemoteEndpointMappingWhenIpAndPortAreMissing() { Endpoint.newBuilder().serviceName("remote-test-service").build(); Span expectedSpan = - zipkinSpan(Span.Kind.SERVER, localIp).toBuilder() - .kind(Span.Kind.CLIENT) + zipkinSpan(Span.Kind.valueOf(spanKind.name()), localIp).toBuilder() .localEndpoint(expectedLocalEndpoint) .remoteEndpoint(expectedRemoteEndpoint) .putTag(PEER_SERVICE.getKey(), "remote-test-service") From 505a7da06c314f98d146b1ef4958b811283ab432 Mon Sep 17 00:00:00 2001 From: Jonatan Ivanov Date: Fri, 18 Nov 2022 12:24:46 -0800 Subject: [PATCH 4/4] Verify INTERNAL span kind too --- .../OtelToZipkinSpanTransformerTest.java | 18 ++++++++++++------ .../exporter/zipkin/ZipkinTestUtil.java | 5 +++-- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/OtelToZipkinSpanTransformerTest.java b/exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/OtelToZipkinSpanTransformerTest.java index 144617fb3d5..230ab38894c 100644 --- a/exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/OtelToZipkinSpanTransformerTest.java +++ b/exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/OtelToZipkinSpanTransformerTest.java @@ -34,6 +34,7 @@ import java.net.InetAddress; import java.util.Arrays; import java.util.Collections; +import javax.annotation.Nullable; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -196,7 +197,7 @@ void generateSpan_RemoteEndpointMapping(SpanKind spanKind) { Endpoint.newBuilder().serviceName("remote-test-service").ip("8.8.8.8").port(42).build(); Span expectedSpan = - zipkinSpan(Span.Kind.valueOf(spanKind.name()), localIp).toBuilder() + zipkinSpan(toZipkinSpanKind(spanKind), localIp).toBuilder() .localEndpoint(expectedLocalEndpoint) .remoteEndpoint(expectedRemoteEndpoint) .putTag(PEER_SERVICE.getKey(), "remote-test-service") @@ -211,7 +212,7 @@ void generateSpan_RemoteEndpointMapping(SpanKind spanKind) { @ParameterizedTest @EnumSource( value = SpanKind.class, - names = {"SERVER", "CONSUMER"}) + names = {"SERVER", "CONSUMER", "INTERNAL"}) void generateSpan_RemoteEndpointMappingWhenKindIsNotClientOrProducer(SpanKind spanKind) { Attributes attributes = Attributes.builder() @@ -234,7 +235,7 @@ void generateSpan_RemoteEndpointMappingWhenKindIsNotClientOrProducer(SpanKind sp .build(); Span expectedSpan = - zipkinSpan(Span.Kind.valueOf(spanKind.name()), localIp).toBuilder() + zipkinSpan(toZipkinSpanKind(spanKind), localIp).toBuilder() .localEndpoint(expectedLocalEndpoint) .remoteEndpoint(null) .putTag(PEER_SERVICE.getKey(), "remote-test-service") @@ -268,7 +269,7 @@ void generateSpan_RemoteEndpointMappingWhenServiceNameIsMissing(SpanKind spanKin .build(); Span expectedSpan = - zipkinSpan(Span.Kind.valueOf(spanKind.name()), localIp).toBuilder() + zipkinSpan(toZipkinSpanKind(spanKind), localIp).toBuilder() .localEndpoint(expectedLocalEndpoint) .remoteEndpoint(null) .putTag(NET_SOCK_PEER_ADDR.getKey(), "8.8.8.8") @@ -307,7 +308,7 @@ void generateSpan_RemoteEndpointMappingWhenPortIsMissing(SpanKind spanKind) { Endpoint.newBuilder().serviceName("remote-test-service").ip("8.8.8.8").build(); Span expectedSpan = - zipkinSpan(Span.Kind.valueOf(spanKind.name()), localIp).toBuilder() + zipkinSpan(toZipkinSpanKind(spanKind), localIp).toBuilder() .localEndpoint(expectedLocalEndpoint) .remoteEndpoint(expectedRemoteEndpoint) .putTag(PEER_SERVICE.getKey(), "remote-test-service") @@ -342,7 +343,7 @@ void generateSpan_RemoteEndpointMappingWhenIpAndPortAreMissing(SpanKind spanKind Endpoint.newBuilder().serviceName("remote-test-service").build(); Span expectedSpan = - zipkinSpan(Span.Kind.valueOf(spanKind.name()), localIp).toBuilder() + zipkinSpan(toZipkinSpanKind(spanKind), localIp).toBuilder() .localEndpoint(expectedLocalEndpoint) .remoteEndpoint(expectedRemoteEndpoint) .putTag(PEER_SERVICE.getKey(), "remote-test-service") @@ -494,4 +495,9 @@ void generateSpan_WithRpcUnsetStatus() { .putTag(SemanticAttributes.RPC_SERVICE.getKey(), "my service name") .build()); } + + @Nullable + private static Span.Kind toZipkinSpanKind(SpanKind spanKind) { + return spanKind != SpanKind.INTERNAL ? Span.Kind.valueOf(spanKind.name()) : null; + } } diff --git a/exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/ZipkinTestUtil.java b/exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/ZipkinTestUtil.java index 1c96e034eeb..ef2f0aac6e3 100644 --- a/exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/ZipkinTestUtil.java +++ b/exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/ZipkinTestUtil.java @@ -19,6 +19,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import javax.annotation.Nullable; import zipkin2.Endpoint; import zipkin2.Span; @@ -60,11 +61,11 @@ static TestSpanData.Builder spanBuilder() { .setHasEnded(true); } - static Span zipkinSpan(Span.Kind kind, InetAddress localIp) { + static Span zipkinSpan(@Nullable Span.Kind kind, InetAddress localIp) { return zipkinSpanBuilder(kind, localIp).build(); } - static Span.Builder zipkinSpanBuilder(Span.Kind kind, InetAddress localIp) { + static Span.Builder zipkinSpanBuilder(@Nullable Span.Kind kind, InetAddress localIp) { return Span.newBuilder() .traceId(TRACE_ID) .parentId(PARENT_SPAN_ID)