Esempio n. 1
0
        public static async Task RunAsync(CancellationToken token)
        {
            // set Instrumentation Key
            var 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
            var collectedItems = new ProcessedItems();
            var 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();

            var client = new TelemetryClient(configuration);

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

            var iteration = 0;
            var http      = new HttpClient();

            while (!token.IsCancellationRequested)
            {
                iteration++;

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

                    try
                    {
                        await http.GetStringAsync("http://bing.com");
                    }
                    catch (Exception exc)
                    {
                        client.TrackException(exc);
                        operation.Telemetry.Success = false;
                    }

                    client.StopOperation(operation);
                    Console.WriteLine($"Iteration {iteration}. Elapsed time: {operation.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);
#pragma warning disable 0618
                    client.TrackMetric("[RAW] Reduction Size", collectedItems.Size - sentItems.Size);
#pragma warning restore 0618
                }
            }
        }
Esempio n. 2
0
        public static async Task RunAsync(CancellationToken token)
        {
            // set Instrumentation Key
            var 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
            var collectedItems = new ProcessedItems();
            var sentItems      = new ProcessedItems();

            // 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 SizeCalculatorTelemetryProcessor(next, collectedItems)); })

            // exemplify dependency telemetry that is faster than 100 msec
            .Use((next) => { return(new DependencyExampleTelemetryProcessor(next)); })

            // sample all telemetry to 10%
            .Use((next) =>
            {
                return(new SamplingTelemetryProcessor(next)
                {
                    SamplingPercentage = 10,
                });
            })

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

            var client = new TelemetryClient(configuration);

            var iteration = 0;
            var http      = new HttpClient();

            while (!token.IsCancellationRequested)
            {
                using (var operation = client.StartOperation <RequestTelemetry>("Process item"))
                {
                    client.TrackEvent("IterationStarted", properties: new Dictionary <string, string>()
                    {
                        { "iteration", iteration.ToString() }
                    });
                    client.TrackTrace($"Iteration {iteration} started", SeverityLevel.Information);

                    try
                    {
                        await http.GetStringAsync("http://bing.com");
                    }
                    catch (Exception exc)
                    {
                        client.TrackException(exc);
                        operation.Telemetry.Success = false;
                    }

                    client.StopOperation(operation);
                    Console.WriteLine($"Iteration {iteration}. Elapsed time: {operation.Telemetry.Duration}. Collected Telemetry: {collectedItems.Size}/{collectedItems.Count}. Sent Telemetry: {sentItems.Size}/{sentItems.Count}. Ratio: {1.0 * collectedItems.Size / sentItems.Size}");
                    iteration++;
                }
            }
        }
Esempio n. 3
0
        public static void Run()
        {
            TelemetryConfiguration configuration = new TelemetryConfiguration();

            configuration.InstrumentationKey = "ad4a5cd4-9fb3-4e4e-9b9c-9f43e9939bfa";

            // 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);

            var iteration = 0;

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

                    try
                    {
                        var task = (new HttpClient()).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}");
                    iteration++;
                }
            }
        }
Esempio n. 4
0
 public SizeCalculatorTelemetryProcessor(ITelemetryProcessor next, ProcessedItems items)
 {
     this.next           = next;
     this.processedItems = items;
 }
 public PriceCalculatorTelemetryProcessor(ITelemetryProcessor next, ProcessedItems state)
 {
     this._next  = next;
     this._state = state;
 }
