コード例 #1
0
        private static TelemetryProcessorChain CreateTelemetryProcessorChainWithSampling(IList <ITelemetry> sentTelemetry, double samplingPercentage, string excludedTypes = null, string includedTypes = null)
        {
            var tc = new TelemetryConfiguration {
                TelemetryChannel = new StubTelemetryChannel()
            };

            tc.InstrumentationKey = Guid.NewGuid().ToString("D");

            var channelBuilder = new TelemetryProcessorChainBuilder(tc);

            channelBuilder.UseSampling(samplingPercentage, excludedTypes, includedTypes);
            channelBuilder.Use(next => new StubTelemetryProcessor(next)
            {
                OnProcess = t => sentTelemetry.Add(t)
            });

            channelBuilder.Build();

            TelemetryProcessorChain processors = tc.TelemetryProcessorChain;

            foreach (ITelemetryProcessor processor in processors.TelemetryProcessors)
            {
                ITelemetryModule m = processor as ITelemetryModule;
                if (m != null)
                {
                    m.Initialize(tc);
                }
            }

            return(processors);
        }
        internal static TelemetryConfiguration CreateTelemetryConfigWithExtractor(IList <ITelemetry> telemetrySentToChannel,
                                                                                  Func <ITelemetryProcessor, AutocollectedMetricsExtractor> extractorFactory)
        {
            ITelemetryChannel channel = new StubTelemetryChannel {
                OnSend = (t) => telemetrySentToChannel.Add(t)
            };
            string iKey = Guid.NewGuid().ToString("D");
            TelemetryConfiguration telemetryConfig = new TelemetryConfiguration(iKey, channel);

            var channelBuilder = new TelemetryProcessorChainBuilder(telemetryConfig);

            channelBuilder.Use(extractorFactory);
            channelBuilder.Build();

            TelemetryProcessorChain processors = telemetryConfig.TelemetryProcessorChain;

            foreach (ITelemetryProcessor processor in processors.TelemetryProcessors)
            {
                ITelemetryModule m = processor as ITelemetryModule;
                if (m != null)
                {
                    m.Initialize(telemetryConfig);
                }
            }


            return(telemetryConfig);
        }
コード例 #3
0
        public void ProactivelySampledOutItemIsNotSent()
        {
            var sentTelemetry = new List <ITelemetry>();
            TelemetryProcessorChain telemetryProcessorChainWithSampling = CreateTelemetryProcessorChainWithSampling(
                sentTelemetry,
                100);
            var sampledOutTelemetry = new RequestTelemetry();

            sampledOutTelemetry.ProactiveSamplingDecision = SamplingDecision.SampledOut;

            telemetryProcessorChainWithSampling.Process(sampledOutTelemetry);
            telemetryProcessorChainWithSampling.Dispose();

            Assert.AreEqual(0, sentTelemetry.Count);
        }
