diff --git a/.github/workflows/code-coverage.yml b/.github/workflows/code-coverage.yml index bf9bf858fd4..42bcde11070 100644 --- a/.github/workflows/code-coverage.yml +++ b/.github/workflows/code-coverage.yml @@ -48,7 +48,7 @@ jobs: - name: Merging test results run: reportgenerator -reports:TestResults/**/*.xml -targetdir:TestResults -reporttypes:Cobertura -assemblyFilters:"-microsoft.data.sqlclient*;-grpc.core*;-opentracing*" - - uses: codecov/codecov-action@v3.0.0 + - uses: codecov/codecov-action@v3.1.0 with: file: TestResults/Cobertura.xml env_vars: OS diff --git a/docs/logs/getting-started/Program.cs b/docs/logs/getting-started/Program.cs index 3cac963fd6f..d13ba546b4b 100644 --- a/docs/logs/getting-started/Program.cs +++ b/docs/logs/getting-started/Program.cs @@ -25,8 +25,10 @@ public static void Main() { using var loggerFactory = LoggerFactory.Create(builder => { - builder.AddOpenTelemetry(options => options - .AddConsoleExporter()); + builder.AddOpenTelemetry(options => + { + options.AddConsoleExporter(); + }); }); var logger = loggerFactory.CreateLogger(); diff --git a/examples/Console/TestLogs.cs b/examples/Console/TestLogs.cs index 15aa157ee5a..f508651bef6 100644 --- a/examples/Console/TestLogs.cs +++ b/examples/Console/TestLogs.cs @@ -29,6 +29,7 @@ internal static object Run(LogsOptions options) builder.AddOpenTelemetry((opt) => { opt.IncludeFormattedMessage = true; + opt.IncludeScopes = true; if (options.UseExporter.Equals("otlp", StringComparison.OrdinalIgnoreCase)) { /* @@ -68,7 +69,12 @@ internal static object Run(LogsOptions options) }); var logger = loggerFactory.CreateLogger(); - logger.LogInformation("Hello from {name} {price}.", "tomato", 2.99); + using (logger.BeginScope("My scope 1 with {food} and {color}", "apple", "green")) + using (logger.BeginScope("My scope 2 with {food} and {color}", "banana", "yellow")) + { + logger.LogInformation("Hello from {name} {price}.", "tomato", 2.99); + } + return null; } } diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md index a25f81c089a..8bca96958d4 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +* LogExporter to support Logging Scopes. + ([#3277](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3217)) + ## 1.3.0-beta.1 Released 2022-Apr-15 diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/LogRecordExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/LogRecordExtensions.cs index 1392aea00bc..2c69a36f589 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/LogRecordExtensions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/LogRecordExtensions.cs @@ -15,6 +15,7 @@ // using System; +using System.Collections.Generic; using System.Runtime.CompilerServices; using Google.Protobuf; using Google.Protobuf.Collections; @@ -132,8 +133,19 @@ internal static OtlpLogs.LogRecord ToOtlpLog(this LogRecord logRecord) otlpLogRecord.Flags = (uint)logRecord.TraceFlags; } - // TODO: Add additional attributes from scope and state - // Might make sense to take an approach similar to https://github.com/open-telemetry/opentelemetry-dotnet-contrib/blob/897b734aa5ea9992538f04f6ea6871fe211fa903/src/OpenTelemetry.Contrib.Preview/Internal/DefaultLogStateConverter.cs + int scopeDepth = -1; + logRecord.ForEachScope(ProcessScope, otlpLogRecord); + + void ProcessScope(LogRecordScope scope, OtlpLogs.LogRecord otlpLog) + { + scopeDepth++; + foreach (var scopeItem in scope) + { + var scopeItemWithDepthInfo = new KeyValuePair($"[Scope.{scopeDepth}]:{scopeItem.Key}", scopeItem.Value); + var otlpAttribute = scopeItemWithDepthInfo.ToOtlpAttribute(); + otlpLog.Attributes.Add(otlpAttribute); + } + } } catch (Exception ex) {