Java Code Examples for brave.handler.MutableSpan#finishTimestamp()

The following examples show how to use brave.handler.MutableSpan#finishTimestamp() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MutableSpanBenchmarks.java    From zipkin-reporter-java with Apache License 2.0 6 votes vote down vote up
public static MutableSpan newBigClientSpan() {
  MutableSpan span = new MutableSpan();
  span.name("getuserinfobyaccesstoken");
  span.kind(Span.Kind.CLIENT);
  span.remoteServiceName("abasdasgad.hsadas.ism");
  span.remoteIpAndPort("219.235.216.11", 0);
  span.startTimestamp(1533706251750057L);
  span.finishTimestamp(1533706251935296L);
  span.tag("address.local", "/10.1.2.3:59618");
  span.tag("address.remote", "abasdasgad.hsadas.ism/219.235.216.11:8080");
  span.tag("http.host", "abasdasgad.hsadas.ism");
  span.tag("http.method", "POST");
  span.tag("http.path", "/thrift/shopForTalk");
  span.tag("http.status_code", "200");
  span.tag("http.url", "tbinary+h2c://abasdasgad.hsadas.ism/thrift/shopForTalk");
  span.tag("instanceId", "line-wallet-api");
  span.tag("phase", "beta");
  span.tag("siteId", "shop");
  return span;
}
 
Example 2
Source File: ConvertingSpanReporterTest.java    From zipkin-reporter-java with Apache License 2.0 6 votes vote down vote up
@Test public void remoteEndpoint() {
  MutableSpan span = new MutableSpan(context, null);

  Endpoint endpoint = Endpoint.newBuilder()
      .serviceName("fooService")
      .ip("1.2.3.4")
      .port(80)
      .build();

  span.kind(CLIENT);
  span.remoteServiceName(endpoint.serviceName());
  span.remoteIpAndPort(endpoint.ipv4(), endpoint.port());
  span.startTimestamp(1L);
  span.finishTimestamp(2L);

  spanReporter.report(span);

  assertThat(spans.get(0).remoteEndpoint())
      .isEqualTo(endpoint);
}
 
Example 3
Source File: MutableSpanBenchmarks.java    From zipkin-reporter-java with Apache License 2.0 5 votes vote down vote up
public static MutableSpan newServerSpan() {
  MutableSpan span = new MutableSpan();
  span.name("get /");
  span.kind(Span.Kind.SERVER);
  span.remoteIpAndPort("::1", 63596);
  span.startTimestamp(1533706251750057L);
  span.finishTimestamp(1533706251935296L);
  span.tag("http.method", "GET");
  span.tag("http.path", "/");
  span.tag("mvc.controller.class", "Frontend");
  span.tag("mvc.controller.method", "callBackend");
  return span;
}
 
Example 4
Source File: ConvertingSpanReporterTest.java    From zipkin-reporter-java with Apache License 2.0 5 votes vote down vote up
@Test public void minimumDurationIsOne() {
  MutableSpan span = new MutableSpan(context, null);

  span.startTimestamp(1L);
  span.finishTimestamp(1L);

  spanReporter.report(span);
  assertThat(spans.get(0).duration()).isEqualTo(1L);
}
 
Example 5
Source File: ConvertingSpanReporterTest.java    From zipkin-reporter-java with Apache License 2.0 5 votes vote down vote up
@Test public void addsAnnotations() {
  MutableSpan span = new MutableSpan(context, null);

  span.startTimestamp(1L);
  span.annotate(2L, "foo");
  span.finishTimestamp(2L);

  spanReporter.report(span);

  assertThat(spans.get(0).annotations())
      .containsOnly(Annotation.create(2L, "foo"));
}
 
Example 6
Source File: ConvertingSpanReporterTest.java    From zipkin-reporter-java with Apache License 2.0 5 votes vote down vote up
void finish(brave.Span.Kind braveKind, Span.Kind span2Kind) {
  MutableSpan span = new MutableSpan(context, null);
  span.kind(braveKind);
  span.startTimestamp(1L);
  span.finishTimestamp(2L);

  spanReporter.report(span);

  Span zipkinSpan = spans.get(0);
  assertThat(zipkinSpan.annotations()).isEmpty();
  assertThat(zipkinSpan.timestamp()).isEqualTo(1L);
  assertThat(zipkinSpan.duration()).isEqualTo(1L);
  assertThat(zipkinSpan.kind()).isEqualTo(span2Kind);
}
 