コード例 #4
0
        public void ProactivelySampledOutItemThatIsLaterSampledInIsAddedToTheNextSampledInItem()
        {
            var sentTelemetry = new List <ITelemetry>();
            TelemetryProcessorChain telemetryProcessorChainWithSampling = CreateTelemetryProcessorChainWithSampling(
                sentTelemetry,
                25);

            // Get the random number of sampled out items
            var sampledOutItemsCount = this.random.Next(100) + 100;

            for (int i = 0; i < sampledOutItemsCount; i++)
            {
                var sampledOutTelemetry = new RequestTelemetry();

                // This makes those items proactively sampled out
                sampledOutTelemetry.ProactiveSamplingDecision = SamplingDecision.SampledOut;

                // This operation ID hash is lower than 25, so every item in this batch is sampled in
                sampledOutTelemetry.Context.Operation.Id = "abcdfeghijk";
                telemetryProcessorChainWithSampling.Process(sampledOutTelemetry);
            }

            // No telemetry is recorded
            Assert.AreEqual(0, sentTelemetry.Count);

            // Sampling Rate has changed up
            ((SamplingTelemetryProcessor)telemetryProcessorChainWithSampling.FirstTelemetryProcessor).SamplingPercentage = 50;

            for (int i = 0; i < 100; i++)
            {
                var sampledInTelemetry = new RequestTelemetry();
                telemetryProcessorChainWithSampling.Process(sampledInTelemetry);
            }

            telemetryProcessorChainWithSampling.Dispose();

            // The item that is sampled in will need to represent all sampled out items and itself:
            // 4 * sampledOutItemsCount + 2, where 4 is (100 / 25) and 2 is (100 / 50) as per chosen sample rates
            double expectedSamplingRate = (double)100 / (100 / 25 * sampledOutItemsCount + (100 / 50));

            Assert.AreEqual(expectedSamplingRate, ((ISupportSampling)sentTelemetry.First()).SamplingPercentage.Value, delta: 0.01);

            // Sampled out items are supposed to be cleared, no more gain up:
            sentTelemetry.RemoveAt(0);
            sentTelemetry.ForEach((item) => Assert.AreEqual(50, ((ISupportSampling)item).SamplingPercentage));
        }
        private void Dispose(bool disposing)
        {
            if (disposing)
            {
                Interlocked.CompareExchange(ref active, null, this);

                ITelemetryChannel telemetryChannel = this.TelemetryChannel;
                if (telemetryChannel != null)
                {
                    telemetryChannel.Dispose();
                }

                TelemetryProcessorChain processorChain = this.telemetryProcessorChain;
                if (processorChain != null)
                {
                    processorChain.Dispose();
                }
            }
        }
        public void ProactivelySampledOutItemIsNotSentEvenIfItIsSampledIn()
        {
            var sentTelemetry = new List <ITelemetry>();
            TelemetryProcessorChain telemetryProcessorChainWithSampling = CreateTelemetryProcessorChainWithSampling(
                sentTelemetry,
                50);

            for (int i = 0; i < 100; i++)
            {
                var sampledOutTelemetry = new RequestTelemetry();
                sampledOutTelemetry.IsSampledOutAtHead = true;

                telemetryProcessorChainWithSampling.Process(sampledOutTelemetry);
            }

            telemetryProcessorChainWithSampling.Dispose();

            Assert.AreEqual(0, sentTelemetry.Count);
        }
コード例 #7
0
        public void ProactivelySampledInItemsAreNotGivenPriorityIfRatesAreNotSet()
        {
            var sentTelemetry = new List <ITelemetry>();
            TelemetryProcessorChain telemetryProcessorChainWithSampling = CreateTelemetryProcessorChainWithSampling(
                sentTelemetry,
                50);

            for (int i = 0; i < 1000; i++)
            {
                var item = new RequestTelemetry();
                item.Context.Operation.Id = ActivityTraceId.CreateRandom().ToHexString();

                // proactively sample in items with big score, so they should not be sampled in
                if (SamplingScoreGenerator.GetSamplingScore(item.Context.Operation.Id) > 50)
                {
                    item.ProactiveSamplingDecision = SamplingDecision.SampledIn;
                }

                telemetryProcessorChainWithSampling.Process(item);
            }

            Assert.AreEqual(0, sentTelemetry.Count(i => ((ISupportAdvancedSampling)i).ProactiveSamplingDecision == SamplingDecision.SampledIn));
        }
コード例 #8
0
        private static void TelemetryTypeSupportsSampling(Func <TelemetryProcessorChain, int> sendAction,
                                                          string excludedTypes   = null,
                                                          string includedTypes   = null,
                                                          int samplingPercentage = 10)
        {
            const int ItemsToGenerate = 100;
            var       sentTelemetry   = new List <ITelemetry>();
            TelemetryProcessorChain telemetryProcessorChainWithSampling = CreateTelemetryProcessorChainWithSampling(
                sentTelemetry,
                samplingPercentage,
                excludedTypes,
                includedTypes);

            int generatedCount = 0;

            for (int i = 0; i < ItemsToGenerate; i++)
            {
                generatedCount += sendAction.Invoke(telemetryProcessorChainWithSampling);
            }

            Assert.IsNotNull(sentTelemetry[0] as ISupportSampling);
            Assert.IsTrue(sentTelemetry.Count > 0);

            if (samplingPercentage == 100)
            {
                Assert.IsTrue(sentTelemetry.Count == generatedCount);
                Assert.AreEqual(null, ((ISupportSampling)sentTelemetry[0]).SamplingPercentage);
            }
            else
            {
                Assert.IsTrue(sentTelemetry.Count < generatedCount);
                Assert.AreEqual(samplingPercentage, ((ISupportSampling)sentTelemetry[0]).SamplingPercentage);
            }

            telemetryProcessorChainWithSampling.Dispose();
        }