io.opencensus.trace.EndSpanOptions Java Examples

The following examples show how to use io.opencensus.trace.EndSpanOptions. 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: RecordEventsSpanImpl.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Override
public void end(EndSpanOptions options) {
  Preconditions.checkNotNull(options, "options");
  synchronized (this) {
    if (hasBeenEnded) {
      logger.log(Level.FINE, "Calling end() on an ended Span.");
      return;
    }
    if (options.getStatus() != null) {
      status = options.getStatus();
    }
    sampleToLocalSpanStore = options.getSampleToLocalSpanStore();
    endNanoTime = clock.nowNanos();
    hasBeenEnded = true;
  }
  startEndHandler.onEnd(this);
}
 
Example #2
Source File: InProcessSampledSpanStoreImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void getErrorSampledSpans_MaxSpansToReturn() {
  RecordEventsSpanImpl span1 = createSampledSpan(REGISTERED_SPAN_NAME);
  testClock.advanceTime(Duration.create(0, 1000));
  span1.end(EndSpanOptions.builder().setStatus(Status.CANCELLED).build());
  // Advance time to allow other spans to be sampled.
  testClock.advanceTime(Duration.create(5, 0));
  RecordEventsSpanImpl span2 = createSampledSpan(REGISTERED_SPAN_NAME);
  testClock.advanceTime(Duration.create(0, 1000));
  span2.end(EndSpanOptions.builder().setStatus(Status.CANCELLED).build());
  Collection<SpanData> samples =
      sampleStore.getErrorSampledSpans(
          ErrorFilter.create(REGISTERED_SPAN_NAME, CanonicalCode.CANCELLED, 1));
  assertThat(samples.size()).isEqualTo(1);
  // No order guaranteed so one of the spans should be in the list.
  assertThat(samples).containsAnyOf(span1.toSpanData(), span2.toSpanData());
}
 
Example #3
Source File: RecordEventsSpanImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void status_ViaEndSpanOptions() {
  RecordEventsSpanImpl span =
      RecordEventsSpanImpl.startSpan(
          spanContext,
          SPAN_NAME,
          null,
          parentSpanId,
          false,
          TraceParams.DEFAULT,
          startEndHandler,
          timestampConverter,
          testClock);
  Mockito.verify(startEndHandler, Mockito.times(1)).onStart(span);
  testClock.advanceTime(Duration.create(0, 100));
  assertThat(span.getStatus()).isEqualTo(Status.OK);
  span.setStatus(Status.CANCELLED);
  assertThat(span.getStatus()).isEqualTo(Status.CANCELLED);
  span.end(EndSpanOptions.builder().setStatus(Status.ABORTED).build());
  assertThat(span.getStatus()).isEqualTo(Status.ABORTED);
}
 
Example #4
Source File: CensusModulesTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void serverTracingSampledToLocalSpanStore() {
  ServerStreamTracer.Factory tracerFactory = censusTracing.getServerTracerFactory();
  ServerStreamTracer serverStreamTracer =
      tracerFactory.newServerStreamTracer(sampledMethod.getFullMethodName(), new Metadata());

  serverStreamTracer.filterContext(Context.ROOT);

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

  serverStreamTracer.streamClosed(Status.CANCELLED);

  verify(spyServerSpan).end(
      EndSpanOptions.builder()
          .setStatus(io.opencensus.trace.Status.CANCELLED)
          .setSampleToLocalSpanStore(true)
          .build());
}
 
Example #5
Source File: CensusModulesTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void clientStreamNeverCreatedStillRecordTracing() {
  CensusTracingModule.ClientCallTracer callTracer =
      censusTracing.newClientCallTracer(fakeClientParentSpan, method);
  verify(tracer).spanBuilderWithExplicitParent(
      eq("Sent.package1.service2.method3"), same(fakeClientParentSpan));
  verify(spyClientSpanBuilder).setRecordEvents(eq(true));

  callTracer.callEnded(Status.DEADLINE_EXCEEDED.withDescription("3 seconds"));
  verify(spyClientSpan).end(
      EndSpanOptions.builder()
          .setStatus(
              io.opencensus.trace.Status.DEADLINE_EXCEEDED
                  .withDescription("3 seconds"))
          .setSampleToLocalSpanStore(false)
          .build());
  verifyNoMoreInteractions(spyClientSpan);
}
 
