Skip to content

Commit

Permalink
[otlp] Switch to TagWriter for handling of tags/attributes (#5585)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch authored May 3, 2024
1 parent e95ae72 commit ac0d1d1
Show file tree
Hide file tree
Showing 11 changed files with 227 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -237,19 +237,18 @@ private static Span.Types.Link ToOtlpLink(in ActivityLink activityLink, SdkLimit
};

int maxTags = sdkLimitOptions.SpanLinkAttributeCountLimit ?? int.MaxValue;

var otlpLinkAttributes = otlpLink.Attributes;

foreach (ref readonly var tag in activityLink.EnumerateTagObjects())
{
if (OtlpTagTransformer.Instance.TryTransformTag(tag, out var attribute, sdkLimitOptions.AttributeValueLengthLimit))
if (otlpLinkAttributes.Count == maxTags)
{
if (otlpLink.Attributes.Count < maxTags)
{
otlpLink.Attributes.Add(attribute);
}
else
{
otlpLink.DroppedAttributesCount++;
}
otlpLink.DroppedAttributesCount++;
continue;
}

OtlpTagWriter.Instance.TryWriteTag(ref otlpLinkAttributes, tag, sdkLimitOptions.AttributeValueLengthLimit);
}

otlpLink.Flags = ToOtlpSpanFlags(activityLink.Context.TraceFlags, activityLink.Context.IsRemote);
Expand All @@ -267,19 +266,18 @@ private static Span.Types.Event ToOtlpEvent(in ActivityEvent activityEvent, SdkL
};

int maxTags = sdkLimitOptions.SpanEventAttributeCountLimit ?? int.MaxValue;

var otlpEventAttributes = otlpEvent.Attributes;

foreach (ref readonly var tag in activityEvent.EnumerateTagObjects())
{
if (OtlpTagTransformer.Instance.TryTransformTag(tag, out var attribute, sdkLimitOptions.AttributeValueLengthLimit))
if (otlpEventAttributes.Count == maxTags)
{
if (otlpEvent.Attributes.Count < maxTags)
{
otlpEvent.Attributes.Add(attribute);
}
else
{
otlpEvent.DroppedAttributesCount++;
}
otlpEvent.DroppedAttributesCount++;
continue;
}

OtlpTagWriter.Instance.TryWriteTag(ref otlpEventAttributes, tag, sdkLimitOptions.AttributeValueLengthLimit);
}

return otlpEvent;
Expand Down Expand Up @@ -322,6 +320,8 @@ private struct TagEnumerationState : PeerServiceResolver.IPeerServiceState

public void EnumerateTags(Activity activity, int maxTags)
{
var otlpSpanAttributes = this.Span.Attributes;

foreach (ref readonly var tag in activity.EnumerateTagObjects())
{
if (tag.Value == null)
Expand All @@ -341,26 +341,22 @@ public void EnumerateTags(Activity activity, int maxTags)
continue;
}

if (OtlpTagTransformer.Instance.TryTransformTag(tag, out var attribute, this.SdkLimitOptions.AttributeValueLengthLimit))
if (otlpSpanAttributes.Count == maxTags)
{
if (this.Span.Attributes.Count < maxTags)
{
this.Span.Attributes.Add(attribute);
}
else
{
this.Span.DroppedAttributesCount++;
}
this.Span.DroppedAttributesCount++;
}
else
{
OtlpTagWriter.Instance.TryWriteTag(ref otlpSpanAttributes, tag, this.SdkLimitOptions.AttributeValueLengthLimit);
}

if (attribute.Value.ValueCase == AnyValue.ValueOneofCase.StringValue)
{
// Note: tag.Value is used and not attribute.Value here because attribute.Value may be truncated
PeerServiceResolver.InspectTag(ref this, key, tag.Value as string);
}
else if (attribute.Value.ValueCase == AnyValue.ValueOneofCase.IntValue)
{
PeerServiceResolver.InspectTag(ref this, key, attribute.Value.IntValue);
}
if (tag.Value is string tagStringValue)
{
PeerServiceResolver.InspectTag(ref this, key, tagStringValue);
}
else if (tag.Value is int tagIntValue)
{
PeerServiceResolver.InspectTag(ref this, key, tagIntValue);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,12 +414,11 @@ internal static OtlpMetrics.Exemplar ToOtlpExemplar<T>(T value, in Metrics.Exemp
otlpExemplar.AsDouble = Convert.ToDouble(value);
}

var otlpExemplarFilteredAttributes = otlpExemplar.FilteredAttributes;

foreach (var tag in exemplar.FilteredTags)
{
if (OtlpTagTransformer.Instance.TryTransformTag(tag, out var result))
{
otlpExemplar.FilteredAttributes.Add(result);
}
OtlpTagWriter.Instance.TryWriteTag(ref otlpExemplarFilteredAttributes, tag);
}

return otlpExemplar;
Expand All @@ -429,21 +428,15 @@ private static void AddAttributes(ReadOnlyTagCollection tags, RepeatedField<Otlp
{
foreach (var tag in tags)
{
if (OtlpTagTransformer.Instance.TryTransformTag(tag, out var result))
{
attributes.Add(result);
}
OtlpTagWriter.Instance.TryWriteTag(ref attributes, tag);
}
}

private static void AddScopeAttributes(IEnumerable<KeyValuePair<string, object>> meterTags, RepeatedField<OtlpCommon.KeyValue> attributes)
{
foreach (var tag in meterTags)
{
if (OtlpTagTransformer.Instance.TryTransformTag(tag, out var result))
{
attributes.Add(result);
}
OtlpTagWriter.Instance.TryWriteTag(ref attributes, tag);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,15 @@ internal OtlpLogs.LogRecord ToOtlpLog(LogRecord logRecord)

if (!string.IsNullOrEmpty(logRecord.EventId.Name))
{
AddStringAttribute(otlpLogRecord, ExperimentalOptions.LogRecordEventNameAttribute, logRecord.EventId.Name, attributeValueLengthLimit, attributeCountLimit);
AddStringAttribute(otlpLogRecord, ExperimentalOptions.LogRecordEventNameAttribute, logRecord.EventId.Name, attributeCountLimit, attributeValueLengthLimit);
}
}

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

bool bodyPopulatedFromFormattedMessage = false;
Expand All @@ -166,9 +166,9 @@ internal OtlpLogs.LogRecord ToOtlpLog(LogRecord logRecord)
{
otlpLogRecord.Body = new OtlpCommon.AnyValue { StringValue = attribute.Value as string };
}
else if (OtlpTagTransformer.Instance.TryTransformTag(attribute, out var result, attributeValueLengthLimit))
else
{
AddAttribute(otlpLogRecord, result, attributeCountLimit);
AddAttribute(otlpLogRecord, attribute, attributeCountLimit, attributeValueLengthLimit);
}
}

Expand Down Expand Up @@ -224,10 +224,7 @@ void ProcessScope(LogRecordScope scope, OtlpLogs.LogRecord otlpLog)
}
else
{
if (OtlpTagTransformer.Instance.TryTransformTag(scopeItem, out var result, attributeValueLengthLimit))
{
AddAttribute(otlpLog, result, attributeCountLimit);
}
AddAttribute(otlpLog, scopeItem, attributeCountLimit, attributeValueLengthLimit);
}
}
}
Expand All @@ -241,36 +238,34 @@ void ProcessScope(LogRecordScope scope, OtlpLogs.LogRecord otlpLog)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void AddAttribute(OtlpLogs.LogRecord logRecord, OtlpCommon.KeyValue attribute, int maxAttributeCount)
private static void AddAttribute(OtlpLogs.LogRecord logRecord, KeyValuePair<string, object> attribute, int maxAttributeCount, int? maxValueLength)
{
if (logRecord.Attributes.Count < maxAttributeCount)
var logRecordAttributes = logRecord.Attributes;

if (logRecordAttributes.Count == maxAttributeCount)
{
logRecord.Attributes.Add(attribute);
logRecord.DroppedAttributesCount++;
}
else
{
logRecord.DroppedAttributesCount++;
OtlpTagWriter.Instance.TryWriteTag(ref logRecordAttributes, attribute, maxValueLength);
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void AddStringAttribute(OtlpLogs.LogRecord logRecord, string key, string value, int? maxValueLength, int maxAttributeCount)
private static void AddStringAttribute(OtlpLogs.LogRecord logRecord, string key, string value, int maxAttributeCount, int? maxValueLength)
{
var attributeItem = new KeyValuePair<string, object>(key, value);
if (OtlpTagTransformer.Instance.TryTransformTag(attributeItem, out var result, maxValueLength))
{
AddAttribute(logRecord, result, maxAttributeCount);
}

AddAttribute(logRecord, attributeItem, maxAttributeCount, maxValueLength);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void AddIntAttribute(OtlpLogs.LogRecord logRecord, string key, int value, int maxAttributeCount)
{
var attributeItem = new KeyValuePair<string, object>(key, value);
if (OtlpTagTransformer.Instance.TryTransformTag(attributeItem, out var result))
{
AddAttribute(logRecord, result, maxAttributeCount);
}

AddAttribute(logRecord, attributeItem, maxAttributeCount, maxValueLength: null);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down

This file was deleted.

Loading

0 comments on commit ac0d1d1

Please sign in to comment.