/// <summary>
        /// Stops metric aggregation in advanced scenarios where a MetricManager was explicitly created using its ctor.
        /// </summary>
        /// <remarks>
        /// Metric Manager does not encapsulate any disposable or native resourses. However, it encapsulates a managed thread.
        /// In normal cases, a metric manager is accessed via convenience methods and consumers never need to worry about that thread.
        /// However, advanced scenarios may explicitly create a metric manager instance. In such cases, consumers may need to call
        /// this method on the explicitly created instance to let the thread know that it no longer needs to run. The thread will not
        /// be aborted proactively. Instead, it will complete the ongoing aggregation cycle and then gracfully exit instead of scheduling
        /// the next iteration. However, the background thread will not send any aggregated metrics if it has been notified to stop.
        /// Therefore, this method flushes current data before sending the notification.
        /// </remarks>
        /// <param name="metricManager">The metric manager</param>
        /// <returns>
        /// You can await the returned Task if you want to be sure that the encapsulated thread completed.
        /// If you just want to notify the thread to stop without waiting for it, do not await this method.
        /// </returns>
        public static Task StopDefaultAggregationCycleAsync(this MetricManager metricManager)
        {
            Util.ValidateNotNull(metricManager, nameof(metricManager));

            metricManager.Flush();
            return(metricManager.AggregationCycle.StopAsync());
        }
        private static void Metrics_SpecifiedPipeline(TelemetryConfiguration telemetryPipeline)
        {
            telemetryPipeline.InstrumentationKey = Guid.NewGuid().ToString("D");

            MetricManager manager1 = telemetryPipeline.GetMetricManager();

            Assert.IsNotNull(manager1);

            MetricManager manager2 = telemetryPipeline.GetMetricManager();

            Assert.IsNotNull(manager2);

            Assert.AreEqual(manager1, manager2);
            Assert.AreSame(manager1, manager2);
            Assert.IsTrue(Object.ReferenceEquals(manager1, manager2));

            StubTelemetryChannel telemetryCollector = new StubTelemetryChannel();

            telemetryPipeline.TelemetryChannel = telemetryCollector;
            Assert.AreSame(telemetryCollector, telemetryPipeline.TelemetryChannel);

            //CollectingTelemetryInitializer telemetryCollector = new CollectingTelemetryInitializer();
            //defaultTelemetryPipeline.TelemetryInitializers.Add(coll);

            IMetricSeriesConfiguration seriesConfig = new MetricSeriesConfigurationForMeasurement(restrictToUInt32Values: false);

            manager1.CreateNewSeries("ns", "Metric A", seriesConfig).TrackValue(42);
            manager1.CreateNewSeries("ns", "Metric A", seriesConfig).TrackValue("18");
            manager2.CreateNewSeries("ns", "Metric A", seriesConfig).TrackValue(10000);
            manager2.CreateNewSeries("ns", "Metric B", seriesConfig).TrackValue(-0.001);
            manager1.Flush();

            Assert.AreEqual(4, telemetryCollector.TelemetryItems.Count);

            Assert.IsInstanceOfType(telemetryCollector.TelemetryItems[0], typeof(MetricTelemetry));
            Assert.AreEqual("Metric B", ((MetricTelemetry)telemetryCollector.TelemetryItems[0]).Name);
            Assert.AreEqual(1, ((MetricTelemetry)telemetryCollector.TelemetryItems[0]).Count);
            Assert.AreEqual(-0.001, ((MetricTelemetry)telemetryCollector.TelemetryItems[0]).Sum);

            Assert.IsInstanceOfType(telemetryCollector.TelemetryItems[1], typeof(MetricTelemetry));
            Assert.AreEqual("Metric A", ((MetricTelemetry)telemetryCollector.TelemetryItems[1]).Name);
            Assert.AreEqual(1, ((MetricTelemetry)telemetryCollector.TelemetryItems[1]).Count);
            Assert.AreEqual(10000, ((MetricTelemetry)telemetryCollector.TelemetryItems[1]).Sum);

            Assert.IsInstanceOfType(telemetryCollector.TelemetryItems[2], typeof(MetricTelemetry));
            Assert.AreEqual("Metric A", ((MetricTelemetry)telemetryCollector.TelemetryItems[2]).Name);
            Assert.AreEqual(1, ((MetricTelemetry)telemetryCollector.TelemetryItems[2]).Count);
            Assert.AreEqual(18, ((MetricTelemetry)telemetryCollector.TelemetryItems[2]).Sum);

            Assert.IsInstanceOfType(telemetryCollector.TelemetryItems[3], typeof(MetricTelemetry));
            Assert.AreEqual("Metric A", ((MetricTelemetry)telemetryCollector.TelemetryItems[3]).Name);
            Assert.AreEqual(1, ((MetricTelemetry)telemetryCollector.TelemetryItems[3]).Count);
            Assert.AreEqual(42, ((MetricTelemetry)telemetryCollector.TelemetryItems[3]).Sum);
        }