Example #6
Source File: CensusModulesTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void clientStreamNeverCreatedStillRecordTracing() {
  CensusTracingModule.ClientCallTracer callTracer =
      censusTracing.newClientCallTracer(fakeClientParentSpan, method);
  verify(tracer).spanBuilderWithExplicitParent(
      eq("Sent.package1.service2.method3"), same(fakeClientParentSpan));
  verify(spyClientSpanBuilder).setRecordEvents(eq(true));

  callTracer.callEnded(Status.DEADLINE_EXCEEDED.withDescription("3 seconds"));
  verify(spyClientSpan).end(
      EndSpanOptions.builder()
          .setStatus(
              io.opencensus.trace.Status.DEADLINE_EXCEEDED
                  .withDescription("3 seconds"))
          .setSampleToLocalSpanStore(false)
          .build());
  verifyNoMoreInteractions(spyClientSpan);
}
 
Example #7
Source File: CensusModulesTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void serverTracingSampledToLocalSpanStore() {
  ServerStreamTracer.Factory tracerFactory = censusTracing.getServerTracerFactory();
  ServerStreamTracer serverStreamTracer =
      tracerFactory.newServerStreamTracer(sampledMethod.getFullMethodName(), new Metadata());

  serverStreamTracer.filterContext(Context.ROOT);

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

  serverStreamTracer.streamClosed(Status.CANCELLED);

  verify(spyServerSpan).end(
      EndSpanOptions.builder()
          .setStatus(io.opencensus.trace.Status.CANCELLED)
          .setSampleToLocalSpanStore(true)
          .build());
}
 
Example #8
Source File: CensusModulesTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void clientTracingSampledToLocalSpanStore() {
  CensusTracingModule.ClientCallTracer callTracer =
      censusTracing.newClientCallTracer(null, sampledMethod);
  callTracer.callEnded(Status.OK);

  verify(spyClientSpan).end(
      EndSpanOptions.builder()
          .setStatus(io.opencensus.trace.Status.OK)
          .setSampleToLocalSpanStore(true)
          .build());
}
 
Example #9
Source File: CensusTracingModule.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
private static EndSpanOptions createEndSpanOptions(
    io.grpc.Status status, boolean sampledToLocalTracing) {
  return EndSpanOptions.builder()
      .setStatus(convertStatus(status))
      .setSampleToLocalSpanStore(sampledToLocalTracing)
      .build();
}
 
Example #10
Source File: OpenCensusUtilsTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testGetEndSpanOptionsOther() {
  EndSpanOptions expected = EndSpanOptions.builder().setStatus(Status.UNKNOWN).build();
  // test some random unsupported statuses
  assertEquals(expected, OpenCensusUtils.getEndSpanOptions(301));
  assertEquals(expected, OpenCensusUtils.getEndSpanOptions(402));
  assertEquals(expected, OpenCensusUtils.getEndSpanOptions(501));
}
 
Example #11
Source File: CensusTracingModule.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
private static EndSpanOptions createEndSpanOptions(
    io.grpc.Status status, boolean sampledToLocalTracing) {
  return EndSpanOptions.builder()
      .setStatus(convertStatus(status))
      .setSampleToLocalSpanStore(sampledToLocalTracing)
      .build();
}
 
Example #12
Source File: OpenCensusUtils.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an {@link EndSpanOptions} to end a http span according to the status code.
 *
 * @param statusCode the status code, can be null to represent no valid response is returned.
 * @return an {@code EndSpanOptions} that best suits the status code.
 */
public static EndSpanOptions getEndSpanOptions(@Nullable Integer statusCode) {
  // Always sample the span, but optionally export it.
  EndSpanOptions.Builder builder = EndSpanOptions.builder();
  if (statusCode == null) {
    builder.setStatus(Status.UNKNOWN);
  } else if (!HttpStatusCodes.isSuccess(statusCode)) {
    switch (statusCode) {
      case HttpStatusCodes.STATUS_CODE_BAD_REQUEST:
        builder.setStatus(Status.INVALID_ARGUMENT);
        break;
      case HttpStatusCodes.STATUS_CODE_UNAUTHORIZED:
        builder.setStatus(Status.UNAUTHENTICATED);
        break;
      case HttpStatusCodes.STATUS_CODE_FORBIDDEN:
        builder.setStatus(Status.PERMISSION_DENIED);
        break;
      case HttpStatusCodes.STATUS_CODE_NOT_FOUND:
        builder.setStatus(Status.NOT_FOUND);
        break;
      case HttpStatusCodes.STATUS_CODE_PRECONDITION_FAILED:
        builder.setStatus(Status.FAILED_PRECONDITION);
        break;
      case HttpStatusCodes.STATUS_CODE_SERVER_ERROR:
        builder.setStatus(Status.UNAVAILABLE);
        break;
      default:
        builder.setStatus(Status.UNKNOWN);
    }
  } else {
    builder.setStatus(Status.OK);
  }
  return builder.build();
}
 
