io.opencensus.trace.samplers.Samplers Java Examples

The following examples show how to use io.opencensus.trace.samplers.Samplers. 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: SpanBuilderImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void startSpan_CreatesTheCorrectSpanImplInstance() {
  assertThat(
          SpanBuilderImpl.createWithParent(SPAN_NAME, null, spanBuilderOptions)
              .setSampler(Samplers.alwaysSample())
              .startSpan())
      .isInstanceOf(RecordEventsSpanImpl.class);
  assertThat(
          SpanBuilderImpl.createWithParent(SPAN_NAME, null, spanBuilderOptions)
              .setRecordEvents(true)
              .setSampler(Samplers.neverSample())
              .startSpan())
      .isInstanceOf(RecordEventsSpanImpl.class);
  assertThat(
          SpanBuilderImpl.createWithParent(SPAN_NAME, null, spanBuilderOptions)
              .setSampler(Samplers.neverSample())
              .startSpan())
      .isInstanceOf(NoRecordEventsSpanImpl.class);
}
 
Example #2
Source File: OpenCensusTracerAdapter.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public Span start() {
  final io.opencensus.trace.SpanBuilder sb;

  if (ignoreActiveSpan) {

    if (parentSpan != null) {
      sb = tracer.spanBuilderWithExplicitParent(opName, parentSpan);
    } else if (parentContext != null) {
      sb = tracer.spanBuilderWithRemoteParent(opName, parentContext);
    } else {
      sb = tracer.spanBuilderWithRemoteParent(opName, INVALID);
    }

  } else {
    sb = tracer.spanBuilder(opName);
  }
  // We only use the always on sampler for our spans.
  // We could set it globally, but some other libraries (gRPC)
  // create spans through the opencensus tracing global and we don't want them always sampled.
  sb.setSampler(Samplers.alwaysSample());

  final io.opencensus.trace.Span span = sb.startSpan();
  span.putAttributes(attributes);
  return new OpenCensusSpanAdapter(span);
}
 
Example #3
Source File: TraceProtoUtils.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
/**
 * Converts {@link TraceConfig} to {@link TraceParams}.
 *
 * @param traceConfigProto {@code TraceConfig}.
 * @param currentTraceParams current {@code TraceParams}.
 * @return updated {@code TraceParams}.
 * @since 0.17
 */
static TraceParams fromTraceConfigProto(
    TraceConfig traceConfigProto, TraceParams currentTraceParams) {
  TraceParams.Builder builder = currentTraceParams.toBuilder();
  if (traceConfigProto.hasConstantSampler()) {
    ConstantSampler constantSampler = traceConfigProto.getConstantSampler();
    ConstantDecision decision = constantSampler.getDecision();
    if (ConstantDecision.ALWAYS_ON.equals(decision)) {
      builder.setSampler(Samplers.alwaysSample());
    } else if (ConstantDecision.ALWAYS_OFF.equals(decision)) {
      builder.setSampler(Samplers.neverSample());
    } // else if (ConstantDecision.ALWAYS_PARENT.equals(decision)) {
    // For ALWAYS_PARENT, don't need to update configs since in Java by default parent sampling
    // decision always takes precedence.
    // }
  } else if (traceConfigProto.hasProbabilitySampler()) {
    builder.setSampler(
        Samplers.probabilitySampler(
            traceConfigProto.getProbabilitySampler().getSamplingProbability()));
  } // TODO: add support for RateLimitingSampler.
  return builder.build();
}
 
Example #4
Source File: TraceProtoUtils.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
/**
 * Converts {@link TraceParams} to {@link TraceConfig}.
 *
 * @param traceParams the {@code TraceParams}.
 * @return {@code TraceConfig}.
 */
static TraceConfig toTraceConfigProto(TraceParams traceParams) {
  TraceConfig.Builder traceConfigProtoBuilder = TraceConfig.newBuilder();
  Sampler librarySampler = traceParams.getSampler();

  if (Samplers.alwaysSample().equals(librarySampler)) {
    traceConfigProtoBuilder.setConstantSampler(
        ConstantSampler.newBuilder().setDecision(ConstantDecision.ALWAYS_ON).build());
  } else if (Samplers.neverSample().equals(librarySampler)) {
    traceConfigProtoBuilder.setConstantSampler(
        ConstantSampler.newBuilder().setDecision(ConstantDecision.ALWAYS_OFF).build());
  } else {
    // TODO: consider exposing the sampling probability of ProbabilitySampler.
    double samplingProbability = parseSamplingProbability(librarySampler);
    traceConfigProtoBuilder.setProbabilitySampler(
        ProbabilitySampler.newBuilder().setSamplingProbability(samplingProbability).build());
  } // TODO: add support for RateLimitingSampler.

  return traceConfigProtoBuilder.build();
}
 
