io.opencensus.trace.MessageEvent Java Examples

The following examples show how to use io.opencensus.trace.MessageEvent. 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: SpanData.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
/**
 * Returns network events recorded for this {@code Span}.
 *
 * @return network events recorded for this {@code Span}.
 * @deprecated Use {@link #getMessageEvents}.
 * @since 0.5
 */
@Deprecated
@SuppressWarnings({"deprecation"})
public TimedEvents<io.opencensus.trace.NetworkEvent> getNetworkEvents() {
  TimedEvents<MessageEvent> timedEvents = getMessageEvents();
  List<TimedEvent<io.opencensus.trace.NetworkEvent>> networkEventsList =
      new ArrayList<TimedEvent<io.opencensus.trace.NetworkEvent>>();
  for (TimedEvent<MessageEvent> timedEvent : timedEvents.getEvents()) {
    networkEventsList.add(
        TimedEvent.<io.opencensus.trace.NetworkEvent>create(
            timedEvent.getTimestamp(),
            BaseMessageEventUtils.asNetworkEvent(timedEvent.getEvent())));
  }
  return TimedEvents.<io.opencensus.trace.NetworkEvent>create(
      networkEventsList, timedEvents.getDroppedEventsCount());
}
 
Example #2
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 #3
Source File: CensusTracingModule.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
private static void recordMessageEvent(
    Span span, MessageEvent.Type type,
    int seqNo, long optionalWireSize, long optionalUncompressedSize) {
  MessageEvent.Builder eventBuilder = MessageEvent.builder(type, seqNo);
  if (optionalUncompressedSize != -1) {
    eventBuilder.setUncompressedMessageSize(optionalUncompressedSize);
  }
  if (optionalWireSize != -1) {
    eventBuilder.setCompressedMessageSize(optionalWireSize);
  }
  span.addMessageEvent(eventBuilder.build());
}
 
Example #4
Source File: CensusTracingModule.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
private static void recordMessageEvent(
    Span span, MessageEvent.Type type,
    int seqNo, long optionalWireSize, long optionalUncompressedSize) {
  MessageEvent.Builder eventBuilder = MessageEvent.builder(type, seqNo);
  if (optionalUncompressedSize != -1) {
    eventBuilder.setUncompressedMessageSize(optionalUncompressedSize);
  }
  if (optionalWireSize != -1) {
    eventBuilder.setCompressedMessageSize(optionalWireSize);
  }
  span.addMessageEvent(eventBuilder.build());
}
 
Example #5
Source File: HttpRequestTracingTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 20_000L)
public void executeCreatesSpan() throws IOException {
  MockLowLevelHttpResponse mockResponse = new MockLowLevelHttpResponse().setStatusCode(200);
  HttpTransport transport =
      new MockHttpTransport.Builder().setLowLevelHttpResponse(mockResponse).build();
  HttpRequest request =
      new HttpRequestFactory(transport, null)
          .buildGetRequest(new GenericUrl("https://google.com/"));
  request.execute();

  // This call blocks - we set a timeout on this test to ensure we don't wait forever
  List<SpanData> spans = testHandler.waitForExport(1);
  assertEquals(1, spans.size());
  SpanData span = spans.get(0);

  // Ensure the span name is set
  assertEquals(SPAN_NAME_HTTP_REQUEST_EXECUTE, span.getName());

  // Ensure we have basic span attributes
  assertAttributeEquals(span, "http.path", "/");
  assertAttributeEquals(span, "http.host", "google.com");
  assertAttributeEquals(span, "http.url", "https://google.com/");
  assertAttributeEquals(span, "http.method", "GET");

  // Ensure we have a single annotation for starting the first attempt
  assertEquals(1, span.getAnnotations().getEvents().size());

  // Ensure we have 2 message events, SENT and RECEIVED
  assertEquals(2, span.getMessageEvents().getEvents().size());
  assertEquals(
      MessageEvent.Type.SENT, span.getMessageEvents().getEvents().get(0).getEvent().getType());
  assertEquals(
      MessageEvent.Type.RECEIVED,
      span.getMessageEvents().getEvents().get(1).getEvent().getType());

  // Ensure we record the span status as OK
  assertEquals(Status.OK, span.getStatus());
}
 
