Minor cleanups to zipkin exporter. (#2836)

* Minor cleanups to zipkin exporter.

* Another tiny cleanup
This commit is contained in:
Anuraag Agrawal 2021-02-17 14:39:29 +09:00 committed by GitHub
parent 5d7f221058
commit 31a6da0bf5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 21 deletions

View File

@ -11,7 +11,6 @@ import static java.util.concurrent.TimeUnit.NANOSECONDS;
import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.AttributeType; import io.opentelemetry.api.common.AttributeType;
import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.StatusCode; import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.sdk.common.CompletableResultCode; import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
@ -78,10 +77,8 @@ public final class ZipkinSpanExporter implements SpanExporter {
} }
} catch (Exception e) { } catch (Exception e) {
// don't crash the caller if there was a problem reading nics. // don't crash the caller if there was a problem reading nics.
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, "error reading nics", e); logger.log(Level.FINE, "error reading nics", e);
} }
}
return null; return null;
} }
@ -155,23 +152,18 @@ public final class ZipkinSpanExporter implements SpanExporter {
@Nullable @Nullable
private static Span.Kind toSpanKind(SpanData spanData) { private static Span.Kind toSpanKind(SpanData spanData) {
// This is a hack because the Span API did not have SpanKind. switch (spanData.getKind()) {
if (spanData.getKind() == SpanKind.SERVER) { case SERVER:
return Span.Kind.SERVER; return Span.Kind.SERVER;
} case CLIENT:
// This is a hack because the Span API did not have SpanKind.
if (spanData.getKind() == SpanKind.CLIENT || spanData.getName().startsWith("Sent.")) {
return Span.Kind.CLIENT; return Span.Kind.CLIENT;
} case PRODUCER:
if (spanData.getKind() == SpanKind.PRODUCER) {
return Span.Kind.PRODUCER; return Span.Kind.PRODUCER;
} case CONSUMER:
if (spanData.getKind() == SpanKind.CONSUMER) {
return Span.Kind.CONSUMER; return Span.Kind.CONSUMER;
case INTERNAL:
return null;
} }
return null; return null;
} }
@ -179,7 +171,7 @@ public final class ZipkinSpanExporter implements SpanExporter {
return NANOSECONDS.toMicros(epochNanos); return NANOSECONDS.toMicros(epochNanos);
} }
private static <T> String valueToString(AttributeKey<?> key, Object attributeValue) { private static String valueToString(AttributeKey<?> key, Object attributeValue) {
AttributeType type = key.getType(); AttributeType type = key.getType();
switch (type) { switch (type) {
case STRING: case STRING:

View File

@ -169,6 +169,23 @@ class ZipkinSpanExporterTest {
assertThat(exporter.generateSpan(data)).isEqualTo(expectedZipkinSpan); assertThat(exporter.generateSpan(data)).isEqualTo(expectedZipkinSpan);
} }
@Test
void generateSpan_defaultResourceServiceName() {
SpanData data = buildStandardSpan().setResource(Resource.getEmpty()).build();
Endpoint expectedEndpoint =
Endpoint.newBuilder()
.serviceName(Resource.getDefault().getAttributes().get(ResourceAttributes.SERVICE_NAME))
.ip(exporter.getLocalAddressForTest())
.build();
Span expectedZipkinSpan =
buildZipkinSpan(Span.Kind.SERVER).toBuilder()
.localEndpoint(expectedEndpoint)
.putTag(ZipkinSpanExporter.OTEL_STATUS_CODE, "OK")
.build();
assertThat(exporter.generateSpan(data)).isEqualTo(expectedZipkinSpan);
}
@Test @Test
void generateSpan_WithAttributes() { void generateSpan_WithAttributes() {
Attributes attributes = Attributes attributes =