Exemplo n.º 1
0
        public static EventData ToEventData(this EventWrittenEventArgs eventSourceEvent, IHealthReporter healthReporter, string context)
        {
            Debug.Assert(healthReporter != null);

            EventData eventData = new EventData
            {
                ProviderName = eventSourceEvent.EventSource.Name,
                Timestamp    = DateTime.UtcNow,
                Level        = (LogLevel)(int)eventSourceEvent.Level,
                Keywords     = (long)eventSourceEvent.Keywords
            };

            eventSourceEvent.ExtractPayloadData(eventData);

            eventData.AddPayloadProperty("EventId", eventSourceEvent.EventId, healthReporter, context);
            eventData.AddPayloadProperty("EventName", eventSourceEvent.EventName, healthReporter, context);
            eventData.AddPayloadProperty("ActivityID", ActivityPathString(eventSourceEvent.ActivityId), healthReporter, context);
            try
            {
                if (eventSourceEvent.Message != null)
                {
                    // If the event has a badly formatted manifest, the FormattedMessage property getter might throw
                    eventData.AddPayloadProperty("Message",
                                                 string.Format(CultureInfo.InvariantCulture, eventSourceEvent.Message, eventSourceEvent.Payload.ToArray()),
                                                 healthReporter,
                                                 context);
                }
            }
            catch { }

            return(eventData);
        }
        public void OnNext(KeyValuePair <string, object> pair)
        {
            var eventData = new EventData {
                ProviderName = _providerName, Timestamp = DateTimePrecise.UtcNow
            };
            var payload = eventData.Payload;

            payload["EventName"] = pair.Key;
            payload["Value"]     = pair.Value;

            var activity = Activity.Current;

            if (activity != null)
            {
                var id = activity.Id;
                if (id != null)
                {
                    payload["ActivityId"] = id;
                }

                var parentId = activity.ParentId;
                if (parentId != null)
                {
                    payload["ActivityParentId"] = parentId;
                }

                var duration = activity.Duration;
                if (duration != TimeSpan.Zero)
                {
                    payload["Duration"] = duration;
                }

                var baggage = activity.Baggage;
                if (baggage != null)
                {
                    foreach (var bag in baggage)
                    {
                        eventData.AddPayloadProperty(bag.Key, bag.Value, _healthReporter, nameof(DiagnosticSourceInput));
                    }
                }

                var tags = activity.Tags;
                if (tags != null)
                {
                    foreach (var tag in tags)
                    {
                        eventData.AddPayloadProperty(tag.Key, tag.Value, _healthReporter, nameof(DiagnosticSourceInput));
                    }
                }
            }

            _output.OnNext(eventData);
        }
Exemplo n.º 3
0
        public void AddPayloadPropertyDoesNotAddDuplicatesIfKeyAndValueEquals()
        {
            var healthReporterMock = new Mock <IHealthReporter>();
            var data = new EventData();

            data.AddPayloadProperty("prop", "value", healthReporterMock.Object, "tests");
            data.AddPayloadProperty("prop", "value", healthReporterMock.Object, "tests");

            Assert.Equal(1, data.Payload.Keys.Count);
            object value;

            Assert.True(data.TryGetPropertyValue("prop", out value));
            Assert.Equal("value", value);
            healthReporterMock.Verify(m => m.ReportWarning(It.IsAny <string>(), It.IsAny <string>()), Times.Never);
        }
Exemplo n.º 4
0
        public void AddPayloadPropertyAddsIndexedValuesIfKeyEqualsButValueDiffers()
        {
            var healthReporterMock = new Mock <IHealthReporter>();
            var data = new EventData();

            data.AddPayloadProperty("prop", "value1", healthReporterMock.Object, "tests");
            data.AddPayloadProperty("prop", "value2", healthReporterMock.Object, "tests");

            Assert.Equal(2, data.Payload.Keys.Count);
            object value;

            Assert.True(data.TryGetPropertyValue("prop", out value));
            Assert.Equal("value1", value);

            Assert.True(data.TryGetPropertyValue("prop_1", out value));
            Assert.Equal("value2", value);
        }