Example 7
Source File: ConvertingSpanReporterTest.java    From zipkin-reporter-java with Apache License 2.0 5 votes vote down vote up
void flush(brave.Span.Kind braveKind, Span.Kind span2Kind) {
  MutableSpan span = new MutableSpan(context, null);
  span.kind(braveKind);
  span.startTimestamp(1L);
  span.finishTimestamp(0L);

  spanReporter.report(span);

  Span zipkinSpan = spans.get(0);
  assertThat(zipkinSpan.annotations()).isEmpty();
  assertThat(zipkinSpan.timestamp()).isEqualTo(1L);
  assertThat(zipkinSpan.duration()).isNull();
  assertThat(zipkinSpan.kind()).isEqualTo(span2Kind);
}
 
Example 8
Source File: ConvertingSpanReporterTest.java    From zipkin-reporter-java with Apache License 2.0 5 votes vote down vote up
@Test public void writeTo_sharedStatus() {
  MutableSpan span = new MutableSpan(context, null);

  span.setShared();
  span.startTimestamp(1L);
  span.kind(SERVER);
  span.finishTimestamp(2L);

  spanReporter.report(span);

  assertThat(spans.get(0).shared())
      .isTrue();
}
 
Example 9
Source File: ConvertingSpanReporterTest.java    From zipkin-reporter-java with Apache License 2.0 5 votes vote down vote up
@Test public void flushUnstartedNeitherSetsTimestampNorDuration() {
  MutableSpan flushed = new MutableSpan(context, null);
  flushed.finishTimestamp(0L);

  spanReporter.report(flushed);

  assertThat(spans.get(0)).extracting(Span::timestampAsLong, Span::durationAsLong)
      .allSatisfy(u -> assertThat(u).isEqualTo(0L));
}
 
Example 10
Source File: ConvertingSpanReporter.java    From zipkin-reporter-java with Apache License 2.0 4 votes vote down vote up
static Span convert(MutableSpan span) {
  Span.Builder result = Span.newBuilder()
      .traceId(span.traceId())
      .parentId(span.parentId())
      .id(span.id())
      .name(span.name());

  long start = span.startTimestamp(), finish = span.finishTimestamp();
  result.timestamp(start);
  if (start != 0 && finish != 0L) result.duration(Math.max(finish - start, 1));

  // use ordinal comparison to defend against version skew
  Kind kind = span.kind();
  if (kind != null) {
    result.kind(BRAVE_TO_ZIPKIN_KIND.get(kind));
  }

  String localServiceName = span.localServiceName(), localIp = span.localIp();
  if (localServiceName != null || localIp != null) {
    result.localEndpoint(Endpoint.newBuilder()
        .serviceName(localServiceName)
        .ip(localIp)
        .port(span.localPort())
        .build());
  }

  String remoteServiceName = span.remoteServiceName(), remoteIp = span.remoteIp();
  if (remoteServiceName != null || remoteIp != null) {
    result.remoteEndpoint(Endpoint.newBuilder()
        .serviceName(remoteServiceName)
        .ip(remoteIp)
        .port(span.remotePort())
        .build());
  }

  span.forEachTag(Consumer.INSTANCE, result);
  span.forEachAnnotation(Consumer.INSTANCE, result);
  if (span.shared()) result.shared(true);
  if (span.debug()) result.debug(true);
  return result.build();
}
 
