-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start Azure SDK log forwarder in AzMon OTel distro (#42374)
* Start Azure SDK log forwarder in AzMon OTel distro
- Loading branch information
Showing
9 changed files
with
398 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.Tracing; | ||
|
||
#nullable enable | ||
|
||
namespace Azure.Core.Shared | ||
{ | ||
/// <summary> | ||
/// Wraps <see cref="EventWrittenEventArgs"/> into <see cref="IReadOnlyList{T}"/> simplifying iterating over | ||
/// payload properties and providing them to logging libraries in a structured way. | ||
/// </summary> | ||
internal readonly struct EventSourceEvent : IReadOnlyList<KeyValuePair<string, object?>> | ||
{ | ||
/// <summary> | ||
/// Gets underlying EventSource event. | ||
/// </summary> | ||
public EventWrittenEventArgs EventData { get; } | ||
|
||
public EventSourceEvent(EventWrittenEventArgs eventData) | ||
{ | ||
EventData = eventData; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IEnumerator<KeyValuePair<string, object?>> GetEnumerator() | ||
{ | ||
if (EventData.PayloadNames == null || EventData.Payload == null) | ||
{ | ||
yield break; | ||
} | ||
|
||
for (int i = 0; i < Count; i++) | ||
{ | ||
yield return new KeyValuePair<string, object?>(EventData.PayloadNames[i], EventData.Payload[i]); | ||
} | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
|
||
/// <summary> | ||
/// Returns the count of payload properties in the Eventsource event. | ||
/// </summary> | ||
public int Count => EventData.PayloadNames?.Count ?? 0; | ||
|
||
/// <summary> | ||
/// Formats EventSource event as a string including all payload properties. | ||
/// </summary> | ||
public string Format() | ||
{ | ||
return EventSourceEventFormatting.Format(EventData); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public KeyValuePair<string, object?> this[int index] | ||
{ | ||
get | ||
{ | ||
if (EventData.PayloadNames == null || EventData.Payload == null || index >= EventData.PayloadNames.Count || index < 0) | ||
{ | ||
throw new IndexOutOfRangeException("Index was out of range."); | ||
} | ||
|
||
return new KeyValuePair<string, object?>(EventData.PayloadNames[index], EventData.Payload[index]); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...tor.OpenTelemetry.AspNetCore/src/Internals/AzureSdkCompat/AzureEventSourceLogForwarder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Collections.Concurrent; | ||
using System.Diagnostics.Tracing; | ||
using Azure.Core.Diagnostics; | ||
using Azure.Core.Shared; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Azure.Monitor.OpenTelemetry.AspNetCore.Internals.AzureSdkCompat | ||
{ | ||
internal sealed class AzureEventSourceLogForwarder : IHostedService, IDisposable | ||
{ | ||
internal static readonly AzureEventSourceLogForwarder Noop = new AzureEventSourceLogForwarder(null); | ||
private readonly ILoggerFactory _loggerFactory; | ||
|
||
private readonly ConcurrentDictionary<string, ILogger> _loggers = new ConcurrentDictionary<string, ILogger>(); | ||
|
||
private readonly Func<EventSourceEvent, Exception, string> _formatMessage = FormatMessage; | ||
|
||
private AzureEventSourceListener _listener; | ||
|
||
public AzureEventSourceLogForwarder(ILoggerFactory loggerFactory) | ||
{ | ||
_loggerFactory = loggerFactory; | ||
} | ||
|
||
private void LogEvent(EventWrittenEventArgs eventData) | ||
{ | ||
var logger = _loggers.GetOrAdd(eventData.EventSource.Name, name => _loggerFactory!.CreateLogger(ToLoggerName(name))); | ||
logger.Log(MapLevel(eventData.Level), new EventId(eventData.EventId, eventData.EventName), new EventSourceEvent(eventData), null, _formatMessage); | ||
} | ||
|
||
private static string ToLoggerName(string name) | ||
{ | ||
return name.Replace('-', '.'); | ||
} | ||
|
||
private static LogLevel MapLevel(EventLevel level) | ||
{ | ||
switch (level) | ||
{ | ||
case EventLevel.Critical: | ||
return LogLevel.Critical; | ||
case EventLevel.Error: | ||
return LogLevel.Error; | ||
case EventLevel.Informational: | ||
return LogLevel.Information; | ||
case EventLevel.Verbose: | ||
return LogLevel.Debug; | ||
case EventLevel.Warning: | ||
return LogLevel.Warning; | ||
case EventLevel.LogAlways: | ||
return LogLevel.Information; | ||
default: | ||
AzureMonitorAspNetCoreEventSource.Log.MapLogLevelFailed(level); | ||
return LogLevel.None; | ||
} | ||
} | ||
|
||
private static string FormatMessage(EventSourceEvent eventSourceEvent, Exception _) => eventSourceEvent.Format(); | ||
|
||
public Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
if (_loggerFactory != null) | ||
{ | ||
_listener ??= new AzureEventSourceListener((e, s) => LogEvent(e), EventLevel.Verbose); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
_listener?.Dispose(); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_listener?.Dispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.