Search in sources :

Example 46 with Span

use of zipkin2.proto3.Span in project zipkin-gcp by openzipkin.

the class TraceTranslatorTest method testMultihostServerRootSpan_noTimestamp.

@Test
public void testMultihostServerRootSpan_noTimestamp() {
    Span span1 = Span.newBuilder().kind(Kind.SERVER).traceId("1").id("1").name("/a").build();
    Span span2 = Span.newBuilder().kind(Kind.CLIENT).traceId("1").parentId("1").id("2").name("/b?client").build();
    Span span3 = Span.newBuilder().kind(Kind.SERVER).shared(true).traceId("1").parentId("1").id("2").name("/b?server").build();
    Span span4 = Span.newBuilder().traceId("1").parentId("2").id("3").name("custom-span").build();
    Collection<Trace> traces = TraceTranslator.translateSpans("test-project", Arrays.asList(span1, span2, span3, span4));
    assertEquals(1, traces.size());
    Trace trace = traces.iterator().next();
    Map<String, TraceSpan> spansByName = getSpansByName(trace);
    assertThat(spansByName).containsOnlyKeys("/a", "/b?client", "/b?server", "custom-span");
    assertDistinctSpanIds(trace);
    assertThat(spansByName.get("custom-span").getParentSpanId()).isEqualTo(spansByName.get("/b?server").getSpanId());
    assertThat(spansByName.get("/b?server").getParentSpanId()).isEqualTo(spansByName.get("/b?client").getSpanId());
    assertThat(spansByName.get("/b?client").getParentSpanId()).isEqualTo(spansByName.get("/a").getSpanId());
// Without the timestamp field, it's not possible to correctly set the root span's parentSpanId
// to 0 because we didn't have enough information to conclude that it had no parent.
}
Also used : Trace(com.google.devtools.cloudtrace.v1.Trace) TraceSpan(com.google.devtools.cloudtrace.v1.TraceSpan) Span(zipkin2.Span) TraceSpan(com.google.devtools.cloudtrace.v1.TraceSpan) Test(org.junit.Test)

Example 47 with Span

use of zipkin2.proto3.Span in project instrumentation-java by census-instrumentation.

the class StackdriverV2ExporterHandler method generateSpan.

@VisibleForTesting
Span generateSpan(SpanData spanData, Map<String, AttributeValue> resourceLabels, Map<String, AttributeValue> fixedAttributes) {
    SpanContext context = spanData.getContext();
    final String spanIdHex = context.getSpanId().toLowerBase16();
    SpanName spanName = SpanName.newBuilder().setProject(projectId).setTrace(context.getTraceId().toLowerBase16()).setSpan(spanIdHex).build();
    Span.Builder spanBuilder = Span.newBuilder().setName(spanName.toString()).setSpanId(spanIdHex).setDisplayName(toTruncatableStringProto(toDisplayName(spanData.getName(), spanData.getKind()))).setStartTime(toTimestampProto(spanData.getStartTimestamp())).setAttributes(toAttributesProto(spanData.getAttributes(), resourceLabels, fixedAttributes)).setTimeEvents(toTimeEventsProto(spanData.getAnnotations(), spanData.getMessageEvents()));
    io.opencensus.trace.Status status = spanData.getStatus();
    if (status != null) {
        spanBuilder.setStatus(toStatusProto(status));
    }
    Timestamp end = spanData.getEndTimestamp();
    if (end != null) {
        spanBuilder.setEndTime(toTimestampProto(end));
    }
    spanBuilder.setLinks(toLinksProto(spanData.getLinks()));
    Integer childSpanCount = spanData.getChildSpanCount();
    if (childSpanCount != null) {
        spanBuilder.setChildSpanCount(Int32Value.newBuilder().setValue(childSpanCount).build());
    }
    if (spanData.getParentSpanId() != null && spanData.getParentSpanId().isValid()) {
        spanBuilder.setParentSpanId(spanData.getParentSpanId().toLowerBase16());
    }
    /*@Nullable*/
    Boolean hasRemoteParent = spanData.getHasRemoteParent();
    if (hasRemoteParent != null) {
        spanBuilder.setSameProcessAsParentSpan(BoolValue.of(!hasRemoteParent));
    }
    return spanBuilder.build();
}
Also used : SpanName(com.google.devtools.cloudtrace.v2.SpanName) SpanContext(io.opencensus.trace.SpanContext) TruncatableString(com.google.devtools.cloudtrace.v2.TruncatableString) Span(com.google.devtools.cloudtrace.v2.Span) Timestamp(io.opencensus.common.Timestamp) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 48 with Span

use of zipkin2.proto3.Span in project instrumentation-java by census-instrumentation.

the class StackdriverV2ExporterHandler method export.

