Skip to content

Commit

Permalink
Fix: Prevent data duplication in TraceTelemetry with IncludeFormatted…
Browse files Browse the repository at this point in the history
…Message (#39308)

* Fix: Prevent data duplication in TraceTelemetry with IncludeFormattedMessage.

* Update Changelog

* PR feedback on Exception original format.

* test changes

* Test changes.
  • Loading branch information
rajkumar-rangaraj authored Oct 18, 2023
1 parent 62c83cc commit de7daf4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 32 deletions.
5 changes: 5 additions & 0 deletions sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,9 +67,27 @@ internal static List<TelemetryItem> OtelToAzureMonitorLogs(Batch<LogRecord> batc
{
string? message = logRecord.Exception?.Message ?? logRecord.FormattedMessage;

if (logRecord.Attributes != null)
foreach (KeyValuePair<string, object?> item in logRecord.Attributes ?? Enumerable.Empty<KeyValuePair<string, object?>>())
{
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);
Expand Down Expand Up @@ -192,33 +211,6 @@ internal static SeverityLevel GetSeverityLevel(LogLevel logLevel)
}
}

private static void ExtractProperties(ref string? message, IDictionary<string, string> properties, IReadOnlyCollection<KeyValuePair<string, object?>> stateDictionary)
{
foreach (KeyValuePair<string, object?> 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}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit de7daf4

Please sign in to comment.