From bfec1ccd22d56a20befe8848b2cce57cfc5b2eab Mon Sep 17 00:00:00 2001 From: Julius Koval Date: Thu, 1 Feb 2024 08:59:20 +0100 Subject: [PATCH 1/3] fixing a bug where LogRecord.CategoryName is null --- .../Implementation/OtlpLogRecordTransformer.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/OtlpLogRecordTransformer.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/OtlpLogRecordTransformer.cs index 3966efad9af..0a4867722c9 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/OtlpLogRecordTransformer.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/OtlpLogRecordTransformer.cs @@ -16,6 +16,8 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation; internal sealed class OtlpLogRecordTransformer { + private const string DefaultScopeName = "unknown_scope"; + internal static readonly ConcurrentBag LogListPool = new(); private readonly SdkLimitOptions sdkLimitOptions; @@ -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); } From 6d243a819547404a99e830d259e0a37cb6a18989 Mon Sep 17 00:00:00 2001 From: Julius Koval Date: Mon, 5 Feb 2024 14:33:04 +0100 Subject: [PATCH 2/3] addressing comments --- .../CHANGELOG.md | 5 +++ .../OtlpLogRecordTransformer.cs | 4 +- .../OtlpLogExporterTests.cs | 38 +++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md index dcd24114fd1..6978f0c8a38 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md @@ -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 diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/OtlpLogRecordTransformer.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/OtlpLogRecordTransformer.cs index 310c90ca370..4851816017c 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/OtlpLogRecordTransformer.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/OtlpLogRecordTransformer.cs @@ -16,10 +16,10 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation; internal sealed class OtlpLogRecordTransformer { - private const string DefaultScopeName = "unknown_scope"; - internal static readonly ConcurrentBag LogListPool = new(); + private const string DefaultScopeName = ""; + private readonly SdkLimitOptions sdkLimitOptions; private readonly ExperimentalOptions experimentalOptions; diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs index 3656dda54aa..1813798c0ad 100644 --- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs +++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs @@ -1467,6 +1467,44 @@ public void VerifyEnvironmentVariablesTakenFromIConfigurationWhenUsingLoggingBui return (sp, factory); }); + } + + [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(); + + 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(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( From fab36de7517896ac3343614c82db2dd1f859050a Mon Sep 17 00:00:00 2001 From: Mikel Blanchard Date: Mon, 5 Feb 2024 09:33:31 -0800 Subject: [PATCH 3/3] Attempt to fix line ending. --- .../OtlpLogExporterTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs index 1813798c0ad..7181557ae5a 100644 --- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs +++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs @@ -1467,7 +1467,7 @@ public void VerifyEnvironmentVariablesTakenFromIConfigurationWhenUsingLoggingBui return (sp, factory); }); - } + } [Theory] [InlineData("my_instrumentation_scope_name", "my_instrumentation_scope_name")]