Search in sources :

Example 1 with SERVER

use of zipkin2.Span.Kind.SERVER in project brave by openzipkin.

the class V2SpanConverter method toSpan.

/**
 * Converts the input, parsing {@link Span#kind()} into RPC annotations.
 */
public static zipkin.Span toSpan(Span in) {
    String traceId = in.traceId();
    zipkin.Span.Builder result = zipkin.Span.builder().traceId(HexCodec.lowerHexToUnsignedLong(traceId)).parentId(in.parentId() != null ? HexCodec.lowerHexToUnsignedLong(in.parentId()) : null).id(HexCodec.lowerHexToUnsignedLong(in.id())).debug(in.debug()).name(// avoid a NPE
    in.name() != null ? in.name() : "");
    if (traceId.length() == 32) {
        result.traceIdHigh(HexCodec.lowerHexToUnsignedLong(traceId, 0));
    }
    long startTs = in.timestamp() == null ? 0L : in.timestamp();
    Long endTs = in.duration() == null ? 0L : in.timestamp() + in.duration();
    if (startTs != 0L) {
        result.timestamp(startTs);
        result.duration(in.duration());
    }
    zipkin.Endpoint local = in.localEndpoint() != null ? toEndpoint(in.localEndpoint()) : null;
    zipkin.Endpoint remote = in.remoteEndpoint() != null ? toEndpoint(in.remoteEndpoint()) : null;
    Kind kind = in.kind();
    Annotation cs = null, sr = null, ss = null, cr = null, ms = null, mr = null, ws = null, wr = null;
    String remoteEndpointType = null;
    boolean wroteEndpoint = false;
    for (int i = 0, length = in.annotations().size(); i < length; i++) {
        zipkin2.Annotation input = in.annotations().get(i);
        Annotation a = Annotation.create(input.timestamp(), input.value(), local);
        if (a.value.length() == 2) {
            if (a.value.equals(Constants.CLIENT_SEND)) {
                kind = Kind.CLIENT;
                cs = a;
                remoteEndpointType = SERVER_ADDR;
            } else if (a.value.equals(Constants.SERVER_RECV)) {
                kind = Kind.SERVER;
                sr = a;
                remoteEndpointType = CLIENT_ADDR;
            } else if (a.value.equals(Constants.SERVER_SEND)) {
                kind = Kind.SERVER;
                ss = a;
            } else if (a.value.equals(Constants.CLIENT_RECV)) {
                kind = Kind.CLIENT;
                cr = a;
            } else if (a.value.equals(Constants.MESSAGE_SEND)) {
                kind = Kind.PRODUCER;
                ms = a;
            } else if (a.value.equals(Constants.MESSAGE_RECV)) {
                kind = Kind.CONSUMER;
                mr = a;
            } else if (a.value.equals(Constants.WIRE_SEND)) {
                ws = a;
            } else if (a.value.equals(Constants.WIRE_RECV)) {
                wr = a;
            } else {
                wroteEndpoint = true;
                result.addAnnotation(a);
            }
        } else {
            wroteEndpoint = true;
            result.addAnnotation(a);
        }
    }
    if (kind != null) {
        switch(kind) {
            case CLIENT:
                remoteEndpointType = Constants.SERVER_ADDR;
                if (startTs != 0L)
                    cs = Annotation.create(startTs, Constants.CLIENT_SEND, local);
                if (endTs != 0L)
                    cr = Annotation.create(endTs, Constants.CLIENT_RECV, local);
                break;
            case SERVER:
                remoteEndpointType = Constants.CLIENT_ADDR;
                if (startTs != 0L)
                    sr = Annotation.create(startTs, Constants.SERVER_RECV, local);
                if (endTs != 0L)
                    ss = Annotation.create(endTs, Constants.SERVER_SEND, local);
                break;
            case PRODUCER:
                remoteEndpointType = Constants.MESSAGE_ADDR;
                if (startTs != 0L)
                    ms = Annotation.create(startTs, Constants.MESSAGE_SEND, local);
                if (endTs != 0L)
                    ws = Annotation.create(endTs, Constants.WIRE_SEND, local);
                break;
            case CONSUMER:
                remoteEndpointType = Constants.MESSAGE_ADDR;
                if (startTs != 0L && endTs != 0L) {
                    wr = Annotation.create(startTs, Constants.WIRE_RECV, local);
                    mr = Annotation.create(endTs, Constants.MESSAGE_RECV, local);
                } else if (startTs != 0L) {
                    mr = Annotation.create(startTs, Constants.MESSAGE_RECV, local);
                }
                break;
            default:
                throw new AssertionError("update kind mapping");
        }
    }
    for (Map.Entry<String, String> tag : in.tags().entrySet()) {
        wroteEndpoint = true;
        result.addBinaryAnnotation(BinaryAnnotation.create(tag.getKey(), tag.getValue(), local));
    }
    if (cs != null || sr != null || ss != null || cr != null || ws != null || wr != null || ms != null || mr != null) {
        if (cs != null)
            result.addAnnotation(cs);
        if (sr != null)
            result.addAnnotation(sr);
        if (ss != null)
            result.addAnnotation(ss);
        if (cr != null)
            result.addAnnotation(cr);
        if (ws != null)
            result.addAnnotation(ws);
        if (wr != null)
            result.addAnnotation(wr);
        if (ms != null)
            result.addAnnotation(ms);
        if (mr != null)
            result.addAnnotation(mr);
        wroteEndpoint = true;
    } else if (local != null && remote != null) {
        // special-case when we are missing core annotations, but we have both address annotations
        result.addBinaryAnnotation(BinaryAnnotation.address(CLIENT_ADDR, local));
        wroteEndpoint = true;
        remoteEndpointType = SERVER_ADDR;
    }
    if (remoteEndpointType != null && remote != null) {
        result.addBinaryAnnotation(BinaryAnnotation.address(remoteEndpointType, remote));
    }
    // don't report server-side timestamp on shared or incomplete spans
    if (Boolean.TRUE.equals(in.shared()) && sr != null) {
        result.timestamp(null).duration(null);
    }
    if (local != null && !wroteEndpoint) {
        // create a dummy annotation
        result.addBinaryAnnotation(BinaryAnnotation.create(LOCAL_COMPONENT, "", local));
    }
    return result.build();
}
Also used : Span(zipkin2.Span) Annotation(zipkin.Annotation) BinaryAnnotation(zipkin.BinaryAnnotation) Endpoint(zipkin2.Endpoint) Kind(zipkin2.Span.Kind) Map(java.util.Map)

