public void TracerBuilder_ValidArgs()
        {
            var builder = new TracerBuilder();

            bool processorFactoryCalled = false;
            bool collectorFactoryCalled = true;

            var sampler      = ProbabilitySampler.Create(0.1);
            var exporter     = new TestExporter(_ => { });
            var options      = new TracerConfiguration(sampler, 1, 1, 1);
            var binaryFormat = new BinaryFormat();
            var textFormat   = new TraceContextFormat();

            builder
            .SetSampler(sampler)
            .AddProcessorPipeline(p => p
                                  .SetExporter(exporter)
                                  .SetExportingProcessor(e =>
            {
                processorFactoryCalled = true;
                Assert.Same(e, exporter);
                return(new SimpleSpanProcessor(e));
            }))
            .SetTracerOptions(options)
            .SetBinaryFormat(binaryFormat)
            .SetTextFormat(textFormat)
            .AddCollector(t =>
            {
                Assert.NotNull(t);
                return(new TestCollector(t));
            });

            Assert.Same(sampler, builder.Sampler);

            Assert.NotNull(builder.ProcessingPipelines);
            Assert.Single(builder.ProcessingPipelines);
            Assert.Same(exporter, builder.ProcessingPipelines[0].Exporter);

            Assert.NotNull(builder.ProcessingPipelines[0].Build());
            Assert.True(processorFactoryCalled);

            Assert.Same(options, builder.TracerConfigurationOptions);
            Assert.Same(binaryFormat, builder.BinaryFormat);
            Assert.Same(textFormat, builder.TextFormat);
            Assert.Single(builder.CollectorFactories);

            var collectorFactory = builder.CollectorFactories.Single();

            Assert.Equal(nameof(TestCollector), collectorFactory.Name);
            Assert.Equal("semver:" + typeof(TestCollector).Assembly.GetName().Version, collectorFactory.Version);

            Assert.NotNull(collectorFactory.Factory);
            collectorFactory.Factory(new Tracer(new SimpleSpanProcessor(exporter), options, binaryFormat, textFormat,
                                                Resource.Empty));

            Assert.True(collectorFactoryCalled);
        }
Example #2
0
        public void ProbabilitySampler_DifferentProbabilities_SampledParentLink()
        {
            ISampler neverSample = ProbabilitySampler.Create(0.0);

            AssertSamplerSamplesWithProbability(
                neverSample, notSampledSpanContext, new List <ISpan>()
            {
                sampledSpan
            }, 1.0);
            ISampler alwaysSample = ProbabilitySampler.Create(1.0);

            AssertSamplerSamplesWithProbability(
                alwaysSample, notSampledSpanContext, new List <ISpan>()
            {
                sampledSpan
            }, 1.0);
            ISampler fiftyPercentSample = ProbabilitySampler.Create(0.5);

            AssertSamplerSamplesWithProbability(
                fiftyPercentSample, notSampledSpanContext, new List <ISpan>()
            {
                sampledSpan
            }, 1.0);
            ISampler twentyPercentSample = ProbabilitySampler.Create(0.2);

            AssertSamplerSamplesWithProbability(
                twentyPercentSample, notSampledSpanContext, new List <ISpan>()
            {
                sampledSpan
            }, 1.0);
            ISampler twoThirdsSample = ProbabilitySampler.Create(2.0 / 3.0);

            AssertSamplerSamplesWithProbability(
                twoThirdsSample, notSampledSpanContext, new List <ISpan>()
            {
                sampledSpan
            }, 1.0);
        }
Example #3
0
        public void ProbabilitySampler_ToString()
        {
            var result = ProbabilitySampler.Create(0.5).ToString();

            Assert.Contains($"0{CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator}5", result);
        }
Example #4
0
 public void ProbabilitySampler_getDescription()
 {
     Assert.Equal(String.Format("ProbabilitySampler({0:F6})", 0.5), ProbabilitySampler.Create(0.5).Description);
 }
Example #5
0
        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>()));
        }
Example #6
0
 public void ProbabilitySampler_OutOfRangeLowProbability()
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => ProbabilitySampler.Create(-0.00001));
 }
 /// <summary>
 /// Gets the probability sampler.
 /// </summary>
 /// <param name="probability">Probability to use.</param>
 /// <returns>Sampler that samples with the given probability.</returns>
 public static ISampler GetProbabilitySampler(double probability)
 {
     return(ProbabilitySampler.Create(probability));
 }
 public void ProbabilitySampler_getDescription()
 {
     Assert.Equal($"ProbabilitySampler({0.5:F6})", ProbabilitySampler.Create(0.5).Description);
 }
        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);
        }