private void RunSamplerTest( ISampler sampler, int iterations, float expectedAutoKeepRate, float acceptableVariancePercent) { var sampleSize = iterations; var autoKeeps = 0; int seed = new Random().Next(); var idGenerator = new SpanIdGenerator(seed); while (sampleSize-- > 0) { var traceId = TraceId.CreateRandom(); var span = GetMyServiceSpan(traceId); var priority = sampler.GetSamplingPriority(span); if (priority == SamplingPriority.AutoKeep) { autoKeeps++; } } var autoKeepRate = autoKeeps / (float)iterations; var lowerLimit = expectedAutoKeepRate * (1 - acceptableVariancePercent); var upperLimit = expectedAutoKeepRate * (1 + acceptableVariancePercent); Assert.True( autoKeepRate >= lowerLimit && autoKeepRate <= upperLimit, $"Expected between {lowerLimit} and {upperLimit}, actual rate is {autoKeepRate}. Random generator seeded with {seed}."); }
private SpanContext(ulong?traceId, string serviceName) { TraceId = traceId > 0 ? traceId.Value : SpanIdGenerator.CreateNew(); ServiceName = serviceName; }
/// <summary> /// Initializes a new instance of the <see cref="SpanContext"/> class /// that is the child of the specified parent context. /// </summary> /// <param name="parent">The parent context.</param> /// <param name="traceContext">The trace context.</param> /// <param name="serviceName">The service name to propagate to child spans.</param> internal SpanContext(ISpanContext parent, ITraceContext traceContext, string serviceName) : this(parent?.TraceId, serviceName) { SpanId = SpanIdGenerator.CreateNew(); Parent = parent; TraceContext = traceContext; if (parent is SpanContext spanContext) { Origin = spanContext.Origin; } }
private void RunSamplerTest( ISampler sampler, int iterations, float expectedAutoKeepRate, float expectedUserKeepRate, float acceptableVariancePercent) { var sampleSize = iterations; var autoKeeps = 0; var userKeeps = 0; int seed = new Random().Next(); var idGenerator = new SpanIdGenerator(seed); while (sampleSize-- > 0) { var traceId = idGenerator.CreateNew(); var span = GetMyServiceSpan(traceId); var priority = sampler.GetSamplingPriority(span); if (priority == SamplingPriorityValues.AutoKeep) { autoKeeps++; } else if (priority == SamplingPriorityValues.UserKeep) { userKeeps++; } } // AUTO_KEEP var autoKeepRate = autoKeeps / (float)iterations; var autoKeepRateLowerLimit = expectedAutoKeepRate * (1 - acceptableVariancePercent); var autoKeepRateUpperLimit = expectedAutoKeepRate * (1 + acceptableVariancePercent); Assert.True( autoKeepRate >= autoKeepRateLowerLimit && autoKeepRate <= autoKeepRateUpperLimit, $"Sampling AUTO_KEEP rate expected between {autoKeepRateLowerLimit} and {autoKeepRateUpperLimit}, actual rate is {autoKeepRate}. Random generator seeded with {seed}."); // USER_KEEP (aka MANUAL_KEEP) var userKeepRate = userKeeps / (float)iterations; var userKeepRateLowerLimit = expectedUserKeepRate * (1 - acceptableVariancePercent); var userKeepRateUpperLimit = expectedUserKeepRate * (1 + acceptableVariancePercent); Assert.True( userKeepRate >= userKeepRateLowerLimit && userKeepRate <= userKeepRateUpperLimit, $"Sampling USER_KEEP rate expected between {userKeepRateLowerLimit} and {userKeepRateUpperLimit}, actual rate is {userKeepRate}. Random generator seeded with {seed}."); }