io.opencensus.trace.Link Java Examples

The following examples show how to use io.opencensus.trace.Link. 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: JaegerExporterHandlerTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
private static List<Link> sampleLinks() {
  return Lists.newArrayList(
      Link.fromSpanContext(
          SpanContext.create(
              TraceId.fromBytes(
                  new byte[] {FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, 0}),
              SpanId.fromBytes(new byte[] {0, 0, 0, 0, 0, 0, 2, 0}),
              TraceOptions.builder().setIsSampled(false).build(),
              Tracestate.builder().build()),
          Link.Type.CHILD_LINKED_SPAN,
          ImmutableMap.of(
              "Bool", AttributeValue.booleanAttributeValue(true),
              "Long", AttributeValue.longAttributeValue(299792458L),
              "String",
                  AttributeValue.stringAttributeValue(
                      "Man is condemned to be free; because once thrown into the world, "
                          + "he is responsible for everything he does. -- Sartre"))));
}
 
Example #2
Source File: InstanaExporterHandlerTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void generateSpan_NullStatus() {
  SpanData data =
      SpanData.create(
          SpanContext.create(
              TraceId.fromLowerBase16(TRACE_ID),
              SpanId.fromLowerBase16(SPAN_ID),
              TraceOptions.builder().setIsSampled(true).build()),
          SpanId.fromLowerBase16(PARENT_SPAN_ID),
          true, /* hasRemoteParent */
          "SpanName", /* name */
          null, /* kind */
          Timestamp.create(1505855794, 194009601) /* startTimestamp */,
          Attributes.create(attributes, 0 /* droppedAttributesCount */),
          TimedEvents.create(annotations, 0 /* droppedEventsCount */),
          TimedEvents.create(messageEvents, 0 /* droppedEventsCount */),
          Links.create(Collections.<Link>emptyList(), 0 /* droppedLinksCount */),
          null, /* childSpanCount */
          null, /* status */
          Timestamp.create(1505855799, 465726528) /* endTimestamp */);

  assertThat(InstanaExporterHandler.convertToJson(Collections.singletonList(data)))
      .isEqualTo("[]");
}
 
Example #3
Source File: SpanDataTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
  attributesMap.put("MyAttributeKey1", AttributeValue.longAttributeValue(10));
  attributesMap.put("MyAttributeKey2", AttributeValue.booleanAttributeValue(true));
  attributes = Attributes.create(attributesMap, 1);
  annotationsList.add(SpanData.TimedEvent.create(eventTimestamp1, annotation));
  annotationsList.add(SpanData.TimedEvent.create(eventTimestamp3, annotation));
  annotations = TimedEvents.create(annotationsList, 2);
  networkEventsList.add(SpanData.TimedEvent.create(eventTimestamp1, recvNetworkEvent));
  networkEventsList.add(SpanData.TimedEvent.create(eventTimestamp2, sentNetworkEvent));
  networkEvents = TimedEvents.create(networkEventsList, 3);
  messageEventsList.add(SpanData.TimedEvent.create(eventTimestamp1, recvMessageEvent));
  messageEventsList.add(SpanData.TimedEvent.create(eventTimestamp2, sentMessageEvent));
  messageEvents = TimedEvents.create(messageEventsList, 3);
  linksList.add(Link.fromSpanContext(spanContext, Type.CHILD_LINKED_SPAN));
  links = Links.create(linksList, 0);
}
 
Example #4
Source File: JsonConversionUtilsTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
  SpanData spanData =
      SpanData.create(
          SpanContext.create(
              TraceId.fromLowerBase16(SAMPLE_TRACE_ID),
              SpanId.fromLowerBase16(SAMPLE_SPAN_ID),
              SAMPLE_TRACE_OPTION,
              SAMPLE_TRACE_STATE),
          SpanId.fromLowerBase16(SAMPLE_PARENT_SPAN_ID),
          true,
          "SpanName",
          null,
          Timestamp.create(155196336, 194009601),
          Attributes.create(attributes, 0),
          TimedEvents.create(annotations, 0),
          TimedEvents.create(messageEvents, 0),
          Links.create(Collections.<Link>emptyList(), 0),
          null,
          Status.OK,
          Timestamp.create(155296336, 465726528));

  spanDataList = new ArrayList<SpanData>();
  spanDataList.add(spanData);
}
 
Example #5
Source File: SpanOperationsBenchmark.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
private void initAttributes() {
  attributeValues = createAttributeValues(size);
  attributeKeys = new String[size];
  attributeMap = new HashMap<>(size);
  messageEvents = new MessageEvent[size];
  links = new Link[size];
  for (int i = 0; i < size; i++) {
    attributeKeys[i] = ATTRIBUTE_KEY + "-i";
    attributeMap.put(attributeKeys[i], attributeValues[i]);
    messageEvents[i] = MessageEvent.builder(MessageEvent.Type.SENT, MESSAGE_ID + i).build();
    links[i] =
        Link.fromSpanContext(
            SpanContext.create(
                TraceId.fromBytes(
                    new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, (byte) i}),
                SpanId.fromBytes(new byte[] {1, 2, 3, 4, 5, 6, 7, (byte) i}),
                TraceOptions.DEFAULT,
                TRACESTATE_DEFAULT),
            Link.Type.PARENT_LINKED_SPAN);
  }
}
 
