Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Prevent data duplication in TraceTelemetry with IncludeFormattedMessage #39308

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
rajkumar-rangaraj marked this conversation as resolved.
Show resolved Hide resolved
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
rajkumar-rangaraj marked this conversation as resolved.
Show resolved Hide resolved
{
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));
rajkumar-rangaraj marked this conversation as resolved.
Show resolved Hide resolved
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