Example #6
Source File: OpenCensusUtilsTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testRecordMessageEvent() {
  try {
    OpenCensusUtils.recordMessageEvent(mockSpan, 0, MessageEvent.Type.SENT);
    fail("expected " + UnsupportedOperationException.class);
  } catch (UnsupportedOperationException e) {
    assertEquals(e.getMessage(), "Span.addMessageEvent");
  }
}
 
Example #7
Source File: OpenCensusUtilsTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testRecordMessageEventInNullSpan() {
  try {
    OpenCensusUtils.recordMessageEvent(null, 0, MessageEvent.Type.SENT);
    fail("expected " + IllegalArgumentException.class);
  } catch (IllegalArgumentException e) {
    assertEquals(e.getMessage(), "span should not be null.");
  }
}
 
Example #8
Source File: OpenCensusUtils.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * Records a message event of a certain {@link MessageEvent.Type}. This method is package
 * protected since {@link MessageEvent} might be deprecated in future releases.
 *
 * @param span The {@code span} in which the event occurs.
 * @param size Size of the message.
 * @param eventType The {@code NetworkEvent.Type} of the message event.
 */
@VisibleForTesting
static void recordMessageEvent(Span span, long size, Type eventType) {
  Preconditions.checkArgument(span != null, "span should not be null.");
  if (size < 0) {
    size = 0;
  }
  MessageEvent event =
      MessageEvent.builder(eventType, idGenerator.getAndIncrement())
          .setUncompressedMessageSize(size)
          .build();
  span.addMessageEvent(event);
}
 
Example #9
Source File: JaegerExporterHandlerTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
private static SpanData.TimedEvent<MessageEvent> sampleMessageEvent() {
  return SpanData.TimedEvent.create(
      Timestamp.create(1519629871L, 123456789),
      MessageEvent.builder(MessageEvent.Type.SENT, 42L)
          .setCompressedMessageSize(69)
          .setUncompressedMessageSize(96)
          .build());
}
 
Example #10
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 #11
Source File: AbstractHttpHandlerTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void handleMessageReceived() {
  Type type = Type.RECEIVED;
  long uncompressed = 456L;
  HttpRequestContext context = new HttpRequestContext(fakeSpan, tagContext);
  handler.handleMessageReceived(context, uncompressed);
  verify(fakeSpan).addMessageEvent(captor.capture());

  MessageEvent messageEvent = captor.getValue();
  assertThat(messageEvent.getType()).isEqualTo(type);
  assertThat(messageEvent.getMessageId()).isEqualTo(1L);
  assertThat(messageEvent.getUncompressedMessageSize()).isEqualTo(uncompressed);
  assertThat(messageEvent.getCompressedMessageSize()).isEqualTo(0);
}
 
Example #12
Source File: AbstractHttpHandlerTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void handleMessageSent() {
  Type type = Type.SENT;
  long uncompressed = 456L;
  HttpRequestContext context = new HttpRequestContext(fakeSpan, tagContext);
  handler.handleMessageSent(context, uncompressed);
  verify(fakeSpan).addMessageEvent(captor.capture());

  MessageEvent messageEvent = captor.getValue();
  assertThat(messageEvent.getType()).isEqualTo(type);
  assertThat(messageEvent.getMessageId()).isEqualTo(1L);
  assertThat(messageEvent.getUncompressedMessageSize()).isEqualTo(uncompressed);
  assertThat(messageEvent.getCompressedMessageSize()).isEqualTo(0);
}
 
Example #13
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 #14
Source File: BasicOperationsBenchmark.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
/** Create a message event. */
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public MessageEvent createMessageEvent(Data data) {
  return MessageEvent.builder(MessageEvent.Type.SENT, MESSAGE_ID).build();
}
 
Example #15
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 #16
Source File: FakeSpan.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
@Override
public void addMessageEvent(MessageEvent messageEvent) {}
 
Example #17
Source File: JaxrsClientFilterTest.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
@Override
public void addMessageEvent(MessageEvent messageEvent) {}
 