Example #5
Source File: TraceProtoUtilsTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void applyUpdatedConfig() {
  TraceConfig configProto =
      TraceConfig.newBuilder()
          .setProbabilitySampler(
              ProbabilitySampler.newBuilder().setSamplingProbability(0.01).build())
          .build();
  UpdatedLibraryConfig updatedLibraryConfig =
      UpdatedLibraryConfig.newBuilder().setConfig(configProto).build();
  TraceParams traceParams =
      TraceProtoUtils.getUpdatedTraceParams(updatedLibraryConfig, mockTraceConfig);
  TraceParams expectedParams =
      DEFAULT_PARAMS.toBuilder().setSampler(Samplers.probabilitySampler(0.01)).build();
  Mockito.verify(mockTraceConfig, Mockito.times(1)).getActiveTraceParams();
  assertThat(traceParams).isEqualTo(expectedParams);
}
 
Example #6
Source File: MultiSpansTracing.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
/**
 * Main method.
 *
 * @param args the main arguments.
 */
public static void main(String[] args) {

  // WARNING: Be careful before you set sampler value to always sample, especially in
  // production environment. Trace data is often very large in size and is expensive to
  // collect. This is why rather than collecting traces for every request(i.e. alwaysSample),
  // downsampling is prefered.
  //
  // By default, OpenCensus provides a probabilistic sampler that will trace once in every
  // 10,000 requests, that's why if default probabilistic sampler is used
  // you might not see trace data printed or exported and this is expected behavior.

  TraceConfig traceConfig = Tracing.getTraceConfig();
  traceConfig.updateActiveTraceParams(
      traceConfig.getActiveTraceParams().toBuilder().setSampler(Samplers.alwaysSample()).build());

  LoggingTraceExporter.register();
  doWork();

  // Wait for a duration longer than reporting duration (5s) to ensure spans are exported.
  // Spans are exported every 5 seconds
  sleep(5100);
}
 
Example #7
Source File: MultiSpansScopedTracing.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
/**
 * Main method.
 *
 * @param args the main arguments.
 */
public static void main(String[] args) {

  // WARNING: Be careful before you set sampler value to always sample, especially in
  // production environment. Trace data is often very large in size and is expensive to
  // collect. This is why rather than collecting traces for every request(i.e. alwaysSample),
  // downsampling is prefered.
  //
  // By default, OpenCensus provides a probabilistic sampler that will trace once in every
  // 10,000 requests, that's why if default probabilistic sampler is used
  // you might not see trace data printed or exported and this is expected behavior.

  TraceConfig traceConfig = Tracing.getTraceConfig();
  traceConfig.updateActiveTraceParams(
      traceConfig.getActiveTraceParams().toBuilder().setSampler(Samplers.alwaysSample()).build());

  LoggingTraceExporter.register();
  try (Scope ss = tracer.spanBuilderWithExplicitParent("MyRootSpan", null).startScopedSpan()) {
    doWork();
  }

  // Wait for a duration longer than reporting duration (5s) to ensure spans are exported.
  // Spans are exported every 5 seconds
  sleep(5100);
}
 
Example #8
Source File: HttpServletFilterIntegrationTests.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Before
@Override
public void setup() {
  super.setup();
  handler = new TestHandler();

  SpanExporter exporter = Tracing.getExportComponent().getSpanExporter();
  exporter.registerHandler("testing", handler);

  TraceParams params =
      Tracing.getTraceConfig()
          .getActiveTraceParams()
          .toBuilder()
          .setSampler(Samplers.alwaysSample())
          .build();
  Tracing.getTraceConfig().updateActiveTraceParams(params);
}
 
