The module subscribed to AppDomain.CurrentDomain.FirstChanceException to send exceptions statistics to ApplicationInsights.
Наследование: ITelemetryModule, IDisposable
Пример #1
0
        public void FirstChanceExceptionStatisticsTelemetryCallMultipleMethods()
        {
            using (var module = new FirstChanceExceptionStatisticsTelemetryModule())
            {
                module.Initialize(this.configuration);

                for (int i = 0; i < 200; i++)
                {
                    try
                    {
                        this.Method1();
                    }
                    catch
                    {
                    }

                    try
                    {
                        throw new Exception("New exception from Method1");
                    }
                    catch
                    {
                    }
                }
            }

            // There should be three unique TrackExceptions and three Metrics
            Assert.Equal(6, this.items.Count);
        }
Пример #2
0
        public void FirstChanceExceptionStatisticsTelemetryModuleDoNotCauseStackOverflow()
        {
            this.configuration.TelemetryInitializers.Add(new StubTelemetryInitializer()
            {
                OnInitialize = (item) =>
                {
                    throw new Exception("this exception may cause stack overflow as will be thrown during the processing of another exception");
                }
            });

            using (var module = new FirstChanceExceptionStatisticsTelemetryModule())
            {
                module.Initialize(this.configuration);

                try
                {
                    // FirstChanceExceptionStatisticsTelemetryModule will process this exception
                    throw new Exception("test");
                }
                catch (Exception exc)
                {
                    // make sure it is the same exception as was initially thrown
                    Assert.Equal("test", exc.Message);
                }
            }
        }
 public void FirstChanceExceptionStatisticsTelemetryModuleDoNotThrowOnNullException()
 {
     EventHandler<FirstChanceExceptionEventArgs> handler = null;
     using (var module = new FirstChanceExceptionStatisticsTelemetryModule(
         h => handler = h,
         _ => { }))
     {
         module.Initialize(this.configuration);
         handler.Invoke(null, new FirstChanceExceptionEventArgs(null));
     }
 }
Пример #4
0
        public void FirstChanceExceptionStatisticsTelemetryModuleDoNotThrowOnNullException()
        {
            EventHandler <FirstChanceExceptionEventArgs> handler = null;

            using (var module = new FirstChanceExceptionStatisticsTelemetryModule(
                       h => handler = h,
                       _ => { }))
            {
                module.Initialize(this.configuration);
                handler.Invoke(null, new FirstChanceExceptionEventArgs(null));
            }
        }
Пример #5
0
        public void FirstChanceExceptionStatisticsTelemetryModuleUsesSetsInternalOperationName()
        {
            var metrics = new List <KeyValuePair <Metric, double> >();
            StubMetricProcessor stub = new StubMetricProcessor()
            {
                OnTrack = (m, v) =>
                {
                    metrics.Add(new KeyValuePair <Metric, double>(m, v));
                }
            };

            this.configuration.TelemetryInitializers.Add(new StubTelemetryInitializer()
            {
                OnInitialize = (item) =>
                {
                    item.Context.Operation.Name = TestOperationName;
                }
            });

            using (var module = new FirstChanceExceptionStatisticsTelemetryModule())
            {
                module.Initialize(this.configuration);
                module.MetricManager.MetricProcessors.Add(stub);

                SdkInternalOperationsMonitor.Enter();
                try
                {
                    try
                    {
                        // FirstChanceExceptionStatisticsTelemetryModule will process this exception
                        throw new Exception("test");
                    }
                    catch (Exception exc)
                    {
                        // code to prevent profiler optimizations
                        Assert.Equal("test", exc.Message);
                    }
                }
                finally
                {
                    SdkInternalOperationsMonitor.Exit();
                }
            }

            Assert.Equal(1, metrics.Count);
            Assert.Equal("Exceptions thrown", metrics[0].Key.Name);

            var dims = metrics[0].Key.Dimensions;

            Assert.Equal(2, dims.Count);

            Assert.True(dims.Contains(new KeyValuePair <string, string>(FirstChanceExceptionStatisticsTelemetryModule.OperationNameTag, "AI (Internal)")));
        }