Example #18
Source File: SpanDataTest.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
@Test
public void spanDataEquals() {
  SpanData allSpanData1 =
      SpanData.create(
          spanContext,
          parentSpanId,
          false,
          SPAN_NAME,
          Kind.CLIENT,
          startTimestamp,
          attributes,
          annotations,
          messageEvents,
          links,
          CHILD_SPAN_COUNT,
          status,
          endTimestamp);
  SpanData allSpanData2 =
      SpanData.create(
          spanContext,
          parentSpanId,
          false,
          SPAN_NAME,
          Kind.CLIENT,
          startTimestamp,
          attributes,
          annotations,
          messageEvents,
          links,
          CHILD_SPAN_COUNT,
          status,
          endTimestamp);
  SpanData emptySpanData =
      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);
  new EqualsTester()
      .addEqualityGroup(allSpanData1, allSpanData2)
      .addEqualityGroup(emptySpanData)
      .testEquals();
}
 
Example #19
Source File: SpanData.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a new immutable {@code SpanData}.
 *
 * @param context the {@code SpanContext} of the {@code Span}.
 * @param parentSpanId the parent {@code SpanId} of the {@code Span}. {@code null} if the {@code
 *     Span} is a root.
 * @param hasRemoteParent {@code true} if the parent {@code Span} is remote. {@code null} if this
 *     is a root span.
 * @param name the name of the {@code Span}.
 * @param kind the kind of the {@code Span}.
 * @param startTimestamp the start {@code Timestamp} of the {@code Span}.
 * @param attributes the attributes associated with the {@code Span}.
 * @param annotations the annotations associated with the {@code Span}.
 * @param messageOrNetworkEvents the message events (or network events for backward compatibility)
 *     associated with the {@code Span}.
 * @param links the links associated with the {@code Span}.
 * @param childSpanCount the number of child spans that were generated while the span was active.
 * @param status the {@code Status} of the {@code Span}. {@code null} if the {@code Span} is still
 *     active.
 * @param endTimestamp the end {@code Timestamp} of the {@code Span}. {@code null} if the {@code
 *     Span} is still active.
 * @return a new immutable {@code SpanData}.
 * @since 0.14
 */
@SuppressWarnings({"deprecation", "InconsistentOverloads"})
public static SpanData create(
    SpanContext context,
    @Nullable SpanId parentSpanId,
    @Nullable Boolean hasRemoteParent,
    String name,
    @Nullable Kind kind,
    Timestamp startTimestamp,
    Attributes attributes,
    TimedEvents<Annotation> annotations,
    TimedEvents<? extends io.opencensus.trace.BaseMessageEvent> messageOrNetworkEvents,
    Links links,
    @Nullable Integer childSpanCount,
    @Nullable Status status,
    @Nullable Timestamp endTimestamp) {
  Utils.checkNotNull(messageOrNetworkEvents, "messageOrNetworkEvents");
  List<TimedEvent<MessageEvent>> messageEventsList = new ArrayList<TimedEvent<MessageEvent>>();
  for (TimedEvent<? extends io.opencensus.trace.BaseMessageEvent> timedEvent :
      messageOrNetworkEvents.getEvents()) {
    io.opencensus.trace.BaseMessageEvent event = timedEvent.getEvent();
    if (event instanceof MessageEvent) {
      @SuppressWarnings("unchecked")
      TimedEvent<MessageEvent> timedMessageEvent = (TimedEvent<MessageEvent>) timedEvent;
      messageEventsList.add(timedMessageEvent);
    } else {
      messageEventsList.add(
          TimedEvent.<MessageEvent>create(
              timedEvent.getTimestamp(), BaseMessageEventUtils.asMessageEvent(event)));
    }
  }
  TimedEvents<MessageEvent> messageEvents =
      TimedEvents.<MessageEvent>create(
          messageEventsList, messageOrNetworkEvents.getDroppedEventsCount());
  return new AutoValue_SpanData(
      context,
      parentSpanId,
      hasRemoteParent,
      name,
      kind,
      startTimestamp,
      attributes,
      annotations,
      messageEvents,
      links,
      childSpanCount,
      status,
      endTimestamp);
}
 