Example 11
Source File: BraveIntegrationTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Test
void testClientInitiatedTrace() throws Exception {
    assertThat(fooClient.hello("Lee")).isEqualTo("Hello, Ms. Lee!");

    final MutableSpan[] spans = spanHandler.take(6);
    final String traceId = spans[0].traceId();
    assertThat(spans).allMatch(s -> s.traceId().equals(traceId));

    // Find all spans.
    final MutableSpan clientFooSpan = findSpan(spans, "client/foo");
    final MutableSpan serviceFooSpan = findSpan(spans, "service/foo");
    final MutableSpan clientBarSpan = findSpan(spans, "client/bar");
    final MutableSpan serviceBarSpan = findSpan(spans, "service/bar");
    final MutableSpan clientQuxSpan = findSpan(spans, "client/qux");
    final MutableSpan serviceQuxSpan = findSpan(spans, "service/qux");

    // client/foo and service/foo should have no parents.
    assertThat(clientFooSpan.parentId()).isNull();
    assertThat(serviceFooSpan.parentId()).isNull();

    // client/foo and service/foo should have the ID values identical with their traceIds.
    assertThat(clientFooSpan.id()).isEqualTo(traceId);
    assertThat(serviceFooSpan.id()).isEqualTo(traceId);

    // The spans that do not cross the network boundary should have the same ID.
    assertThat(clientFooSpan.id()).isEqualTo(serviceFooSpan.id());
    assertThat(clientBarSpan.id()).isEqualTo(serviceBarSpan.id());
    assertThat(clientQuxSpan.id()).isEqualTo(serviceQuxSpan.id());

    // Check the parentIds.
    assertThat(clientBarSpan.parentId()).isEqualTo(clientFooSpan.id());
    assertThat(serviceBarSpan.parentId()).isEqualTo(clientFooSpan.id());
    assertThat(clientQuxSpan.parentId()).isEqualTo(clientBarSpan.id());
    assertThat(serviceQuxSpan.parentId()).isEqualTo(clientBarSpan.id());

    // Check the service names.
    assertThat(clientFooSpan.localServiceName()).isEqualTo("client/foo");
    assertThat(serviceFooSpan.localServiceName()).isEqualTo("service/foo");
    assertThat(clientBarSpan.localServiceName()).isEqualTo("client/bar");
    assertThat(serviceBarSpan.localServiceName()).isEqualTo("service/bar");
    assertThat(clientQuxSpan.localServiceName()).isEqualTo("client/qux");
    assertThat(serviceQuxSpan.localServiceName()).isEqualTo("service/qux");

    // Check RPC request can update http request.
    assertThat(clientFooSpan.tags().get("http.protocol")).isEqualTo("h2c");
    assertThat(clientFooSpan.tags().get("http.host")).startsWith("127.0.0.1");

    // Check the span names.
    assertThat(spans).allMatch(s -> "hello".equals(s.name()));

    // Check wire times
    final long clientStartTime = clientFooSpan.startTimestamp();
    final long clientWireSendTime = clientFooSpan.annotations().stream()
                                                 .filter(a -> "ws".equals(a.getValue()))
                                                 .findFirst().get().getKey();
    final long clientWireReceiveTime = clientFooSpan.annotations().stream()
                                                    .filter(a -> "wr".equals(a.getValue()))
                                                    .findFirst().get().getKey();
    final long clientEndTime = clientFooSpan.finishTimestamp();

    final long serverStartTime = serviceFooSpan.startTimestamp();
    final long serverWireSendTime = serviceFooSpan.annotations().stream()
                                                  .filter(a -> "ws".equals(a.getValue()))
                                                  .findFirst().get().getKey();
    final long serverWireReceiveTime = serviceFooSpan.annotations().stream()
                                                     .filter(a -> "wr".equals(a.getValue()))
                                                     .findFirst().get().getKey();
    final long serverEndTime = serviceFooSpan.finishTimestamp();

    // These values are taken at microsecond precision and should be reliable to compare to each other.

    // Because of the small deltas among these numbers in a unit test, a thread context switch can cause
    // client - server values to not compare correctly. We go ahead and only verify values recorded from the
    // same thread.

    assertThat(clientStartTime).isNotZero();
    assertThat(clientWireSendTime).isGreaterThanOrEqualTo(clientStartTime);
    assertThat(clientWireReceiveTime).isGreaterThanOrEqualTo(clientWireSendTime);
    assertThat(clientEndTime).isGreaterThanOrEqualTo(clientWireReceiveTime);

    // Server start time and wire receive time are essentially the same in our current model, and whether
    // one is greater than the other is mostly an implementation detail, so we don't compare them to each
    // other.

    assertThat(serverWireSendTime).isGreaterThanOrEqualTo(serverStartTime);
    assertThat(serverWireSendTime).isGreaterThanOrEqualTo(serverWireReceiveTime);
    assertThat(serverEndTime).isGreaterThanOrEqualTo(serverWireSendTime);
}