Exemplo n.º 5
0
 private static void ExtractEventCounterPayloadData(this EventWrittenEventArgs eventSourceEvent, EventData eventData, IHealthReporter healthReporter, string context)
 {
     foreach (var payload in (IDictionary <string, object>)eventSourceEvent.Payload[0])
     {
         eventData.AddPayloadProperty(payload.Key, payload.Value, healthReporter, context);
         eventData.SetMetadata(CreateMetricMetadata(payload.Key));
     }
 }
Exemplo n.º 6
0
        public async Task ProducesJsonLinesIfRequested()
        {
            var config = new HttpOutputConfiguration();

            config.ServiceUri = "http://*****:*****@"
                {
                    ""Timestamp"":""2018-01-02T14:12:00+00:00"",
                    ""ProviderName"":""HttpOutputTests"",
                    ""Level"":4,
                    ""Keywords"":0,
                    ""Payload"":{""Message"":""Hey!""}
                }
                {
                    ""Timestamp"":""2018-01-02T14:14:20+00:00"",
                    ""ProviderName"":""HttpOutputTests"",
                    ""Level"":3,
                    ""Keywords"":0,
                    ""Payload"":{""Message"":""Hey!""}
                }";

            expectedContent = RemoveWhitespace(expectedContent);

            await output.SendEventsAsync(events, 78, CancellationToken.None);

            httpClientMock.Verify(client => client.PostAsync(
                                      new Uri("http://logcollector:1234"),
                                      It.Is <HttpContent>(content => RemoveWhitespace(content.ReadAsStringAsync().GetAwaiter().GetResult()) == expectedContent)
                                      ), Times.Once());
        }
Exemplo n.º 7
0
        private static void ExtractEventPayloadData(this EventWrittenEventArgs eventSourceEvent, EventData eventData, IHealthReporter healthReporter, string context)
        {
            IEnumerator <object> payloadEnumerator       = eventSourceEvent.Payload.GetEnumerator();
            IEnumerator <string> payloadNamesEnunmerator = eventSourceEvent.PayloadNames.GetEnumerator();

            while (payloadEnumerator.MoveNext())
            {
                payloadNamesEnunmerator.MoveNext();
                eventData.AddPayloadProperty(payloadNamesEnunmerator.Current, payloadEnumerator.Current, healthReporter, context);
            }
        }
Exemplo n.º 8
0
        private EventData ToEventData(LogEvent logEvent)
        {
            EventData eventData = new EventData
            {
                ProviderName = nameof(SerilogInput),
                Timestamp    = logEvent.Timestamp,
                Level        = ToLogLevel[logEvent.Level],
                Keywords     = 0
            };

            var payload = eventData.Payload;

            foreach (var property in logEvent.Properties.Where(property => property.Value != null))
            {
                try
                {
                    payload[property.Key] = ToRawValue(property.Value);
                }
                catch (Exception e)
                {
                    healthReporter.ReportWarning($"{nameof(SerilogInput)}: event property '{property.Key}' could not be rendered{Environment.NewLine}{e.ToString()}");
                }
            }

            if (logEvent.Exception != null)
            {
                eventData.AddPayloadProperty("Exception", logEvent.Exception, healthReporter, nameof(SerilogInput));
            }

            // Inability to render the message, or any other LogEvent property, should not stop us from sending the event down the pipeline
            try
            {
                eventData.AddPayloadProperty("Message", logEvent.RenderMessage(), healthReporter, nameof(SerilogInput));
            }
            catch (Exception e)
            {
                healthReporter.ReportWarning($"{nameof(SerilogInput)}: event message could not be rendered{Environment.NewLine}{e.ToString()}");
            }

            return(eventData);
        }