Example 2 with SERVER

use of zipkin2.Span.Kind.SERVER in project crnk-framework by crnk-project.

the class AbstractBraveModuleTest method testFindTargets.

@Test
public void testFindTargets() {
    RelationshipRepositoryV2<Project, Serializable, Task, Serializable> relRepo = client.getRepositoryForType(Project.class, Task.class);
    relRepo.findManyTargets(123L, "tasks", new QuerySpec(Task.class));
    // check client call and link span
    ArgumentCaptor<Span> clientSpanCaptor = ArgumentCaptor.forClass(Span.class);
    Mockito.verify(clientReporter, Mockito.times(1)).report(clientSpanCaptor.capture());
    List<Span> clientSpans = clientSpanCaptor.getAllValues();
    Span callSpan = clientSpans.get(0);
    Assert.assertEquals("get", callSpan.name());
    Assert.assertEquals(Span.Kind.CLIENT, callSpan.kind());
    // check server local span
    ArgumentCaptor<Span> serverSpanCaptor = ArgumentCaptor.forClass(Span.class);
    Mockito.verify(serverReporter, Mockito.times(2)).report(serverSpanCaptor.capture());
    List<Span> serverSpans = serverSpanCaptor.getAllValues();
    Span repositorySpan0 = serverSpans.get(0);
    Assert.assertEquals("crnk:get:/tasks/", repositorySpan0.name());
    Assert.assertTrue(repositorySpan0.toString().contains("\"lc\""));
    assertTag(repositorySpan0, "lc", "crnk");
    assertTag(repositorySpan0, "crnk.results", "0");
    assertTag(repositorySpan0, "crnk.status", "OK");
    Span repositorySpan1 = serverSpans.get(1);
    Assert.assertEquals("crnk:get:/projects/123/tasks/", repositorySpan1.name());
    Assert.assertTrue(repositorySpan1.toString().contains("\"lc\""));
    assertTag(repositorySpan1, "lc", "crnk");
    assertTag(repositorySpan1, "crnk.query", "?");
    assertTag(repositorySpan1, "crnk.results", "0");
    assertTag(repositorySpan1, "crnk.status", "OK");
}
Also used : Project(io.crnk.monitor.brave.mock.models.Project) Serializable(java.io.Serializable) Task(io.crnk.monitor.brave.mock.models.Task) QuerySpec(io.crnk.core.queryspec.QuerySpec) Span(zipkin2.Span) Test(org.junit.Test)

