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

OTLP LogExporter to support ILogger Scopes #3218

Merged
merged 6 commits into from
Apr 21, 2022
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
6 changes: 4 additions & 2 deletions docs/logs/getting-started/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Program>();
Expand Down
8 changes: 7 additions & 1 deletion examples/Console/TestLogs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
/*
Expand Down Expand Up @@ -68,7 +69,12 @@ internal static object Run(LogsOptions options)
});

var logger = loggerFactory.CreateLogger<Program>();
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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// </copyright>

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Google.Protobuf;
using Google.Protobuf.Collections;
Expand Down Expand Up @@ -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<string, object>($"[Scope.{scopeDepth}]:{scopeItem.Key}", scopeItem.Value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I anticipate users wanting to change this prefix behavior, but something we can tackle later 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be nice to avoid the new string allocation each time, doesn't block this PR though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. The description has this left as open question.
Merging to make progress. (OTLP Log Exporter is now almost ready feature-wise, except the SDK level issue related to buffering : #2905)

var otlpAttribute = scopeItemWithDepthInfo.ToOtlpAttribute();
otlpLog.Attributes.Add(otlpAttribute);
}
}
}
catch (Exception ex)
{
Expand Down