Example #9
Source File: HelloWorldClient.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
/** Say hello to server. */
public void greet(String name) {
  logger.info("Will try to greet " + name + " ...");
  HelloRequest request = HelloRequest.newBuilder().setName(name).build();
  HelloReply response;

  SpanBuilder spanBuilder =
      tracer.spanBuilder("client").setRecordEvents(true).setSampler(Samplers.alwaysSample());
  try (Scope scope = spanBuilder.startScopedSpan()) {
    tracer.getCurrentSpan().addAnnotation("Saying Hello to Server.");
    response = blockingStub.sayHello(request);
    tracer.getCurrentSpan().addAnnotation("Received response from Server.");
  } catch (StatusRuntimeException e) {
    tracer
        .getCurrentSpan()
        .setStatus(
            CanonicalCode.valueOf(e.getStatus().getCode().name())
                .toStatus()
                .withDescription(e.getMessage()));
    logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
    return;
  }
  logger.info("Greeting: " + response.getMessage());
}
 
Example #10
Source File: TraceParamsTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void updateTraceParams_All() {
  TraceParams traceParams =
      TraceParams.DEFAULT
          .toBuilder()
          .setSampler(Samplers.alwaysSample())
          .setMaxNumberOfAttributes(8)
          .setMaxNumberOfAnnotations(9)
          .setMaxNumberOfMessageEvents(10)
          .setMaxNumberOfLinks(11)
          .build();
  assertThat(traceParams.getSampler()).isEqualTo(Samplers.alwaysSample());
  assertThat(traceParams.getMaxNumberOfAttributes()).isEqualTo(8);
  assertThat(traceParams.getMaxNumberOfAnnotations()).isEqualTo(9);
  // test maxNumberOfNetworkEvent can be set via maxNumberOfMessageEvent
  assertThat(traceParams.getMaxNumberOfNetworkEvents()).isEqualTo(10);
  assertThat(traceParams.getMaxNumberOfMessageEvents()).isEqualTo(10);
  assertThat(traceParams.getMaxNumberOfLinks()).isEqualTo(11);
}
 
Example #11
Source File: TraceConfigImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void updateTraceParams() {
  TraceParams traceParams =
      TraceParams.DEFAULT
          .toBuilder()
          .setSampler(Samplers.alwaysSample())
          .setMaxNumberOfAttributes(8)
          .setMaxNumberOfAnnotations(9)
          .setMaxNumberOfNetworkEvents(10)
          .setMaxNumberOfLinks(11)
          .build();
  traceConfig.updateActiveTraceParams(traceParams);
  assertThat(traceConfig.getActiveTraceParams()).isEqualTo(traceParams);
  traceConfig.updateActiveTraceParams(TraceParams.DEFAULT);
  assertThat(traceConfig.getActiveTraceParams()).isEqualTo(TraceParams.DEFAULT);
}
 
Example #12
Source File: SpanBuilderImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void startChildSpan_SampledLinkedParent() {
  Span rootSpanUnsampled =
      SpanBuilderImpl.createWithParent(SPAN_NAME, null, spanBuilderOptions)
          .setSampler(Samplers.neverSample())
          .startSpan();
  assertThat(rootSpanUnsampled.getContext().getTraceOptions().isSampled()).isFalse();
  Span rootSpanSampled =
      SpanBuilderImpl.createWithParent(SPAN_NAME, null, spanBuilderOptions)
          .setSampler(Samplers.alwaysSample())
          .startSpan();
  assertThat(rootSpanSampled.getContext().getTraceOptions().isSampled()).isTrue();
  // Sampled because the linked parent is sampled.
  Span childSpan =
      SpanBuilderImpl.createWithParent(SPAN_NAME, rootSpanUnsampled, spanBuilderOptions)
          .setParentLinks(Collections.singletonList(rootSpanSampled))
          .startSpan();
  assertThat(childSpan.getContext().isValid()).isTrue();
  assertThat(childSpan.getContext().getTraceId())
      .isEqualTo(rootSpanUnsampled.getContext().getTraceId());
  assertThat(childSpan.getContext().getTraceOptions().isSampled()).isTrue();
}
 
Example #13
Source File: SpanBuilderImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void startChildSpan_WithSpecifiedSampler() {
  Span rootSpan =
      SpanBuilderImpl.createWithParent(SPAN_NAME, null, spanBuilderOptions)
          .setSampler(Samplers.alwaysSample())
          .startSpan();
  assertThat(rootSpan.getContext().isValid()).isTrue();
  assertThat(rootSpan.getContext().getTraceOptions().isSampled()).isTrue();
  // Apply the given sampler for child spans.
  Span childSpan =
      SpanBuilderImpl.createWithParent(SPAN_NAME, rootSpan, spanBuilderOptions)
          .setSampler(Samplers.neverSample())
          .startSpan();
  assertThat(childSpan.getContext().isValid()).isTrue();
  assertThat(childSpan.getContext().getTraceId()).isEqualTo(rootSpan.getContext().getTraceId());
  assertThat(childSpan.getContext().getTraceOptions().isSampled()).isFalse();
}
 
