diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/CHANGELOG.md b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/CHANGELOG.md index b44da84baa9e..86b4c17399db 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/CHANGELOG.md +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/CHANGELOG.md @@ -18,6 +18,11 @@ the telemetry offline for retrying at a later time. ([#38832](https://github.com/Azure/azure-sdk-for-net/pull/38832)) +* Fixed an issue where `OriginalFormat` persisted in TraceTelemetry properties + with IncludeFormattedMessage enabled in OpenTelemetry LoggerProvider. This fix + prevents data duplication in message fields and properties. + ([#39308](https://github.com/Azure/azure-sdk-for-net/pull/39308)) + ### Other Changes * Update OpenTelemetry dependencies diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/LogsHelper.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/LogsHelper.cs index 9bc8b6fae5f7..e64c3867cd72 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/LogsHelper.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/LogsHelper.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Globalization; +using System.Linq; using System.Reflection; using System.Text; using Azure.Monitor.OpenTelemetry.Exporter.Internals.Diagnostics; @@ -66,9 +67,27 @@ internal static List OtelToAzureMonitorLogs(Batch batc { string? message = logRecord.Exception?.Message ?? logRecord.FormattedMessage; - if (logRecord.Attributes != null) + foreach (KeyValuePair item in logRecord.Attributes ?? Enumerable.Empty>()) { - ExtractProperties(ref message, properties, logRecord.Attributes); + if (item.Key.Length <= SchemaConstants.KVP_MaxKeyLength && item.Value != null) + { + // Note: if Key exceeds MaxLength, the entire KVP will be dropped. + if (item.Key == "{OriginalFormat}") + { + if (logRecord.Exception?.Message != null) + { + properties.Add("OriginalFormat", item.Value.ToString().Truncate(SchemaConstants.KVP_MaxValueLength) ?? "null"); + } + else if (message == null) + { + message = item.Value.ToString(); + } + } + else + { + properties.Add(item.Key, item.Value.ToString().Truncate(SchemaConstants.KVP_MaxValueLength) ?? "null"); + } + } } WriteScopeInformation(logRecord, properties); @@ -192,33 +211,6 @@ internal static SeverityLevel GetSeverityLevel(LogLevel logLevel) } } - private static void ExtractProperties(ref string? message, IDictionary properties, IReadOnlyCollection> stateDictionary) - { - foreach (KeyValuePair item in stateDictionary) - { - if (item.Key.Length <= SchemaConstants.KVP_MaxKeyLength && item.Value != null) - { - // Note: if Key exceeds MaxLength, the entire KVP will be dropped. - - if (item.Key == "{OriginalFormat}") - { - if (message == null) - { - message = item.Value.ToString(); - } - else - { - properties.Add("OriginalFormat", item.Value.ToString().Truncate(SchemaConstants.KVP_MaxValueLength) ?? "null"); - } - } - else - { - properties.Add(item.Key, item.Value.ToString().Truncate(SchemaConstants.KVP_MaxValueLength) ?? "null"); - } - } - } - } - private static string ConvertDepthToString(int depth) => $"{depth}"; } } diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/LogsHelperTests.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/LogsHelperTests.cs index 99a931fb7507..a781d69873f9 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/LogsHelperTests.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/LogsHelperTests.cs @@ -87,13 +87,11 @@ public void MessageIsSetToFormattedMessageWhenIncludeFormattedMessageIsSet() var message = LogsHelper.GetMessageAndSetProperties(logRecords[0], properties); Assert.Equal("Hello from tomato 2.99.", message); - Assert.True(properties.TryGetValue("OriginalFormat", out string value)); - Assert.Equal(log, value); Assert.True(properties.TryGetValue("name", out string name)); Assert.Equal("tomato", name); Assert.True(properties.TryGetValue("price", out string price)); Assert.Equal("2.99", price); - Assert.Equal(3, properties.Count); + Assert.Equal(2, properties.Count); } [Fact]