Esempio n. 6
0
        public static async Task RunAsync(CancellationToken token)
        {
            // set Instrumentation Key
            var configuration = new TelemetryConfiguration();

            configuration.InstrumentationKey = "4282169f-e83f-46f7-b38a-436087fa856d";

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

            // initialize state for the telemetry size calculation
            var collectedItems = new ProcessedItems();
            var 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)); })

            .Use((next) =>
            {
                return(new AggressivelySampleFastRequests(next)
                {
                    InitialSamplingPercentage = 5,
                    MaxSamplingPercentage = 5,
                    MinSamplingPercentage = 5,
                });
            })

            .Use((next) =>
            {
                return(new AggressivelySampleFastDependencies(next)
                {
                    InitialSamplingPercentage = 5,
                    MaxSamplingPercentage = 5,
                    MinSamplingPercentage = 5,
                });
            })

            // this is a standard adaptive sampling telemetry processor that will sample in/out any telemetry item it receives
            .Use((next) =>
            {
                return(new AdaptiveSamplingTelemetryProcessor(next)
                {
                    InitialSamplingPercentage = 25,
                    MaxSamplingPercentage = 25,
                    MinSamplingPercentage = 25,
                });
            })

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

            var client = new TelemetryClient(configuration);

            var iteration = 0;
            var http      = new HttpClient();

            while (!token.IsCancellationRequested)
            {
                iteration++;

                var t1 = new Task(() =>
                {
                    using (var operation = client.StartOperation <RequestTelemetry>("Slow request"))
                    {
                        client.TrackEvent("test", new Dictionary <string, string>()
                        {
                            { "iteration", iteration.ToString() }
                        });
                        client.TrackTrace($"Iteration {iteration} happened", SeverityLevel.Information);

                        using (var fastDependency = client.StartOperation <DependencyTelemetry>("Fast dependency"))
                        {
                            Thread.Sleep(TimeSpan.FromMilliseconds(2));
                        }

                        using (var slowDependency = client.StartOperation <DependencyTelemetry>("slow dependency"))
                        {
                            Thread.Sleep(TimeSpan.FromMilliseconds(1000));
                        }

                        client.StopOperation(operation);
                        Console.WriteLine($"Iteration {iteration}. Elapsed time: {operation.Telemetry.Duration}. Collected Telemetry: {collectedItems.Size}/{collectedItems.Count}. Sent Telemetry: {sentItems.Size}/{sentItems.Count}. Ratio: {1.0 * collectedItems.Size / sentItems.Size}");

                        client.TrackMetric("[RAW] Reduction Size", collectedItems.Size - sentItems.Size);
                    }
                });

                var t2 = new Task(() =>
                {
                    using (var operation = client.StartOperation <RequestTelemetry>("Fast request"))
                    {
                        client.TrackEvent("test", new Dictionary <string, string>()
                        {
                            { "iteration", iteration.ToString() }
                        });
                        client.TrackTrace($"Iteration {iteration} happened", SeverityLevel.Information);

                        using (var fastDependency = client.StartOperation <DependencyTelemetry>("Fast dependency"))
                        {
                            Thread.Sleep(TimeSpan.FromMilliseconds(2));
                        }

                        using (var slowDependency = client.StartOperation <DependencyTelemetry>("slow dependency"))
                        {
                            Thread.Sleep(TimeSpan.FromMilliseconds(400));
                        }

                        client.StopOperation(operation);
                        Console.WriteLine($"Iteration {iteration}. Elapsed time: {operation.Telemetry.Duration}. Collected Telemetry: {collectedItems.Size}/{collectedItems.Count}. Sent Telemetry: {sentItems.Size}/{sentItems.Count}. Ratio: {1.0 * collectedItems.Size / sentItems.Size}");

                        client.TrackMetric("[RAW] Reduction Size", collectedItems.Size - sentItems.Size);
                    }
                });

                t1.Start();
                t2.Start();
                Task.WaitAll(new Task[] { t1, t2 });
            }
        }
Esempio n. 7
0
        public static void Run()
        {
            TelemetryConfiguration configuration = new TelemetryConfiguration();

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

            var telemetryChannel = new ServerTelemetryChannel();

            telemetryChannel.Initialize(configuration);
            configuration.TelemetryChannel = telemetryChannel;

            // 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();

            // 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, CollectedItems)); })

            // filter telemetry that is faster than 100 msec
            //.Use((next) => { return new DependencyFilteringTelemetryProcessor(next); })

            // filter telemetry that is faster than 100 msec
            // extract metrics
            .Use((next) => { return(new DependencyFilteringWithMetricsTelemetryProcessor(next, configuration)); })

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


            TelemetryClient client = new TelemetryClient(configuration);

            var iteration = 0;


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

                    try
                    {
                        var task = (new HttpClient()).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}");
                    iteration++;
                }
            }
        }