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

Fixing bridge API bug #6113

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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 @@ -12,6 +12,10 @@ Notes](../../RELEASENOTES.md).
exports are correctly marked as successful.
([#6099](https://github.com/open-telemetry/opentelemetry-dotnet/pull/6099))

* Fixed a bug in logging bridge API where the \{OriginalFormat\} attribute would
overwrite `Body` of the emitted `LogRecordData`.
([#6113](https://github.com/open-telemetry/opentelemetry-dotnet/pull/6113))

## 1.11.1

Released 2025-Jan-22
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,11 @@ internal static int WriteLogRecord(byte[] buffer, int writePosition, SdkLimitOpt
}

bool bodyPopulatedFromFormattedMessage = false;
bool isLogRecordBodySet = false;

if (logRecord.FormattedMessage != null)
{
otlpTagWriterState.WritePosition = WriteLogRecordBody(otlpTagWriterState.Buffer, otlpTagWriterState.WritePosition, logRecord.FormattedMessage.AsSpan());
bodyPopulatedFromFormattedMessage = true;
isLogRecordBodySet = true;
}

if (logRecord.Attributes != null)
Expand All @@ -226,21 +224,12 @@ internal static int WriteLogRecord(byte[] buffer, int writePosition, SdkLimitOpt
if (attribute.Key.Equals("{OriginalFormat}") && !bodyPopulatedFromFormattedMessage)
{
otlpTagWriterState.WritePosition = WriteLogRecordBody(otlpTagWriterState.Buffer, otlpTagWriterState.WritePosition, (attribute.Value as string).AsSpan());
isLogRecordBodySet = true;
}
else
{
AddLogAttribute(state, attribute);
}
}

// Supports setting Body directly on LogRecord for the Logs Bridge API.
if (!isLogRecordBodySet && logRecord.Body != null)
{
// If {OriginalFormat} is not present in the attributes,
// use logRecord.Body if it is set.
otlpTagWriterState.WritePosition = WriteLogRecordBody(otlpTagWriterState.Buffer, otlpTagWriterState.WritePosition, logRecord.Body.AsSpan());
}
}

if (logRecord.TraceId != default && logRecord.SpanId != default)
Expand Down
1 change: 1 addition & 0 deletions src/OpenTelemetry/Logs/LoggerSdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public override void EmitLog(in LogRecordData data, in LogRecordAttributeList at
logRecord.Logger = this;

logRecord.AttributeData = attributes.Export(ref logRecord.AttributeStorage);
logRecord.FormattedMessage = logRecord.Body;

processor.OnEnd(logRecord);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,55 @@ public void LogRecordBodyIsExportedWhenUsingBridgeApi(bool isBodySet)
Assert.Equal("Hello from {name} {price}.", otlpLogRecord.Body.StringValue);
}

[Fact]
public void LogRecordBodyAndOriginalFormatIsExportedWhenUsingBridgeApi()
{
LogRecordAttributeList attributes = default;
attributes.Add("name", "tomato");
attributes.Add("price", 2.99);
attributes.Add("{OriginalFormat}", "Hello from {name} {price}.");

var logRecords = new List<LogRecord>();

using (var loggerProvider = Sdk.CreateLoggerProviderBuilder()
.AddInMemoryExporter(logRecords)
.Build())
{
var logger = loggerProvider.GetLogger();

logger.EmitLog(
new LogRecordData()
{
Body = "Hello world",
},
attributes);
}

Assert.Single(logRecords);

var otlpLogRecord = ToOtlpLogs(DefaultSdkLimitOptions, new ExperimentalOptions(), logRecords[0]);

Assert.NotNull(otlpLogRecord);

Assert.Equal("Hello world", otlpLogRecord.Body?.StringValue);

Assert.NotNull(otlpLogRecord);
Assert.Equal(3, otlpLogRecord.Attributes.Count);

var index = 0;
var attribute = otlpLogRecord.Attributes[index];
Assert.Equal("name", attribute.Key);
Assert.Equal("tomato", attribute.Value.StringValue);

attribute = otlpLogRecord.Attributes[++index];
Assert.Equal("price", attribute.Key);
Assert.Equal(2.99, attribute.Value.DoubleValue);

attribute = otlpLogRecord.Attributes[++index];
Assert.Equal("{OriginalFormat}", attribute.Key);
Assert.Equal("Hello from {name} {price}.", attribute.Value.StringValue);
}

[Fact]
public void CheckToOtlpLogRecordExceptionAttributes()
{
Expand Down
Loading