Search in sources :

Example 31 with Span

use of zipkin2.proto3.Span in project brave by openzipkin.

the class MutableSpanMapTest method reportOrphanedSpans_afterGC.

/**
 * This is the key feature. Spans orphaned via GC are reported to zipkin on the next action.
 *
 * <p>This is a customized version of https://github.com/raphw/weak-lock-free/blob/master/src/test/java/com/blogspot/mydailyjava/weaklockfree/WeakConcurrentMapTest.java
 */
@Test
public void reportOrphanedSpans_afterGC() {
    TraceContext context1 = context.toBuilder().spanId(1).build();
    map.getOrCreate(context1);
    TraceContext context2 = context.toBuilder().spanId(2).build();
    map.getOrCreate(context2);
    TraceContext context3 = context.toBuilder().spanId(3).build();
    map.getOrCreate(context3);
    TraceContext context4 = context.toBuilder().spanId(4).build();
    map.getOrCreate(context4);
    // By clearing strong references in this test, we are left with the weak ones in the map
    context1 = context2 = null;
    blockOnGC();
    // After GC, we expect that the weak references of context1 and context2 to be cleared
    assertThat(map.delegate.keySet()).extracting(o -> ((Reference) o).get()).containsExactlyInAnyOrder(null, null, context3, context4);
    map.reportOrphanedSpans();
    // After reporting, we expect no the weak references of null
    assertThat(map.delegate.keySet()).extracting(o -> ((Reference) o).get()).containsExactlyInAnyOrder(context3, context4);
    // We also expect the spans to have been reported
    assertThat(spans).flatExtracting(Span::annotations).extracting(Annotation::value).containsExactly("brave.flush", "brave.flush");
}
Also used : Tracing(brave.Tracing) PowerMockito.mockStatic(org.powermock.api.mockito.PowerMockito.mockStatic) PowerMockito.when(org.powermock.api.mockito.PowerMockito.when) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Span(zipkin2.Span) Test(org.junit.Test) TraceContext(brave.propagation.TraceContext) ArrayList(java.util.ArrayList) Reference(java.lang.ref.Reference) List(java.util.List) Annotation(zipkin2.Annotation) Platform(brave.internal.Platform) Endpoint(zipkin2.Endpoint) After(org.junit.After) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) PowerMockIgnore(org.powermock.core.classloader.annotations.PowerMockIgnore) Reference(java.lang.ref.Reference) TraceContext(brave.propagation.TraceContext) Span(zipkin2.Span) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 32 with Span

use of zipkin2.proto3.Span in project spring-cloud-gcp by spring-cloud.

the class LabelExtractor method extract.

/**
 * Extracts the Stackdriver span labels that are equivalent to the Zipkin Span
 * annotations.
 *
 * @param zipkinSpan The Zipkin Span
 * @return A map of the Stackdriver span labels equivalent to the Zipkin annotations.
 */
public Map<String, String> extract(Span zipkinSpan) {
    Map<String, String> result = new LinkedHashMap<>();
    for (Map.Entry<String, String> tag : zipkinSpan.tags().entrySet()) {
        result.put(label(tag.getKey()), tag.getValue());
    }
    // trace might not show the final destination.
    if (zipkinSpan.localEndpoint() != null && zipkinSpan.kind() == Span.Kind.SERVER) {
        if (zipkinSpan.localEndpoint().ipv4() != null) {
            result.put(label("endpoint.ipv4"), zipkinSpan.localEndpoint().ipv4());
        }
        if (zipkinSpan.localEndpoint().ipv6() != null) {
            result.put(label("endpoint.ipv6"), zipkinSpan.localEndpoint().ipv6());
        }
    }
    for (Annotation annotation : zipkinSpan.annotations()) {
        result.put(label(annotation.value()), formatTimestamp(annotation.timestamp()));
    }
    if (zipkinSpan.localEndpoint() != null && !zipkinSpan.localEndpoint().serviceName().isEmpty()) {
        result.put("/component", zipkinSpan.localEndpoint().serviceName());
    }
    if (zipkinSpan.parentId() == null) {
        String agentName = System.getProperty("stackdriver.trace.zipkin.agent", "spring-cloud-gcp-trace");
        result.put("/agent", agentName);
    }
    return result;
}
Also used : LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashMap(java.util.HashMap) Annotation(zipkin2.Annotation) LinkedHashMap(java.util.LinkedHashMap)

Example 33 with Span

use of zipkin2.proto3.Span in project spring-cloud-gcp by spring-cloud.

the class LabelExtractorTests method testRpcClientBasics.

