Skip to content

Commit

Permalink
fixing bridge API bug
Browse files Browse the repository at this point in the history
  • Loading branch information
juliuskoval committed Jan 29, 2025
1 parent 89ea5c6 commit 572f602
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
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

0 comments on commit 572f602

Please sign in to comment.