@Override
public void export(Collection<SpanData> spanDataList) {
    // Start a new span with explicit 1/10000 sampling probability to avoid the case when user
    // sets the default sampler to always sample and we get the gRPC span of the stackdriver
    // export call always sampled and go to an infinite loop.
    io.opencensus.trace.Span span = tracer.spanBuilder(EXPORT_STACKDRIVER_TRACES).setSampler(probabilitySampler).setRecordEvents(true).startSpan();
    Scope scope = tracer.withSpan(span);
    try {
        List<Span> spans = new ArrayList<>(spanDataList.size());
        for (SpanData spanData : spanDataList) {
            spans.add(generateSpan(spanData, RESOURCE_LABELS, fixedAttributes));
        }
        // Sync call because it is already called for a batch of data, and on a separate thread.
        // TODO(bdrutu): Consider to make this async in the future.
        traceServiceClient.batchWriteSpans(projectName, spans);
    } finally {
        scope.close();
        span.end(END_SPAN_OPTIONS);
    }
}
Also used : Scope(io.opencensus.common.Scope) SpanData(io.opencensus.trace.export.SpanData) ArrayList(java.util.ArrayList) Span(com.google.devtools.cloudtrace.v2.Span)

Example 49 with Span

use of zipkin2.proto3.Span in project instrumentation-java by census-instrumentation.

the class StackdriverV2ExporterHandlerProtoTest method generateSpan.

@Test
public void generateSpan() {
    SpanData spanData = SpanData.create(spanContext, parentSpanId, /* hasRemoteParent= */
    true, SPAN_NAME, null, startTimestamp, attributes, annotations, messageEvents, links, CHILD_SPAN_COUNT, status, endTimestamp);
    TimeEvent annotationTimeEvent1 = TimeEvent.newBuilder().setAnnotation(TimeEvent.Annotation.newBuilder().setDescription(TruncatableString.newBuilder().setValue(ANNOTATION_TEXT).build()).setAttributes(Span.Attributes.newBuilder().build()).build()).setTime(com.google.protobuf.Timestamp.newBuilder().setSeconds(eventTimestamp1.getSeconds()).setNanos(eventTimestamp1.getNanos()).build()).build();
    TimeEvent annotationTimeEvent2 = TimeEvent.newBuilder().setAnnotation(TimeEvent.Annotation.newBuilder().setDescription(TruncatableString.newBuilder().setValue(ANNOTATION_TEXT).build()).setAttributes(Span.Attributes.newBuilder().build()).build()).setTime(com.google.protobuf.Timestamp.newBuilder().setSeconds(eventTimestamp3.getSeconds()).setNanos(eventTimestamp3.getNanos()).build()).build();
    TimeEvent sentTimeEvent = TimeEvent.newBuilder().setMessageEvent(TimeEvent.MessageEvent.newBuilder().setType(MessageEvent.Type.SENT).setId(sentMessageEvent.getMessageId())).setTime(com.google.protobuf.Timestamp.newBuilder().setSeconds(eventTimestamp2.getSeconds()).setNanos(eventTimestamp2.getNanos()).build()).build();
    TimeEvent recvTimeEvent = TimeEvent.newBuilder().setMessageEvent(TimeEvent.MessageEvent.newBuilder().setType(MessageEvent.Type.RECEIVED).setId(recvMessageEvent.getMessageId())).setTime(com.google.protobuf.Timestamp.newBuilder().setSeconds(eventTimestamp1.getSeconds()).setNanos(eventTimestamp1.getNanos()).build()).build();
    Span.Links spanLinks = Span.Links.newBuilder().setDroppedLinksCount(DROPPED_LINKS_COUNT).addLink(Span.Link.newBuilder().setType(Span.Link.Type.CHILD_LINKED_SPAN).setTraceId(TRACE_ID).setSpanId(SPAN_ID).setAttributes(Span.Attributes.newBuilder().build()).build()).build();
    com.google.rpc.Status spanStatus = com.google.rpc.Status.newBuilder().setCode(com.google.rpc.Code.DEADLINE_EXCEEDED.getNumber()).setMessage("TooSlow").build();
    com.google.protobuf.Timestamp startTime = com.google.protobuf.Timestamp.newBuilder().setSeconds(startTimestamp.getSeconds()).setNanos(startTimestamp.getNanos()).build();
    com.google.protobuf.Timestamp endTime = com.google.protobuf.Timestamp.newBuilder().setSeconds(endTimestamp.getSeconds()).setNanos(endTimestamp.getNanos()).build();
    Span span = handler.generateSpan(spanData, EMPTY_RESOURCE_LABELS, Collections.<String, AttributeValue>emptyMap());
    assertThat(span.getName()).isEqualTo(SD_SPAN_NAME);
    assertThat(span.getSpanId()).isEqualTo(SPAN_ID);
    assertThat(span.getParentSpanId()).isEqualTo(PARENT_SPAN_ID);
    assertThat(span.getDisplayName()).isEqualTo(TruncatableString.newBuilder().setValue(SPAN_NAME).build());
    assertThat(span.getStartTime()).isEqualTo(startTime);
    assertThat(span.getEndTime()).isEqualTo(endTime);
    assertThat(span.getAttributes().getDroppedAttributesCount()).isEqualTo(DROPPED_ATTRIBUTES_COUNT);
    // The generated attributes map contains more values (e.g. agent). We only test what we added.
    assertThat(span.getAttributes().getAttributeMapMap()).containsEntry(ATTRIBUTE_KEY_1, AttributeValue.newBuilder().setIntValue(10L).build());
    assertThat(span.getAttributes().getAttributeMapMap()).containsEntry(ATTRIBUTE_KEY_2, AttributeValue.newBuilder().setBoolValue(true).build());
    // TODO(@Hailong): add stack trace test in the future.
    assertThat(span.getStackTrace()).isEqualTo(StackTrace.newBuilder().build());
    assertThat(span.getTimeEvents().getDroppedMessageEventsCount()).isEqualTo(DROPPED_NETWORKEVENTS_COUNT);
    assertThat(span.getTimeEvents().getDroppedAnnotationsCount()).isEqualTo(DROPPED_ANNOTATIONS_COUNT);
    assertThat(span.getTimeEvents().getTimeEventList()).containsExactly(annotationTimeEvent1, annotationTimeEvent2, sentTimeEvent, recvTimeEvent);
    assertThat(span.getLinks()).isEqualTo(spanLinks);
    assertThat(span.getStatus()).isEqualTo(spanStatus);
    assertThat(span.getSameProcessAsParentSpan()).isEqualTo(com.google.protobuf.BoolValue.of(false));
    assertThat(span.getChildSpanCount()).isEqualTo(Int32Value.newBuilder().setValue(CHILD_SPAN_COUNT).build());
}
Also used : SpanData(io.opencensus.trace.export.SpanData) TimeEvent(com.google.devtools.cloudtrace.v2.Span.TimeEvent) Span(com.google.devtools.cloudtrace.v2.Span) Test(org.junit.Test)

