-
Notifications
You must be signed in to change notification settings - Fork 772
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 Logs - remove depth from scope fields #3843
Changes from 5 commits
09f1c5c
49787e1
dfe3694
bf8abc8
a95575a
9e40df7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)) | ||
{ | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
|
||
{ | ||
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?>> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just mention
|
||
// 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. | ||
Comment on lines
+173
to
+175
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you suggesting that in the future we might add an exporter option to override this behavior? My preference would be to try and avoid this solution if it becomes a need in the future. Given we have ILogger specific notions into our LogRecord data model we're required to handle them somehow in our exporters. But, I'd prefer to avoid spilling ILogger specific notions into our OTLP exporter options. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. deduping is not going to be Ilogger specific. It'll be same for any source which itself prevent duplicates. |
||
} | ||
else | ||
{ | ||
if (OtlpKeyValueTransformer.Instance.TryTransformTag(scopeItem, out var result, attributeValueLengthLimit)) | ||
Comment on lines
+177
to
+179
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Looks like you could combine these into a single |
||
{ | ||
otlpLog.AddAttribute(result, attributeCountLimit); | ||
} | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not opposed to this change, but can you give a little more background on why we're making this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Main motivation is the duplication/de-duplication cost, and undesired modification of user attributes.
Explanation for each of the case:
With plain string, the key is null/empty, which is unallowed in OTel. But even if we were to use some magic key like "scope", it'll be duplicated, when user have nested scopes. The .NET official docs are being fixed already to no longer showing scopes with strings.
With {OriginalFormat} - this is an unintentional side effect in Ms.Ext.Logging. It makes sense for Log message, where there is a Format() function, which can be used to get the fully formatted string. There is nothing like that for Scopes. "{OriginalFormat}" key will be duplicated if user has nested scopes, and we do not want to pay for dedup efforts.
The current approach simply prefixed a numeric value (based on scope depth), to solve this problem. But overriding user provide attributes with something else is undesirable (and affects perf too).
The following example can show why this is bad:
using (logger.BeginScope(new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>("userid", "cijo")
}))
using (logger.BeginScope(new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>("transactionid", "abcd")
}))
{
logger.LogInformation("Hello from {food} {price}.", "artichoke", 3.99);
}
the above would have resulted in attributes ("[0]:userid", "cijo"), ("[1]:transactionid", "abcd") as per current code, instead of ("userid", "cijo"). ("transactionid", "abcd"), per this PR.