public void HandlesDuplicatePropertyNames()
        {
            var healthReporterMock = new Mock <IHealthReporter>();

            var inputConfiguration = new List <EventSourceConfiguration>();

            inputConfiguration.Add(new EventSourceConfiguration()
            {
                ProviderName = "EventSourceInput-TestEventSource"
            });
            var eventSourceInput = new EventSourceInput(inputConfiguration, healthReporterMock.Object);

            eventSourceInput.Activate();

            var observer = new Mock <IObserver <EventData> >();

            using (eventSourceInput.Subscribe(observer.Object))
            {
                EventSourceInputTestSource.Log.Tricky(7, "TrickyEvent", "Actual message");

                observer.Verify(s => s.OnNext(It.Is <EventData>(data =>
                                                                data.Payload["Message"].Equals("Manifest message") &&
                                                                data.Payload["EventId"].Equals(1) &&
                                                                data.Payload["EventName"].Equals("Tricky") &&
                                                                data.Payload[data.Payload.Keys.First(key => key.StartsWith("Message") && key != "Message")].Equals("Actual message") &&
                                                                data.Payload[data.Payload.Keys.First(key => key.StartsWith("EventId") && key != "EventId")].Equals(7) &&
                                                                data.Payload[data.Payload.Keys.First(key => key.StartsWith("EventName") && key != "EventName")].Equals("TrickyEvent")
                                                                )), Times.Exactly(1));

                healthReporterMock.Verify(o => o.ReportWarning(
                                              It.Is <string>(s => s.Contains("already exist in the event payload")),
                                              It.Is <string>(s => s == nameof(EventSourceInput))),
                                          Times.Exactly(3));
            }
        }
        public void CapturesEventsFromEventSourceExistingBeforeInputCreated()
        {
            var healthReporterMock = new Mock <IHealthReporter>();

            var inputConfiguration = new List <EventSourceConfiguration>();

            inputConfiguration.Add(new EventSourceConfiguration()
            {
                ProviderName = "EventSourceInput-TestEventSource"
            });

            // EventSourceInputTestSource has a static instance that exists before the input is created.
            // But it won't be actually hooked up to EventSource/EventListener infrastructure until an event is raised.
            EventSourceInputTestSource.Log.Message("ignored");

            var eventSourceInput = new EventSourceInput(inputConfiguration, healthReporterMock.Object);

            eventSourceInput.Activate();

            var observer = new Mock <IObserver <EventData> >();

            using (eventSourceInput.Subscribe(observer.Object))
            {
                EventSourceInputTestSource.Log.Message("Hello!");

                observer.Verify(s => s.OnNext(It.Is <EventData>(data =>
                                                                data.Payload["Message"].Equals("Hello!") &&
                                                                data.Payload["EventId"].Equals(2) &&
                                                                data.Payload["EventName"].Equals("Message")
                                                                )), Times.Exactly(1));

                healthReporterMock.Verify(o => o.ReportWarning(It.IsAny <string>(), It.IsAny <string>()), Times.Never());
                healthReporterMock.Verify(o => o.ReportProblem(It.IsAny <string>(), It.IsAny <string>()), Times.Never());
            }
        }
        public void CapturesEventsFromSourcesIdentifiedByNamePrefix()
        {
            var healthReporterMock = new Mock <IHealthReporter>();

            var inputConfiguration = new List <EventSourceConfiguration>();

            inputConfiguration.Add(new EventSourceConfiguration()
            {
                ProviderNamePrefix = "EventSourceInput-Test"
            });

            var eventSourceInput = new EventSourceInput(inputConfiguration, healthReporterMock.Object);

            eventSourceInput.Activate();

            var observer = new Mock <IObserver <EventData> >();

            using (eventSourceInput.Subscribe(observer.Object))
            {
                EventSourceInputTestSource.Log.Message("Hello!");

                observer.Verify(s => s.OnNext(It.Is <EventData>(data =>
                                                                data.Payload["Message"].Equals("Hello!") &&
                                                                data.Payload["EventId"].Equals(2) &&
                                                                data.Payload["EventName"].Equals("Message")
                                                                )), Times.Exactly(1));

                healthReporterMock.Verify(o => o.ReportWarning(It.IsAny <string>(), It.IsAny <string>()), Times.Never());
                healthReporterMock.Verify(o => o.ReportProblem(It.IsAny <string>(), It.IsAny <string>()), Times.Never());
            }
        }
        public void CapturesEventsFromEventSourceCreatedAfterInputCreated()
        {
            var healthReporterMock = new Mock <IHealthReporter>();

            var inputConfiguration = new List <EventSourceConfiguration>();

            inputConfiguration.Add(new EventSourceConfiguration()
            {
                ProviderName = "EventSourceInput-OtherTestEventSource"
            });

            var eventSourceInput = new EventSourceInput(inputConfiguration, healthReporterMock.Object);

            eventSourceInput.Activate();

            var observer = new Mock <IObserver <EventData> >();

            using (eventSourceInput.Subscribe(observer.Object))
                using (var eventSource = new EventSourceInputTestOtherSource())
                {
                    eventSource.Message("Wow!");

                    observer.Verify(s => s.OnNext(It.Is <EventData>(data =>
                                                                    data.Payload["Message"].Equals("Wow!") &&
                                                                    data.Payload["EventId"].Equals(3) &&
                                                                    data.Payload["EventName"].Equals("Message")
                                                                    )), Times.Exactly(1));

                    healthReporterMock.Verify(o => o.ReportWarning(It.IsAny <string>(), It.IsAny <string>()), Times.Never());
                    healthReporterMock.Verify(o => o.ReportProblem(It.IsAny <string>(), It.IsAny <string>()), Times.Never());
                }
        }
        public async Task CapturesEventCountersFromEventSource()
        {
            var healthReporterMock = new Mock <IHealthReporter>();

            var inputConfiguration = new List <EventSourceConfiguration>();

            inputConfiguration.Add(new EventSourceConfiguration()
            {
                ProviderName = "EventSourceInput-TestEventSource",
                EventCountersSamplingInterval = 1
            });

            var eventSourceInput = new EventSourceInput(inputConfiguration, healthReporterMock.Object);

            eventSourceInput.Activate();

            var testTaskCompletionSource = new TaskCompletionSource <EventData>();

            var observer = new Mock <IObserver <EventData> >();

            observer.Setup(o => o.OnNext(It.IsAny <EventData>())).Callback <EventData>(eventData =>
            {
                testTaskCompletionSource.TrySetResult(eventData);
            });

            using (eventSourceInput.Subscribe(observer.Object))
            {
                EventSourceInputTestSource.Log.ReportTestMetric(5);
                EventSourceInputTestSource.Log.ReportTestMetric(1);

                var firstTaskToComplete = await Task.WhenAny(
                    testTaskCompletionSource.Task,
                    Task.Delay(TimeSpan.FromSeconds(3))
                    );

                Assert.Equal(testTaskCompletionSource.Task, firstTaskToComplete);

                var data = testTaskCompletionSource.Task.Result;

                Assert.Equal(-1, data.Payload["EventId"]);
                Assert.Equal("EventCounters", data.Payload["EventName"]);
                Assert.Equal("testCounter", data.Payload["Name"]);
                Assert.Equal(2, data.Payload["Count"]);
#if NETCOREAPP3_1 || NET5_0
                Assert.Equal((double)5, data.Payload["Max"]);
                Assert.Equal((double)1, data.Payload["Min"]);
#else
                Assert.Equal((float)5, data.Payload["Max"]);
                Assert.Equal((float)1, data.Payload["Min"]);
#endif

                healthReporterMock.Verify(o => o.ReportWarning(It.IsAny <string>(), It.IsAny <string>()), Times.Never());
                healthReporterMock.Verify(o => o.ReportProblem(It.IsAny <string>(), It.IsAny <string>()), Times.Never());
            }
        }
        public void OmitsEventsFromSourcesDisabledByNamePrefix()
        {
            var healthReporterMock = new Mock <IHealthReporter>();

            var inputConfiguration = new List <EventSourceConfiguration>();

            inputConfiguration.Add(new EventSourceConfiguration()
            {
                ProviderNamePrefix = "EventSourceInput-Other"
            });

            var observer = new Mock <IObserver <EventData> >();

            using (var otherSource = new EventSourceInputTestOtherSource())
            {
                using (var eventSourceInput = new EventSourceInput(inputConfiguration, healthReporterMock.Object))
                {
                    eventSourceInput.Activate();

                    using (eventSourceInput.Subscribe(observer.Object))
                    {
                        otherSource.Message("Hey!");

                        observer.Verify(s => s.OnNext(It.IsAny <EventData>()), Times.Exactly(1));

                        healthReporterMock.Verify(o => o.ReportWarning(It.IsAny <string>(), It.IsAny <string>()), Times.Never());
                        healthReporterMock.Verify(o => o.ReportProblem(It.IsAny <string>(), It.IsAny <string>()), Times.Never());
                    }
                }

                inputConfiguration.Add(new EventSourceConfiguration()
                {
                    DisabledProviderNamePrefix = "EventSourceInput-Other"
                });
                observer.ResetCalls();

                using (var eventSourceInput = new EventSourceInput(inputConfiguration, healthReporterMock.Object))
                {
                    eventSourceInput.Activate();

                    using (eventSourceInput.Subscribe(observer.Object))
                    {
                        otherSource.Message("You!");

                        observer.Verify(s => s.OnNext(It.IsAny <EventData>()), Times.Exactly(0));  // Source disabled--should get zero events out of the input

                        healthReporterMock.Verify(o => o.ReportWarning(It.IsAny <string>(), It.IsAny <string>()), Times.Never());
                        healthReporterMock.Verify(o => o.ReportProblem(It.IsAny <string>(), It.IsAny <string>()), Times.Never());
                    }
                }
            }
        }
        public async Task AddsMetricDataToEventCounters()
        {
            var healthReporterMock = new Mock <IHealthReporter>();

            var inputConfiguration = new List <EventSourceConfiguration>();

            inputConfiguration.Add(new EventSourceConfiguration()
            {
                ProviderName = "EventSourceInput-TestEventSource",
                EventCountersSamplingInterval = 1
            });

            var eventSourceInput = new EventSourceInput(inputConfiguration, healthReporterMock.Object);

            eventSourceInput.Activate();

            var testTaskCompletionSource = new TaskCompletionSource <EventData>();

            var observer = new Mock <IObserver <EventData> >();

            observer.Setup(o => o.OnNext(It.IsAny <EventData>())).Callback <EventData>(eventData =>
            {
                testTaskCompletionSource.TrySetResult(eventData);
            });

            using (eventSourceInput.Subscribe(observer.Object))
            {
                EventSourceInputTestSource.Log.ReportTestMetric(5);
                EventSourceInputTestSource.Log.ReportTestMetric(1);

                var firstTaskToComplete = await Task.WhenAny(
                    testTaskCompletionSource.Task,
                    Task.Delay(TimeSpan.FromSeconds(3))
                    );

                Assert.Equal(testTaskCompletionSource.Task, firstTaskToComplete);

                var data = testTaskCompletionSource.Task.Result;

                Assert.True(data.TryGetMetadata(MetricData.MetricMetadataKind, out var metadata));
                Assert.Equal(6, metadata.Count);

                var expectedMetrics = new List <string>(new string[] { "Mean", "StandardDeviation", "Count", "Min", "Max", "IntervalSec" });
                Assert.All(metadata, em => expectedMetrics.Contains(em.Properties[MetricData.MetricValuePropertyMoniker], StringComparer.InvariantCulture));
                Assert.All(metadata, em => expectedMetrics.Contains(em.Properties[MetricData.MetricNameMoniker], StringComparer.InvariantCulture));
            }
        }