Пример #3
0
        public static void Run()
        {
            TelemetryConfiguration configuration = new TelemetryConfiguration();

            configuration.InstrumentationKey = "fb8a0b03-235a-4b52-b491-307e9fd6b209";

            // automatically track dependency calls
            var dependencies = new DependencyTrackingTelemetryModule();

            dependencies.Initialize(configuration);

            // automatically correlate all telemetry data with request
            configuration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());

            // set default properties for all telemetry items
            configuration.TelemetryInitializers.Add(new DefaultTelemetryInitializer());

            // send all event telemetry to a different iKey
            configuration.TelemetryInitializers.Add(new BusinessTelemetryInitializer());

            // configure all telemetry to be sent to a single node
            configuration.TelemetryInitializers.Add(new NodeNameTelemetryInitializer());

            // initialize price calculation logic
            var state = new _State();

            state.Initialize();

            // enable sampling
            configuration.TelemetryProcessorChainBuilder
            // this telemetry processor will be executed first for all telemetry items to calculate the size and # of items
            .Use((next) => { return(new PriceCalculatorTelemetryProcessor(next, state.Collected)); })

            // this telemetry processor will be execuyted ONLY when telemetry is sampled in
            .Use((next) => { return(new PriceCalculatorTelemetryProcessor(next, state.Sent)); })
            .Build();


            TelemetryClient client = new TelemetryClient(configuration);

            var iterations = 0;

            // configure metrics collection
            MetricManager metricManager    = new MetricManager(client);
            var           itemsProcessed   = metricManager.CreateMetric("Iterations");
            var           processingFailed = metricManager.CreateMetric("Failed processing");
            var           processingSize   = metricManager.CreateMetric("Processing size");

            while (!state.IsTerminated)
            {
                iterations++;

                using (var operaiton = client.StartOperation <RequestTelemetry>("Process item"))
                {
                    client.TrackEvent("test");
                    client.TrackTrace("Something happened", SeverityLevel.Information);

                    try
                    {
                        HttpClient http = new HttpClient();
                        var        task = http.GetStringAsync("http://bing.com");
                        task.Wait();

                        // metrics aggregation. Metrics are aggregated and sent once per minute
                        itemsProcessed.Track(1);
                        processingSize.Track(task.Result.Length);
                        processingFailed.Track(0);

                        // raw metric telemetry. Each call represents a document.
                        client.TrackMetric("[RAW] Response size", task.Result.Length);
                    }
                    catch (Exception exc)
                    {
                        // raw metric telemetry
                        client.TrackMetric("[RAW] Successful responses", 0);

                        // metrics aggregation:
                        processingFailed.Track(1);

                        client.TrackException(exc);
                        operaiton.Telemetry.Success = false;
                    }
                    finally
                    {
                        client.TrackMetric("[RAW] Iterations", 1);
                        itemsProcessed.Track(1);
                    }

                    //                    client.StopOperation(operaiton);
                    //                    Console.WriteLine($"Iteration {iterations}. Elapesed time: {operaiton.Telemetry.Duration}");
                }
            }

            // send all metrics before exiting the program
            metricManager.Flush();

            Console.WriteLine($"Program sent 100K of telemetry in {iterations} iterations!");
            Console.ReadLine();
        }
