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

Commit d3c6cf5

Browse files
authored
Exporter/Zipkin: Add a note about status tags. (#1931)
* Exporter/Zipkin: Add a note about status tags. * Set error tag when status is not OK.
1 parent f0c0ca2 commit d3c6cf5

2 files changed

Lines changed: 58 additions & 6 deletions

File tree

exporters/trace/zipkin/src/main/java/io/opencensus/exporter/trace/zipkin/ZipkinExporterHandler.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static java.util.concurrent.TimeUnit.NANOSECONDS;
2020
import static java.util.concurrent.TimeUnit.SECONDS;
2121

22+
import com.google.common.annotations.VisibleForTesting;
2223
import io.opencensus.common.Duration;
2324
import io.opencensus.common.Function;
2425
import io.opencensus.common.Functions;
@@ -53,8 +54,13 @@
5354
final class ZipkinExporterHandler extends TimeLimitedHandler {
5455
private static final Logger logger = Logger.getLogger(ZipkinExporterHandler.class.getName());
5556
private static final String EXPORT_SPAN_NAME = "SendZipkinSpans";
56-
private static final String STATUS_CODE = "census.status_code";
57-
private static final String STATUS_DESCRIPTION = "census.status_description";
57+
58+
// The naming follows Zipkin convention. As an example see:
59+
// https://github.com/apache/incubator-zipkin-brave/blob/643b7245c462dc14d47afcdb076b2603fd421497/instrumentation/grpc/src/main/java/brave/grpc/GrpcParser.java#L67-L73
60+
@VisibleForTesting static final String STATUS_CODE = "census.status_code";
61+
@VisibleForTesting static final String STATUS_DESCRIPTION = "census.status_description";
62+
@VisibleForTesting static final String STATUS_ERROR = "error";
63+
5864
private final SpanBytesEncoder encoder;
5965
private final Sender sender;
6066
private final Endpoint localEndpoint;
@@ -130,6 +136,9 @@ static Span generateSpan(SpanData spanData, Endpoint localEndpoint) {
130136
if (status.getDescription() != null) {
131137
spanBuilder.putTag(STATUS_DESCRIPTION, status.getDescription());
132138
}
139+
if (!status.isOk()) {
140+
spanBuilder.putTag(STATUS_ERROR, status.getCanonicalCode().toString());
141+
}
133142
}
134143

135144
for (TimedEvent<Annotation> annotation : spanData.getAnnotations().getEvents()) {

exporters/trace/zipkin/src/test/java/io/opencensus/exporter/trace/zipkin/ZipkinExporterHandlerTest.java

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public void generateSpan_NoKindAndRemoteParent() {
102102
.localEndpoint(localEndpoint)
103103
.addAnnotation(1505855799000000L + 433901068L / 1000, "RECEIVED")
104104
.addAnnotation(1505855799000000L + 459486280L / 1000, "SENT")
105-
.putTag("census.status_code", "OK")
105+
.putTag(ZipkinExporterHandler.STATUS_CODE, "OK")
106106
.build());
107107
}
108108

@@ -143,7 +143,7 @@ public void generateSpan_ServerKind() {
143143
.localEndpoint(localEndpoint)
144144
.addAnnotation(1505855799000000L + 433901068L / 1000, "RECEIVED")
145145
.addAnnotation(1505855799000000L + 459486280L / 1000, "SENT")
146-
.putTag("census.status_code", "OK")
146+
.putTag(ZipkinExporterHandler.STATUS_CODE, "OK")
147147
.build());
148148
}
149149

@@ -184,7 +184,7 @@ public void generateSpan_ClientKind() {
184184
.localEndpoint(localEndpoint)
185185
.addAnnotation(1505855799000000L + 433901068L / 1000, "RECEIVED")
186186
.addAnnotation(1505855799000000L + 459486280L / 1000, "SENT")
187-
.putTag("census.status_code", "OK")
187+
.putTag(ZipkinExporterHandler.STATUS_CODE, "OK")
188188
.build());
189189
}
190190

@@ -229,10 +229,53 @@ public void generateSpan_WithAttributes() {
229229
.localEndpoint(localEndpoint)
230230
.addAnnotation(1505855799000000L + 433901068L / 1000, "RECEIVED")
231231
.addAnnotation(1505855799000000L + 459486280L / 1000, "SENT")
232-
.putTag("census.status_code", "OK")
232+
.putTag(ZipkinExporterHandler.STATUS_CODE, "OK")
233233
.putTag("string", "string value")
234234
.putTag("boolean", "false")
235235
.putTag("long", "9999")
236236
.build());
237237
}
238+
239+
@Test
240+
public void generateSpan_WithErrorStatus() {
241+
String errorMessage = "timeout";
242+
SpanData data =
243+
SpanData.create(
244+
SpanContext.create(
245+
TraceId.fromLowerBase16(TRACE_ID),
246+
SpanId.fromLowerBase16(SPAN_ID),
247+
TraceOptions.builder().setIsSampled(true).build()),
248+
SpanId.fromLowerBase16(PARENT_SPAN_ID),
249+
true, /* hasRemoteParent */
250+
"Recv.helloworld.Greeter.SayHello", /* name */
251+
Kind.SERVER, /* kind */
252+
Timestamp.create(1505855794, 194009601) /* startTimestamp */,
253+
Attributes.create(attributes, 0 /* droppedAttributesCount */),
254+
TimedEvents.create(annotations, 0 /* droppedEventsCount */),
255+
TimedEvents.create(messageEvents, 0 /* droppedEventsCount */),
256+
Links.create(Collections.<Link>emptyList(), 0 /* droppedLinksCount */),
257+
null, /* childSpanCount */
258+
Status.DEADLINE_EXCEEDED.withDescription(errorMessage),
259+
Timestamp.create(1505855799, 465726528) /* endTimestamp */);
260+
261+
assertThat(ZipkinExporterHandler.generateSpan(data, localEndpoint))
262+
.isEqualTo(
263+
Span.newBuilder()
264+
.traceId(TRACE_ID)
265+
.parentId(PARENT_SPAN_ID)
266+
.id(SPAN_ID)
267+
.kind(Span.Kind.SERVER)
268+
.name(data.getName())
269+
.timestamp(1505855794000000L + 194009601L / 1000)
270+
.duration(
271+
(1505855799000000L + 465726528L / 1000)
272+
- (1505855794000000L + 194009601L / 1000))
273+
.localEndpoint(localEndpoint)
274+
.addAnnotation(1505855799000000L + 433901068L / 1000, "RECEIVED")
275+
.addAnnotation(1505855799000000L + 459486280L / 1000, "SENT")
276+
.putTag(ZipkinExporterHandler.STATUS_CODE, "DEADLINE_EXCEEDED")
277+
.putTag(ZipkinExporterHandler.STATUS_DESCRIPTION, errorMessage)
278+
.putTag(ZipkinExporterHandler.STATUS_ERROR, "DEADLINE_EXCEEDED")
279+
.build());
280+
}
238281
}

0 commit comments

Comments
 (0)