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