Пример #6
0
        public void FirstChanceExceptionStatisticsTelemetryModuleWasTrackedReturnsFalseForAggExcWithNotTrackedInnerExceptions()
        {
            using (var module = new FirstChanceExceptionStatisticsTelemetryModule())
            {
                module.Initialize(this.configuration);

                var exception = new Exception();

                var aggExc = new AggregateException(exception);
                Assert.False(module.WasExceptionTracked(aggExc));
            }
        }
        public void DisposeCallsUnregister()
        {
            EventHandler<FirstChanceExceptionEventArgs> handler = null;
            using (var module = new FirstChanceExceptionStatisticsTelemetryModule(
                _ => { },
                h => handler = h))
            {
                module.Initialize(this.configuration);
            }

            Assert.NotNull(handler);
        }
        public void FirstChanceExceptionStatisticsTelemetryModuleDoNotIncrementOnRethrow()
        {
            var metrics = new List <KeyValuePair <Metric, double> >();
            StubMetricProcessor stub = new StubMetricProcessor()
            {
                OnTrack = (m, v) =>
                {
                    metrics.Add(new KeyValuePair <Metric, double>(m, v));
                }
            };

            using (var module = new FirstChanceExceptionStatisticsTelemetryModule())
            {
                module.Initialize(this.configuration);
                module.MetricManager.MetricProcessors.Add(stub);

                try
                {
                    try
                    {
                        // FirstChanceExceptionStatisticsTelemetryModule will process this exception
                        throw new Exception("test");
                    }
                    catch (Exception ex)
                    {
                        // this assert is neede to avoid code optimization
                        Assert.Equal("test", ex.Message);
                        throw;
                    }
                }
                catch (Exception exc)
                {
                    Assert.Equal("test", exc.Message);
                }
            }

            Assert.Equal(1, metrics.Count);
            Assert.Equal("Exceptions thrown", metrics[0].Key.Name);

            Assert.Equal(1, metrics[0].Value, 15);

            Assert.Equal(2, this.items.Count);

            ITelemetry[] testItems = this.items.ToArray();

            foreach (ITelemetry i in testItems)
            {
                if (i is MetricTelemetry)
                {
                    Assert.Equal(1, ((MetricTelemetry)i).Count);
                }
            }
        }
Пример #9
0
        public void ModuleConstructorCallsRegister()
        {
            EventHandler <FirstChanceExceptionEventArgs> handler = null;

            using (var module = new FirstChanceExceptionStatisticsTelemetryModule(
                       h => handler = h,
                       _ => { }))
            {
                module.Initialize(this.configuration);
            }

            Assert.NotNull(handler);
        }
Пример #10
0
        public void DisposeCallsUnregister()
        {
            EventHandler <FirstChanceExceptionEventArgs> handler = null;

            using (var module = new FirstChanceExceptionStatisticsTelemetryModule(
                       _ => { },
                       h => handler = h))
            {
                module.Initialize(this.configuration);
            }

            Assert.NotNull(handler);
        }