Example #20
Source File: CensusModulesTest.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Test
public void serverBasicTracingNoHeaders() {
  ServerStreamTracer.Factory tracerFactory = censusTracing.getServerTracerFactory();
  ServerStreamTracer serverStreamTracer =
      tracerFactory.newServerStreamTracer(method.getFullMethodName(), new Metadata());
  verifyZeroInteractions(mockTracingPropagationHandler);
  verify(tracer).spanBuilderWithRemoteParent(
      eq("Recv.package1.service2.method3"), isNull(SpanContext.class));
  verify(spyServerSpanBuilder).setRecordEvents(eq(true));

  Context filteredContext = serverStreamTracer.filterContext(Context.ROOT);
  assertSame(spyServerSpan, ContextUtils.CONTEXT_SPAN_KEY.get(filteredContext));

  serverStreamTracer.serverCallStarted(
      new ServerCallInfoImpl<String, String>(method, Attributes.EMPTY, null));

  verify(spyServerSpan, never()).end(any(EndSpanOptions.class));

  serverStreamTracer.outboundMessage(0);
  serverStreamTracer.outboundMessageSent(0, 882, -1);
  serverStreamTracer.inboundMessage(0);
  serverStreamTracer.outboundMessage(1);
  serverStreamTracer.outboundMessageSent(1, -1, 27);
  serverStreamTracer.inboundMessageRead(0, 255, 90);

  serverStreamTracer.streamClosed(Status.CANCELLED);

  InOrder inOrder = inOrder(spyServerSpan);
  inOrder.verify(spyServerSpan, times(3)).addMessageEvent(messageEventCaptor.capture());
  List<MessageEvent> events = messageEventCaptor.getAllValues();
  assertEquals(
      MessageEvent.builder(Type.SENT, 0).setCompressedMessageSize(882).build(), events.get(0));
  assertEquals(
      MessageEvent.builder(Type.SENT, 1).setUncompressedMessageSize(27).build(), events.get(1));
  assertEquals(
      MessageEvent.builder(Type.RECEIVED, 0)
          .setCompressedMessageSize(255)
          .setUncompressedMessageSize(90)
          .build(),
      events.get(2));
  inOrder.verify(spyServerSpan).end(
      EndSpanOptions.builder()
          .setStatus(io.opencensus.trace.Status.CANCELLED)
          .setSampleToLocalSpanStore(false)
          .build());
  verifyNoMoreInteractions(spyServerSpan);
}
 
Example #21
Source File: CensusModulesTest.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Test
public void clientBasicTracingDefaultSpan() {
  CensusTracingModule.ClientCallTracer callTracer =
      censusTracing.newClientCallTracer(null, method);
  Metadata headers = new Metadata();
  ClientStreamTracer clientStreamTracer =
      callTracer.newClientStreamTracer(CallOptions.DEFAULT, headers);
  verify(tracer).spanBuilderWithExplicitParent(
      eq("Sent.package1.service2.method3"), isNull(Span.class));
  verify(spyClientSpan, never()).end(any(EndSpanOptions.class));

  clientStreamTracer.outboundMessage(0);
  clientStreamTracer.outboundMessageSent(0, 882, -1);
  clientStreamTracer.inboundMessage(0);
  clientStreamTracer.outboundMessage(1);
  clientStreamTracer.outboundMessageSent(1, -1, 27);
  clientStreamTracer.inboundMessageRead(0, 255, 90);

  clientStreamTracer.streamClosed(Status.OK);
  callTracer.callEnded(Status.OK);

  InOrder inOrder = inOrder(spyClientSpan);
  inOrder.verify(spyClientSpan, times(3)).addMessageEvent(messageEventCaptor.capture());
  List<MessageEvent> events = messageEventCaptor.getAllValues();
  assertEquals(
      MessageEvent.builder(Type.SENT, 0).setCompressedMessageSize(882).build(), events.get(0));
  assertEquals(
      MessageEvent.builder(Type.SENT, 1).setUncompressedMessageSize(27).build(), events.get(1));
  assertEquals(
      MessageEvent.builder(Type.RECEIVED, 0)
          .setCompressedMessageSize(255)
          .setUncompressedMessageSize(90)
          .build(),
      events.get(2));
  inOrder.verify(spyClientSpan).end(
      EndSpanOptions.builder()
          .setStatus(io.opencensus.trace.Status.OK)
          .setSampleToLocalSpanStore(false)
          .build());
  verifyNoMoreInteractions(spyClientSpan);
  verifyNoMoreInteractions(tracer);
}
 