Exemplo n.º 9
0
        public void AddPayloadPropertyDoesNotAddDuplicatesIfKeyAndValueEqualsAtIndexedValues()
        {
            var healthReporterMock = new Mock <IHealthReporter>();
            var data = new EventData();

            data.AddPayloadProperty("prop", "value1", healthReporterMock.Object, "tests");
            data.AddPayloadProperty("prop", "value2", healthReporterMock.Object, "tests");
            data.AddPayloadProperty("prop", "value3", healthReporterMock.Object, "tests");
            data.AddPayloadProperty("prop", "value3", healthReporterMock.Object, "tests");

            Assert.Equal(3, data.Payload.Keys.Count);
            object value;

            Assert.True(data.TryGetPropertyValue("prop", out value));
            Assert.Equal("value1", value);
            Assert.True(data.TryGetPropertyValue("prop_1", out value));
            Assert.Equal("value2", value);
            Assert.True(data.TryGetPropertyValue("prop_2", out value));
            Assert.Equal("value3", value);
            healthReporterMock.Verify(m => m.ReportWarning(It.Is <string>(s => s.StartsWith("The property with the key 'prop' already exist in the event payload with equivalent value under key 'prop_2'")), It.IsIn("tests")));
        }
Exemplo n.º 10
0
 FilterResult IFilter.Evaluate(EventData eventData)
 {
     // TODO: check the eventData object to see if the event data involves a dependency
     // call to the telemetry collector, and return
     // FilterResult.DiscardEvent instead of KeepEvent
     if (eventData.Payload["TelemetryType"].ToString() == "dependency" && eventData.Payload["Name"].ToString().StartsWith("POST") && eventData.Payload["Name"].ToString().EndsWith("/api/Collect"))
     {
         return(FilterResult.DiscardEvent);
     }
     else
     {
         eventData.AddPayloadProperty("ServerName", MachineName, HealthReporter, "CustomFilter");
         return(FilterResult.KeepEvent);
     }
 }
Exemplo n.º 11
0
 FilterResult IFilter.Evaluate(EventData eventData)
 {
     // By default, my ApplicationInsightsIngester accepts new telemetry posted to an endpoint that ends with /api/Collect.so i'm not interested in
     // If I were to record telemetry about this dependency there would be an continuous
     // loop of telemetry recorded about telemetry recorded about telemetry recorded about telemetry... ..you get the idea.
     if (eventData.Payload["TelemetryType"].ToString() == "dependency" && eventData.Payload["Name"].ToString().StartsWith("POST") && eventData.Payload["Name"].ToString().EndsWith("/api/Collect"))
     {
         return(FilterResult.DiscardEvent);
     }
     else
     {
         eventData.AddPayloadProperty("ServerName", MachineName, HealthReporter, "CustomFilter");
         return(FilterResult.KeepEvent);
     }
 }
Exemplo n.º 12
0
        private List <EventData> GetTestBatch(IHealthReporter healthReporter)
        {
            var events = new List <EventData>(2);

            var e = new EventData();

            e.Timestamp    = new DateTimeOffset(2018, 1, 2, 14, 12, 0, TimeSpan.Zero);
            e.ProviderName = nameof(HttpOutputTests);
            e.Level        = LogLevel.Informational;
            e.AddPayloadProperty("Message", "Hey!", healthReporter, "tests");
            events.Add(e);

            e              = new EventData();
            e.Timestamp    = new DateTimeOffset(2018, 1, 2, 14, 14, 20, TimeSpan.Zero);
            e.ProviderName = nameof(HttpOutputTests);
            e.Level        = LogLevel.Warning;
            e.AddPayloadProperty("Message", "Hey!", healthReporter, "tests");
            events.Add(e);

            return(events);
        }
        private static void ExtractPayloadData(this TraceEvent traceEvent, EventData eventData, IHealthReporter healthReporter)
        {
            bool hasPayload = traceEvent.PayloadNames != null && traceEvent.PayloadNames.Length > 0;

            if (!hasPayload)
            {
                return;
            }

            IDictionary <string, object> payloadData = eventData.Payload;

            for (int i = 0; i < traceEvent.PayloadNames.Length; i++)
            {
                try
                {
                    var payloadName  = traceEvent.PayloadNames[i];
                    var payloadValue = traceEvent.PayloadValue(i);

                    eventData.AddPayloadProperty(payloadName, payloadValue, healthReporter, nameof(EtwInput));
                }
                catch { }
            }
        }
 private void AddPayloadProperty(EventData eventData, string key, object value)
 {
     eventData.AddPayloadProperty(key, value, this.healthReporter, nameof(EventFlowTelemetryProcessor));
 }