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;
}
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());
}
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"));
}
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"));
}
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);
}
Aggregations