Skip to content

Commit

Permalink
OTLP Logs - remove depth from scope fields (#3843)
Browse files Browse the repository at this point in the history
  • Loading branch information
cijothomas authored Oct 31, 2022
1 parent c154931 commit d4cca85
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
"contoso",
"cref",
"grafana",
"ilogger",
"inheritdoc",
"janotti",
"kanzhelev",
"langword",
"liudmila",
"loglevel",
"mikel",
"molkova",
"monocytogenes",
Expand Down Expand Up @@ -42,6 +45,7 @@
"Tracestate",
"triager",
"typeparam",
"ulong",
"umesan",
"unencrypted",
"unvalidated",
Expand Down
4 changes: 2 additions & 2 deletions examples/Console/TestLogs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ internal static object Run(LogsOptions options)
});

var logger = loggerFactory.CreateLogger<Program>();
using (logger.BeginScope("My scope 1 with {food} and {color}", "apple", "green"))
using (logger.BeginScope("My scope 2 with {food} and {color}", "banana", "yellow"))
using (logger.BeginScope("{city}", "Seattle"))
using (logger.BeginScope("{storeType}", "Physical"))
{
logger.LogInformation("Hello from {name} {price}.", "tomato", 2.99);
}
Expand Down
5 changes: 5 additions & 0 deletions src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

* Log Exporter modified to no longer prefix scope-depth when exporting ILogger
scopes as attributes. Empty keys and {OriginalFormat} key will be ignored from
scopes.
([3843](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3843))

## 1.4.0-beta.2

Released 2022-Oct-17
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ internal static OtlpLogs.LogRecord ToOtlpLog(this LogRecord logRecord, SdkLimitO
var attributeValueLengthLimit = sdkLimitOptions.AttributeValueLengthLimit;
var attributeCountLimit = sdkLimitOptions.AttributeCountLimit ?? int.MaxValue;

// First add the generic attributes like category, eventid and exception, so they are less likely being dropped because of AttributeCountLimit
// First add the generic attributes like Category, EventId and Exception,
// so they are less likely being dropped because of AttributeCountLimit.

if (!string.IsNullOrEmpty(logRecord.CategoryName))
{
Expand Down Expand Up @@ -145,18 +146,40 @@ internal static OtlpLogs.LogRecord ToOtlpLog(this LogRecord logRecord, SdkLimitO
otlpLogRecord.Flags = (uint)logRecord.TraceFlags;
}

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);
if (OtlpKeyValueTransformer.Instance.TryTransformTag(scopeItemWithDepthInfo, out var result, attributeValueLengthLimit))
if (scopeItem.Key.Equals("{OriginalFormat}") || string.IsNullOrEmpty(scopeItem.Key))
{
otlpLog.AddAttribute(result, attributeCountLimit);
// Ignore if the scope key is empty.
// Ignore if the scope key is {OriginalFormat}
// Attributes should not contain duplicates,
// and it is expensive to de-dup, so this
// exporter is going to pass the scope items as is.
// {OriginalFormat} is going to be the key
// if one uses formatted string for scopes
// and if there are nested scopes, this is
// guaranteed to create duplicate keys.
// Similar for empty keys, which is what the
// key is going to be if user simply
// passes a string as scope.
// To summarize this exporter only allows
// IReadOnlyList<KeyValuePair<string, object?>>
// or IEnumerable<KeyValuePair<string, object?>>.
// and expect users to provide unique keys.
// Note: It is possible that we allow users
// to override this exporter feature. So not blocking
// empty/{OriginalFormat} in the SDK itself.
}
else
{
if (OtlpKeyValueTransformer.Instance.TryTransformTag(scopeItem, out var result, attributeValueLengthLimit))
{
otlpLog.AddAttribute(result, attributeCountLimit);
}
}
}
}
Expand Down

0 comments on commit d4cca85

Please sign in to comment.