Example #6
Source File: JaegerExporterHandler.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
private List<SpanRef> linksToReferences(final List<Link> links) {
  final List<SpanRef> spanRefs = Lists.newArrayListWithExpectedSize(links.size());
  for (final Link link : links) {
    copyToBuffer(link.getTraceId());
    spanRefs.add(
        new SpanRef(
            linkTypeToRefType(link.getType()),
            traceIdLow(),
            traceIdHigh(),
            spanIdToLong(link.getSpanId())));
  }
  return spanRefs;
}
 
Example #7
Source File: RecordEventsSpanImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void droppingLinks() {
  final int maxNumberOfLinks = 8;
  TraceParams traceParams =
      TraceParams.DEFAULT.toBuilder().setMaxNumberOfLinks(maxNumberOfLinks).build();
  RecordEventsSpanImpl span =
      RecordEventsSpanImpl.startSpan(
          spanContext,
          SPAN_NAME,
          null,
          parentSpanId,
          false,
          traceParams,
          startEndHandler,
          timestampConverter,
          testClock);
  Link link = Link.fromSpanContext(spanContext, Link.Type.CHILD_LINKED_SPAN);
  for (int i = 0; i < 2 * maxNumberOfLinks; i++) {
    span.addLink(link);
  }
  SpanData spanData = span.toSpanData();
  assertThat(spanData.getLinks().getDroppedLinksCount()).isEqualTo(maxNumberOfLinks);
  assertThat(spanData.getLinks().getLinks().size()).isEqualTo(maxNumberOfLinks);
  for (int i = 0; i < maxNumberOfLinks; i++) {
    assertThat(spanData.getLinks().getLinks().get(i)).isEqualTo(link);
  }
  span.end();
  spanData = span.toSpanData();
  assertThat(spanData.getLinks().getDroppedLinksCount()).isEqualTo(maxNumberOfLinks);
  assertThat(spanData.getLinks().getLinks().size()).isEqualTo(maxNumberOfLinks);
  for (int i = 0; i < maxNumberOfLinks; i++) {
    assertThat(spanData.getLinks().getLinks().get(i)).isEqualTo(link);
  }
}
 
Example #8
Source File: NoRecordEventsSpanImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void doNotCrash() {
  Map<String, AttributeValue> attributes = new HashMap<String, AttributeValue>();
  attributes.put(
      "MyStringAttributeKey", AttributeValue.stringAttributeValue("MyStringAttributeValue"));
  Map<String, AttributeValue> multipleAttributes = new HashMap<String, AttributeValue>();
  multipleAttributes.put(
      "MyStringAttributeKey", AttributeValue.stringAttributeValue("MyStringAttributeValue"));
  multipleAttributes.put("MyBooleanAttributeKey", AttributeValue.booleanAttributeValue(true));
  multipleAttributes.put("MyLongAttributeKey", AttributeValue.longAttributeValue(123));
  // Tests only that all the methods are not crashing/throwing errors.
  noRecordEventsSpan.putAttribute(
      "MyStringAttributeKey2", AttributeValue.stringAttributeValue("MyStringAttributeValue2"));
  noRecordEventsSpan.addAttributes(attributes);
  noRecordEventsSpan.addAttributes(multipleAttributes);
  noRecordEventsSpan.addAnnotation("MyAnnotation");
  noRecordEventsSpan.addAnnotation("MyAnnotation", attributes);
  noRecordEventsSpan.addAnnotation("MyAnnotation", multipleAttributes);
  noRecordEventsSpan.addAnnotation(Annotation.fromDescription("MyAnnotation"));
  noRecordEventsSpan.addNetworkEvent(NetworkEvent.builder(NetworkEvent.Type.SENT, 1L).build());
  noRecordEventsSpan.addMessageEvent(MessageEvent.builder(MessageEvent.Type.SENT, 1L).build());
  noRecordEventsSpan.addLink(
      Link.fromSpanContext(SpanContext.INVALID, Link.Type.CHILD_LINKED_SPAN));
  noRecordEventsSpan.setStatus(Status.OK);
  noRecordEventsSpan.end(EndSpanOptions.DEFAULT);
  noRecordEventsSpan.end();
}
 