Example #14
Source File: SpanBuilderImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void startRemoteChildSpan_WithoutSpecifiedSampler() {
  Span rootSpan =
      SpanBuilderImpl.createWithParent(SPAN_NAME, null, spanBuilderOptions)
          .setSampler(Samplers.neverSample())
          .startSpan();
  assertThat(rootSpan.getContext().isValid()).isTrue();
  assertThat(rootSpan.getContext().getTraceOptions().isSampled()).isFalse();
  // Apply default sampler (always true in the tests) for spans with remote parent.
  Span childSpan =
      SpanBuilderImpl.createWithRemoteParent(SPAN_NAME, rootSpan.getContext(), spanBuilderOptions)
          .startSpan();
  assertThat(childSpan.getContext().isValid()).isTrue();
  assertThat(childSpan.getContext().getTraceId()).isEqualTo(rootSpan.getContext().getTraceId());
  assertThat(childSpan.getContext().getTraceOptions().isSampled()).isTrue();
}
 
Example #15
Source File: SpanBuilderImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void startRemoteChildSpan_WithSpecifiedSampler() {
  Span rootSpan =
      SpanBuilderImpl.createWithParent(SPAN_NAME, null, spanBuilderOptions)
          .setSampler(Samplers.alwaysSample())
          .startSpan();
  assertThat(rootSpan.getContext().isValid()).isTrue();
  assertThat(rootSpan.getContext().getTraceOptions().isSampled()).isTrue();
  // Apply given sampler before default sampler for spans with remote parent.
  Span childSpan =
      SpanBuilderImpl.createWithRemoteParent(SPAN_NAME, rootSpan.getContext(), spanBuilderOptions)
          .setSampler(Samplers.neverSample())
          .startSpan();
  assertThat(childSpan.getContext().isValid()).isTrue();
  assertThat(childSpan.getContext().getTraceId()).isEqualTo(rootSpan.getContext().getTraceId());
  assertThat(childSpan.getContext().getTraceOptions().isSampled()).isFalse();
}
 
Example #16
Source File: SpanBuilderImplTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void startSpanIncreaseNumberOfChildren() {
  RecordEventsSpanImpl parent =
      (RecordEventsSpanImpl)
          SpanBuilderImpl.createWithParent(SPAN_NAME, null, spanBuilderOptions)
              .setSampler(Samplers.alwaysSample())
              .startSpan();
  assertThat(parent.getContext().getTraceOptions().isSampled()).isTrue();
  assertThat(parent.toSpanData().getChildSpanCount()).isEqualTo(0);
  RecordEventsSpanImpl span =
      (RecordEventsSpanImpl)
          SpanBuilderImpl.createWithParent(SPAN_NAME, parent, spanBuilderOptions)
              .setSampler(Samplers.alwaysSample())
              .startSpan();
  assertThat(span.getContext().getTraceOptions().isSampled()).isTrue();
  assertThat(span.toSpanData().getChildSpanCount()).isEqualTo(0);
  assertThat(parent.toSpanData().getChildSpanCount()).isEqualTo(1);
  span =
      (RecordEventsSpanImpl)
          SpanBuilderImpl.createWithParent(SPAN_NAME, parent, spanBuilderOptions)
              .setSampler(Samplers.alwaysSample())
              .startSpan();
  assertThat(span.getContext().getTraceOptions().isSampled()).isTrue();
  assertThat(span.toSpanData().getChildSpanCount()).isEqualTo(0);
  assertThat(parent.toSpanData().getChildSpanCount()).isEqualTo(2);
}
 
Example #17
Source File: TraceProtoUtilsTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void toTraceConfigProto_AlwaysSampler() {
  assertThat(TraceProtoUtils.toTraceConfigProto(getTraceParams(Samplers.alwaysSample())))
      .isEqualTo(
          TraceConfig.newBuilder()
              .setConstantSampler(
                  ConstantSampler.newBuilder().setDecision(ConstantDecision.ALWAYS_ON).build())
              .build());
}
 
