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

[Logs Bridge API] Add support for Body set directly on LogRecord for OTLP Exporter. #5268

Merged
merged 16 commits into from
Feb 1, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

* **Experimental (pre-release builds only):** Added
support for `Body` set directly on `LogRecord` via the Logs Bridge API.
[#5268](https:/open-telemetry/opentelemetry-dotnet/pull/5268)
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved

* **Experimental (pre-release builds only):** Added
`LoggerProviderBuilder.AddOtlpExporter` registration extensions.
[#5103](https:/open-telemetry/opentelemetry-dotnet/pull/5103)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ internal OtlpLogs.LogRecord ToOtlpLog(LogRecord logRecord)
AddAttribute(otlpLogRecord, result, attributeCountLimit);
}
}

// Supports Body set directly on LogRecord for the Logs Bridge API.
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
if (otlpLogRecord.Body == null && logRecord.Body != null)
{
// If {OriginalFormat} is not present in the attributes,
// use logRecord.Body if it is set.
otlpLogRecord.Body = new OtlpCommon.AnyValue { StringValue = logRecord.Body };
}
}

if (logRecord.TraceId != default && logRecord.SpanId != default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,38 @@ public void CheckToOtlpLogRecordBodyIsPopulated(bool includeFormattedMessage)
Assert.Equal("state", otlpLogRecord.Body.StringValue);
}

[Fact]
public void LogRecordBodyIsExportedWhenUsingBridgeApi()
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
{
var logRecords = new List<LogRecord>();

using (var loggerProvider = Sdk.CreateLoggerProviderBuilder()
.ConfigureServices(services =>
{
services.AddLogging(builder => builder.AddOpenTelemetry(options =>
CodeBlanch marked this conversation as resolved.
Show resolved Hide resolved
{
options.AddInMemoryExporter(logRecords);
}));
})
.Build())
{
var logger = loggerProvider.GetLogger();

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

Assert.Single(logRecords);

var otlpLogRecordTransformer = new OtlpLogRecordTransformer(DefaultSdkLimitOptions, new());

var otlpLogRecord = otlpLogRecordTransformer.ToOtlpLog(logRecords[0]);

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

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