Example 3 with SERVER

use of zipkin2.Span.Kind.SERVER in project crnk-framework by crnk-project.

the class AbstractBraveModuleTest method testFindAll.

@Test
public void testFindAll() {
    Task task = new Task();
    task.setId(13L);
    task.setName("myTask");
    QuerySpec querySpec = new QuerySpec(Task.class);
    querySpec.addFilter(new FilterSpec(Arrays.asList("name"), FilterOperator.EQ, "doe"));
    taskRepo.findAll(querySpec);
    // check client call and link span
    ArgumentCaptor<Span> clientSpanCaptor = ArgumentCaptor.forClass(Span.class);
    Mockito.verify(clientReporter, Mockito.times(isOkHttp ? 1 : 1)).report(clientSpanCaptor.capture());
    List<Span> clientSpans = clientSpanCaptor.getAllValues();
    Span callSpan = clientSpans.get(0);
    Assert.assertEquals("get", callSpan.name());
    Assert.assertEquals(Span.Kind.CLIENT, callSpan.kind());
    // check server local span
    ArgumentCaptor<Span> serverSpanCaptor = ArgumentCaptor.forClass(Span.class);
    Mockito.verify(serverReporter, Mockito.times(1)).report(serverSpanCaptor.capture());
    List<Span> serverSpans = serverSpanCaptor.getAllValues();
    Span repositorySpan = serverSpans.get(0);
    Assert.assertEquals("crnk:get:/tasks/", repositorySpan.name());
    Assert.assertTrue(repositorySpan.toString().contains("\"lc\""));
    assertTag(repositorySpan, "lc", "crnk");
    assertTag(repositorySpan, "crnk.query", "?filter[tasks][name][EQ]=doe");
    assertTag(repositorySpan, "crnk.results", "0");
    assertTag(repositorySpan, "crnk.status", "OK");
}
Also used : Task(io.crnk.monitor.brave.mock.models.Task) QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec) Span(zipkin2.Span) Test(org.junit.Test)

Example 4 with SERVER

use of zipkin2.Span.Kind.SERVER in project crnk-framework by crnk-project.

the class AbstractBraveModuleTest method testCreate.

