Search in sources :

Example 1 with Sampler

use of io.opencensus.trace.Sampler in project instrumentation-java by census-instrumentation.

the class SamplersTest method probabilitySampler_DifferentProbabilities_SampledParent.

@Test
public void probabilitySampler_DifferentProbabilities_SampledParent() {
    final Sampler neverSample = Samplers.probabilitySampler(0.0);
    assertSamplerSamplesWithProbability(neverSample, sampledSpanContext, Collections.<Span>emptyList(), 1.0);
    final Sampler alwaysSample = Samplers.probabilitySampler(1.0);
    assertSamplerSamplesWithProbability(alwaysSample, sampledSpanContext, Collections.<Span>emptyList(), 1.0);
    final Sampler fiftyPercentSample = Samplers.probabilitySampler(0.5);
    assertSamplerSamplesWithProbability(fiftyPercentSample, sampledSpanContext, Collections.<Span>emptyList(), 1.0);
    final Sampler twentyPercentSample = Samplers.probabilitySampler(0.2);
    assertSamplerSamplesWithProbability(twentyPercentSample, sampledSpanContext, Collections.<Span>emptyList(), 1.0);
    final Sampler twoThirdsSample = Samplers.probabilitySampler(2.0 / 3.0);
    assertSamplerSamplesWithProbability(twoThirdsSample, sampledSpanContext, Collections.<Span>emptyList(), 1.0);
}
Also used : Sampler(io.opencensus.trace.Sampler) Test(org.junit.Test)

Example 2 with Sampler

use of io.opencensus.trace.Sampler in project instrumentation-java by census-instrumentation.

the class SamplersTest method probabilitySampler_DifferentProbabilities_NotSampledParent.

@Test
public void probabilitySampler_DifferentProbabilities_NotSampledParent() {
    final Sampler neverSample = Samplers.probabilitySampler(0.0);
    assertSamplerSamplesWithProbability(neverSample, notSampledSpanContext, Collections.<Span>emptyList(), 0.0);
    final Sampler alwaysSample = Samplers.probabilitySampler(1.0);
    assertSamplerSamplesWithProbability(alwaysSample, notSampledSpanContext, Collections.<Span>emptyList(), 1.0);
    final Sampler fiftyPercentSample = Samplers.probabilitySampler(0.5);
    assertSamplerSamplesWithProbability(fiftyPercentSample, notSampledSpanContext, Collections.<Span>emptyList(), 0.5);
    final Sampler twentyPercentSample = Samplers.probabilitySampler(0.2);
    assertSamplerSamplesWithProbability(twentyPercentSample, notSampledSpanContext, Collections.<Span>emptyList(), 0.2);
    final Sampler twoThirdsSample = Samplers.probabilitySampler(2.0 / 3.0);
    assertSamplerSamplesWithProbability(twoThirdsSample, notSampledSpanContext, Collections.<Span>emptyList(), 2.0 / 3.0);
}
Also used : Sampler(io.opencensus.trace.Sampler) Test(org.junit.Test)

Example 3 with Sampler

use of io.opencensus.trace.Sampler in project instrumentation-java by census-instrumentation.

the class TraceProtoUtils method toTraceConfigProto.

/**
 * 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());
    }
    return traceConfigProtoBuilder.build();
}
Also used : ConstantSampler(io.opencensus.proto.trace.v1.ConstantSampler) ProbabilitySampler(io.opencensus.proto.trace.v1.ProbabilitySampler) Sampler(io.opencensus.trace.Sampler) TraceConfig(io.opencensus.proto.trace.v1.TraceConfig)

Example 4 with Sampler

use of io.opencensus.trace.Sampler in project instrumentation-java by census-instrumentation.

the class SamplersTest method probabilitySampler_SampleBasedOnTraceId.

@Test
public void probabilitySampler_SampleBasedOnTraceId() {
    final Sampler defaultProbability = Samplers.probabilitySampler(0.0001);
    // This traceId will not be sampled by the ProbabilitySampler because the first 8 bytes as long
    // is not less than probability * Long.MAX_VALUE;
    TraceId notSampledtraceId = TraceId.fromBytes(new byte[] { (byte) 0x8F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0, 0, 0, 0, 0, 0, 0, 0 });
    assertThat(defaultProbability.shouldSample(null, false, notSampledtraceId, SpanId.generateRandomId(random), SPAN_NAME, Collections.<Span>emptyList())).isFalse();
    // This traceId will be sampled by the ProbabilitySampler because the first 8 bytes as long
    // is less than probability * Long.MAX_VALUE;
    TraceId sampledtraceId = TraceId.fromBytes(new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0, 0, 0, 0, 0, 0, 0, 0 });
    assertThat(defaultProbability.shouldSample(null, false, sampledtraceId, SpanId.generateRandomId(random), SPAN_NAME, Collections.<Span>emptyList())).isTrue();
}
Also used : Sampler(io.opencensus.trace.Sampler) TraceId(io.opencensus.trace.TraceId) Test(org.junit.Test)

Example 5 with Sampler

use of io.opencensus.trace.Sampler in project instrumentation-java by census-instrumentation.

the class SamplersTest method probabilitySampler_DifferentProbabilities_SampledParentLink.

@Test
public void probabilitySampler_DifferentProbabilities_SampledParentLink() {
    final Sampler neverSample = Samplers.probabilitySampler(0.0);
    assertSamplerSamplesWithProbability(neverSample, notSampledSpanContext, Arrays.asList(sampledSpan), 1.0);
    final Sampler alwaysSample = Samplers.probabilitySampler(1.0);
    assertSamplerSamplesWithProbability(alwaysSample, notSampledSpanContext, Arrays.asList(sampledSpan), 1.0);
    final Sampler fiftyPercentSample = Samplers.probabilitySampler(0.5);
    assertSamplerSamplesWithProbability(fiftyPercentSample, notSampledSpanContext, Arrays.asList(sampledSpan), 1.0);
    final Sampler twentyPercentSample = Samplers.probabilitySampler(0.2);
    assertSamplerSamplesWithProbability(twentyPercentSample, notSampledSpanContext, Arrays.asList(sampledSpan), 1.0);
    final Sampler twoThirdsSample = Samplers.probabilitySampler(2.0 / 3.0);
    assertSamplerSamplesWithProbability(twoThirdsSample, notSampledSpanContext, Arrays.asList(sampledSpan), 1.0);
}
Also used : Sampler(io.opencensus.trace.Sampler) Test(org.junit.Test)

Aggregations

Sampler (io.opencensus.trace.Sampler)5 Test (org.junit.Test)4 ConstantSampler (io.opencensus.proto.trace.v1.ConstantSampler)1 ProbabilitySampler (io.opencensus.proto.trace.v1.ProbabilitySampler)1 TraceConfig (io.opencensus.proto.trace.v1.TraceConfig)1 TraceId (io.opencensus.trace.TraceId)1