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

[sdk-1.5.0-hotfix] Fix LogRecord.State being null when TState matches known interfaces #4609

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
10 changes: 10 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

## Unreleased

## 1.5.1

Released 2023-Jun-23

* Fixed a breaking change causing `LogRecord.State` to be `null` where it was
previously set to a valid value when
`OpenTelemetryLoggerOptions.ParseStateValues` is `false` and states implement
`IReadOnlyList` or `IEnumerable` of `KeyValuePair<string, object>`s.
([#4609](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4609))

## 1.5.0

Released 2023-Jun-05
Expand Down
22 changes: 14 additions & 8 deletions src/OpenTelemetry/Logs/ILogger/OpenTelemetryLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except

LogRecordData.SetActivityContext(ref data, activity);

var attributes = record.Attributes = this.options.IncludeAttributes
? ProcessState(record, ref iloggerData, in state, this.options.ParseStateValues)
: null;
var attributes = record.Attributes =
ProcessState(record, ref iloggerData, in state, this.options.IncludeAttributes, this.options.ParseStateValues);

if (!TryGetOriginalFormatFromAttributes(attributes, out var originalFormat))
{
Expand Down Expand Up @@ -153,9 +152,19 @@ internal static void SetLogRecordSeverityFields(ref LogRecordData logRecordData,
LogRecord logRecord,
ref LogRecord.LogRecordILoggerData iLoggerData,
in TState state,
bool includeAttributes,
bool parseStateValues)
{
iLoggerData.State = null;
if (!includeAttributes
|| (!typeof(TState).IsValueType && state is null))
{
iLoggerData.State = null;
return null;
}

// Note: This is to preserve legacy behavior where State is
// exposed if we didn't parse state.
iLoggerData.State = !parseStateValues ? state : null;

/* TODO: Enable this if/when LogRecordAttributeList becomes public.
if (typeof(TState) == typeof(LogRecordAttributeList))
Expand Down Expand Up @@ -187,11 +196,8 @@ internal static void SetLogRecordSeverityFields(ref LogRecordData logRecordData,
return attributeStorage;
}
}
else if (!parseStateValues || state is null)
else if (!parseStateValues)
{
// Note: This is to preserve legacy behavior where State is
// exposed if we didn't parse state.
iLoggerData.State = state;
return null;
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void AddOtlpLogExporterReceivesAttributesWithParseStateValueSetToFalse()
Assert.Single(logRecords);
var logRecord = logRecords[0];
#pragma warning disable CS0618 // Type or member is obsolete
Assert.Null(logRecord.State);
Assert.NotNull(logRecord.State);
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
#pragma warning restore CS0618 // Type or member is obsolete
Assert.NotNull(logRecord.Attributes);
}
Expand Down
39 changes: 30 additions & 9 deletions test/OpenTelemetry.Tests/Logs/LogRecordTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void CheckStateForUnstructuredLog(bool includeFormattedMessage)
const string message = "Hello, World!";
logger.LogInformation(message);

Assert.Null(exportedItems[0].State);
Assert.NotNull(exportedItems[0].State);

var attributes = exportedItems[0].Attributes;
Assert.NotNull(attributes);
Expand Down Expand Up @@ -113,7 +113,7 @@ public void CheckStateForUnstructuredLogWithStringInterpolation(bool includeForm
var message = $"Hello from potato {0.99}.";
logger.LogInformation(message);

Assert.Null(exportedItems[0].State);
Assert.NotNull(exportedItems[0].State);

var attributes = exportedItems[0].Attributes;
Assert.NotNull(attributes);
Expand Down Expand Up @@ -143,7 +143,7 @@ public void CheckStateForStructuredLogWithTemplate(bool includeFormattedMessage)
const string message = "Hello from {name} {price}.";
logger.LogInformation(message, "tomato", 2.99);

Assert.Null(exportedItems[0].State);
Assert.NotNull(exportedItems[0].State);

var attributes = exportedItems[0].Attributes;
Assert.NotNull(attributes);
Expand Down Expand Up @@ -185,7 +185,7 @@ public void CheckStateForStructuredLogWithStrongType(bool includeFormattedMessag
var food = new Food { Name = "artichoke", Price = 3.99 };
logger.LogInformation("{food}", food);

Assert.Null(exportedItems[0].State);
Assert.NotNull(exportedItems[0].State);

var attributes = exportedItems[0].Attributes;
Assert.NotNull(attributes);
Expand Down Expand Up @@ -226,7 +226,7 @@ public void CheckStateForStructuredLogWithAnonymousType(bool includeFormattedMes
var anonymousType = new { Name = "pumpkin", Price = 5.99 };
logger.LogInformation("{food}", anonymousType);

Assert.Null(exportedItems[0].State);
Assert.NotNull(exportedItems[0].State);

var attributes = exportedItems[0].Attributes;
Assert.NotNull(attributes);
Expand Down Expand Up @@ -271,7 +271,7 @@ public void CheckStateForStructuredLogWithGeneralType(bool includeFormattedMessa
};
logger.LogInformation("{food}", food);

Assert.Null(exportedItems[0].State);
Assert.NotNull(exportedItems[0].State);

var attributes = exportedItems[0].Attributes;
Assert.NotNull(attributes);
Expand Down Expand Up @@ -321,7 +321,13 @@ public void CheckStateForExceptionLogged()
const string message = "Exception Occurred";
logger.LogInformation(exception, message);

Assert.Null(exportedItems[0].State);
Assert.NotNull(exportedItems[0].State);

var state = exportedItems[0].State;
var itemCount = state.GetType().GetProperty("Count").GetValue(state);

// state only has {OriginalFormat}
Assert.Equal(1, itemCount);

var attributes = exportedItems[0].Attributes;
Assert.NotNull(attributes);
Expand All @@ -334,6 +340,7 @@ public void CheckStateForExceptionLogged()
Assert.Equal(exceptionMessage, loggedException.Message);

Assert.Equal(message, exportedItems[0].Body);
Assert.Equal(message, state.ToString());
Assert.Null(exportedItems[0].FormattedMessage);
}

Expand Down Expand Up @@ -711,7 +718,14 @@ public void VerifyParseStateValues_UsingStandardExtensions(bool parseStateValues
var logRecord = exportedItems[0];

Assert.NotNull(logRecord.StateValues);
Assert.Null(logRecord.State);
if (parseStateValues)
{
Assert.Null(logRecord.State);
}
else
{
Assert.NotNull(logRecord.State);
}

Assert.NotNull(logRecord.StateValues);
Assert.Equal(3, logRecord.StateValues.Count);
Expand All @@ -725,7 +739,14 @@ public void VerifyParseStateValues_UsingStandardExtensions(bool parseStateValues
logRecord = exportedItems[1];

Assert.NotNull(logRecord.StateValues);
Assert.Null(logRecord.State);
if (parseStateValues)
{
Assert.Null(logRecord.State);
}
else
{
Assert.NotNull(logRecord.State);
}

Assert.NotNull(logRecord.StateValues);
Assert.Equal(4, logRecord.StateValues.Count);
Expand Down