Example #18
Source File: CensusSpringAspectTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
  handler = new TestHandler();

  SpanExporter exporter = Tracing.getExportComponent().getSpanExporter();
  exporter.registerHandler("testing", handler);

  TraceParams params =
      Tracing.getTraceConfig()
          .getActiveTraceParams()
          .toBuilder()
          .setSampler(Samplers.alwaysSample())
          .build();
  Tracing.getTraceConfig().updateActiveTraceParams(params);
}
 
Example #19
Source File: TraceWebAsyncClientAutoConfigurationTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
  handler = new TestHandler();

  SpanExporter exporter = Tracing.getExportComponent().getSpanExporter();
  exporter.registerHandler("testing", handler);

  TraceParams params =
      Tracing.getTraceConfig()
          .getActiveTraceParams()
          .toBuilder()
          .setSampler(Samplers.alwaysSample())
          .build();
  Tracing.getTraceConfig().updateActiveTraceParams(params);
}
 
Example #20
Source File: TraceConfigzZPageHandler.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private void maybeApplyChanges(Map<String, String> queryMap) {
  String changeStr = queryMap.get(CHANGE);
  if (PERMANENT_CHANGE.equals(changeStr)) {
    TraceParams.Builder traceParamsBuilder = traceConfig.getActiveTraceParams().toBuilder();
    String samplingProbabilityStr = queryMap.get(QUERY_COMPONENT_SAMPLING_PROBABILITY);
    if (!isNullOrEmpty(samplingProbabilityStr)) {
      double samplingProbability = Double.parseDouble(samplingProbabilityStr);
      traceParamsBuilder.setSampler(Samplers.probabilitySampler(samplingProbability));
    }
    String maxNumberOfAttributesStr = queryMap.get(QUERY_COMPONENT_MAX_NUMBER_OF_ATTRIBUTES);
    if (!isNullOrEmpty(maxNumberOfAttributesStr)) {
      int maxNumberOfAttributes = Integer.parseInt(maxNumberOfAttributesStr);
      traceParamsBuilder.setMaxNumberOfAttributes(maxNumberOfAttributes);
    }
    String maxNumberOfAnnotationsStr = queryMap.get(QUERY_COMPONENT_MAX_NUMBER_OF_ANNOTATIONS);
    if (!isNullOrEmpty(maxNumberOfAnnotationsStr)) {
      int maxNumberOfAnnotations = Integer.parseInt(maxNumberOfAnnotationsStr);
      traceParamsBuilder.setMaxNumberOfAnnotations(maxNumberOfAnnotations);
    }
    String maxNumberOfNetworkEventsStr =
        queryMap.get(QUERY_COMPONENT_MAX_NUMBER_OF_NETWORK_EVENTS);
    if (!isNullOrEmpty(maxNumberOfNetworkEventsStr)) {
      int maxNumberOfNetworkEvents = Integer.parseInt(maxNumberOfNetworkEventsStr);
      traceParamsBuilder.setMaxNumberOfNetworkEvents(maxNumberOfNetworkEvents);
    }
    String maxNumverOfLinksStr = queryMap.get(QUERY_COMPONENT_MAX_NUMBER_OF_LINKS);
    if (!isNullOrEmpty(maxNumverOfLinksStr)) {
      int maxNumberOfLinks = Integer.parseInt(maxNumverOfLinksStr);
      traceParamsBuilder.setMaxNumberOfLinks(maxNumberOfLinks);
    }
    traceConfig.updateActiveTraceParams(traceParamsBuilder.build());
  } else if (RESTORE_DEFAULT_CHANGE.equals(changeStr)) {
    traceConfig.updateActiveTraceParams(TraceParams.DEFAULT);
  }
}
 
Example #21
Source File: TraceStrategyImpl.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@MustBeClosed
@Override
public Closeable startScopedSpan(String spanName) {
  checkNotNull(spanName, "spanName");

  return Tracing.getTracer()
      .spanBuilder(spanName)
      .setSampler(Samplers.alwaysSample())
      .setRecordEvents(true)
      .startScopedSpan();
}
 
Example #22
Source File: TraceProtoUtilsTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void fromTraceConfigProto_NeverSampler() {
  TraceConfig traceConfig =
      TraceConfig.newBuilder()
          .setConstantSampler(
              ConstantSampler.newBuilder().setDecision(ConstantDecision.ALWAYS_OFF).build())
          .build();
  assertThat(TraceProtoUtils.fromTraceConfigProto(traceConfig, DEFAULT_PARAMS).getSampler())
      .isEqualTo(Samplers.neverSample());
}
 
