aboutsummaryrefslogtreecommitdiff
path: root/exporters
diff options
context:
space:
mode:
authorYang Song <songy23@users.noreply.github.com>2018-07-22 18:15:09 -0700
committerGitHub <noreply@github.com>2018-07-22 18:15:09 -0700
commit74f335c227bf8d3f6d1e5b074dcd18eeb56d70b9 (patch)
tree952d13bc362046d881c9f39e2f8628a109c1f470 /exporters
parent82e2988b912f8ec73f391f3ad5e9fe64dee2b5fe (diff)
downloadopencensus-java-74f335c227bf8d3f6d1e5b074dcd18eeb56d70b9.tar.gz
Zipkin exporter: Use the correct attribute value. (#1334)
* Zipkin exporter: Use the correct attribute value. * Add a unit test.
Diffstat (limited to 'exporters')
-rw-r--r--exporters/trace/zipkin/src/main/java/io/opencensus/exporter/trace/zipkin/ZipkinExporterHandler.java32
-rw-r--r--exporters/trace/zipkin/src/test/java/io/opencensus/exporter/trace/zipkin/ZipkinExporterHandlerTest.java49
2 files changed, 80 insertions, 1 deletions
diff --git a/exporters/trace/zipkin/src/main/java/io/opencensus/exporter/trace/zipkin/ZipkinExporterHandler.java b/exporters/trace/zipkin/src/main/java/io/opencensus/exporter/trace/zipkin/ZipkinExporterHandler.java
index 827dfd88..4d0c44e7 100644
--- a/exporters/trace/zipkin/src/main/java/io/opencensus/exporter/trace/zipkin/ZipkinExporterHandler.java
+++ b/exporters/trace/zipkin/src/main/java/io/opencensus/exporter/trace/zipkin/ZipkinExporterHandler.java
@@ -19,6 +19,8 @@ package io.opencensus.exporter.trace.zipkin;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
+import io.opencensus.common.Function;
+import io.opencensus.common.Functions;
import io.opencensus.common.Scope;
import io.opencensus.common.Timestamp;
import io.opencensus.trace.Annotation;
@@ -165,8 +167,36 @@ final class ZipkinExporterHandler extends SpanExporter.Handler {
return SECONDS.toMicros(timestamp.getSeconds()) + NANOSECONDS.toMicros(timestamp.getNanos());
}
+ private static final Function<String, String> STRING_ATTRIBUTE_FUNCTION =
+ new Function<String, String>() {
+ @Override
+ public String apply(String stringValue) {
+ return stringValue;
+ }
+ };
+
+ private static final Function<Boolean, String> BOOLEAN_ATTRIBUTE_FUNCTION =
+ new Function<Boolean, String>() {
+ @Override
+ public String apply(Boolean booleanValue) {
+ return booleanValue.toString();
+ }
+ };
+
+ private static final Function<Long, String> LONG_ATTRIBUTE_FUNCTION =
+ new Function<Long, String>() {
+ @Override
+ public String apply(Long longValue) {
+ return longValue.toString();
+ }
+ };
+
private static String attributeValueToString(AttributeValue attributeValue) {
- return attributeValue.toString();
+ return attributeValue.match(
+ STRING_ATTRIBUTE_FUNCTION,
+ BOOLEAN_ATTRIBUTE_FUNCTION,
+ LONG_ATTRIBUTE_FUNCTION,
+ Functions.<String>returnConstant(""));
}
@Override
diff --git a/exporters/trace/zipkin/src/test/java/io/opencensus/exporter/trace/zipkin/ZipkinExporterHandlerTest.java b/exporters/trace/zipkin/src/test/java/io/opencensus/exporter/trace/zipkin/ZipkinExporterHandlerTest.java
index 636ffc95..5de247a4 100644
--- a/exporters/trace/zipkin/src/test/java/io/opencensus/exporter/trace/zipkin/ZipkinExporterHandlerTest.java
+++ b/exporters/trace/zipkin/src/test/java/io/opencensus/exporter/trace/zipkin/ZipkinExporterHandlerTest.java
@@ -37,6 +37,7 @@ import io.opencensus.trace.export.SpanData.Links;
import io.opencensus.trace.export.SpanData.TimedEvent;
import io.opencensus.trace.export.SpanData.TimedEvents;
import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
@@ -186,4 +187,52 @@ public class ZipkinExporterHandlerTest {
.putTag("census.status_code", "OK")
.build());
}
+
+ @Test
+ public void generateSpan_WithAttributes() {
+ Map<String, AttributeValue> attributeMap = new HashMap<String, AttributeValue>();
+ attributeMap.put("string", AttributeValue.stringAttributeValue("string value"));
+ attributeMap.put("boolean", AttributeValue.booleanAttributeValue(false));
+ attributeMap.put("long", AttributeValue.longAttributeValue(9999L));
+ SpanData data =
+ SpanData.create(
+ SpanContext.create(
+ TraceId.fromLowerBase16(TRACE_ID),
+ SpanId.fromLowerBase16(SPAN_ID),
+ TraceOptions.fromBytes(new byte[] {1} /* sampled */)),
+ // TODO SpanId.fromLowerBase16
+ SpanId.fromLowerBase16(PARENT_SPAN_ID),
+ true, /* hasRemoteParent */
+ "Sent.helloworld.Greeter.SayHello", /* name */
+ Kind.CLIENT, /* kind */
+ Timestamp.create(1505855794, 194009601) /* startTimestamp */,
+ Attributes.create(attributeMap, 0 /* droppedAttributesCount */),
+ TimedEvents.create(annotations, 0 /* droppedEventsCount */),
+ TimedEvents.create(messageEvents, 0 /* droppedEventsCount */),
+ Links.create(Collections.<Link>emptyList(), 0 /* droppedLinksCount */),
+ null, /* childSpanCount */
+ Status.OK,
+ Timestamp.create(1505855799, 465726528) /* endTimestamp */);
+
+ assertThat(ZipkinExporterHandler.generateSpan(data, localEndpoint))
+ .isEqualTo(
+ Span.newBuilder()
+ .traceId(TRACE_ID)
+ .parentId(PARENT_SPAN_ID)
+ .id(SPAN_ID)
+ .kind(Span.Kind.CLIENT)
+ .name(data.getName())
+ .timestamp(1505855794000000L + 194009601L / 1000)
+ .duration(
+ (1505855799000000L + 465726528L / 1000)
+ - (1505855794000000L + 194009601L / 1000))
+ .localEndpoint(localEndpoint)
+ .addAnnotation(1505855799000000L + 433901068L / 1000, "RECEIVED")
+ .addAnnotation(1505855799000000L + 459486280L / 1000, "SENT")
+ .putTag("census.status_code", "OK")
+ .putTag("string", "string value")
+ .putTag("boolean", "false")
+ .putTag("long", "9999")
+ .build());
+ }
}