Пример #11
0
        public void FirstChanceExceptionStatisticsTelemetryModuleDoNotIncrementOnRethrow()
        {
            var metrics = new List <KeyValuePair <Metric, double> >();

            this.configuration.MetricProcessors.Add(new StubMetricProcessor()
            {
                OnTrack = (m, v) =>
                {
                    metrics.Add(new KeyValuePair <Metric, double>(m, v));
                }
            });

            using (var module = new FirstChanceExceptionStatisticsTelemetryModule())
            {
                module.Initialize(this.configuration);

                try
                {
                    try
                    {
                        // FirstChanceExceptionStatisticsTelemetryModule will process this exception
                        throw new Exception("test");
                    }
                    catch (Exception ex)
                    {
                        // this assert is neede to avoid code optimization
                        Assert.Equal("test", ex.Message);
                        throw;
                    }
                }
                catch (Exception exc)
                {
                    Assert.Equal("test", exc.Message);
                }
            }

            Assert.Equal(2, metrics.Count);
            Assert.Equal("Exceptions Thrown", metrics[0].Key.Name);

            Assert.Equal(1, metrics[0].Value, 15);
            Assert.Equal(0, metrics[1].Value, 15);

            Assert.Equal(2, this.items.Count);

            Assert.Equal(1, ((MetricTelemetry)this.items[0]).Count);
            Assert.Equal(1, ((MetricTelemetry)this.items[1]).Count);

            // One of them should be 0 as re-thorwn, another - one
            Assert.Equal(0, Math.Min(((MetricTelemetry)this.items[0]).Sum, ((MetricTelemetry)this.items[1]).Sum), 15);
            Assert.Equal(1, Math.Max(((MetricTelemetry)this.items[0]).Sum, ((MetricTelemetry)this.items[1]).Sum), 15);
        }
Пример #12
0
        public void FirstChanceExceptionStatisticsTelemetryModuleWillDimCapAfterCacheTimeout()
        {
            var metrics = new List <KeyValuePair <Metric, double> >();
            StubMetricProcessor stub = new StubMetricProcessor()
            {
                OnTrack = (m, v) =>
                {
                    metrics.Add(new KeyValuePair <Metric, double>(m, v));
                }
            };

            int operationId = 0;

            this.configuration.TelemetryInitializers.Add(new StubTelemetryInitializer()
            {
                OnInitialize = (item) =>
                {
                    item.Context.Operation.Name = "operationName " + (operationId++);
                }
            });

            using (var module = new FirstChanceExceptionStatisticsTelemetryModule())
            {
                module.Initialize(this.configuration);
                module.MetricManager.MetricProcessors.Add(stub);

                module.DimCapTimeout = DateTime.UtcNow.Ticks - 1;

                for (int i = 0; i < 200; i++)
                {
                    if (i == 101)
                    {
                        module.DimCapTimeout = DateTime.UtcNow.Ticks - 1;
                    }

                    try
                    {
                        // FirstChanceExceptionStatisticsTelemetryModule will process this exception
                        throw new Exception("test");
                    }
                    catch (Exception exc)
                    {
                        // code to prevent profiler optimizations
                        Assert.Equal("test", exc.Message);
                    }
                }
            }

            Assert.Equal(200, metrics.Count);
            Assert.Equal(400, this.items.Count);
        }
Пример #13
0
        public void FirstChanceExceptionStatisticsTelemetryModuleWasTrackedReturnsTrueForTheSameException()
        {
            using (var module = new FirstChanceExceptionStatisticsTelemetryModule())
            {
                module.Initialize(this.configuration);

                var exception = new Exception();

                Assert.False(module.WasExceptionTracked(exception));
                Assert.True(module.WasExceptionTracked(exception));
                Assert.True(module.WasExceptionTracked(exception));
                Assert.True(module.WasExceptionTracked(exception));
            }
        }
Пример #14
0
        public void FirstChanceExceptionStatisticsTelemetryModuleWasTrackedReturnsFalseForInnerExceptionTwoLevelsUp()
        {
            using (var module = new FirstChanceExceptionStatisticsTelemetryModule())
            {
                module.Initialize(this.configuration);

                var exception = new Exception();

                Assert.False(module.WasExceptionTracked(exception));

                var wrapper1 = new Exception("wrapper 1", exception);
                var wrapper2 = new Exception("wrapper 2", wrapper1);

                Assert.False(module.WasExceptionTracked(wrapper2));
            }
        }