@Test
public void testCreate() {
    Task task = new Task();
    task.setId(13L);
    task.setName("myTask");
    taskRepo.create(task);
    // check client call and link span
    ArgumentCaptor<Span> clientSpanCaptor = ArgumentCaptor.forClass(Span.class);
    Mockito.verify(clientReporter, Mockito.times(1)).report(clientSpanCaptor.capture());
    List<Span> clientSpans = clientSpanCaptor.getAllValues();
    Span callSpan = clientSpans.get(0);
    Assert.assertEquals("post", callSpan.name());
    Assert.assertEquals(Span.Kind.CLIENT, callSpan.kind());
    // check server local span
    ArgumentCaptor<Span> serverSpanCaptor = ArgumentCaptor.forClass(Span.class);
    Mockito.verify(serverReporter, Mockito.times(1)).report(serverSpanCaptor.capture());
    List<Span> serverSpans = serverSpanCaptor.getAllValues();
    Span repositorySpan = serverSpans.get(0);
    Assert.assertEquals("crnk:post:/tasks/13/", repositorySpan.name());
    Assert.assertTrue(repositorySpan.toString().contains("\"lc\""));
    assertTag(repositorySpan, "lc", "crnk");
    assertTag(repositorySpan, "crnk.query", "?");
}
Also used : Task(io.crnk.monitor.brave.mock.models.Task) Span(zipkin2.Span) Test(org.junit.Test)

Example 5 with SERVER

use of zipkin2.Span.Kind.SERVER in project crnk-framework by crnk-project.

the class AbstractBraveModuleTest method testError.

@Test
public void testError() {
    Task task = new Task();
    task.setId(13L);
    try {
        taskRepo.create(task);
    } catch (Exception e) {
    // ok
    }
    // check client call and link span
    ArgumentCaptor<Span> clientSpanCaptor = ArgumentCaptor.forClass(Span.class);
    Mockito.verify(clientReporter, Mockito.times(1)).report(clientSpanCaptor.capture());
    List<Span> clientSpans = clientSpanCaptor.getAllValues();
    Span callSpan = clientSpans.get(0);
    Assert.assertEquals("post", callSpan.name());
    Assert.assertEquals(Span.Kind.CLIENT, callSpan.kind());
    assertTag(callSpan, "http.status_code", "500");
    // check server local span
    ArgumentCaptor<Span> serverSpanCaptor = ArgumentCaptor.forClass(Span.class);
    Mockito.verify(serverReporter, Mockito.times(1)).report(serverSpanCaptor.capture());
    List<Span> serverSpans = serverSpanCaptor.getAllValues();
    Span repositorySpan = serverSpans.get(0);
    Assert.assertEquals("crnk:post:/tasks/13/", repositorySpan.name());
    Assert.assertTrue(repositorySpan.toString().contains("\"lc\""));
    assertTag(repositorySpan, "lc", "crnk");
    assertTag(repositorySpan, "crnk.query", "?");
    assertTag(repositorySpan, "crnk.status", "EXCEPTION");
}
Also used : Task(io.crnk.monitor.brave.mock.models.Task) Span(zipkin2.Span) Test(org.junit.Test)

Aggregations

Span (zipkin2.Span)36 Test (org.junit.Test)31 Endpoint (zipkin2.Endpoint)17 Test (org.junit.jupiter.api.Test)9 TraceSpan (com.google.devtools.cloudtrace.v1.TraceSpan)7 Trace (com.google.devtools.cloudtrace.v1.Trace)5 NoopHealthCheckManager (com.wavefront.agent.channel.NoopHealthCheckManager)5 SpanSampler (com.wavefront.agent.sampler.SpanSampler)5 ByteBuf (io.netty.buffer.ByteBuf)5 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)5 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)5 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)5 Span (wavefront.report.Span)5 Task (io.crnk.monitor.brave.mock.models.Task)4 TestObjects.newClientSpan (zipkin2.TestObjects.newClientSpan)4 SpanBytesEncoder (zipkin2.codec.SpanBytesEncoder)4 RateSampler (com.wavefront.sdk.entities.tracing.sampling.RateSampler)3 Annotation (wavefront.report.Annotation)3 ElasticsearchStorage (zipkin2.elasticsearch.ElasticsearchStorage)3 V1Span (zipkin2.v1.V1Span)3