Example #13
Source File: HttpClientHandlerTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void handleEndShouldEndSpan() {
  HttpRequestContext context = new HttpRequestContext(parentSpan, tagContext);
  when(extractor.getStatusCode(any(Object.class))).thenReturn(0);
  handler.handleEnd(context, request, response, null);
  verify(parentSpan).end(optionsCaptor.capture());
  EndSpanOptions options = optionsCaptor.getValue();
  assertThat(options).isEqualTo(EndSpanOptions.DEFAULT);
}
 
Example #14
Source File: HttpServerHandlerTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void handleEndShouldEndSpan() {
  HttpRequestContext context = new HttpRequestContext(spanWithLocalParent, tagContext);
  when(extractor.getStatusCode(any(Object.class))).thenReturn(0);

  handler.handleEnd(context, carrier, response, null);
  verify(spanWithLocalParent).end(optionsCaptor.capture());
  EndSpanOptions options = optionsCaptor.getValue();
  assertThat(options).isEqualTo(EndSpanOptions.DEFAULT);
}
 
Example #15
Source File: AbstractHttpHandlerTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void handleEndShouldEndSpan() {
  when(extractor.getStatusCode(any(Object.class))).thenReturn(0);

  handler.spanEnd(fakeSpan, 0, error);
  verify(fakeSpan).end(optionsCaptor.capture());
  assertThat(optionsCaptor.getValue()).isEqualTo(EndSpanOptions.DEFAULT);
}
 
Example #16
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 #17
Source File: InProcessSampledSpanStoreImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void getErrorSampledSpans_NullCode_MaxSpansToReturn() {
  RecordEventsSpanImpl span1 = createSampledSpan(REGISTERED_SPAN_NAME);
  testClock.advanceTime(Duration.create(0, 1000));
  span1.end(EndSpanOptions.builder().setStatus(Status.CANCELLED).build());
  RecordEventsSpanImpl span2 = createSampledSpan(REGISTERED_SPAN_NAME);
  testClock.advanceTime(Duration.create(0, 1000));
  span2.end(EndSpanOptions.builder().setStatus(Status.UNKNOWN).build());
  Collection<SpanData> samples =
      sampleStore.getErrorSampledSpans(ErrorFilter.create(REGISTERED_SPAN_NAME, null, 1));
  assertThat(samples.size()).isEqualTo(1);
  assertThat(samples).containsAnyOf(span1.toSpanData(), span2.toSpanData());
}
 
Example #18
Source File: CensusModulesTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void serverTracingNotSampledToLocalSpanStore_whenServerCallNotCreated() {
  ServerStreamTracer.Factory tracerFactory = censusTracing.getServerTracerFactory();
  ServerStreamTracer serverStreamTracer =
      tracerFactory.newServerStreamTracer(sampledMethod.getFullMethodName(), new Metadata());

  serverStreamTracer.streamClosed(Status.CANCELLED);

  verify(spyServerSpan).end(
      EndSpanOptions.builder()
          .setStatus(io.opencensus.trace.Status.CANCELLED)
          .setSampleToLocalSpanStore(false)
          .build());
}
 
Example #19
Source File: InProcessSampledSpanStoreImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void getErrorSampledSpans() {
  RecordEventsSpanImpl span = createSampledSpan(REGISTERED_SPAN_NAME);
  testClock.advanceTime(Duration.create(0, 1000));
  span.end(EndSpanOptions.builder().setStatus(Status.CANCELLED).build());
  Collection<SpanData> samples =
      sampleStore.getErrorSampledSpans(
          ErrorFilter.create(REGISTERED_SPAN_NAME, CanonicalCode.CANCELLED, 0));
  assertThat(samples.size()).isEqualTo(1);
  assertThat(samples.contains(span.toSpanData())).isTrue();
}
 
Example #20
Source File: InProcessSampledSpanStoreImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void registerSpanNamesViaSpanBuilderOption() {
  assertThat(sampleStore.getRegisteredSpanNamesForCollection())
      .containsExactly(REGISTERED_SPAN_NAME);
  createSampledSpan(NOT_REGISTERED_SPAN_NAME)
      .end(EndSpanOptions.builder().setSampleToLocalSpanStore(true).build());
  assertThat(sampleStore.getRegisteredSpanNamesForCollection())
      .containsExactly(REGISTERED_SPAN_NAME, NOT_REGISTERED_SPAN_NAME);
}
 
Example #21
Source File: InProcessSampledSpanStoreImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
private void addSpanNameToAllErrorBuckets(String spanName) {
  for (CanonicalCode code : CanonicalCode.values()) {
    if (code != CanonicalCode.OK) {
      Span sampledSpan = createSampledSpan(spanName);
      Span notSampledSpan = createNotSampledSpan(spanName);
      testClock.advanceTime(Duration.create(0, 1000));
      sampledSpan.end(EndSpanOptions.builder().setStatus(code.toStatus()).build());
      notSampledSpan.end(EndSpanOptions.builder().setStatus(code.toStatus()).build());
    }
  }
}
 