Пример #15
0
        public void FirstChanceExceptionStatisticsTelemetryModuleMarksOperationAsInternal()
        {
            var metrics = new List <KeyValuePair <Metric, double> >();
            StubMetricProcessor stub = new StubMetricProcessor()
            {
                OnTrack = (m, v) =>
                {
                    metrics.Add(new KeyValuePair <Metric, double>(m, v));
                }
            };

            using (var module = new FirstChanceExceptionStatisticsTelemetryModule())
            {
                module.Initialize(this.configuration);
                module.MetricManager.MetricProcessors.Add(stub);

                try
                {
                    SdkInternalOperationsMonitor.Enter();

                    // FirstChanceExceptionStatisticsTelemetryModule will process this exception
                    throw new Exception("test");
                }
                catch (Exception exc)
                {
                    // code to prevent profiler optimizations
                    Assert.Equal("test", exc.Message);
                }
                finally
                {
                    SdkInternalOperationsMonitor.Exit();
                }
            }

            Assert.Equal(1, metrics.Count);
            Assert.Equal("Exceptions thrown", metrics[0].Key.Name);

            var dims = metrics[0].Key.Dimensions;

            Assert.Equal(2, dims.Count);

            string operationName;

            Assert.True(dims.TryGetValue(FirstChanceExceptionStatisticsTelemetryModule.OperationNameTag, out operationName));
            Assert.Equal("AI (Internal)", operationName);
        }
Пример #16
0
        public void FirstChanceExceptionStatisticsTelemetryModuleUsesOperationNameAsDimension()
        {
            var metrics = new List <KeyValuePair <Metric, double> >();

            this.configuration.MetricProcessors.Add(new StubMetricProcessor()
            {
                OnTrack = (m, v) =>
                {
                    metrics.Add(new KeyValuePair <Metric, double>(m, v));
                }
            });

            this.configuration.TelemetryInitializers.Add(new StubTelemetryInitializer()
            {
                OnInitialize = (item) =>
                {
                    item.Context.Operation.Name = "operationName";
                }
            });

            using (var module = new FirstChanceExceptionStatisticsTelemetryModule())
            {
                module.Initialize(this.configuration);

                try
                {
                    // FirstChanceExceptionStatisticsTelemetryModule will process this exception
                    throw new Exception("test");
                }
                catch (Exception exc)
                {
                    // code to prevent profiler optimizations
                    Assert.Equal("test", exc.Message);
                }
            }

            Assert.Equal(1, metrics.Count);
            Assert.Equal("Exceptions Thrown", metrics[0].Key.Name);

            var dims = metrics[0].Key.Dimensions;

            Assert.Equal(3, dims.Count);

            Assert.True(dims.Contains(new KeyValuePair <string, string>("operation", "operationName")));
        }
Пример #17
0
        public void FirstChanceExceptionStatisticsTelemetryModuleWillNotDimCapTheSameOperationName()
        {
            var metrics = new List <KeyValuePair <Metric, double> >();
            StubMetricProcessor stub = new StubMetricProcessor()
            {
                OnTrack = (m, v) =>
                {
                    metrics.Add(new KeyValuePair <Metric, double>(m, v));
                }
            };

            this.configuration.TelemetryInitializers.Add(new StubTelemetryInitializer()
            {
                OnInitialize = (item) =>
                {
                    item.Context.Operation.Name = TestOperationName;
                }
            });

            using (var module = new FirstChanceExceptionStatisticsTelemetryModule())
            {
                module.Initialize(this.configuration);
                module.MetricManager.MetricProcessors.Add(stub);

                for (int i = 0; i < 200; i++)
                {
                    try
                    {
                        // FirstChanceExceptionStatisticsTelemetryModule will process this exception
                        throw new Exception("test");
                    }
                    catch (Exception exc)
                    {
                        // code to prevent profiler optimizations
                        Assert.Equal("test", exc.Message);
                    }
                }
            }

            Assert.Equal(200, metrics.Count);
            Assert.Equal(2, this.items.Count);
        }