Example #22
Source File: HttpRequestTracingTest.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 20_000L)
public void executeExceptionCreatesSpan() throws IOException {
  HttpTransport transport =
      new MockHttpTransport.Builder()
          .setLowLevelHttpRequest(
              new MockLowLevelHttpRequest() {
                @Override
                public LowLevelHttpResponse execute() throws IOException {
                  throw new IOException("some IOException");
                }
              })
          .build();
  HttpRequest request =
      new HttpRequestFactory(transport, null)
          .buildGetRequest(new GenericUrl("https://google.com/"));

  try {
    request.execute();
    fail("expected to throw an IOException");
  } catch (IOException expected) {
  }

  // This call blocks - we set a timeout on this test to ensure we don't wait forever
  List<SpanData> spans = testHandler.waitForExport(1);
  assertEquals(1, spans.size());
  SpanData span = spans.get(0);

  // Ensure the span name is set
  assertEquals(SPAN_NAME_HTTP_REQUEST_EXECUTE, span.getName());

  // Ensure we have basic span attributes
  assertAttributeEquals(span, "http.path", "/");
  assertAttributeEquals(span, "http.host", "google.com");
  assertAttributeEquals(span, "http.url", "https://google.com/");
  assertAttributeEquals(span, "http.method", "GET");

  // Ensure we have a single annotation for starting the first attempt
  assertEquals(1, span.getAnnotations().getEvents().size());

  // Ensure we have 2 message events, SENT and RECEIVED
  assertEquals(1, span.getMessageEvents().getEvents().size());
  assertEquals(
      MessageEvent.Type.SENT, span.getMessageEvents().getEvents().get(0).getEvent().getType());

  // Ensure we record the span status as UNKNOWN
  assertEquals(Status.UNKNOWN, span.getStatus());
}
 
Example #23
Source File: StatsTestUtils.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Override
public void addMessageEvent(MessageEvent messageEvent) {}
 
Example #24
Source File: CensusModulesTest.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Test
public void clientBasicTracingDefaultSpan() {
  CensusTracingModule.ClientCallTracer callTracer =
      censusTracing.newClientCallTracer(null, method);
  Metadata headers = new Metadata();
  ClientStreamTracer clientStreamTracer = callTracer.newClientStreamTracer(STREAM_INFO, headers);
  verify(tracer).spanBuilderWithExplicitParent(
      eq("Sent.package1.service2.method3"), ArgumentMatchers.<Span>isNull());
  verify(spyClientSpan, never()).end(any(EndSpanOptions.class));

  clientStreamTracer.outboundMessage(0);
  clientStreamTracer.outboundMessageSent(0, 882, -1);
  clientStreamTracer.inboundMessage(0);
  clientStreamTracer.outboundMessage(1);
  clientStreamTracer.outboundMessageSent(1, -1, 27);
  clientStreamTracer.inboundMessageRead(0, 255, 90);

  clientStreamTracer.streamClosed(Status.OK);
  callTracer.callEnded(Status.OK);

  InOrder inOrder = inOrder(spyClientSpan);
  inOrder.verify(spyClientSpan, times(3)).addMessageEvent(messageEventCaptor.capture());
  List<MessageEvent> events = messageEventCaptor.getAllValues();
  assertEquals(
      MessageEvent.builder(Type.SENT, 0).setCompressedMessageSize(882).build(), events.get(0));
  assertEquals(
      MessageEvent.builder(Type.SENT, 1).setUncompressedMessageSize(27).build(), events.get(1));
  assertEquals(
      MessageEvent.builder(Type.RECEIVED, 0)
          .setCompressedMessageSize(255)
          .setUncompressedMessageSize(90)
          .build(),
      events.get(2));
  inOrder.verify(spyClientSpan).end(
      EndSpanOptions.builder()
          .setStatus(io.opencensus.trace.Status.OK)
          .setSampleToLocalSpanStore(false)
          .build());
  verifyNoMoreInteractions(spyClientSpan);
  verifyNoMoreInteractions(tracer);
}
 
