OTLP LogExporter to support ILogger Scopes (#3218)
This commit is contained in:
parent
60f743a4e9
commit
1918e4b3cc
|
|
@ -25,8 +25,10 @@ public class Program
|
|||
{
|
||||
using var loggerFactory = LoggerFactory.Create(builder =>
|
||||
{
|
||||
builder.AddOpenTelemetry(options => options
|
||||
.AddConsoleExporter());
|
||||
builder.AddOpenTelemetry(options =>
|
||||
{
|
||||
options.AddConsoleExporter();
|
||||
});
|
||||
});
|
||||
|
||||
var logger = loggerFactory.CreateLogger<Program>();
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ namespace Examples.Console
|
|||
builder.AddOpenTelemetry((opt) =>
|
||||
{
|
||||
opt.IncludeFormattedMessage = true;
|
||||
opt.IncludeScopes = true;
|
||||
if (options.UseExporter.Equals("otlp", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
/*
|
||||
|
|
@ -68,7 +69,12 @@ namespace Examples.Console
|
|||
});
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@
|
|||
// </copyright>
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Google.Protobuf;
|
||||
using Google.Protobuf.Collections;
|
||||
|
|
@ -132,8 +133,19 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation
|
|||
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);
|
||||
var otlpAttribute = scopeItemWithDepthInfo.ToOtlpAttribute();
|
||||
otlpLog.Attributes.Add(otlpAttribute);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue