Search in sources :

Example 31 with Span

use of io.opencensus.proto.trace.v1.Span in project zipkin-gcp by openzipkin.

the class TraceTranslator method translateSpans.

/**
 * Convert a Collection of Zipkin Spans into a Collection of Stackdriver Trace Spans.
 *
 * @param projectId The Google Cloud Platform projectId that should be used for Stackdriver Trace
 * Traces.
 * @param zipkinSpans The Collection of Zipkin Spans.
 * @return A Collection of Stackdriver Trace Spans.
 */
public static List<Trace> translateSpans(String projectId, Collection<Span> zipkinSpans) {
    List<Span> sortedByTraceAndSpanId = sortByTraceAndSpanId(zipkinSpans);
    Trace.Builder currentTrace = null;
    List<Trace> result = new ArrayList<>();
    for (int i = 0, length = sortedByTraceAndSpanId.size(); i < length; i++) {
        Span currentSpan = sortedByTraceAndSpanId.get(i);
        // Zipkin trace ID is conditionally 16 or 32 characters, but Stackdriver needs 32
        String traceId = currentSpan.traceId();
        if (traceId.length() == 16)
            traceId = "0000000000000000" + traceId;
        if (currentTrace == null || !traceId.equals(currentTrace.getTraceId())) {
            finishTrace(currentTrace, result);
            currentTrace = Trace.newBuilder();
            currentTrace.setProjectId(projectId);
            currentTrace.setTraceId(traceId);
        }
        appendSpan(currentTrace, currentSpan);
    }
    finishTrace(currentTrace, result);
    return result;
}
Also used : Trace(com.google.devtools.cloudtrace.v1.Trace) ArrayList(java.util.ArrayList) TraceSpan(com.google.devtools.cloudtrace.v1.TraceSpan) Span(zipkin2.Span)

Example 32 with Span

use of io.opencensus.proto.trace.v1.Span in project zipkin-gcp by openzipkin.

the class SpanTranslatorTest method translate_clientSpan.

/**
 * This test is intentionally sensitive, so changing other parts makes obvious impact here
 */
@Test
public void translate_clientSpan() {
    Span zipkinSpan = Span.newBuilder().traceId("7180c278b62e8f6a216a2aea45d08fc9").parentId("6b221d5bc9e6496c").id("5b4185666d50f68b").name("get").kind(Span.Kind.CLIENT).localEndpoint(Endpoint.newBuilder().serviceName("frontend").build()).remoteEndpoint(Endpoint.newBuilder().serviceName("backend").ip("192.168.99.101").port(9000).build()).timestamp(// 1 second after epoch
    1_000_000L).duration(123_456L).addAnnotation(1_123_000L, "foo").putTag("http.path", "/api").putTag("clnt/finagle.version", "6.45.0").build();
    TraceSpan translated = SpanTranslator.translate(TraceSpan.newBuilder(), zipkinSpan).build();
    assertThat(translated).isEqualTo(TraceSpan.newBuilder().setSpanId(Long.parseUnsignedLong(zipkinSpan.id(), 16) ^ 0x3f6a2ec3c810c2abL).setParentSpanId(Long.parseUnsignedLong(zipkinSpan.parentId(), 16)).setKind(TraceSpan.SpanKind.RPC_CLIENT).setName("get").setStartTime(Timestamp.newBuilder().setSeconds(1).build()).setEndTime(Timestamp.newBuilder().setSeconds(1).setNanos(123_456_000).build()).putLabels("zipkin.io/clnt/finagle.version", "6.45.0").putLabels("zipkin.io/http.path", "/api").putLabels("/component", "frontend").putLabels("zipkin.io/foo", "1970-01-01 (00:00:01.123)").build());
}
Also used : TraceSpan(com.google.devtools.cloudtrace.v1.TraceSpan) TraceSpan(com.google.devtools.cloudtrace.v1.TraceSpan) Span(zipkin2.Span) Test(org.junit.Test)

Example 33 with Span

use of io.opencensus.proto.trace.v1.Span in project zipkin-gcp by openzipkin.

the class LabelExtractorTest method testPrefixIsApplied.

@Test
public void testPrefixIsApplied() {
    String prefix = "test.prefix";
    LabelExtractor extractor = new LabelExtractor(Collections.<String, String>emptyMap(), prefix);
    Span zipkinSpan = Span.newBuilder().traceId("4").name("test-span").id("5").addAnnotation(1, "annotation.key.1").putTag("binary.annotation.key.1", "value").build();
    Map<String, String> labels = extractor.extract(zipkinSpan);
    assertTrue(labels.containsKey(prefix + "annotation.key.1"));
    assertTrue(labels.containsKey(prefix + "binary.annotation.key.1"));
}
Also used : Span(zipkin2.Span) Test(org.junit.Test)

Example 34 with Span

use of io.opencensus.proto.trace.v1.Span in project zipkin-gcp by openzipkin.

the class LabelExtractorTest method testComponentLabelIsSet.

@Test
public void testComponentLabelIsSet() {
    LabelExtractor extractor = new LabelExtractor(Collections.<String, String>emptyMap(), "test.prefix");
    Span clientSpan = Span.newBuilder().traceId("4").name("test-span").id("5").localEndpoint(Endpoint.newBuilder().serviceName("service1").build()).build();
    Span serverSpan = Span.newBuilder().traceId("4").name("child-span").id("6").localEndpoint(Endpoint.newBuilder().serviceName("service2").build()).parentId("5").build();
    Map<String, String> clientLabels = extractor.extract(clientSpan);
    assertEquals("service1", clientLabels.get("/component"));
    Map<String, String> serverLabels = extractor.extract(serverSpan);
    assertEquals("service2", serverLabels.get("/component"));
}
Also used : Span(zipkin2.Span) Test(org.junit.Test)

Example 35 with Span

use of io.opencensus.proto.trace.v1.Span in project zipkin-gcp by openzipkin.

the class TraceTranslatorTest method testMultihostServerRootSpan.

@Test
public void testMultihostServerRootSpan() {
    Span span1 = Span.newBuilder().traceId("1").id("1").name("/a").timestamp(// This is set because the server owns the span
    1474488796000000L).duration(5000000L).build();
    Span span2 = Span.newBuilder().kind(Kind.CLIENT).traceId("1").parentId("1").id("2").name("/b?client").timestamp(// This is set because the client owns the span.
    1474488797000000L).duration(1500000L).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").timestamp(1474488797600000L).duration(200000L).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());
    assertThat(spansByName.get("/a").getParentSpanId()).isEqualTo(0);
}
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)

Aggregations

Span (zipkin2.Span)290 Test (org.junit.Test)192 Test (org.junit.jupiter.api.Test)67 TestObjects.newClientSpan (zipkin2.TestObjects.newClientSpan)41 Endpoint (zipkin2.Endpoint)37 ArrayList (java.util.ArrayList)24 V1Span (zipkin2.v1.V1Span)17 List (java.util.List)14 AggregateCall (zipkin2.internal.AggregateCall)13 TraceSpan (com.google.devtools.cloudtrace.v1.TraceSpan)12 Arrays.asList (java.util.Arrays.asList)9 Map (java.util.Map)9 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)9 Span (com.google.devtools.cloudtrace.v2.Span)8 LinkedHashMap (java.util.LinkedHashMap)8 Trace (com.google.devtools.cloudtrace.v1.Trace)7 Span (zipkin2.proto3.Span)7 SpanData (io.opencensus.trace.export.SpanData)6 SpanCustomizer (brave.SpanCustomizer)5 IOException (java.io.IOException)5