Example #9
Source File: HttpServerHandler.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
/**
 * Instrument an incoming request before it is handled.
 *
 * <p>This method will create a span under the deserialized propagated parent context. If the
 * parent context is not present, the span will be created under the current context.
 *
 * <p>The generated span will NOT be set as current context. User can control when to enter the
 * scope of this span. Use {@link AbstractHttpHandler#getSpanFromContext} to retrieve the span.
 *
 * @param carrier the entity that holds the HTTP information.
 * @param request the request entity.
 * @return the {@link HttpRequestContext} that contains stats and trace data associated with the
 *     request.
 * @since 0.19
 */
public HttpRequestContext handleStart(C carrier, Q request) {
  checkNotNull(carrier, "carrier");
  checkNotNull(request, "request");
  SpanBuilder spanBuilder = null;
  String spanName = getSpanName(request, extractor);
  // de-serialize the context
  SpanContext spanContext = null;
  try {
    spanContext = textFormat.extract(carrier, getter);
  } catch (SpanContextParseException e) {
    // TODO: Currently we cannot distinguish between context parse error and missing context.
    // Logging would be annoying so we just ignore this error and do not even log a message.
  }
  if (spanContext == null || publicEndpoint) {
    spanBuilder = tracer.spanBuilder(spanName);
  } else {
    spanBuilder = tracer.spanBuilderWithRemoteParent(spanName, spanContext);
  }

  Span span = spanBuilder.setSpanKind(Kind.SERVER).startSpan();
  if (publicEndpoint && spanContext != null) {
    span.addLink(Link.fromSpanContext(spanContext, Type.PARENT_LINKED_SPAN));
  }

  if (span.getOptions().contains(Options.RECORD_EVENTS)) {
    addSpanRequestAttributes(span, request, extractor);
  }

  return getNewContext(span, tagger.getCurrentTagContext());
}
 
Example #10
Source File: HttpServerHandlerTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void handleStartWithPublicEndpointShouldAddLink() throws Exception {
  handlerForPublicEndpoint.handleStart(carrier, request);
  verify(tracer).spanBuilderWithExplicitParent(any(String.class), any(Span.class));
  verify(spanWithLocalParent).addLink(captor.capture());

  Link link = captor.getValue();
  assertThat(link.getSpanId()).isEqualTo(spanContextRemote.getSpanId());
  assertThat(link.getTraceId()).isEqualTo(spanContextRemote.getTraceId());
  assertThat(link.getType()).isEqualTo(Type.PARENT_LINKED_SPAN);
}
 
Example #11
Source File: JaegerExporterHandlerTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void convertErrorSpanDataToJaegerThriftSpan() throws SenderException {
  long startTime = 1519629870001L;
  long endTime = 1519630148002L;
  String statusMessage = "timeout";
  SpanData spanData =
      SpanData.create(
          sampleSpanContext(),
          SpanId.fromBytes(new byte[] {(byte) 0x7F, FF, FF, FF, FF, FF, FF, FF}),
          true,
          "test",
          Kind.SERVER,
          Timestamp.fromMillis(startTime),
          SpanData.Attributes.create(Collections.<String, AttributeValue>emptyMap(), 0),
          SpanData.TimedEvents.create(Collections.<TimedEvent<Annotation>>emptyList(), 0),
          SpanData.TimedEvents.create(Collections.<TimedEvent<MessageEvent>>emptyList(), 0),
          SpanData.Links.create(Collections.<Link>emptyList(), 0),
          0,
          Status.DEADLINE_EXCEEDED.withDescription(statusMessage),
          Timestamp.fromMillis(endTime));

  handler.export(singletonList(spanData));

  verify(mockSender).send(eq(process), captor.capture());
  List<Span> spans = captor.getValue();

  assertThat(spans.size()).isEqualTo(1);
  Span span = spans.get(0);

  assertThat(span.tags.size()).isEqualTo(3);
  assertThat(span.tags)
      .containsExactly(
          new Tag(JaegerExporterHandler.SPAN_KIND, TagType.STRING).setVStr("server"),
          new Tag(JaegerExporterHandler.STATUS_CODE, TagType.LONG).setVLong(4),
          new Tag(JaegerExporterHandler.STATUS_MESSAGE, TagType.STRING).setVStr(statusMessage));
}
 
Example #12
Source File: JaegerExporterHandler.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
private static SpanRefType linkTypeToRefType(final Link.Type type) {
  switch (type) {
    case CHILD_LINKED_SPAN:
      return SpanRefType.CHILD_OF;
    case PARENT_LINKED_SPAN:
      return SpanRefType.FOLLOWS_FROM;
  }
  throw new UnsupportedOperationException(
      format("Failed to convert link type [%s] to a Jaeger SpanRefType.", type));
}
 