Example #23
Source File: TraceProtoUtilsTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void fromTraceConfigProto_ProbabilitySampler() {
  TraceConfig traceConfig =
      TraceConfig.newBuilder()
          .setProbabilitySampler(
              ProbabilitySampler.newBuilder().setSamplingProbability(0.01).build())
          .build();
  assertThat(TraceProtoUtils.fromTraceConfigProto(traceConfig, DEFAULT_PARAMS).getSampler())
      .isEqualTo(Samplers.probabilitySampler(0.01));
}
 
Example #24
Source File: SpanBuilderImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void startRootSpan_WithSpecifiedSampler() {
  // Apply given sampler before default sampler for root spans.
  Span rootSpan =
      SpanBuilderImpl.createWithParent(SPAN_NAME, null, spanBuilderOptions)
          .setSampler(Samplers.neverSample())
          .startSpan();
  assertThat(rootSpan.getContext().isValid()).isTrue();
  assertThat(rootSpan.getContext().getTraceOptions().isSampled()).isFalse();
}
 
Example #25
Source File: SpanBuilderImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void startSpanNullParentNoRecordOptions() {
  Span span =
      SpanBuilderImpl.createWithParent(SPAN_NAME, null, spanBuilderOptions)
          .setSampler(Samplers.neverSample())
          .startSpan();
  assertThat(span.getContext().isValid()).isTrue();
  assertThat(span.getOptions().contains(Options.RECORD_EVENTS)).isFalse();
  assertThat(span.getContext().getTraceOptions().isSampled()).isFalse();
}
 
Example #26
Source File: SpanBuilderImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void startSpanNullParentWithRecordEvents() {
  RecordEventsSpanImpl span =
      (RecordEventsSpanImpl)
          SpanBuilderImpl.createWithParent(SPAN_NAME, null, spanBuilderOptions)
              .setSampler(Samplers.neverSample())
              .setRecordEvents(true)
              .startSpan();
  assertThat(span.getContext().isValid()).isTrue();
  assertThat(span.getOptions().contains(Options.RECORD_EVENTS)).isTrue();
  assertThat(span.getContext().getTraceOptions().isSampled()).isFalse();
  SpanData spanData = span.toSpanData();
  assertThat(spanData.getParentSpanId()).isNull();
  assertThat(spanData.getHasRemoteParent()).isNull();
}
 
Example #27
Source File: BasicOperationsBenchmark.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
/** Create a root span. */
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public Span createRootSpan(Data data) {
  Span span =
      data.tracer
          .spanBuilderWithExplicitParent("RootSpan", null)
          .setRecordEvents(data.recorded)
          .setSampler(data.sampled ? Samplers.alwaysSample() : Samplers.neverSample())
          .startSpan();
  span.end();
  return span;
}
 
Example #28
Source File: SpanOperationsBenchmark.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
private Span createSpan(String suffix) {
  return tracer
      .spanBuilderWithExplicitParent(SPAN_NAME + suffix, null)
      .setRecordEvents(recorded)
      .setSampler(sampled ? Samplers.alwaysSample() : Samplers.neverSample())
      .startSpan();
}
 
Example #29
Source File: BasicOperationsBenchmark.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
/** Create a child span from the current span. */
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public Span createSpanWithCurrentSpan(Data data) {
  Span span =
      data.tracer
          .spanBuilder("ChildSpanFromCurrent")
          .setRecordEvents(data.recorded)
          .setSampler(data.sampled ? Samplers.alwaysSample() : Samplers.neverSample())
          .startSpan();
  span.end();
  return span;
}
 
Example #30
Source File: SpanBuilderImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void startChildSpan_WithoutSpecifiedSampler() {
  Span rootSpan =
      SpanBuilderImpl.createWithParent(SPAN_NAME, null, spanBuilderOptions)
          .setSampler(Samplers.neverSample())
          .startSpan();
  assertThat(rootSpan.getContext().isValid()).isTrue();
  assertThat(rootSpan.getContext().getTraceOptions().isSampled()).isFalse();
  // Don't apply the default sampler (always true) for child spans.
  Span childSpan =
      SpanBuilderImpl.createWithParent(SPAN_NAME, rootSpan, spanBuilderOptions).startSpan();
  assertThat(childSpan.getContext().isValid()).isTrue();
  assertThat(childSpan.getContext().getTraceId()).isEqualTo(rootSpan.getContext().getTraceId());
  assertThat(childSpan.getContext().getTraceOptions().isSampled()).isFalse();
}