internal static bool ShouldSample(SpanContext parentContext, string name, ISampler sampler, out SpanContext context, out SpanId parentSpanId) { var traceId = TraceId.Invalid; var tracestate = Tracestate.Empty; parentSpanId = SpanId.Invalid; var parentOptions = TraceOptions.Default; if (parentContext.IsValid) { traceId = parentContext.TraceId; parentSpanId = parentContext.SpanId; parentOptions = parentContext.TraceOptions; } else { traceId = TraceId.FromBytes(Guid.NewGuid().ToByteArray()); } var result = parentOptions.IsSampled; var spanId = SpanId.FromBytes(Guid.NewGuid().ToByteArray(), 8); var traceOptions = TraceOptions.Default; if (sampler != null) { var builder = TraceOptions.Builder(parentContext.TraceOptions); result = sampler.ShouldSample(parentContext, traceId, spanId, name, null); builder = builder.SetIsSampled(result); traceOptions = builder.Build(); } context = SpanContext.Create(traceId, spanId, traceOptions, parentContext.Tracestate); return(result); }
private static bool MakeSamplingDecision( SpanContext parent, string name, ISampler sampler, IEnumerable <ISpan> parentLinks, ITraceId traceId, ISpanId spanId, ITraceParams activeTraceParams) { // If users set a specific sampler in the SpanBuilder, use it. if (sampler != null) { return(sampler.ShouldSample(parent, 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 (parent == null || !parent.IsValid) { return(activeTraceParams .Sampler .ShouldSample(parent, traceId, spanId, name, parentLinks)); } // Parent is always different than null because otherwise we use the default sampler. return(parent.TraceOptions.IsSampled || IsAnyParentLinkSampled(parentLinks)); }
private static bool MakeSamplingDecision( SpanContext parent, string name, ISampler sampler, IEnumerable <Link> parentLinks, ActivityTraceId traceId, ActivitySpanId spanId, TracerConfiguration tracerConfiguration) { // If users set a specific sampler in the SpanBuilder, use it. if (sampler != null) { return(sampler.ShouldSample(parent, traceId, spanId, name, parentLinks).IsSampled); } // Use the default sampler if this is a root Span or this is an entry point Span (has remote // parent). if (parent == null || !parent.IsValid) { return(tracerConfiguration .Sampler .ShouldSample(parent, traceId, spanId, name, parentLinks).IsSampled); } // Parent is always different than null because otherwise we use the default sampler. return((parent.TraceOptions & ActivityTraceFlags.Recorded) != 0 || IsAnyParentLinkSampled(parentLinks)); }
// Applies the given sampler to NUM_SAMPLE_TRIES random traceId/spanId pairs. private static void AssertSamplerSamplesWithProbability( ISampler sampler, ISpanContext parent, IList <ISpan> parentLinks, double probability) { RandomGenerator random = new RandomGenerator(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. Assert.True(proportionSampled <probability + 0.1 && proportionSampled> probability - 0.1); }
public ISpan Start(ISpanBuilder spanBuilder) { if (spanBuilder == null) { throw new ArgumentNullException(nameof(spanBuilder)); } var traceId = spanBuilder.References?.FirstOrDefault()?.SpanContext?.TraceId ?? Guid.NewGuid().ToString(); var spanId = Guid.NewGuid().ToString(); var baggage = new Baggage(); if (spanBuilder.References != null) { foreach (var reference in spanBuilder.References) { baggage.Merge(reference.SpanContext.Baggage); } } var sampled = spanBuilder.Sampled ?? _sampler?.ShouldSample(); var spanContext = _spanContextFactory.Create(new SpanContextPackage(traceId, spanId, sampled.GetValueOrDefault(true), baggage, spanBuilder.References)); return(new Span(spanBuilder.OperationName, spanBuilder.StartTimestamp ?? DateTimeOffset.UtcNow, spanContext, _spanRecorder)); }
public Decision ShouldSample(SpanContext parentContext, ActivityTraceId traceId, ActivitySpanId spanId, string name, IEnumerable <Link> links) { if (name == "/health") { return(new Decision(false)); } return(_sampler.ShouldSample(parentContext, traceId, spanId, name, links)); }
// Applies the given sampler to NUM_SAMPLE_TRIES random traceId/spanId pairs. private static void AssertSamplerSamplesWithProbability( ISampler sampler, SpanContext parent, List <ILink> links, double probability) { var count = 0; // Count of spans with sampling enabled for (var i = 0; i < NUM_SAMPLE_TRIES; i++) { if (sampler.ShouldSample( parent, ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), SPAN_NAME, links).IsSampled) { count++; } } var proportionSampled = (double)count / NUM_SAMPLE_TRIES; // Allow for a large amount of slop (+/- 10%) in number of sampled traces, to avoid flakiness. Assert.True(proportionSampled <probability + 0.1 && proportionSampled> probability - 0.1); }
internal static bool ShouldSample(SpanContext parentContext, string name, ISampler sampler, out SpanContext context, out ActivitySpanId parentSpanId) { ActivityTraceId traceId = default; var tracestate = Tracestate.Empty; parentSpanId = default; var parentOptions = ActivityTraceFlags.None; if (parentContext.IsValid) { traceId = parentContext.TraceId; parentSpanId = parentContext.SpanId; parentOptions = parentContext.TraceOptions; } else { traceId = ActivityTraceId.CreateRandom(); } var result = (parentOptions & ActivityTraceFlags.Recorded) != 0; var spanId = ActivitySpanId.CreateRandom(); var traceOptions = ActivityTraceFlags.None; if (sampler != null) { traceOptions = parentContext.TraceOptions; result = sampler.ShouldSample(parentContext, traceId, spanId, name, null); if (result) { traceOptions |= ActivityTraceFlags.Recorded; } } context = SpanContext.Create(traceId, spanId, traceOptions, parentContext.Tracestate); return(result); }
public void ProbabilitySampler_SampleBasedOnTraceId() { ISampler defaultProbability = ProbabilitySampler.Create(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; ITraceId notSampledtraceId = TraceId.FromBytes( new byte[] { 0x8F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0, }); Assert.False( defaultProbability.ShouldSample( null, notSampledtraceId, SpanId.GenerateRandomId(random), SPAN_NAME, new List <ISpan>())); // This traceId will be sampled by the ProbabilitySampler because the first 8 bytes as long // is less than probability * Long.MAX_VALUE; ITraceId sampledtraceId = TraceId.FromBytes( new byte[] { 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0, }); Assert.True( defaultProbability.ShouldSample( null, sampledtraceId, SpanId.GenerateRandomId(random), SPAN_NAME, new List <ISpan>())); }
public void ProbabilitySampler_SampleBasedOnTraceId() { ISampler defaultProbability = ProbabilitySampler.Create(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; var notSampledtraceId = ActivityTraceId.CreateFromBytes( new byte[] { 0x8F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0, }); Assert.False( defaultProbability.ShouldSample( null, notSampledtraceId, ActivitySpanId.CreateRandom(), SPAN_NAME, null).IsSampled); // This traceId will be sampled by the ProbabilitySampler because the first 8 bytes as long // is less than probability * Long.MAX_VALUE; var sampledtraceId = ActivityTraceId.CreateFromBytes( new byte[] { 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0, }); Assert.True( defaultProbability.ShouldSample( null, sampledtraceId, ActivitySpanId.CreateRandom(), SPAN_NAME, null).IsSampled); }
public ISpan Start(ISpanBuilder spanBuilder) { if (spanBuilder == null) { throw new ArgumentNullException(nameof(spanBuilder)); } var traceId = spanBuilder.References?.FirstOrDefault()?.SpanContext?.TraceId ?? Guid.NewGuid().ToString(); var spanId = Guid.NewGuid().ToString(); Baggage baggage = new Baggage(); foreach (var reference in spanBuilder.References) { baggage.Merge(reference.SpanContext.Baggage); } var spanContext = _spanContextFactory.Create(new SpanContextPackage(traceId, spanId, _sampler.ShouldSample(), baggage)); return(new Span(spanBuilder.OperationName, spanContext, _spanQueue)); }