Пример #18
0
        public void FirstChanceExceptionStatisticsTelemetryModuleTracksMetricWithTypeAndMethodOnException()
        {
            var metrics = new List <KeyValuePair <Metric, double> >();
            StubMetricProcessor stub = new StubMetricProcessor()
            {
                OnTrack = (m, v) =>
                {
                    metrics.Add(new KeyValuePair <Metric, double>(m, v));
                }
            };

            using (var module = new FirstChanceExceptionStatisticsTelemetryModule())
            {
                module.Initialize(this.configuration);
                module.MetricManager.MetricProcessors.Add(stub);

                try
                {
                    // FirstChanceExceptionStatisticsTelemetryModule will process this exception
                    throw new Exception("test");
                }
                catch (Exception exc)
                {
                    // code to prevent compiler optimizations
                    Assert.Equal("test", exc.Message);
                }
            }

            Assert.Equal(1, metrics.Count);
            Assert.Equal("Exceptions thrown", metrics[0].Key.Name);

            var dims = metrics[0].Key.Dimensions;

            Assert.Equal(1, dims.Count);

            Assert.True(dims["problemId"].StartsWith(typeof(Exception).FullName, StringComparison.Ordinal));

            int nameStart = dims["problemId"].IndexOf(" at ", StringComparison.OrdinalIgnoreCase) + 4;

            Assert.True(dims["problemId"].Substring(nameStart).StartsWith(typeof(FirstChanceExceptionStatisticsTelemetryModuleTest).FullName + "." + nameof(this.FirstChanceExceptionStatisticsTelemetryModuleTracksMetricWithTypeAndMethodOnException), StringComparison.Ordinal));
        }