Example #13
Source File: ZipkinExporterHandlerTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void generateSpan_NoKindAndRemoteParent() {
  SpanData data =
      SpanData.create(
          SpanContext.create(
              TraceId.fromLowerBase16(TRACE_ID),
              SpanId.fromLowerBase16(SPAN_ID),
              TraceOptions.builder().setIsSampled(true).build()),
          // TODO SpanId.fromLowerBase16
          SpanId.fromLowerBase16(PARENT_SPAN_ID),
          true, /* hasRemoteParent */
          "Recv.helloworld.Greeter.SayHello", /* name */
          null, /* kind */
          Timestamp.create(1505855794, 194009601) /* startTimestamp */,
          Attributes.create(attributes, 0 /* droppedAttributesCount */),
          TimedEvents.create(annotations, 0 /* droppedEventsCount */),
          TimedEvents.create(messageEvents, 0 /* droppedEventsCount */),
          Links.create(Collections.<Link>emptyList(), 0 /* droppedLinksCount */),
          null, /* childSpanCount */
          Status.OK,
          Timestamp.create(1505855799, 465726528) /* endTimestamp */);

  assertThat(ZipkinExporterHandler.generateSpan(data, localEndpoint))
      .isEqualTo(
          Span.newBuilder()
              .traceId(TRACE_ID)
              .parentId(PARENT_SPAN_ID)
              .id(SPAN_ID)
              .kind(Span.Kind.SERVER)
              .name(data.getName())
              .timestamp(1505855794000000L + 194009601L / 1000)
              .duration(
                  (1505855799000000L + 465726528L / 1000)
                      - (1505855794000000L + 194009601L / 1000))
              .localEndpoint(localEndpoint)
              .addAnnotation(1505855799000000L + 433901068L / 1000, "RECEIVED")
              .addAnnotation(1505855799000000L + 459486280L / 1000, "SENT")
              .putTag(ZipkinExporterHandler.STATUS_CODE, "OK")
              .build());
}
 
Example #14
Source File: InstanaExporterHandlerTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void generateSpan_NoKindAndRemoteParent() {
  SpanData data =
      SpanData.create(
          SpanContext.create(
              TraceId.fromLowerBase16(TRACE_ID),
              SpanId.fromLowerBase16(SPAN_ID),
              TraceOptions.builder().setIsSampled(true).build()),
          SpanId.fromLowerBase16(PARENT_SPAN_ID),
          true, /* hasRemoteParent */
          "SpanName", /* name */
          null, /* kind */
          Timestamp.create(1505855794, 194009601) /* startTimestamp */,
          Attributes.create(attributes, 0 /* droppedAttributesCount */),
          TimedEvents.create(annotations, 0 /* droppedEventsCount */),
          TimedEvents.create(messageEvents, 0 /* droppedEventsCount */),
          Links.create(Collections.<Link>emptyList(), 0 /* droppedLinksCount */),
          null, /* childSpanCount */
          Status.OK,
          Timestamp.create(1505855799, 465726528) /* endTimestamp */);

  assertThat(InstanaExporterHandler.convertToJson(Collections.singletonList(data)))
      .isEqualTo(
          "["
              + "{"
              + "\"spanId\":\"9cc1e3049173be09\","
              + "\"traceId\":\"d239036e7d5cec11\","
              + "\"parentId\":\"8b03ab423da481c5\","
              + "\"timestamp\":1505855794194,"
              + "\"duration\":5271,"
              + "\"name\":\"SpanName\","
              + "\"type\":\"ENTRY\","
              + "\"data\":"
              + "{\"http.url\":\"http://localhost/foo\"}"
              + "}"
              + "]");
}
 
Example #15
Source File: InstanaExporterHandlerTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void generateSpan_ServerKind() {
  SpanData data =
      SpanData.create(
          SpanContext.create(
              TraceId.fromLowerBase16(TRACE_ID),
              SpanId.fromLowerBase16(SPAN_ID),
              TraceOptions.builder().setIsSampled(true).build()),
          SpanId.fromLowerBase16(PARENT_SPAN_ID),
          true, /* hasRemoteParent */
          "SpanName", /* name */
          Kind.SERVER, /* kind */
          Timestamp.create(1505855794, 194009601) /* startTimestamp */,
          Attributes.create(attributes, 0 /* droppedAttributesCount */),
          TimedEvents.create(annotations, 0 /* droppedEventsCount */),
          TimedEvents.create(messageEvents, 0 /* droppedEventsCount */),
          Links.create(Collections.<Link>emptyList(), 0 /* droppedLinksCount */),
          null, /* childSpanCount */
          Status.OK,
          Timestamp.create(1505855799, 465726528) /* endTimestamp */);

  assertThat(InstanaExporterHandler.convertToJson(Collections.singletonList(data)))
      .isEqualTo(
          "["
              + "{"
              + "\"spanId\":\"9cc1e3049173be09\","
              + "\"traceId\":\"d239036e7d5cec11\","
              + "\"parentId\":\"8b03ab423da481c5\","
              + "\"timestamp\":1505855794194,"
              + "\"duration\":5271,"
              + "\"name\":\"SpanName\","
              + "\"type\":\"ENTRY\","
              + "\"data\":"
              + "{\"http.url\":\"http://localhost/foo\"}"
              + "}"
              + "]");
}
 