Пример #4
0
        public static void Run()
        {
            TelemetryConfiguration configuration = new TelemetryConfiguration();

            configuration.InstrumentationKey = "fb8a0b03-235a-4b52-b491-307e9fd6b209";

            // automatically track dependency calls
            var dependencies = new DependencyTrackingTelemetryModule();

            dependencies.Initialize(configuration);

            // automatically correlate all telemetry data with request
            configuration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());

            // initialize state for the telemetry size calculation
            ProcessedItems CollectedItems = new ProcessedItems();
            ProcessedItems SentItems      = new ProcessedItems();

            // build telemetry processing pipeline
            configuration.TelemetryProcessorChainBuilder
            // this telemetry processor will be executed first for all telemetry items to calculate the size and # of items
            .Use((next) => { return(new SizeCalculatorTelemetryProcessor(next, CollectedItems)); })

            // this is a standard fixed sampling processor that will let only 10%
            .Use((next) =>
            {
                return(new SamplingTelemetryProcessor(next)
                {
                    IncludedTypes = "Dependency",
                    SamplingPercentage = 10
                });
            })

            // this is a standard adaptive sampling telemetry processor that will sample in/out any telemetry item it receives
            .Use((next) =>
            {
                return(new AdaptiveSamplingTelemetryProcessor(next)
                {
                    ExcludedTypes = "Event",                                     // exclude custom events from being sampled
                    MaxTelemetryItemsPerSecond = 1,                              //default: 5 calls/sec
                    SamplingPercentageIncreaseTimeout = TimeSpan.FromSeconds(1), //default: 2 min
                    SamplingPercentageDecreaseTimeout = TimeSpan.FromSeconds(1), //default: 30 sec
                    EvaluationInterval = TimeSpan.FromSeconds(1),                //default: 15 sec
                    InitialSamplingPercentage = 25                               //default: 100%
                });
            })

            // this telemetry processor will be execuyted ONLY when telemetry is sampled in
            .Use((next) => { return(new SizeCalculatorTelemetryProcessor(next, SentItems)); })
            .Build();

            TelemetryClient client = new TelemetryClient(configuration);

            // configure metrics collection
            MetricManager metricManager = new MetricManager(client);
            var           reductionsize = metricManager.CreateMetric("Reduction Size");

            var iteration = 0;

            while (true)
            {
                iteration++;

                using (var operaiton = client.StartOperation <RequestTelemetry>("Process item"))
                {
                    client.TrackEvent("test", new Dictionary <string, string>()
                    {
                        { "iteration", iteration.ToString() }
                    });
                    client.TrackTrace($"Iteration {iteration} happened", SeverityLevel.Information);

                    try
                    {
                        HttpClient http = new HttpClient();
                        var        task = http.GetStringAsync("http://bing.com");
                        task.Wait();
                    }
                    catch (Exception exc)
                    {
                        client.TrackException(exc);
                        operaiton.Telemetry.Success = false;
                    }
                    client.StopOperation(operaiton);
                    Console.WriteLine($"Iteration {iteration}. Elapesed time: {operaiton.Telemetry.Duration}. Collected Telemetry: {CollectedItems.size}/{CollectedItems.count}. Sent Telemetry: {SentItems.size}/{SentItems.count}. Ratio: {1.0 * CollectedItems.size / SentItems.size}");

                    reductionsize.Track(CollectedItems.size - SentItems.size);
                    client.TrackMetric("[RAW] Reduction Size", CollectedItems.size - SentItems.size);
                }
            }
            metricManager.Flush();
        }