Ejemplo n.º 8
0
        public async Task AddsMetricDataToEventCounters()
        {
            var healthReporterMock = new Mock <IHealthReporter>();

            var inputConfiguration = new List <EventSourceConfiguration>();

            inputConfiguration.Add(new EventSourceConfiguration()
            {
                ProviderName = "EventSourceInput-TestEventSource",
                EventCountersSamplingInterval = 1
            });

            var eventSourceInput = new EventSourceInput(inputConfiguration, healthReporterMock.Object);

            eventSourceInput.Activate();

            var testTaskCompletionSource = new TaskCompletionSource <EventData>();

            var observer = new Mock <IObserver <EventData> >();

            observer.Setup(o => o.OnNext(It.IsAny <EventData>())).Callback <EventData>(eventData =>
            {
                testTaskCompletionSource.TrySetResult(eventData);
            });

            using (eventSourceInput.Subscribe(observer.Object))
            {
                EventSourceInputTestSource.Log.ReportTestMetric(5);
                EventSourceInputTestSource.Log.ReportTestMetric(1);

                var firstTaskToComplete = await Task.WhenAny(
                    testTaskCompletionSource.Task,
                    Task.Delay(TimeSpan.FromSeconds(3))
                    );

                Assert.Equal(testTaskCompletionSource.Task, firstTaskToComplete);

                var data = testTaskCompletionSource.Task.Result;

                Assert.True(data.TryGetMetadata(MetricData.MetricMetadataKind, out var metadata));
                foreach (EventMetadata eventMetadata in metadata)
                {
                    Assert.Equal(data.Payload[eventMetadata.Properties[MetricData.MetricNamePropertyMoniker]], data.Payload[eventMetadata.Properties[MetricData.MetricValuePropertyMoniker]]);
                }
            }
        }
        public void CanEnableSameSourceWithDifferentLevelsAndKeywords()
        {
            var healthReporterMock = new Mock <IHealthReporter>();

            var inputConfiguration = new List <EventSourceConfiguration>();

            inputConfiguration.Add(new EventSourceConfiguration()
            {
                ProviderNamePrefix = "EventSourceInput-Test",
                Level    = EventLevel.Warning,
                Keywords = EventSourceInputTestSource.Keywords.Important
            });
            inputConfiguration.Add(new EventSourceConfiguration()
            {
                ProviderNamePrefix = "EventSourceInput-Test",
                Level    = EventLevel.Informational,
                Keywords = EventSourceInputTestSource.Keywords.Negligible
            });

            var eventSourceInput = new EventSourceInput(inputConfiguration, healthReporterMock.Object);

            eventSourceInput.Activate();

            var observer = new Mock <IObserver <EventData> >();

            using (eventSourceInput.Subscribe(observer.Object))
            {
                EventSourceInputTestSource.Log.Tricky(1, "Foo", "Bar");     // Not captured because it is only Level=Informational
                EventSourceInputTestSource.Log.Message("Hey!");             // Captured
                EventSourceInputTestSource.Log.DebugMessage("Yo!");         // Not captured because Level=Verbose

                observer.Verify(s => s.OnNext(It.IsAny <EventData>()), Times.Exactly(1));

                healthReporterMock.Verify(o => o.ReportWarning(It.IsAny <string>(), It.IsAny <string>()), Times.Never());
                healthReporterMock.Verify(o => o.ReportProblem(It.IsAny <string>(), It.IsAny <string>()), Times.Never());
            }
        }