Skip to content

Commit

Permalink
Respect SdkConfiguration.AttributeValueLengthLimit so otlp data point…
Browse files Browse the repository at this point in the history
…s are not rejected by monitoring tools
  • Loading branch information
Jonathan Wilhelm authored and Jonathan Wilhelm committed Sep 28, 2022
1 parent d10f1f9 commit 0d2fe25
Showing 1 changed file with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Google.Protobuf;
using Google.Protobuf.Collections;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Configuration;
using OpenTelemetry.Internal;
using OpenTelemetry.Logs;
using OpenTelemetry.Trace;
Expand Down Expand Up @@ -75,14 +76,16 @@ internal static OtlpLogs.LogRecord ToOtlpLog(this LogRecord logRecord)
SeverityText = LogLevels[(int)logRecord.LogLevel],
};

var attributeValueLengthLimit = SdkConfiguration.Instance.AttributeValueLengthLimit;

if (!string.IsNullOrEmpty(logRecord.CategoryName))
{
// TODO:
// 1. Track the following issue, and map CategoryName to Name
// if it makes it to log data model.
// https://github.com/open-telemetry/opentelemetry-specification/issues/2398
// 2. Confirm if this name for attribute is good.
otlpLogRecord.Attributes.AddStringAttribute("dotnet.ilogger.category", logRecord.CategoryName);
otlpLogRecord.Attributes.AddStringAttribute("dotnet.ilogger.category", logRecord.CategoryName, attributeValueLengthLimit);
}

bool bodyPopulatedFromFormattedMessage = false;
Expand All @@ -103,7 +106,7 @@ internal static OtlpLogs.LogRecord ToOtlpLog(this LogRecord logRecord)
{
otlpLogRecord.Body = new OtlpCommon.AnyValue { StringValue = stateValue.Value as string };
}
else if (OtlpKeyValueTransformer.Instance.TryTransformTag(stateValue, out var result))
else if (OtlpKeyValueTransformer.Instance.TryTransformTag(stateValue, out var result, attributeValueLengthLimit))
{
otlpLogRecord.Attributes.Add(result);
}
Expand All @@ -117,14 +120,14 @@ internal static OtlpLogs.LogRecord ToOtlpLog(this LogRecord logRecord)

if (!string.IsNullOrEmpty(logRecord.EventId.Name))
{
otlpLogRecord.Attributes.AddStringAttribute(nameof(logRecord.EventId.Name), logRecord.EventId.Name);
otlpLogRecord.Attributes.AddStringAttribute(nameof(logRecord.EventId.Name), logRecord.EventId.Name, attributeValueLengthLimit);
}

if (logRecord.Exception != null)
{
otlpLogRecord.Attributes.AddStringAttribute(SemanticConventions.AttributeExceptionType, logRecord.Exception.GetType().Name);
otlpLogRecord.Attributes.AddStringAttribute(SemanticConventions.AttributeExceptionMessage, logRecord.Exception.Message);
otlpLogRecord.Attributes.AddStringAttribute(SemanticConventions.AttributeExceptionStacktrace, logRecord.Exception.ToInvariantString());
otlpLogRecord.Attributes.AddStringAttribute(SemanticConventions.AttributeExceptionType, logRecord.Exception.GetType().Name, attributeValueLengthLimit);
otlpLogRecord.Attributes.AddStringAttribute(SemanticConventions.AttributeExceptionMessage, logRecord.Exception.Message, attributeValueLengthLimit);
otlpLogRecord.Attributes.AddStringAttribute(SemanticConventions.AttributeExceptionStacktrace, logRecord.Exception.ToInvariantString(), attributeValueLengthLimit);
}

if (logRecord.TraceId != default && logRecord.SpanId != default)
Expand All @@ -149,7 +152,7 @@ void ProcessScope(LogRecordScope scope, OtlpLogs.LogRecord otlpLog)
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))
if (OtlpKeyValueTransformer.Instance.TryTransformTag(scopeItemWithDepthInfo, out var result, attributeValueLengthLimit))
{
otlpLog.Attributes.Add(result);
}
Expand All @@ -164,12 +167,15 @@ void ProcessScope(LogRecordScope scope, OtlpLogs.LogRecord otlpLog)
return otlpLogRecord;
}

private static void AddStringAttribute(this RepeatedField<OtlpCommon.KeyValue> repeatedField, string key, string value)
private static void AddStringAttribute(this RepeatedField<OtlpCommon.KeyValue> repeatedField, string key, string value, int? maxLength)
{
var truncatedValue = maxLength.HasValue && value?.Length > maxLength
? value.Substring(0, maxLength.Value)
: value;
repeatedField.Add(new OtlpCommon.KeyValue
{
Key = key,
Value = new OtlpCommon.AnyValue { StringValue = value },
Value = new OtlpCommon.AnyValue { StringValue = truncatedValue },
});
}

Expand Down

0 comments on commit 0d2fe25

Please sign in to comment.