Skip to content
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 @@ -21,6 +21,11 @@
experimental Logs Bridge API.
([#5268](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5268))

* Updated `OtlpLogExporter` to set instrumentation scope name on the data model
from `LogRecord.Logger.Name` if `LogRecord.CategoryName` is `null`. This is
typically the case when using the experimental Logs Bridge API.
([#5300](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5300))

## 1.7.0

Released 2023-Dec-08
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ internal sealed class OtlpLogRecordTransformer
{
internal static readonly ConcurrentBag<OtlpLogs.ScopeLogs> LogListPool = new();

private const string DefaultScopeName = "";

private readonly SdkLimitOptions sdkLimitOptions;
private readonly ExperimentalOptions experimentalOptions;

Expand Down Expand Up @@ -47,10 +49,11 @@ internal OtlpCollector.ExportLogsServiceRequest BuildExportRequest(
var otlpLogRecord = this.ToOtlpLog(logRecord);
if (otlpLogRecord != null)
{
if (!logsByCategory.TryGetValue(logRecord.CategoryName, out var scopeLogs))
var scopeName = logRecord.CategoryName ?? logRecord.Logger?.Name ?? DefaultScopeName;
if (!logsByCategory.TryGetValue(scopeName, out var scopeLogs))
{
scopeLogs = this.GetLogListFromPool(logRecord.CategoryName);
logsByCategory.Add(logRecord.CategoryName, scopeLogs);
scopeLogs = this.GetLogListFromPool(scopeName);
logsByCategory.Add(scopeName, scopeLogs);
resourceLogs.ScopeLogs.Add(scopeLogs);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,44 @@ public void VerifyEnvironmentVariablesTakenFromIConfigurationWhenUsingLoggingBui
});
}

[Theory]
[InlineData("my_instrumentation_scope_name", "my_instrumentation_scope_name")]
[InlineData(null, "")]
public void LogRecordLoggerNameIsExportedWhenUsingBridgeApi(string loggerName, string expectedScopeName)
{
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(loggerName);

logger.EmitLog(new LogRecordData());
}

Assert.Single(logRecords);

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

var batch = new Batch<LogRecord>(new[] { logRecords[0] }, 1);

var request = otlpLogRecordTransformer.BuildExportRequest(
new Proto.Resource.V1.Resource(),
batch);

Assert.NotNull(request);
Assert.Single(request.ResourceLogs);
Assert.Single(request.ResourceLogs[0].ScopeLogs);

Assert.Equal(expectedScopeName, request.ResourceLogs[0].ScopeLogs[0].Scope?.Name);
}

private static void RunVerifyEnvironmentVariablesTakenFromIConfigurationTest(
string optionsName,
Func<Action<IServiceCollection>, (IDisposable Container, ILoggerFactory LoggerFactory)> createLoggerFactoryFunc)
Expand Down