Skip to content

Commit

Permalink
Populate Zipkin remoteEndpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatan-ivanov committed Nov 10, 2022
1 parent e814602 commit 5855a24
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -72,8 +75,6 @@ private OtelToZipkinSpanTransformer(Supplier<InetAddress> ipAddressSupplier) {
* @return a new Zipkin Span
*/
Span generateSpan(SpanData spanData) {
Endpoint endpoint = getEndpoint(spanData);

long startTimestamp = toEpochMicros(spanData.getStartEpochNanos());
long endTimestamp = toEpochMicros(spanData.getEndEpochNanos());

Expand All @@ -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());
Expand Down Expand Up @@ -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();
Expand All @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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 =
Expand Down

0 comments on commit 5855a24

Please sign in to comment.