Example #16
Source File: InstanaExporterHandlerTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void generateSpan_ClientKind() {
  SpanData data =
      SpanData.create(
          SpanContext.create(
              TraceId.fromLowerBase16(TRACE_ID),
              SpanId.fromLowerBase16(SPAN_ID),
              TraceOptions.builder().setIsSampled(true).build()),
          SpanId.fromLowerBase16(PARENT_SPAN_ID),
          true, /* hasRemoteParent */
          "SpanName", /* name */
          Kind.CLIENT, /* kind */
          Timestamp.create(1505855794, 194009601) /* startTimestamp */,
          Attributes.create(attributes, 0 /* droppedAttributesCount */),
          TimedEvents.create(annotations, 0 /* droppedEventsCount */),
          TimedEvents.create(messageEvents, 0 /* droppedEventsCount */),
          Links.create(Collections.<Link>emptyList(), 0 /* droppedLinksCount */),
          null, /* childSpanCount */
          Status.OK,
          Timestamp.create(1505855799, 465726528) /* endTimestamp */);

  assertThat(InstanaExporterHandler.convertToJson(Collections.singletonList(data)))
      .isEqualTo(
          "["
              + "{"
              + "\"spanId\":\"9cc1e3049173be09\","
              + "\"traceId\":\"d239036e7d5cec11\","
              + "\"parentId\":\"8b03ab423da481c5\","
              + "\"timestamp\":1505855794194,"
              + "\"duration\":5271,"
              + "\"name\":\"SpanName\","
              + "\"type\":\"EXIT\","
              + "\"data\":"
              + "{\"http.url\":\"http://localhost/foo\"}"
              + "}"
              + "]");
}
 
Example #17
Source File: InstanaExporterHandlerTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void generateSpan_ErrorStatus() {
  SpanData data =
      SpanData.create(
          SpanContext.create(
              TraceId.fromLowerBase16(TRACE_ID),
              SpanId.fromLowerBase16(SPAN_ID),
              TraceOptions.builder().setIsSampled(true).build()),
          SpanId.fromLowerBase16(PARENT_SPAN_ID),
          true, /* hasRemoteParent */
          "SpanName", /* name */
          Kind.CLIENT, /* kind */
          Timestamp.create(1505855794, 194009601) /* startTimestamp */,
          Attributes.create(attributes, 0 /* droppedAttributesCount */),
          TimedEvents.create(annotations, 0 /* droppedEventsCount */),
          TimedEvents.create(messageEvents, 0 /* droppedEventsCount */),
          Links.create(Collections.<Link>emptyList(), 0 /* droppedLinksCount */),
          null, /* childSpanCount */
          Status.OUT_OF_RANGE, /* status, any but OK */
          Timestamp.create(1505855799, 465726528) /* endTimestamp */);

  assertThat(InstanaExporterHandler.convertToJson(Collections.singletonList(data)))
      .isEqualTo(
          "["
              + "{"
              + "\"spanId\":\"9cc1e3049173be09\","
              + "\"traceId\":\"d239036e7d5cec11\","
              + "\"parentId\":\"8b03ab423da481c5\","
              + "\"timestamp\":1505855794194,"
              + "\"duration\":5271,"
              + "\"name\":\"SpanName\","
              + "\"type\":\"EXIT\","
              + "\"error\":true,"
              + "\"data\":"
              + "{\"http.url\":\"http://localhost/foo\"}"
              + "}"
              + "]");
}
 
Example #18
Source File: RecordEventsSpanImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void noEventsRecordedAfterEnd() {
  RecordEventsSpanImpl span =
      RecordEventsSpanImpl.startSpan(
          spanContext,
          SPAN_NAME,
          null,
          parentSpanId,
          false,
          TraceParams.DEFAULT,
          startEndHandler,
          timestampConverter,
          testClock);
  span.end();
  // Check that adding trace events after Span#end() does not throw any exception and are not
  // recorded.
  span.putAttributes(attributes);
  span.putAttribute(
      "MySingleStringAttributeKey",
      AttributeValue.stringAttributeValue("MySingleStringAttributeValue"));
  span.addAnnotation(Annotation.fromDescription(ANNOTATION_DESCRIPTION));
  span.addAnnotation(ANNOTATION_DESCRIPTION, attributes);
  span.addNetworkEvent(
      NetworkEvent.builder(NetworkEvent.Type.RECV, 1).setUncompressedMessageSize(3).build());
  span.addLink(Link.fromSpanContext(spanContext, Link.Type.CHILD_LINKED_SPAN));
  SpanData spanData = span.toSpanData();
  assertThat(spanData.getStartTimestamp()).isEqualTo(timestamp);
  assertThat(spanData.getAttributes().getAttributeMap()).isEmpty();
  assertThat(spanData.getAnnotations().getEvents()).isEmpty();
  assertThat(spanData.getNetworkEvents().getEvents()).isEmpty();
  assertThat(spanData.getLinks().getLinks()).isEmpty();
  assertThat(spanData.getStatus()).isEqualTo(Status.OK);
  assertThat(spanData.getEndTimestamp()).isEqualTo(timestamp);
}
 
