Skip to content

Commit

Permalink
[AzureMonitorExporter] Fix DuplicateKeyException when LogRecord conta…
Browse files Browse the repository at this point in the history
…ins EventId or EventName in both LogRecord and Attributes (#44748)

* Fix DuplicateKeyException when LogRecord

* Update changelog

* Update changelog
  • Loading branch information
rajkumar-rangaraj authored Jun 27, 2024
1 parent 4b5fde8 commit 45db82d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@

### Bugs Fixed

* Fixed an issue where a `DuplicateKeyException` could be thrown if `EventId`
and `EventName` were present in both `LogRecord` (`LogRecord.EventId`,
`LogRecord.EventName`) and `LogRecord.Attributes`. The method now uses
`EventId` and `EventName` from `LogRecord.Attributes` when both are present.
If they are not in `LogRecord.Attributes`, it uses the values from
`LogRecord.EventId` or `LogRecord.EventName`, preventing the `LogRecord` from
being dropped.
([#44748](https://github.com/Azure/azure-sdk-for-net/pull/44748))

### Other Changes

* Enabled support for log collection from Azure SDKs via `Microsoft.Extensions.Logging`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ internal static List<TelemetryItem> OtelToAzureMonitorLogs(Batch<LogRecord> batc

logRecord.ForEachScope(s_processScope, properties);

if (logRecord.EventId.Id != 0)
if (!properties.ContainsKey("EventId") && logRecord.EventId.Id != 0)
{
properties.Add("EventId", logRecord.EventId.Id.ToString(CultureInfo.InvariantCulture));
}

if (!string.IsNullOrEmpty(logRecord.EventId.Name))
if (!properties.ContainsKey("EventName") && !string.IsNullOrEmpty(logRecord.EventId.Name))
{
properties.Add("EventName", logRecord.EventId.Name!.Truncate(SchemaConstants.KVP_MaxValueLength));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,36 @@ public void PropertiesContainEventIdAndEventNameIfSetOnLog()
Assert.Equal(2, properties.Count);
}

[Fact]
public void LogRecordAndAttributesContainEventIdAndEventName()
{
var logRecords = new List<LogRecord>();
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options =>
{
options.AddInMemoryExporter(logRecords);
});
builder.AddFilter(typeof(LogsHelperTests).FullName, LogLevel.Trace);
});

var logger = loggerFactory.CreateLogger<LogsHelperTests>();

EventId id = new EventId(1, "TestEvent");

string log = "Log Information {EventId} {EventName}.";
logger.LogInformation(id, log, 100, "TestAttributeEventName");

var properties = new ChangeTrackingDictionary<string, string>();
LogsHelper.GetMessageAndSetProperties(logRecords[0], properties);

Assert.True(properties.TryGetValue("EventId", out string eventId));
Assert.Equal("100", eventId);
Assert.True(properties.TryGetValue("EventName", out string eventName));
Assert.Equal("TestAttributeEventName", eventName);
Assert.Equal(2, properties.Count);
}

[Fact]
public void ValidateSeverityLevels()
{
Expand Down

0 comments on commit 45db82d

Please sign in to comment.