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 CanReadKeywordsInHexFormat()
        {
            string inputConfiguration = @"
                {
                    ""type"": ""EventSource"",
                    ""sources"": [
                        { ""providerName"": ""EventSourceInput-TestEventSource"", ""keywords"": ""0x05"" }
                    ]
                }";


            using (var configFile = new TemporaryFile())
            {
                configFile.Write(inputConfiguration);
                var cb = new ConfigurationBuilder();
                cb.AddJsonFile(configFile.FilePath);
                var configuration = cb.Build();

                var healthReporterMock = new Mock <IHealthReporter>();
                var input = new EventSourceInput(configuration, healthReporterMock.Object);

                Assert.Equal((EventKeywords)0x5, input.EventSources.First().Keywords);

                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 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 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));
            }
        }
Пример #9
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 CannotEnableByNameAndByPrefixBySingleConfigurationItem()
        {
            var healthReporterMock = new Mock <IHealthReporter>();

            var inputConfiguration = new List <EventSourceConfiguration>();

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

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

            using (var eventSourceInput = new EventSourceInput(inputConfiguration, healthReporterMock.Object))
            {
                healthReporterMock.Verify(o => o.ReportProblem(It.IsAny <string>(), It.Is <string>(s => s == EventFlowContextIdentifiers.Configuration)), Times.Once());
            }
        }
        public void CannotEnableWhenEventCountersSamplingIntervalBelowZero()
        {
            var healthReporterMock = new Mock <IHealthReporter>();

            var inputConfiguration = new List <EventSourceConfiguration>();

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

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

            using (var eventSourceInput = new EventSourceInput(inputConfiguration, healthReporterMock.Object))
            {
                healthReporterMock.Verify(o => o.ReportProblem(It.IsAny <string>(), It.Is <string>(s => s == EventFlowContextIdentifiers.Configuration)), Times.Once());
            }
        }
        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());
            }
        }
        public void DisabledSourcesCannotSpecifyLevelOrKeywords()
        {
            var healthReporterMock = new Mock <IHealthReporter>();

            var inputConfiguration = new List <EventSourceConfiguration>();

            inputConfiguration.Add(new EventSourceConfiguration()
            {
                DisabledProviderNamePrefix = "EventSourceInput-Other",
                Level = EventLevel.Warning
            });
            inputConfiguration.Add(new EventSourceConfiguration()
            {
                DisabledProviderNamePrefix = "EventSourceInput-Test",
                Keywords = (EventKeywords)0x4
            });

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

            using (var eventSourceInput = new EventSourceInput(inputConfiguration, healthReporterMock.Object))
            {
                healthReporterMock.Verify(o => o.ReportProblem(It.IsAny <string>(), It.Is <string>(s => s == EventFlowContextIdentifiers.Configuration)), Times.Exactly(2));
            }
        }
Пример #14
0
        static void Main(string[] args)
        {
            var healthReporter = new BenchmarkHealthReporter();

            var esConfiguration = new EventSourceConfiguration();

            esConfiguration.ProviderName = "Microsoft-AzureTools-BenchmarkEventSource";
            var eventSourceInput = new EventSourceInput(new EventSourceConfiguration[] { esConfiguration }, healthReporter);

            var metadata = new EventMetadata("importantEvent");

            metadata.Properties.Add("Importance", "High");
            EventMetadataFilter metadataFilter = new EventMetadataFilter(metadata);

            metadataFilter.IncludeCondition = "name==ImportantEvent";

            var oddSequenceNumberFilter  = new CallbackFilter((e) => HasLongPropertyWhere(e.Payload, "sequenceNumber", (n) => (n & 0x1) != 0));
            var evenSequenceNumberFilter = new CallbackFilter((e) => HasLongPropertyWhere(e.Payload, "sequenceNumber", (n) => (n & 0x1) == 0));
            var oddSequenceNumberOutput  = new BenchmarkOutput("Odd sequence number output");
            var evenSequenceNumberOutput = new BenchmarkOutput("Even sequence number output");
            var sinks = new EventSink[]
            {
                new EventSink(oddSequenceNumberOutput, new IFilter[] { oddSequenceNumberFilter }),
                new EventSink(evenSequenceNumberOutput, new IFilter[] { evenSequenceNumberFilter })
            };

            var configuration = new DiagnosticPipelineConfiguration();

            configuration.PipelineCompletionTimeoutMsec = Convert.ToInt32(CoolDownTime.TotalMilliseconds);
            var pipeline = new DiagnosticPipeline(
                healthReporter,
                new IObservable <EventData>[] { eventSourceInput },
                new IFilter[] { metadataFilter },
                sinks,
                configuration);

            Console.WriteLine(string.Format("Starting test... will take {0} seconds", (WarmUpTime + MeasurementTime).TotalSeconds));
            Console.WriteLine("A dot represents 10 000 events submitted");

            DateTimeOffset startTime            = DateTimeOffset.Now;
            DateTimeOffset measurementStartTime = startTime + WarmUpTime;
            DateTimeOffset measurementStopTime  = measurementStartTime + MeasurementTime;
            DateTimeOffset stopTime             = measurementStopTime + CoolDownTime;

            bool debugMode = args.Length == 1 && string.Equals(args[0], "debug", StringComparison.OrdinalIgnoreCase);

            long eventSequenceNo    = 1;
            bool measuring          = debugMode;
            long measuredEventCount = 0;

            while (true)
            {
                // Every tenth event is important
                string name = "RegularEvent";
                if (eventSequenceNo % 10 == 0)
                {
                    name = "ImportantEvent";
                }

                BenchmarkEventSource.Log.ComplexMessage(
                    Guid.NewGuid(),
                    Guid.NewGuid(),
                    eventSequenceNo++,
                    name,
                    DateTime.Now,
                    DateTime.UtcNow,
                    "Complex event message. blah blah");

                if (measuring)
                {
                    measuredEventCount++;
                }

                if (debugMode)
                {
                    if (measuredEventCount == 10)
                    {
                        break;
                    }
                }
                else
                {
                    if (eventSequenceNo % EventBatchSize == 0)
                    {
                        DateTimeOffset now = DateTimeOffset.Now;

                        // In this benchmmark we do not really have a cooldown period, so we do not have to account for not-measuring period at the end.
                        if (!measuring)
                        {
                            bool shouldBeMeasuring = (now >= measurementStartTime && now < measurementStopTime);
                            if (shouldBeMeasuring)
                            {
                                oddSequenceNumberOutput.ResetCounter();
                                evenSequenceNumberOutput.ResetCounter();
                                healthReporter.ResetCounters();
                                measuring = true;
                            }
                        }


                        if (eventSequenceNo % 10000 == 0)
                        {
                            Console.Write(".");
                        }

                        if (now >= stopTime)
                        {
                            break;
                        }
                    }
                }
            }

            Console.WriteLine(string.Empty);
            Console.WriteLine("Enterning cooldown period...");
            Thread.Sleep(CoolDownTime);
            pipeline.Dispose();

            Console.WriteLine(string.Empty);
            Console.WriteLine($"Total events raised during measurement period: {measuredEventCount}");
            Console.WriteLine(oddSequenceNumberOutput.Summary);
            Console.WriteLine(evenSequenceNumberOutput.Summary);
            double processingRate = (oddSequenceNumberOutput.EventCount + evenSequenceNumberOutput.EventCount) / MeasurementTime.TotalSeconds;

            Console.WriteLine($"Processing rate (events/sec): {processingRate}");
            Console.WriteLine(healthReporter.Summary);

            Console.WriteLine("Press any key to exit");
            Console.ReadKey(intercept: true);
        }