Example #19
Source File: ZipkinExporterHandlerTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void generateSpan_ServerKind() {
  SpanData data =
      SpanData.create(
          SpanContext.create(
              TraceId.fromLowerBase16(TRACE_ID),
              SpanId.fromLowerBase16(SPAN_ID),
              TraceOptions.builder().setIsSampled(true).build()),
          // TODO SpanId.fromLowerBase16
          SpanId.fromLowerBase16(PARENT_SPAN_ID),
          true, /* hasRemoteParent */
          "Recv.helloworld.Greeter.SayHello", /* name */
          Kind.SERVER, /* kind */
          Timestamp.create(1505855794, 194009601) /* startTimestamp */,
          Attributes.create(attributes, 0 /* droppedAttributesCount */),
          TimedEvents.create(annotations, 0 /* droppedEventsCount */),
          TimedEvents.create(messageEvents, 0 /* droppedEventsCount */),
          Links.create(Collections.<Link>emptyList(), 0 /* droppedLinksCount */),
          null, /* childSpanCount */
          Status.OK,
          Timestamp.create(1505855799, 465726528) /* endTimestamp */);

  assertThat(ZipkinExporterHandler.generateSpan(data, localEndpoint))
      .isEqualTo(
          Span.newBuilder()
              .traceId(TRACE_ID)
              .parentId(PARENT_SPAN_ID)
              .id(SPAN_ID)
              .kind(Span.Kind.SERVER)
              .name(data.getName())
              .timestamp(1505855794000000L + 194009601L / 1000)
              .duration(
                  (1505855799000000L + 465726528L / 1000)
                      - (1505855794000000L + 194009601L / 1000))
              .localEndpoint(localEndpoint)
              .addAnnotation(1505855799000000L + 433901068L / 1000, "RECEIVED")
              .addAnnotation(1505855799000000L + 459486280L / 1000, "SENT")
              .putTag(ZipkinExporterHandler.STATUS_CODE, "OK")
              .build());
}
 
Example #20
Source File: SpanBuilderImpl.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
private static void linkSpans(Span span, List<Span> parentLinks) {
  if (!parentLinks.isEmpty()) {
    Link childLink = Link.fromSpanContext(span.getContext(), Type.CHILD_LINKED_SPAN);
    for (Span linkedSpan : parentLinks) {
      linkedSpan.addLink(childLink);
      span.addLink(Link.fromSpanContext(linkedSpan.getContext(), Type.PARENT_LINKED_SPAN));
    }
  }
}
 
Example #21
Source File: RecordEventsSpanImpl.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@GuardedBy("this")
private TraceEvents<Link> getInitializedLinks() {
  if (links == null) {
    links = new TraceEvents<Link>(traceParams.getMaxNumberOfLinks());
  }
  return links;
}
 
Example #22
Source File: RecordEventsSpanImpl.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Override
public void addLink(Link link) {
  Preconditions.checkNotNull(link, "link");
  synchronized (this) {
    if (hasBeenEnded) {
      logger.log(Level.FINE, "Calling addLink() on an ended Span.");
      return;
    }
    getInitializedLinks().addEvent(link);
  }
}
 
Example #23
Source File: RecordEventsSpanImpl.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an immutable representation of all the data from this {@code Span}.
 *
 * @return an immutable representation of all the data from this {@code Span}.
 * @throws IllegalStateException if the Span doesn't have RECORD_EVENTS option.
 */
public SpanData toSpanData() {
  synchronized (this) {
    SpanData.Attributes attributesSpanData =
        attributes == null
            ? SpanData.Attributes.create(Collections.<String, AttributeValue>emptyMap(), 0)
            : SpanData.Attributes.create(attributes, attributes.getNumberOfDroppedAttributes());
    SpanData.TimedEvents<Annotation> annotationsSpanData =
        createTimedEvents(getInitializedAnnotations(), timestampConverter);
    SpanData.TimedEvents<io.opencensus.trace.MessageEvent> messageEventsSpanData =
        createTimedEvents(getInitializedNetworkEvents(), timestampConverter);
    SpanData.Links linksSpanData =
        links == null
            ? SpanData.Links.create(Collections.<Link>emptyList(), 0)
            : SpanData.Links.create(
                new ArrayList<Link>(links.events), links.getNumberOfDroppedEvents());
    return SpanData.create(
        getContext(),
        parentSpanId,
        hasRemoteParent,
        name,
        kind,
        timestampConverter.convertNanoTime(startNanoTime),
        attributesSpanData,
        annotationsSpanData,
        messageEventsSpanData,
        linksSpanData,
        numberOfChildren,
        hasBeenEnded ? getStatusWithDefault() : null,
        hasBeenEnded ? timestampConverter.convertNanoTime(endNanoTime) : null);
  }
}
 