@Test
public void testRpcClientBasics() {
    LabelExtractor extractor = new LabelExtractor();
    String instanceId = "localhost";
    long begin = 1238912378081L;
    long end = 1238912378123L;
    Span span = Span.newBuilder().traceId("123").id("9999").timestamp(begin).duration(end - begin).putTag("http.host", "localhost").putTag("custom-tag", "hello").localEndpoint(Endpoint.newBuilder().serviceName("hello-service").build()).build();
    Map<String, String> labels = extractor.extract(span);
    Assert.assertNotNull("span shouldn't be null", span);
    Assert.assertEquals("localhost", labels.get("/http/host"));
    Assert.assertEquals("spring-cloud-gcp-trace", labels.get("/agent"));
    Assert.assertEquals("hello-service", labels.get("/component"));
    Assert.assertEquals("hello", labels.get("cloud.spring.io/custom-tag"));
}
Also used : Span(zipkin2.Span) Test(org.junit.Test)

Example 34 with Span

use of zipkin2.proto3.Span in project spring-cloud-gcp by spring-cloud.

the class StackdriverTraceReporterTests method testSingleClientSpan.

@Test
public void testSingleClientSpan() {
    Span parent = Span.newBuilder().traceId("123").id("9999").name("http:call").timestamp(beginTime).duration(endTime - beginTime).kind(Span.Kind.CLIENT).build();
    this.reporter.report(parent);
    Assert.assertEquals(1, this.test.traceSpans.size());
    TraceSpan traceSpan = this.test.traceSpans.get(0);
    Assert.assertEquals("http:call", traceSpan.getName());
    // Client span chould use CS and CR time, not Span begin or end time.
    Assert.assertEquals(this.spanTranslator.createTimestamp(beginTime), traceSpan.getStartTime());
    Assert.assertEquals(this.spanTranslator.createTimestamp(endTime), traceSpan.getEndTime());
    Assert.assertEquals(TraceSpan.SpanKind.RPC_CLIENT, traceSpan.getKind());
}
Also used : TraceSpan(com.google.devtools.cloudtrace.v1.TraceSpan) Span(zipkin2.Span) TraceSpan(com.google.devtools.cloudtrace.v1.TraceSpan) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 35 with Span

use of zipkin2.proto3.Span in project spring-cloud-sleuth by spring-cloud.

the class WebClientDiscoveryExceptionTests method shouldCloseSpanUponException.

// issue #240
private void shouldCloseSpanUponException(ResponseEntityProvider provider) throws IOException, InterruptedException {
    Span span = this.tracer.nextSpan().name("new trace");
    try (Tracer.SpanInScope ws = this.tracer.withSpanInScope(span.start())) {
        provider.get(this);
        Assertions.fail("should throw an exception");
    } catch (RuntimeException e) {
    } finally {
        span.finish();
    }
    // hystrix commands should finish at this point
    Thread.sleep(200);
    List<zipkin2.Span> spans = this.reporter.getSpans();
    then(spans).hasSize(2);
    then(spans.stream().filter(span1 -> span1.kind() == zipkin2.Span.Kind.CLIENT).findFirst().get().tags()).containsKey("error");
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) EnableFeignClients(org.springframework.cloud.openfeign.EnableFeignClients) RunWith(org.junit.runner.RunWith) Span(brave.Span) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) EnableDiscoveryClient(org.springframework.cloud.client.discovery.EnableDiscoveryClient) Sampler(brave.sampler.Sampler) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) RibbonClient(org.springframework.cloud.netflix.ribbon.RibbonClient) Map(java.util.Map) ArrayListSpanReporter(org.springframework.cloud.sleuth.util.ArrayListSpanReporter) Assertions(org.assertj.core.api.Assertions) Reporter(zipkin2.reporter.Reporter) RANDOM_PORT(org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT) RestTemplate(org.springframework.web.client.RestTemplate) Before(org.junit.Before) EurekaClientAutoConfiguration(org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration) Tracing(brave.Tracing) Tracer(brave.Tracer) EnableAutoConfiguration(org.springframework.boot.autoconfigure.EnableAutoConfiguration) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) IOException(java.io.IOException) Test(org.junit.Test) TestPropertySource(org.springframework.test.context.TestPropertySource) BDDAssertions.then(org.assertj.core.api.BDDAssertions.then) Configuration(org.springframework.context.annotation.Configuration) LoadBalanced(org.springframework.cloud.client.loadbalancer.LoadBalanced) List(java.util.List) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) TraceWebServletAutoConfiguration(org.springframework.cloud.sleuth.instrument.web.TraceWebServletAutoConfiguration) ResponseEntity(org.springframework.http.ResponseEntity) Bean(org.springframework.context.annotation.Bean) FeignClient(org.springframework.cloud.openfeign.FeignClient) Tracer(brave.Tracer) Span(brave.Span)

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