コード例 #1
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);
        }
コード例 #2
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);
        }
コード例 #5
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();
        }