Example #24
Source File: BasicOperationsBenchmark.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
/** Create a link. */
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public Link createLink(Data data) {
  return Link.fromSpanContext(
      SpanContext.create(
          TraceId.fromBytes(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0}),
          SpanId.fromBytes(new byte[] {1, 2, 3, 4, 5, 6, 7, 0}),
          TraceOptions.DEFAULT,
          TRACESTATE_DEFAULT),
      Link.Type.PARENT_LINKED_SPAN);
}
 
Example #25
Source File: RecordTraceEventsBenchmark.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
/** This benchmark attempts to measure performance of adding a link to the span. */
@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public Span addLink(Data data) {
  data.span.addLink(
      Link.fromSpanContext(data.linkedSpan.getContext(), Link.Type.PARENT_LINKED_SPAN));
  return data.span;
}
 
Example #26
Source File: ZipkinExporterHandlerTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void generateSpan_ClientKind() {
  SpanData data =
      SpanData.create(
          SpanContext.create(
              TraceId.fromLowerBase16(TRACE_ID),
              SpanId.fromLowerBase16(SPAN_ID),
              TraceOptions.builder().setIsSampled(true).build()),
          // TODO SpanId.fromLowerBase16
          SpanId.fromLowerBase16(PARENT_SPAN_ID),
          true, /* hasRemoteParent */
          "Sent.helloworld.Greeter.SayHello", /* name */
          Kind.CLIENT, /* kind */
          Timestamp.create(1505855794, 194009601) /* startTimestamp */,
          Attributes.create(attributes, 0 /* droppedAttributesCount */),
          TimedEvents.create(annotations, 0 /* droppedEventsCount */),
          TimedEvents.create(messageEvents, 0 /* droppedEventsCount */),
          Links.create(Collections.<Link>emptyList(), 0 /* droppedLinksCount */),
          null, /* childSpanCount */
          Status.OK,
          Timestamp.create(1505855799, 465726528) /* endTimestamp */);

  assertThat(ZipkinExporterHandler.generateSpan(data, localEndpoint))
      .isEqualTo(
          Span.newBuilder()
              .traceId(TRACE_ID)
              .parentId(PARENT_SPAN_ID)
              .id(SPAN_ID)
              .kind(Span.Kind.CLIENT)
              .name(data.getName())
              .timestamp(1505855794000000L + 194009601L / 1000)
              .duration(
                  (1505855799000000L + 465726528L / 1000)
                      - (1505855794000000L + 194009601L / 1000))
              .localEndpoint(localEndpoint)
              .addAnnotation(1505855799000000L + 433901068L / 1000, "RECEIVED")
              .addAnnotation(1505855799000000L + 459486280L / 1000, "SENT")
              .putTag(ZipkinExporterHandler.STATUS_CODE, "OK")
              .build());
}
 
Example #27
Source File: SpanDataTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void spanData_AllDataEmpty() {
  SpanData spanData =
      SpanData.create(
          spanContext,
          parentSpanId,
          false,
          SPAN_NAME,
          null,
          startTimestamp,
          Attributes.create(Collections.<String, AttributeValue>emptyMap(), 0),
          TimedEvents.create(Collections.<SpanData.TimedEvent<Annotation>>emptyList(), 0),
          TimedEvents.create(Collections.<SpanData.TimedEvent<MessageEvent>>emptyList(), 0),
          Links.create(Collections.<Link>emptyList(), 0),
          0,
          status,
          endTimestamp);
  assertThat(spanData.getContext()).isEqualTo(spanContext);
  assertThat(spanData.getParentSpanId()).isEqualTo(parentSpanId);
  assertThat(spanData.getHasRemoteParent()).isFalse();
  assertThat(spanData.getName()).isEqualTo(SPAN_NAME);
  assertThat(spanData.getStartTimestamp()).isEqualTo(startTimestamp);
  assertThat(spanData.getAttributes().getAttributeMap().isEmpty()).isTrue();
  assertThat(spanData.getAnnotations().getEvents().isEmpty()).isTrue();
  assertThat(spanData.getNetworkEvents().getEvents().isEmpty()).isTrue();
  assertThat(spanData.getMessageEvents().getEvents().isEmpty()).isTrue();
  assertThat(spanData.getLinks().getLinks().isEmpty()).isTrue();
  assertThat(spanData.getChildSpanCount()).isEqualTo(0);
  assertThat(spanData.getStatus()).isEqualTo(status);
  assertThat(spanData.getEndTimestamp()).isEqualTo(endTimestamp);
}
 
