Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit 48106bc

Browse files
authored
Exporter/Jaeger: Set span status as tags. (#1928)
* Exporter/Jaeger: Set span status as tags. Fixes #1927. * Expose constants to tests. * Remove the error tag.
1 parent 29b5085 commit 48106bc

3 files changed

Lines changed: 70 additions & 9 deletions

File tree

exporters/trace/jaeger/src/main/java/io/opencensus/exporter/trace/jaeger/JaegerExporterHandler.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static java.util.concurrent.TimeUnit.NANOSECONDS;
2222
import static java.util.concurrent.TimeUnit.SECONDS;
2323

24+
import com.google.common.annotations.VisibleForTesting;
2425
import com.google.common.collect.Lists;
2526
import com.google.common.primitives.Ints;
2627
import com.google.common.primitives.Longs;
@@ -44,6 +45,7 @@
4445
import io.opencensus.trace.MessageEvent.Type;
4546
import io.opencensus.trace.SpanContext;
4647
import io.opencensus.trace.SpanId;
48+
import io.opencensus.trace.Status;
4749
import io.opencensus.trace.TraceId;
4850
import io.opencensus.trace.TraceOptions;
4951
import io.opencensus.trace.export.SpanData;
@@ -57,7 +59,7 @@
5759
@NotThreadSafe
5860
final class JaegerExporterHandler extends TimeLimitedHandler {
5961
private static final String EXPORT_SPAN_NAME = "ExportJaegerTraces";
60-
private static final String SPAN_KIND = "span.kind";
62+
@VisibleForTesting static final String SPAN_KIND = "span.kind";
6163
private static final Tag SERVER_KIND_TAG = new Tag(SPAN_KIND, TagType.STRING).setVStr("server");
6264
private static final Tag CLIENT_KIND_TAG = new Tag(SPAN_KIND, TagType.STRING).setVStr("client");
6365
private static final String DESCRIPTION = "message";
@@ -68,6 +70,8 @@ final class JaegerExporterHandler extends TimeLimitedHandler {
6870
private static final String MESSAGE_EVENT_ID = "id";
6971
private static final String MESSAGE_EVENT_COMPRESSED_SIZE = "compressed_size";
7072
private static final String MESSAGE_EVENT_UNCOMPRESSED_SIZE = "uncompressed_size";
73+
@VisibleForTesting static final String STATUS_CODE = "status.code";
74+
@VisibleForTesting static final String STATUS_MESSAGE = "status.message";
7175

7276
private static final Function<? super String, Tag> stringAttributeConverter =
7377
new Function<String, Tag>() {
@@ -161,6 +165,11 @@ private Span spanDataToJaegerThriftSpan(final SpanData spanData) {
161165
final SpanContext context = spanData.getContext();
162166
copyToBuffer(context.getTraceId());
163167

168+
List<Tag> tags =
169+
attributesToTags(
170+
spanData.getAttributes().getAttributeMap(), spanKindToTag(spanData.getKind()));
171+
addStatusTags(tags, spanData.getStatus());
172+
164173
return new io.jaegertracing.thriftjava.Span(
165174
traceIdLow(),
166175
traceIdHigh(),
@@ -171,9 +180,7 @@ private Span spanDataToJaegerThriftSpan(final SpanData spanData) {
171180
startTimeInMicros,
172181
endTimeInMicros - startTimeInMicros)
173182
.setReferences(linksToReferences(spanData.getLinks().getLinks()))
174-
.setTags(
175-
attributesToTags(
176-
spanData.getAttributes().getAttributeMap(), spanKindToTag(spanData.getKind())))
183+
.setTags(tags)
177184
.setLogs(
178185
timedEventsToLogs(
179186
spanData.getAnnotations().getEvents(), spanData.getMessageEvents().getEvents()));
@@ -335,4 +342,15 @@ private static Tag spanKindToTag(@Nullable final io.opencensus.trace.Span.Kind k
335342
}
336343
return null;
337344
}
345+
346+
private static void addStatusTags(List<Tag> tags, @Nullable Status status) {
347+
if (status == null) {
348+
return;
349+
}
350+
Tag statusTag = new Tag(STATUS_CODE, TagType.LONG).setVLong(status.getCanonicalCode().value());
351+
tags.add(statusTag);
352+
if (status.getDescription() != null) {
353+
tags.add(new Tag(STATUS_MESSAGE, TagType.STRING).setVStr(status.getDescription()));
354+
}
355+
}
338356
}

exporters/trace/jaeger/src/test/java/io/opencensus/exporter/trace/jaeger/JaegerExporterHandlerIntegrationTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,15 @@ public void exportToJaeger() throws IOException, InterruptedException {
173173
assertThat(span.get("duration").getAsLong()).isAtMost(durationInMicros);
174174

175175
JsonArray tags = span.get("tags").getAsJsonArray();
176-
assertThat(tags.size()).isEqualTo(1);
176+
assertThat(tags.size()).isEqualTo(2);
177177
JsonObject tag = tags.get(0).getAsJsonObject();
178178
assertThat(tag.get("key").getAsString()).isEqualTo("foo");
179179
assertThat(tag.get("type").getAsString()).isEqualTo("string");
180180
assertThat(tag.get("value").getAsString()).isEqualTo("bar");
181+
JsonObject statusTag = tags.get(1).getAsJsonObject();
182+
assertThat(statusTag.get("key").getAsString()).isEqualTo(JaegerExporterHandler.STATUS_CODE);
183+
assertThat(statusTag.get("type").getAsString()).isEqualTo("int64");
184+
assertThat(statusTag.get("value").getAsLong()).isEqualTo(0);
181185

182186
JsonArray logs = span.get("logs").getAsJsonArray();
183187
assertThat(logs.size()).isEqualTo(2);

exporters/trace/jaeger/src/test/java/io/opencensus/exporter/trace/jaeger/JaegerExporterHandlerTest.java

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
import io.opencensus.trace.TraceOptions;
4949
import io.opencensus.trace.Tracestate;
5050
import io.opencensus.trace.export.SpanData;
51+
import io.opencensus.trace.export.SpanData.TimedEvent;
52+
import java.util.Collections;
5153
import java.util.List;
5254
import org.junit.Test;
5355
import org.junit.runner.RunWith;
@@ -103,15 +105,15 @@ public void exportShouldConvertFromSpanDataToJaegerThriftSpan() throws SenderExc
103105
assertThat(span.startTime).isEqualTo(MILLISECONDS.toMicros(startTime));
104106
assertThat(span.duration).isEqualTo(MILLISECONDS.toMicros(endTime - startTime));
105107

106-
assertThat(span.tags.size()).isEqualTo(4);
108+
assertThat(span.tags.size()).isEqualTo(5);
107109
assertThat(span.tags)
108110
.containsExactly(
109111
new Tag("BOOL", TagType.BOOL).setVBool(false),
110112
new Tag("LONG", TagType.LONG).setVLong(Long.MAX_VALUE),
111-
new Tag("span.kind", TagType.STRING).setVStr("server"),
113+
new Tag(JaegerExporterHandler.SPAN_KIND, TagType.STRING).setVStr("server"),
112114
new Tag("STRING", TagType.STRING)
113-
.setVStr(
114-
"Judge of a man by his questions rather than by his answers. -- Voltaire"));
115+
.setVStr("Judge of a man by his questions rather than by his answers. -- Voltaire"),
116+
new Tag(JaegerExporterHandler.STATUS_CODE, TagType.LONG).setVLong(0));
115117

116118
assertThat(span.logs.size()).isEqualTo(2);
117119
Log log = span.logs.get(0);
@@ -142,6 +144,43 @@ public void exportShouldConvertFromSpanDataToJaegerThriftSpan() throws SenderExc
142144
assertThat(reference.refType).isEqualTo(SpanRefType.CHILD_OF);
143145
}
144146

147+
@Test
148+
public void convertErrorSpanDataToJaegerThriftSpan() throws SenderException {
149+
long startTime = 1519629870001L;
150+
long endTime = 1519630148002L;
151+
String statusMessage = "timeout";
152+
SpanData spanData =
153+
SpanData.create(
154+
sampleSpanContext(),
155+
SpanId.fromBytes(new byte[] {(byte) 0x7F, FF, FF, FF, FF, FF, FF, FF}),
156+
true,
157+
"test",
158+
Kind.SERVER,
159+
Timestamp.fromMillis(startTime),
160+
SpanData.Attributes.create(Collections.<String, AttributeValue>emptyMap(), 0),
161+
SpanData.TimedEvents.create(Collections.<TimedEvent<Annotation>>emptyList(), 0),
162+
SpanData.TimedEvents.create(Collections.<TimedEvent<MessageEvent>>emptyList(), 0),
163+
SpanData.Links.create(Collections.<Link>emptyList(), 0),
164+
0,
165+
Status.DEADLINE_EXCEEDED.withDescription(statusMessage),
166+
Timestamp.fromMillis(endTime));
167+
168+
handler.export(singletonList(spanData));
169+
170+
verify(mockSender).send(eq(process), captor.capture());
171+
List<Span> spans = captor.getValue();
172+
173+
assertThat(spans.size()).isEqualTo(1);
174+
Span span = spans.get(0);
175+
176+
assertThat(span.tags.size()).isEqualTo(3);
177+
assertThat(span.tags)
178+
.containsExactly(
179+
new Tag(JaegerExporterHandler.SPAN_KIND, TagType.STRING).setVStr("server"),
180+
new Tag(JaegerExporterHandler.STATUS_CODE, TagType.LONG).setVLong(4),
181+
new Tag(JaegerExporterHandler.STATUS_MESSAGE, TagType.STRING).setVStr(statusMessage));
182+
}
183+
145184
private static SpanContext sampleSpanContext() {
146185
return SpanContext.create(
147186
TraceId.fromBytes(new byte[] {FF, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}),

0 commit comments

Comments
 (0)