Example #25
Source File: CensusModulesTest.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Test
public void serverBasicTracingNoHeaders() {
  ServerStreamTracer.Factory tracerFactory = censusTracing.getServerTracerFactory();
  ServerStreamTracer serverStreamTracer =
      tracerFactory.newServerStreamTracer(method.getFullMethodName(), new Metadata());
  verifyZeroInteractions(mockTracingPropagationHandler);
  verify(tracer).spanBuilderWithRemoteParent(
      eq("Recv.package1.service2.method3"), ArgumentMatchers.<SpanContext>isNull());
  verify(spyServerSpanBuilder).setRecordEvents(eq(true));

  Context filteredContext = serverStreamTracer.filterContext(Context.ROOT);
  assertSame(spyServerSpan, ContextUtils.getValue(filteredContext));

  serverStreamTracer.serverCallStarted(
      new CallInfo<>(method, Attributes.EMPTY, null));

  verify(spyServerSpan, never()).end(any(EndSpanOptions.class));

  serverStreamTracer.outboundMessage(0);
  serverStreamTracer.outboundMessageSent(0, 882, -1);
  serverStreamTracer.inboundMessage(0);
  serverStreamTracer.outboundMessage(1);
  serverStreamTracer.outboundMessageSent(1, -1, 27);
  serverStreamTracer.inboundMessageRead(0, 255, 90);

  serverStreamTracer.streamClosed(Status.CANCELLED);

  InOrder inOrder = inOrder(spyServerSpan);
  inOrder.verify(spyServerSpan, times(3)).addMessageEvent(messageEventCaptor.capture());
  List<MessageEvent> events = messageEventCaptor.getAllValues();
  assertEquals(
      MessageEvent.builder(Type.SENT, 0).setCompressedMessageSize(882).build(), events.get(0));
  assertEquals(
      MessageEvent.builder(Type.SENT, 1).setUncompressedMessageSize(27).build(), events.get(1));
  assertEquals(
      MessageEvent.builder(Type.RECEIVED, 0)
          .setCompressedMessageSize(255)
          .setUncompressedMessageSize(90)
          .build(),
      events.get(2));
  inOrder.verify(spyServerSpan).end(
      EndSpanOptions.builder()
          .setStatus(io.opencensus.trace.Status.CANCELLED)
          .setSampleToLocalSpanStore(false)
          .build());
  verifyNoMoreInteractions(spyServerSpan);
}
 
Example #26
Source File: StatsTestUtils.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Override
public void addMessageEvent(MessageEvent messageEvent) {}
 
Example #27
Source File: AbstractHttpHandler.java    From opencensus-java with Apache License 2.0 3 votes vote down vote up
/**
 * A convenience to record a {@link MessageEvent} with given parameters.
 *
 * @param span the span which this {@code MessageEvent} will be added to.
 * @param id the id of the event.
 * @param type the {@code MessageEvent.Type} of the event.
 * @param uncompressedMessageSize size of the message before compressed (optional).
 * @param compressedMessageSize size of the message after compressed (optional).
 * @since 0.19
 */
static void recordMessageEvent(
    Span span, long id, Type type, long uncompressedMessageSize, long compressedMessageSize) {
  MessageEvent messageEvent =
      MessageEvent.builder(type, id)
          .setUncompressedMessageSize(uncompressedMessageSize)
          .setCompressedMessageSize(compressedMessageSize)
          .build();
  span.addMessageEvent(messageEvent);
}
 
Example #28
Source File: SpanData.java    From opencensus-java with Apache License 2.0 2 votes vote down vote up
/**
 * Returns message events recorded for this {@code Span}.
 *
 * @return message events recorded for this {@code Span}.
 * @since 0.12
 */
public abstract TimedEvents<MessageEvent> getMessageEvents();