Example #28
Source File: FakeSpan.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
@Override
public void addLink(Link link) {}
 
Example #29
Source File: InstanaExporterHandlerTest.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
@Test
public void generateSpan_MultipleAttributes() {
  Map<String, AttributeValue> multipleAttributes =
      ImmutableMap.of(
          "http.url", AttributeValue.stringAttributeValue("http://localhost/foo"),
          "http.method", AttributeValue.stringAttributeValue("GET"));

  SpanData data =
      SpanData.create(
          SpanContext.create(
              TraceId.fromLowerBase16(TRACE_ID),
              SpanId.fromLowerBase16(SPAN_ID),
              TraceOptions.builder().setIsSampled(true).build()),
          SpanId.fromLowerBase16(PARENT_SPAN_ID),
          true, /* hasRemoteParent */
          "SpanName", /* name */
          Kind.CLIENT, /* kind */
          Timestamp.create(1505855794, 194009601) /* startTimestamp */,
          Attributes.create(multipleAttributes, 0 /* droppedAttributesCount */),
          TimedEvents.create(annotations, 0 /* droppedEventsCount */),
          TimedEvents.create(messageEvents, 0 /* droppedEventsCount */),
          Links.create(Collections.<Link>emptyList(), 0 /* droppedLinksCount */),
          null, /* childSpanCount */
          Status.OK,
          Timestamp.create(1505855799, 465726528) /* endTimestamp */);

  assertThat(InstanaExporterHandler.convertToJson(Collections.singletonList(data)))
      .isEqualTo(
          "["
              + "{"
              + "\"spanId\":\"9cc1e3049173be09\","
              + "\"traceId\":\"d239036e7d5cec11\","
              + "\"parentId\":\"8b03ab423da481c5\","
              + "\"timestamp\":1505855794194,"
              + "\"duration\":5271,"
              + "\"name\":\"SpanName\","
              + "\"type\":\"EXIT\","
              + "\"data\":"
              + "{"
              + "\"http.url\":\"http://localhost/foo\","
              + "\"http.method\":\"GET\""
              + "}"
              + "}"
              + "]");
}
 
Example #30
Source File: ZipkinExporterHandlerTest.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
@Test
public void generateSpan_WithAttributes() {
  Map<String, AttributeValue> attributeMap = new HashMap<String, AttributeValue>();
  attributeMap.put("string", AttributeValue.stringAttributeValue("string value"));
  attributeMap.put("boolean", AttributeValue.booleanAttributeValue(false));
  attributeMap.put("long", AttributeValue.longAttributeValue(9999L));
  SpanData data =
      SpanData.create(
          SpanContext.create(
              TraceId.fromLowerBase16(TRACE_ID),
              SpanId.fromLowerBase16(SPAN_ID),
              TraceOptions.builder().setIsSampled(true).build()),
          // TODO SpanId.fromLowerBase16
          SpanId.fromLowerBase16(PARENT_SPAN_ID),
          true, /* hasRemoteParent */
          "Sent.helloworld.Greeter.SayHello", /* name */
          Kind.CLIENT, /* kind */
          Timestamp.create(1505855794, 194009601) /* startTimestamp */,
          Attributes.create(attributeMap, 0 /* droppedAttributesCount */),
          TimedEvents.create(annotations, 0 /* droppedEventsCount */),
          TimedEvents.create(messageEvents, 0 /* droppedEventsCount */),
          Links.create(Collections.<Link>emptyList(), 0 /* droppedLinksCount */),
          null, /* childSpanCount */
          Status.OK,
          Timestamp.create(1505855799, 465726528) /* endTimestamp */);

  assertThat(ZipkinExporterHandler.generateSpan(data, localEndpoint))
      .isEqualTo(
          Span.newBuilder()
              .traceId(TRACE_ID)
              .parentId(PARENT_SPAN_ID)
              .id(SPAN_ID)
              .kind(Span.Kind.CLIENT)
              .name(data.getName())
              .timestamp(1505855794000000L + 194009601L / 1000)
              .duration(
                  (1505855799000000L + 465726528L / 1000)
                      - (1505855794000000L + 194009601L / 1000))
              .localEndpoint(localEndpoint)
              .addAnnotation(1505855799000000L + 433901068L / 1000, "RECEIVED")
              .addAnnotation(1505855799000000L + 459486280L / 1000, "SENT")
              .putTag(ZipkinExporterHandler.STATUS_CODE, "OK")
              .putTag("string", "string value")
              .putTag("boolean", "false")
              .putTag("long", "9999")
              .build());
}