Пример #19
0
        public void FirstChanceExceptionStatisticsTelemetryModuleTracksMetricWithTypeAndMethodOnException()
        {
            var metrics = new List <KeyValuePair <Metric, double> >();

            this.configuration.MetricProcessors.Add(new StubMetricProcessor()
            {
                OnTrack = (m, v) =>
                {
                    metrics.Add(new KeyValuePair <Metric, double>(m, v));
                }
            });

            using (var module = new FirstChanceExceptionStatisticsTelemetryModule())
            {
                module.Initialize(this.configuration);

                try
                {
                    // FirstChanceExceptionStatisticsTelemetryModule will process this exception
                    throw new Exception("test");
                }
                catch (Exception exc)
                {
                    // code to prevent compiler optimizations
                    Assert.Equal("test", exc.Message);
                }
            }

            Assert.Equal(1, metrics.Count);
            Assert.Equal("Exceptions Thrown", metrics[0].Key.Name);

            var dims = metrics[0].Key.Dimensions;

            Assert.Equal(2, dims.Count);

            Assert.True(dims.Contains(new KeyValuePair <string, string>("type", typeof(Exception).FullName)));
            string value;

            Assert.True(dims.TryGetValue("method", out value));
            Assert.True(value.StartsWith(typeof(FirstChanceExceptionStatisticsTelemetryModuleTest).FullName + "." + nameof(this.FirstChanceExceptionStatisticsTelemetryModuleTracksMetricWithTypeAndMethodOnException), StringComparison.Ordinal));
        }
        public void FirstChanceExceptionStatisticsTelemetryModuleDoNotCauseStackOverflow()
        {
            this.configuration.TelemetryInitializers.Add(new StubTelemetryInitializer()
            {
                OnInitialize = (item) =>
                {
                    throw new Exception("this exception may cause stack overflow");
                }
            });

            using (var module = new FirstChanceExceptionStatisticsTelemetryModule())
            {
                module.Initialize(this.configuration);

                try
                {
                    throw new Exception("test");
                }
                catch (Exception exc)
                {
                    Assert.Equal("test", exc.Message);
                }
            }
        }
        public void FirstChanceExceptionStatisticsTelemetryModuleDoNotCauseStackOverflow()
        {
            this.configuration.TelemetryInitializers.Add(new StubTelemetryInitializer()
            {
                OnInitialize = (item) =>
                {
                    throw new Exception("this exception may cause stack overflow");
                }
            });

            using (var module = new FirstChanceExceptionStatisticsTelemetryModule())
            {
                module.Initialize(this.configuration);

                try
                {
                    throw new Exception("test");
                }
                catch (Exception exc)
                {
                    Assert.Equal("test", exc.Message);
                }
            }
        }
        public void FirstChanceExceptionStatisticsTelemetryModuleTracksMetricWithTypeAndMethodOnException()
        {
            var metrics = new List <KeyValuePair <Metric, double> >();

            this.configuration.MetricProcessors.Add(new StubMetricProcessor()
            {
                OnTrack = (m, v) =>
                {
                    metrics.Add(new KeyValuePair <Metric, double>(m, v));
                }
            });

            using (var module = new FirstChanceExceptionStatisticsTelemetryModule())
            {
                module.Initialize(this.configuration);

                try
                {
                    throw new Exception("test");
                }
                catch (Exception exc)
                {
                    Assert.Equal("test", exc.Message);
                }
            }

            Assert.Equal(1, metrics.Count);
            Assert.Equal("Exceptions Thrown", metrics[0].Key.Name);

            var dims = metrics[0].Key.Dimensions;

            Assert.Equal(2, dims.Count);

            Assert.True(dims.Contains(new KeyValuePair <string, string>("type", typeof(Exception).FullName)));
            Assert.True(dims.Contains(new KeyValuePair <string, string>("method", typeof(FirstChanceExceptionStatisticsTelemetryModuleTest).FullName + ".FirstChanceExceptionStatisticsTelemetryModuleTracksMetricWithTypeAndMethodOnException")));
        }
        public void FirstChanceExceptionStatisticsTelemetryModuleUsesOperationNameAsDimension()
        {
            var metrics = new List<KeyValuePair<Metric, double>>();
            this.configuration.MetricProcessors.Add(new StubMetricProcessor()
            {
                OnTrack = (m, v) =>
                {
                    metrics.Add(new KeyValuePair<Metric, double>(m, v));
                }
            });

            this.configuration.TelemetryInitializers.Add(new StubTelemetryInitializer()
            {
                OnInitialize = (item) =>
                {
                    item.Context.Operation.Name = "operationName";
                }
            });

            using (var module = new FirstChanceExceptionStatisticsTelemetryModule())
            {
                module.Initialize(this.configuration);

                try
                {
                    throw new Exception("test");
                }
                catch (Exception exc)
                {
                    Assert.Equal("test", exc.Message);
                }
            }

            Assert.Equal(1, metrics.Count);
            Assert.Equal("Exceptions Thrown", metrics[0].Key.Name);

            var dims = metrics[0].Key.Dimensions;
            Assert.Equal(3, dims.Count);

            Assert.True(dims.Contains(new KeyValuePair<string, string>("operation", "operationName")));
        }
        public void ModuleConstructorCallsRegister()
        {
            EventHandler<FirstChanceExceptionEventArgs> handler = null;
            using (var module = new FirstChanceExceptionStatisticsTelemetryModule(
                h => handler = h,
                _ => { }))
            {
                module.Initialize(this.configuration);
            }

            Assert.NotNull(handler);
        }
        public void FirstChanceExceptionStatisticsTelemetryModuleTracksMetricWithTypeAndMethodOnException()
        {
            var metrics = new List<KeyValuePair<Metric, double>>();
            this.configuration.MetricProcessors.Add(new StubMetricProcessor()
            {
                OnTrack = (m, v) =>
                {
                    metrics.Add(new KeyValuePair<Metric, double>(m, v));
                }
            });

            using (var module = new FirstChanceExceptionStatisticsTelemetryModule())
            {
                module.Initialize(this.configuration);

                try
                {
                    throw new Exception("test");
                }
                catch (Exception exc)
                {
                    Assert.Equal("test", exc.Message);
                }
            }

            Assert.Equal(1, metrics.Count);
            Assert.Equal("Exceptions Thrown", metrics[0].Key.Name);

            var dims = metrics[0].Key.Dimensions;
            Assert.Equal(2, dims.Count);

            Assert.True(dims.Contains(new KeyValuePair<string, string>("type", typeof(Exception).FullName)));
            Assert.True(dims.Contains(new KeyValuePair<string, string>("method", typeof(FirstChanceExceptionStatisticsTelemetryModuleTest).FullName + ".FirstChanceExceptionStatisticsTelemetryModuleTracksMetricWithTypeAndMethodOnException")));
        }
