io.opencensus.trace.Sampler Java Examples
The following examples show how to use
io.opencensus.trace.Sampler.
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: TraceProtoUtils.java From opencensus-java with Apache License 2.0 | 6 votes |
/** * 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 #2
Source File: SpanBuilderImpl.java From opencensus-java with Apache License 2.0 | 6 votes |
private static boolean makeSamplingDecision( @Nullable SpanContext parent, @Nullable Boolean hasRemoteParent, String name, @Nullable Sampler sampler, List<Span> parentLinks, TraceId traceId, SpanId spanId, TraceParams activeTraceParams) { // If users set a specific sampler in the SpanBuilder, use it. if (sampler != null) { return sampler.shouldSample(parent, hasRemoteParent, traceId, spanId, name, parentLinks); } // Use the default sampler if this is a root Span or this is an entry point Span (has remote // parent). if (Boolean.TRUE.equals(hasRemoteParent) || parent == null || !parent.isValid()) { return activeTraceParams .getSampler() .shouldSample(parent, hasRemoteParent, traceId, spanId, name, parentLinks); } // Parent is always different than null because otherwise we use the default sampler. return parent.getTraceOptions().isSampled() || isAnyParentLinkSampled(parentLinks); }
Example #3
Source File: SamplersTest.java From opencensus-java with Apache License 2.0 | 6 votes |
private static void assertSamplerSamplesWithProbability( Sampler sampler, SpanContext parent, List<Span> parentLinks, double probability) { Random random = new Random(1234); int count = 0; // Count of spans with sampling enabled for (int i = 0; i < NUM_SAMPLE_TRIES; i++) { if (sampler.shouldSample( parent, false, TraceId.generateRandomId(random), SpanId.generateRandomId(random), SPAN_NAME, parentLinks)) { count++; } } double proportionSampled = (double) count / NUM_SAMPLE_TRIES; // Allow for a large amount of slop (+/- 10%) in number of sampled traces, to avoid flakiness. assertThat(proportionSampled < probability + 0.1 && proportionSampled > probability - 0.1) .isTrue(); }
Example #4
Source File: SamplersTest.java From opencensus-java with Apache License 2.0 | 6 votes |
@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); }
Example #5
Source File: SamplersTest.java From opencensus-java with Apache License 2.0 | 6 votes |
@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); }
Example #6
Source File: SamplersTest.java From opencensus-java with Apache License 2.0 | 6 votes |
@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); }
Example #7
Source File: TraceProtoUtils.java From opencensus-java with Apache License 2.0 | 5 votes |
private static double parseSamplingProbability(Sampler sampler) { String description = sampler.getDescription(); // description follows format "ProbabilitySampler{%.6f}", samplingProbability. int leftParenIndex = description.indexOf("{"); int rightParenIndex = description.indexOf("}"); return Double.parseDouble(description.substring(leftParenIndex + 1, rightParenIndex)); }
Example #8
Source File: StatsTestUtils.java From grpc-java with Apache License 2.0 | 4 votes |
@Override public SpanBuilder setSampler(Sampler sampler) { return this; }
Example #9
Source File: StatsTestUtils.java From grpc-nebula-java with Apache License 2.0 | 4 votes |
@Override public SpanBuilder setSampler(Sampler sampler) { return this; }
Example #10
Source File: TraceProtoUtilsTest.java From opencensus-java with Apache License 2.0 | 4 votes |
private static TraceParams getTraceParams(Sampler sampler) { return DEFAULT_PARAMS.toBuilder().setSampler(sampler).build(); }
Example #11
Source File: SpanBuilderImpl.java From opencensus-java with Apache License 2.0 | 4 votes |
@Override public SpanBuilderImpl setSampler(Sampler sampler) { this.sampler = checkNotNull(sampler, "sampler"); return this; }
Example #12
Source File: SpanBuilderImpl.java From opencensus-java with Apache License 2.0 | 4 votes |
private Span startSpanInternal( @Nullable SpanContext parentContext, @Nullable Boolean hasRemoteParent, String name, @Nullable Sampler sampler, List<Span> parentLinks, @Nullable Boolean recordEvents, @Nullable Kind kind, @Nullable Span parentSpan) { TraceParams activeTraceParams = options.traceConfig.getActiveTraceParams(); Random random = options.randomHandler.current(); TraceId traceId; SpanId spanId = SpanId.generateRandomId(random); SpanId parentSpanId = null; // TODO(bdrutu): Handle tracestate correctly not just propagate. Tracestate tracestate = TRACESTATE_DEFAULT; if (parentContext == null || !parentContext.isValid()) { // New root span. traceId = TraceId.generateRandomId(random); // This is a root span so no remote or local parent. hasRemoteParent = null; } else { // New child span. traceId = parentContext.getTraceId(); parentSpanId = parentContext.getSpanId(); tracestate = parentContext.getTracestate(); } TraceOptions traceOptions = makeSamplingDecision( parentContext, hasRemoteParent, name, sampler, parentLinks, traceId, spanId, activeTraceParams) ? SAMPLED_TRACE_OPTIONS : NOT_SAMPLED_TRACE_OPTIONS; if (traceOptions.isSampled() || Boolean.TRUE.equals(recordEvents)) { // Pass the timestamp converter from the parent to ensure that the recorded events are in // the right order. Implementation uses System.nanoTime() which is monotonically increasing. TimestampConverter timestampConverter = null; if (parentSpan instanceof RecordEventsSpanImpl) { RecordEventsSpanImpl parentRecordEventsSpan = (RecordEventsSpanImpl) parentSpan; timestampConverter = parentRecordEventsSpan.getTimestampConverter(); parentRecordEventsSpan.addChild(); } Span span = RecordEventsSpanImpl.startSpan( SpanContext.create(traceId, spanId, traceOptions, tracestate), name, kind, parentSpanId, hasRemoteParent, activeTraceParams, options.startEndHandler, timestampConverter, options.clock); linkSpans(span, parentLinks); return span; } else { return NoRecordEventsSpanImpl.create( SpanContext.create(traceId, spanId, traceOptions, tracestate)); } }
Example #13
Source File: SamplersTest.java From opencensus-java with Apache License 2.0 | 4 votes |
@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(); }
Example #14
Source File: TelemetryUtils.java From meghanada-server with GNU General Public License v3.0 | 4 votes |
private static ScopedSpan startScopedSpan(String name, Sampler sampler) { Scope scope = tracer.spanBuilder(name).setSampler(sampler).startScopedSpan(); return new ScopedSpan(scope); }
Example #15
Source File: TraceParams.java From opencensus-java with Apache License 2.0 | 2 votes |
/** * Sets the global default {@code Sampler}. It must be not {@code null} otherwise {@link * #build()} will throw an exception. * * @param sampler the global default {@code Sampler}. * @return this. * @since 0.5 */ public abstract Builder setSampler(Sampler sampler);
Example #16
Source File: TraceParams.java From opencensus-java with Apache License 2.0 | 2 votes |
/** * Returns the global default {@code Sampler}. Used if no {@code Sampler} is provided in {@link * io.opencensus.trace.SpanBuilder#setSampler(Sampler)}. * * @return the global default {@code Sampler}. * @since 0.5 */ public abstract Sampler getSampler();
Example #17
Source File: Samplers.java From opencensus-java with Apache License 2.0 | 2 votes |
/** * Returns a {@link Sampler} that makes a "yes" decision with a given probability. * * @param probability The desired probability of sampling. Must be within [0.0, 1.0]. * @return a {@code Sampler} that makes a "yes" decision with a given probability. * @throws IllegalArgumentException if {@code probability} is out of range * @since 0.5 */ public static Sampler probabilitySampler(double probability) { return ProbabilitySampler.create(probability); }
Example #18
Source File: Samplers.java From opencensus-java with Apache License 2.0 | 2 votes |
/** * Returns a {@link Sampler} that always makes a "no" decision on {@link Span} sampling. * * @return a {@code Sampler} that always makes a "no" decision on {@code Span} sampling. * @since 0.5 */ public static Sampler neverSample() { return NEVER_SAMPLE; }
Example #19
Source File: Samplers.java From opencensus-java with Apache License 2.0 | 2 votes |
/** * Returns a {@link Sampler} that always makes a "yes" decision on {@link Span} sampling. * * @return a {@code Sampler} that always makes a "yes" decision on {@code Span} sampling. * @since 0.5 */ public static Sampler alwaysSample() { return ALWAYS_SAMPLE; }