private static void AddEventCounterIfNotExist(EventCounterCollectionModule eventCounterModule, string eventSource, string eventCounterName)
 {
     if (!eventCounterModule.Counters.Any(req => req.EventSourceName.Equals(eventSource, StringComparison.Ordinal) && req.EventCounterName.Equals(eventCounterName, StringComparison.Ordinal)))
     {
         eventCounterModule.Counters.Add(new EventCounterCollectionRequest(eventSource, eventCounterName));
     }
 }
コード例 #2
0
 /// <summary>
 /// Add Event Counters to Application Insights
 /// @see: https://docs.microsoft.com/en-us/azure/azure-monitor/app/eventcounters
 /// </summary>
 /// <param name="module">Modules</param>
 internal static void SetEventCounterCollectionModule(
     EventCounterCollectionModule module)
 {
     // in .NET Core 3 there are no default Counters (Event counters)
     module.Counters.Clear();
     // https://docs.microsoft.com/en-us/dotnet/core/diagnostics/available-counters
     module.Counters.Add(
         new EventCounterCollectionRequest("System.Runtime",
                                           "gen-0-size"));
     module.Counters.Add(
         new EventCounterCollectionRequest("System.Runtime",
                                           "time-in-gc"));
     module.Counters.Add(
         new EventCounterCollectionRequest("System.Runtime",
                                           "cpu-usage"));
     // memory usage
     module.Counters.Add(
         new EventCounterCollectionRequest("System.Runtime",
                                           "working-set"));
     // Retrieves the number of bytes currently thought to be allocated
     module.Counters.Add(
         new EventCounterCollectionRequest("System.Runtime",
                                           "gc-heap-size"));
     module.Counters.Add(
         new EventCounterCollectionRequest("Microsoft.AspNetCore.Hosting",
                                           "current-request"));
 }
        public void IgnoresUnconfiguredEventCounter()
        {
            // ARRANGE
            const int refreshTimeInSecs = 1;
            ConcurrentQueue <ITelemetry> itemsReceived = new ConcurrentQueue <ITelemetry>();

            using (var eventListener = new EventCounterCollectorDiagnosticListener())
                using (var module = new EventCounterCollectionModule(refreshTimeInSecs))
                {
                    module.Counters.Add(new EventCounterCollectionRequest()
                    {
                        EventSourceName = this.TestEventCounterSourceName, EventCounterName = this.TestEventCounterName1
                    });
                    module.Initialize(GetTestTelemetryConfiguration(itemsReceived));

                    // ACT
                    // These will fire counters 'mycountername2' which is not in the configured list.
                    TestEventCounter.Log.SampleCounter2(1500);
                    TestEventCounter.Log.SampleCounter2(400);

                    // Wait at least for refresh time.
                    Task.Delay(((int)refreshTimeInSecs * 1000) + 500).Wait();

                    // VALIDATE
                    Assert.IsTrue(CheckEventReceived(eventListener.EventsReceived, nameof(EventCounterCollectorEventSource.IgnoreEventWrittenAsCounterNotInConfiguredList)));
                }
        }
        public void ValidateConfiguredNamingOptions()
        {
            // ARRANGE
            const int refreshTimeInSecs = 1;
            ConcurrentQueue <ITelemetry> itemsReceived = new ConcurrentQueue <ITelemetry>();
            string expectedName            = this.TestEventCounterName1;
            string expectedMetricNamespace = this.TestEventCounterSourceName;
            double expectedMetricValue     = 1000;
            int    expectedMetricCount     = 1;

            using (var module = new EventCounterCollectionModule(refreshTimeInSecs))
            {
                module.UseEventSourceNameAsMetricsNamespace = true;
                module.Counters.Add(new EventCounterCollectionRequest()
                {
                    EventSourceName = this.TestEventCounterSourceName, EventCounterName = this.TestEventCounterName1
                });
                module.Initialize(GetTestTelemetryConfiguration(itemsReceived));

                // ACT
                // Making a call with 1000
                TestEventCounter.Log.SampleCounter1(1000);

                // Wait at least for refresh time.
                Task.Delay(((int)refreshTimeInSecs * 1000) + 500).Wait();

                PrintTelemetryItems(itemsReceived);

                // VALIDATE
                ValidateTelemetry(itemsReceived, expectedName, expectedMetricNamespace, expectedMetricValue, expectedMetricCount);
            }
        }
コード例 #5
0
        public void SetEventCounterCollectionModule_ShouldContainItems()
        {
            var module = new EventCounterCollectionModule();

            ApplicationInsightsExtension.SetEventCounterCollectionModule(module);
            Assert.IsTrue(module.Counters.Count >= 1);
        }
 public void WarnsIfNoCountersConfigured()
 {
     using (var eventListener = new EventCounterCollectorDiagnosticListener())
         using (var module = new EventCounterCollectionModule())
         {
             ConcurrentQueue <ITelemetry> itemsReceived = new ConcurrentQueue <ITelemetry>();
             module.Initialize(GetTestTelemetryConfiguration(itemsReceived));
             Assert.IsTrue(CheckEventReceived(eventListener.EventsReceived, nameof(EventCounterCollectorEventSource.ModuleIsBeingInitializedEvent)));
             Assert.IsTrue(CheckEventReceived(eventListener.EventsReceived, nameof(EventCounterCollectorEventSource.EventCounterCollectorNoCounterConfigured)));
         }
 }
        public void ValidateSingleEventCounterCollection()
        {
            // ARRANGE
            const int refreshTimeInSecs = 1;
            ConcurrentQueue <ITelemetry> itemsReceived = new ConcurrentQueue <ITelemetry>();
            string expectedName            = this.TestEventCounterSourceName + "|" + this.TestEventCounterName1;
            string expectedMetricNamespace = String.Empty;
            double expectedMetricValue     = (1000 + 1500 + 1500 + 400) / 4;
            int    expectedMetricCount     = 4;

            using (var module = new EventCounterCollectionModule(refreshTimeInSecs))
            {
                module.Counters.Add(new EventCounterCollectionRequest()
                {
                    EventSourceName = this.TestEventCounterSourceName, EventCounterName = this.TestEventCounterName1
                });
                module.Initialize(GetTestTelemetryConfiguration(itemsReceived));

                // ACT
                // Making 4 calls with 1000, 1500, 1500, 400 value, leading to an average of 1100.
                TestEventCounter.Log.SampleCounter1(1000);
                TestEventCounter.Log.SampleCounter1(1500);
                TestEventCounter.Log.SampleCounter1(1500);
                TestEventCounter.Log.SampleCounter1(400);

                // Wait at least for refresh time.
                Task.Delay(((int)refreshTimeInSecs * 1000) + 500).Wait();

                PrintTelemetryItems(itemsReceived);

                // VALIDATE
                ValidateTelemetry(itemsReceived, expectedName, expectedMetricNamespace, expectedMetricValue, expectedMetricCount);

                // Clear the items.
                Trace.WriteLine("Clearing items received.");
                itemsReceived.Clear();

                // Wait another refresh interval to receive more events, but with zero as counter values.
                // as nobody is publishing events.
                Task.Delay(((int)refreshTimeInSecs * 1000)).Wait();
                Assert.IsTrue(itemsReceived.Count >= 1);
                PrintTelemetryItems(itemsReceived);
                ValidateTelemetry(itemsReceived, expectedName, expectedMetricNamespace, 0.0, 0);
            }
        }