Example #22
Source File: RecordEventsSpanImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void sampleToLocalSpanStore() {
  RecordEventsSpanImpl span =
      RecordEventsSpanImpl.startSpan(
          spanContext,
          SPAN_NAME,
          null,
          parentSpanId,
          false,
          TraceParams.DEFAULT,
          startEndHandler,
          timestampConverter,
          testClock);
  span.end(EndSpanOptions.builder().setSampleToLocalSpanStore(true).build());
  Mockito.verify(startEndHandler, Mockito.times(1)).onEnd(span);
  assertThat(span.getSampleToLocalSpanStore()).isTrue();
  span =
      RecordEventsSpanImpl.startSpan(
          spanContext,
          SPAN_NAME,
          null,
          parentSpanId,
          false,
          TraceParams.DEFAULT,
          startEndHandler,
          timestampConverter,
          testClock);
  span.end();
  Mockito.verify(startEndHandler, Mockito.times(1)).onEnd(span);
  assertThat(span.getSampleToLocalSpanStore()).isFalse();
}
 
Example #23
Source File: InProcessSampledSpanStoreImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void getErrorSampledSpans_NullCode() {
  RecordEventsSpanImpl span1 = createSampledSpan(REGISTERED_SPAN_NAME);
  testClock.advanceTime(Duration.create(0, 1000));
  span1.end(EndSpanOptions.builder().setStatus(Status.CANCELLED).build());
  RecordEventsSpanImpl span2 = createSampledSpan(REGISTERED_SPAN_NAME);
  testClock.advanceTime(Duration.create(0, 1000));
  span2.end(EndSpanOptions.builder().setStatus(Status.UNKNOWN).build());
  Collection<SpanData> samples =
      sampleStore.getErrorSampledSpans(ErrorFilter.create(REGISTERED_SPAN_NAME, null, 0));
  assertThat(samples.size()).isEqualTo(2);
  assertThat(samples).containsExactly(span1.toSpanData(), span2.toSpanData());
}
 
Example #24
Source File: CensusModulesTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void clientTracingSampledToLocalSpanStore() {
  CensusTracingModule.ClientCallTracer callTracer =
      censusTracing.newClientCallTracer(null, sampledMethod);
  callTracer.callEnded(Status.OK);

  verify(spyClientSpan).end(
      EndSpanOptions.builder()
          .setStatus(io.opencensus.trace.Status.OK)
          .setSampleToLocalSpanStore(true)
          .build());
}
 
Example #25
Source File: CensusModulesTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void serverTracingNotSampledToLocalSpanStore_whenServerCallNotCreated() {
  ServerStreamTracer.Factory tracerFactory = censusTracing.getServerTracerFactory();
  ServerStreamTracer serverStreamTracer =
      tracerFactory.newServerStreamTracer(sampledMethod.getFullMethodName(), new Metadata());

  serverStreamTracer.streamClosed(Status.CANCELLED);

  verify(spyServerSpan).end(
      EndSpanOptions.builder()
          .setStatus(io.opencensus.trace.Status.CANCELLED)
          .setSampleToLocalSpanStore(false)
          .build());
}
 
Example #26
Source File: OpenCensusUtilsTest.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
public void testGetEndSpanOptionsNotFound() {
  EndSpanOptions expected = EndSpanOptions.builder().setStatus(Status.NOT_FOUND).build();
  assertEquals(expected, OpenCensusUtils.getEndSpanOptions(404));
}
 
Example #27
Source File: StatsTestUtils.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Override
public void end(EndSpanOptions options) {}
 
Example #28
Source File: OpenCensusUtilsTest.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
public void testGetEndSpanOptionsPreconditionFailed() {
  EndSpanOptions expected =
      EndSpanOptions.builder().setStatus(Status.FAILED_PRECONDITION).build();
  assertEquals(expected, OpenCensusUtils.getEndSpanOptions(412));
}
 
Example #29
Source File: OpenCensusUtilsTest.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
public void testGetEndSpanOptionsServerError() {
  EndSpanOptions expected = EndSpanOptions.builder().setStatus(Status.UNAVAILABLE).build();
  assertEquals(expected, OpenCensusUtils.getEndSpanOptions(500));
}
 
Example #30
Source File: MockSpan.java    From styx with Apache License 2.0 4 votes vote down vote up
@Override
public void end(EndSpanOptions options) {
  this.ended = true;
}