Example 50 with Span

use of zipkin2.proto3.Span in project instrumentation-java by census-instrumentation.

the class StackdriverV2ExporterHandlerProtoTest method mapHttpAttributes.

@Test
public void mapHttpAttributes() {
    Map<String, io.opencensus.trace.AttributeValue> attributesMap = new HashMap<String, io.opencensus.trace.AttributeValue>();
    attributesMap.put("http.host", io.opencensus.trace.AttributeValue.stringAttributeValue("host"));
    attributesMap.put("http.method", io.opencensus.trace.AttributeValue.stringAttributeValue("method"));
    attributesMap.put("http.path", io.opencensus.trace.AttributeValue.stringAttributeValue("path"));
    attributesMap.put("http.route", io.opencensus.trace.AttributeValue.stringAttributeValue("route"));
    attributesMap.put("http.user_agent", io.opencensus.trace.AttributeValue.stringAttributeValue("user_agent"));
    attributesMap.put("http.status_code", io.opencensus.trace.AttributeValue.longAttributeValue(200L));
    SpanData.Attributes httpAttributes = SpanData.Attributes.create(attributesMap, 0);
    SpanData spanData = SpanData.create(spanContext, parentSpanId, /* hasRemoteParent= */
    true, SPAN_NAME, null, startTimestamp, httpAttributes, annotations, messageEvents, links, CHILD_SPAN_COUNT, status, endTimestamp);
    Span span = handler.generateSpan(spanData, EMPTY_RESOURCE_LABELS, Collections.<String, AttributeValue>emptyMap());
    Map<String, AttributeValue> attributes = span.getAttributes().getAttributeMapMap();
    assertThat(attributes).containsEntry("/http/host", toStringAttributeValueProto("host"));
    assertThat(attributes).containsEntry("/http/method", toStringAttributeValueProto("method"));
    assertThat(attributes).containsEntry("/http/path", toStringAttributeValueProto("path"));
    assertThat(attributes).containsEntry("/http/route", toStringAttributeValueProto("route"));
    assertThat(attributes).containsEntry("/http/user_agent", toStringAttributeValueProto("user_agent"));
    assertThat(attributes).containsEntry("/http/status_code", AttributeValue.newBuilder().setIntValue(200L).build());
}
Also used : AttributeValue(com.google.devtools.cloudtrace.v2.AttributeValue) SpanData(io.opencensus.trace.export.SpanData) HashMap(java.util.HashMap) TruncatableString(com.google.devtools.cloudtrace.v2.TruncatableString) Span(com.google.devtools.cloudtrace.v2.Span) Test(org.junit.Test)

Aggregations

Span (zipkin2.Span)290 Test (org.junit.Test)203 Test (org.junit.jupiter.api.Test)69 Endpoint (zipkin2.Endpoint)62 TestObjects.newClientSpan (zipkin2.TestObjects.newClientSpan)41 ArrayList (java.util.ArrayList)29 V1Span (zipkin2.v1.V1Span)23 List (java.util.List)19 Map (java.util.Map)18 Annotation (zipkin2.Annotation)13 AggregateCall (zipkin2.internal.AggregateCall)13 TraceSpan (com.google.devtools.cloudtrace.v1.TraceSpan)12 IOException (java.io.IOException)10 LinkedHashMap (java.util.LinkedHashMap)10 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)10 Arrays.asList (java.util.Arrays.asList)9 Span (com.google.devtools.cloudtrace.v2.Span)8 Trace (com.google.devtools.cloudtrace.v1.Trace)7 Call (zipkin2.Call)7 Span (zipkin2.proto3.Span)7