Skip to content

Commit

Permalink
Emit event source using the events API.
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch committed Sep 22, 2022
1 parent 1ae1966 commit d9560cc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// </copyright>

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Tracing;
Expand All @@ -32,12 +33,13 @@ public sealed class OpenTelemetryEventSourceLogEmitter : EventListener
{
private readonly bool includeFormattedMessage;
private readonly LoggerProvider loggerProvider;
private readonly InstrumentationScope instrumentationScope;
private readonly object lockObj = new();
private readonly Func<string, EventLevel?> shouldListenToFunc;
private readonly List<EventSource> eventSources = new();
private readonly List<EventSource>? eventSourcesBeforeConstructor = new();
private readonly bool disposeProvider;
private readonly Logger logger;
private readonly ConcurrentDictionary<string, Logger> loggers = new();

/// <summary>
/// Initializes a new instance of the <see
Expand Down Expand Up @@ -70,11 +72,10 @@ public sealed class OpenTelemetryEventSourceLogEmitter : EventListener
this.disposeProvider = disposeProvider;
this.shouldListenToFunc = shouldListenToFunc;

this.logger = loggerProvider.GetLogger(new LoggerOptions(
new InstrumentationScope("OpenTelemetry.Extensions.EventSource")
{
Version = $"semver:{typeof(OpenTelemetryEventSourceLogEmitter).Assembly.GetName().Version}",
}));
this.instrumentationScope = new InstrumentationScope("OpenTelemetry.Extensions.EventSource")
{
Version = $"semver:{typeof(OpenTelemetryEventSourceLogEmitter).Assembly.GetName().Version}",
};

lock (this.lockObj)
{
Expand Down Expand Up @@ -140,6 +141,21 @@ protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
Debug.Assert(eventData != null, "EventData was null.");

var eventSource = eventData!.EventSource;

Debug.Assert(eventSource != null, "eventSource was null.");

if (!this.loggers.TryGetValue(eventSource!.Name, out Logger? logger))
{
logger = this.loggerProvider.GetLogger(
new LoggerOptions(this.instrumentationScope)
{
EventDomain = eventSource!.Name,
});

this.loggers.TryAdd(eventSource.Name, logger);
}

string? rawMessage = eventData!.Message;

LogRecordData data = new(Activity.Current)
Expand All @@ -152,9 +168,8 @@ protected override void OnEventWritten(EventWrittenEventArgs eventData)

LogRecordAttributeList attributes = default;

attributes.Add("event_source.name", eventData.EventSource.Name);
// todo: Should there be event.id in the spec?
attributes.Add("event_source.event_id", eventData.EventId);
attributes.Add("event_source.event_name", eventData.EventName);

if (eventData.ActivityId != Guid.Empty)
{
Expand Down Expand Up @@ -202,7 +217,10 @@ protected override void OnEventWritten(EventWrittenEventArgs eventData)
}
}

this.logger.EmitLog(in data, in attributes);
logger.EmitEvent(
eventData.EventName ?? $"{eventData.EventId}",
in data,
in attributes);
}
#pragma warning restore CA1062 // Validate arguments of public methods

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ public void OpenTelemetryEventSourceLogEmitterSimpleEventTest()
Assert.Equal(ActivityTraceFlags.None, logRecord.TraceFlags);

Assert.NotNull(logRecord.Attributes);
Assert.Contains(logRecord.Attributes, kvp => kvp.Key == "event_source.name" && (string?)kvp.Value == TestEventSource.EventSourceName);
Assert.Contains(logRecord.Attributes, kvp => kvp.Key == "event.domain" && (string?)kvp.Value == TestEventSource.EventSourceName);
Assert.Contains(logRecord.Attributes, kvp => kvp.Key == "event_source.event_id" && (int?)kvp.Value == TestEventSource.SimpleEventId);
Assert.Contains(logRecord.Attributes, kvp => kvp.Key == "event_source.event_name" && (string?)kvp.Value == nameof(TestEventSource.SimpleEvent));
Assert.Contains(logRecord.Attributes, kvp => kvp.Key == "event.name" && (string?)kvp.Value == nameof(TestEventSource.SimpleEvent));
}

[Fact]
Expand Down Expand Up @@ -218,12 +218,12 @@ public void OpenTelemetryEventSourceLogEmitterComplexEventTest(bool formatMessag
Assert.Equal(ActivityTraceFlags.None, logRecord.TraceFlags);

Assert.NotNull(logRecord.Attributes);
Assert.Contains(logRecord.Attributes, kvp => kvp.Key == "event_source.name" && (string?)kvp.Value == TestEventSource.EventSourceName);
Assert.Contains(logRecord.Attributes, kvp => kvp.Key == "event.domain" && (string?)kvp.Value == TestEventSource.EventSourceName);
Assert.Contains(logRecord.Attributes, kvp => kvp.Key == "arg1" && (string?)kvp.Value == "Test_Message");
Assert.Contains(logRecord.Attributes, kvp => kvp.Key == "arg2" && (int?)kvp.Value == 18);

Assert.Contains(logRecord.Attributes, kvp => kvp.Key == "event_source.event_id" && (int?)kvp.Value == TestEventSource.ComplexEventId);
Assert.Contains(logRecord.Attributes, kvp => kvp.Key == "event_source.event_name" && (string?)kvp.Value == nameof(TestEventSource.ComplexEvent));
Assert.Contains(logRecord.Attributes, kvp => kvp.Key == "event.name" && (string?)kvp.Value == nameof(TestEventSource.ComplexEvent));

if (formatMessage)
{
Expand Down

0 comments on commit d9560cc

Please sign in to comment.