Пример #26
0
        public void FirstChanceExceptionStatisticsTelemetryModuleThrowFromTaskAsync()
        {
            var metrics = new List <KeyValuePair <Metric, double> >();
            StubMetricProcessor stub = new StubMetricProcessor()
            {
                OnTrack = (m, v) =>
                {
                    metrics.Add(new KeyValuePair <Metric, double>(m, v));
                }
            };

            using (var module = new FirstChanceExceptionStatisticsTelemetryModule())
            {
                module.Initialize(this.configuration);
                module.MetricManager.MetricProcessors.Add(stub);

                for (int i = 0; i < 10; i++)
                {
                    try
                    {
                        Task <int> task1 = Task <int> .Factory.StartNew(() => this.Method5(0));

                        Task <int> task2 = Task <int> .Factory.StartNew(() => this.Method4(0));

                        try
                        {
                            task1.Wait();
                        }
                        catch (Exception)
                        {
                            throw;
                        }

                        try
                        {
                            task2.Wait();
                        }
                        catch (Exception)
                        {
                            throw;
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }

            Assert.Equal(30, metrics.Count);

            Assert.Equal("Exceptions thrown", metrics[0].Key.Name);
            Assert.Equal(1, metrics[0].Value, 15);

            Assert.Equal("Exceptions thrown", metrics[1].Key.Name);
            Assert.Equal(1, metrics[1].Value, 15);

            Assert.Equal("Exceptions thrown", metrics[2].Key.Name);
            Assert.Equal(1, metrics[2].Value, 15);

            // There should be 3 telemetry items and 3 metric items
            Assert.Equal(6, this.items.Count);
        }
Пример #27
0
        public void FirstChanceExceptionStatisticsTelemetryExceptionsAreThrottled()
        {
            var metrics = new List <KeyValuePair <Metric, double> >();
            StubMetricProcessor stub = new StubMetricProcessor()
            {
                OnTrack = (m, v) =>
                {
                    metrics.Add(new KeyValuePair <Metric, double>(m, v));
                }
            };

            this.configuration.TelemetryInitializers.Add(new StubTelemetryInitializer()
            {
                OnInitialize = (item) =>
                {
                    item.Context.Operation.Name = TestOperationName;
                }
            });

            using (var module = new FirstChanceExceptionStatisticsTelemetryModule())
            {
                module.Initialize(this.configuration);
                module.MetricManager.MetricProcessors.Add(stub);

                module.TargetMovingAverage = 50;

                for (int i = 0; i < 200; i++)
                {
                    try
                    {
                        // FirstChanceExceptionStatisticsTelemetryModule will process this exception
                        throw new Exception("test");
                    }
                    catch (Exception exc)
                    {
                        // code to prevent profiler optimizations
                        Assert.Equal("test", exc.Message);
                    }
                }
            }

            int countProcessed = 0;
            int countThrottled = 0;

            foreach (KeyValuePair <Metric, double> items in metrics)
            {
                if (items.Key.Dimensions.Count == 1)
                {
                    countThrottled++;
                }
                else
                {
                    countProcessed++;
                }
            }

            // The test starts with the current moving average being 0. With the setting of the
            // weight for the new sample being .3 and the target moving average being 50 (as
            // set in the test), this means 50 / .3 = 166 becomes the throttle limit for this window.
            Assert.Equal(166, countProcessed);
            Assert.Equal(34, countThrottled);
        }