Skip to content

Commit ac0d1d1

Browse files
authored
[otlp] Switch to TagWriter for handling of tags/attributes (#5585)
1 parent e95ae72 commit ac0d1d1

File tree

11 files changed

+227
-223
lines changed

11 files changed

+227
-223
lines changed

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ActivityExtensions.cs

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -237,19 +237,18 @@ private static Span.Types.Link ToOtlpLink(in ActivityLink activityLink, SdkLimit
237237
};
238238

239239
int maxTags = sdkLimitOptions.SpanLinkAttributeCountLimit ?? int.MaxValue;
240+
241+
var otlpLinkAttributes = otlpLink.Attributes;
242+
240243
foreach (ref readonly var tag in activityLink.EnumerateTagObjects())
241244
{
242-
if (OtlpTagTransformer.Instance.TryTransformTag(tag, out var attribute, sdkLimitOptions.AttributeValueLengthLimit))
245+
if (otlpLinkAttributes.Count == maxTags)
243246
{
244-
if (otlpLink.Attributes.Count < maxTags)
245-
{
246-
otlpLink.Attributes.Add(attribute);
247-
}
248-
else
249-
{
250-
otlpLink.DroppedAttributesCount++;
251-
}
247+
otlpLink.DroppedAttributesCount++;
248+
continue;
252249
}
250+
251+
OtlpTagWriter.Instance.TryWriteTag(ref otlpLinkAttributes, tag, sdkLimitOptions.AttributeValueLengthLimit);
253252
}
254253

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

269268
int maxTags = sdkLimitOptions.SpanEventAttributeCountLimit ?? int.MaxValue;
269+
270+
var otlpEventAttributes = otlpEvent.Attributes;
271+
270272
foreach (ref readonly var tag in activityEvent.EnumerateTagObjects())
271273
{
272-
if (OtlpTagTransformer.Instance.TryTransformTag(tag, out var attribute, sdkLimitOptions.AttributeValueLengthLimit))
274+
if (otlpEventAttributes.Count == maxTags)
273275
{
274-
if (otlpEvent.Attributes.Count < maxTags)
275-
{
276-
otlpEvent.Attributes.Add(attribute);
277-
}
278-
else
279-
{
280-
otlpEvent.DroppedAttributesCount++;
281-
}
276+
otlpEvent.DroppedAttributesCount++;
277+
continue;
282278
}
279+
280+
OtlpTagWriter.Instance.TryWriteTag(ref otlpEventAttributes, tag, sdkLimitOptions.AttributeValueLengthLimit);
283281
}
284282

285283
return otlpEvent;
@@ -322,6 +320,8 @@ private struct TagEnumerationState : PeerServiceResolver.IPeerServiceState
322320

323321
public void EnumerateTags(Activity activity, int maxTags)
324322
{
323+
var otlpSpanAttributes = this.Span.Attributes;
324+
325325
foreach (ref readonly var tag in activity.EnumerateTagObjects())
326326
{
327327
if (tag.Value == null)
@@ -341,26 +341,22 @@ public void EnumerateTags(Activity activity, int maxTags)
341341
continue;
342342
}
343343

344-
if (OtlpTagTransformer.Instance.TryTransformTag(tag, out var attribute, this.SdkLimitOptions.AttributeValueLengthLimit))
344+
if (otlpSpanAttributes.Count == maxTags)
345345
{
346-
if (this.Span.Attributes.Count < maxTags)
347-
{
348-
this.Span.Attributes.Add(attribute);
349-
}
350-
else
351-
{
352-
this.Span.DroppedAttributesCount++;
353-
}
346+
this.Span.DroppedAttributesCount++;
347+
}
348+
else
349+
{
350+
OtlpTagWriter.Instance.TryWriteTag(ref otlpSpanAttributes, tag, this.SdkLimitOptions.AttributeValueLengthLimit);
351+
}
354352

355-
if (attribute.Value.ValueCase == AnyValue.ValueOneofCase.StringValue)
356-
{
357-
// Note: tag.Value is used and not attribute.Value here because attribute.Value may be truncated
358-
PeerServiceResolver.InspectTag(ref this, key, tag.Value as string);
359-
}
360-
else if (attribute.Value.ValueCase == AnyValue.ValueOneofCase.IntValue)
361-
{
362-
PeerServiceResolver.InspectTag(ref this, key, attribute.Value.IntValue);
363-
}
353+
if (tag.Value is string tagStringValue)
354+
{
355+
PeerServiceResolver.InspectTag(ref this, key, tagStringValue);
356+
}
357+
else if (tag.Value is int tagIntValue)
358+
{
359+
PeerServiceResolver.InspectTag(ref this, key, tagIntValue);
364360
}
365361
}
366362
}

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/MetricItemExtensions.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -414,12 +414,11 @@ internal static OtlpMetrics.Exemplar ToOtlpExemplar<T>(T value, in Metrics.Exemp
414414
otlpExemplar.AsDouble = Convert.ToDouble(value);
415415
}
416416

417+
var otlpExemplarFilteredAttributes = otlpExemplar.FilteredAttributes;
418+
417419
foreach (var tag in exemplar.FilteredTags)
418420
{
419-
if (OtlpTagTransformer.Instance.TryTransformTag(tag, out var result))
420-
{
421-
otlpExemplar.FilteredAttributes.Add(result);
422-
}
421+
OtlpTagWriter.Instance.TryWriteTag(ref otlpExemplarFilteredAttributes, tag);
423422
}
424423

425424
return otlpExemplar;
@@ -429,21 +428,15 @@ private static void AddAttributes(ReadOnlyTagCollection tags, RepeatedField<Otlp
429428
{
430429
foreach (var tag in tags)
431430
{
432-
if (OtlpTagTransformer.Instance.TryTransformTag(tag, out var result))
433-
{
434-
attributes.Add(result);
435-
}
431+
OtlpTagWriter.Instance.TryWriteTag(ref attributes, tag);
436432
}
437433
}
438434

439435
private static void AddScopeAttributes(IEnumerable<KeyValuePair<string, object>> meterTags, RepeatedField<OtlpCommon.KeyValue> attributes)
440436
{
441437
foreach (var tag in meterTags)
442438
{
443-
if (OtlpTagTransformer.Instance.TryTransformTag(tag, out var result))
444-
{
445-
attributes.Add(result);
446-
}
439+
OtlpTagWriter.Instance.TryWriteTag(ref attributes, tag);
447440
}
448441
}
449442
}

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/OtlpLogRecordTransformer.cs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,15 @@ internal OtlpLogs.LogRecord ToOtlpLog(LogRecord logRecord)
137137

138138
if (!string.IsNullOrEmpty(logRecord.EventId.Name))
139139
{
140-
AddStringAttribute(otlpLogRecord, ExperimentalOptions.LogRecordEventNameAttribute, logRecord.EventId.Name, attributeValueLengthLimit, attributeCountLimit);
140+
AddStringAttribute(otlpLogRecord, ExperimentalOptions.LogRecordEventNameAttribute, logRecord.EventId.Name, attributeCountLimit, attributeValueLengthLimit);
141141
}
142142
}
143143

144144
if (logRecord.Exception != null)
145145
{
146-
AddStringAttribute(otlpLogRecord, SemanticConventions.AttributeExceptionType, logRecord.Exception.GetType().Name, attributeValueLengthLimit, attributeCountLimit);
147-
AddStringAttribute(otlpLogRecord, SemanticConventions.AttributeExceptionMessage, logRecord.Exception.Message, attributeValueLengthLimit, attributeCountLimit);
148-
AddStringAttribute(otlpLogRecord, SemanticConventions.AttributeExceptionStacktrace, logRecord.Exception.ToInvariantString(), attributeValueLengthLimit, attributeCountLimit);
146+
AddStringAttribute(otlpLogRecord, SemanticConventions.AttributeExceptionType, logRecord.Exception.GetType().Name, attributeCountLimit, attributeValueLengthLimit);
147+
AddStringAttribute(otlpLogRecord, SemanticConventions.AttributeExceptionMessage, logRecord.Exception.Message, attributeCountLimit, attributeValueLengthLimit);
148+
AddStringAttribute(otlpLogRecord, SemanticConventions.AttributeExceptionStacktrace, logRecord.Exception.ToInvariantString(), attributeCountLimit, attributeValueLengthLimit);
149149
}
150150

151151
bool bodyPopulatedFromFormattedMessage = false;
@@ -166,9 +166,9 @@ internal OtlpLogs.LogRecord ToOtlpLog(LogRecord logRecord)
166166
{
167167
otlpLogRecord.Body = new OtlpCommon.AnyValue { StringValue = attribute.Value as string };
168168
}
169-
else if (OtlpTagTransformer.Instance.TryTransformTag(attribute, out var result, attributeValueLengthLimit))
169+
else
170170
{
171-
AddAttribute(otlpLogRecord, result, attributeCountLimit);
171+
AddAttribute(otlpLogRecord, attribute, attributeCountLimit, attributeValueLengthLimit);
172172
}
173173
}
174174

@@ -224,10 +224,7 @@ void ProcessScope(LogRecordScope scope, OtlpLogs.LogRecord otlpLog)
224224
}
225225
else
226226
{
227-
if (OtlpTagTransformer.Instance.TryTransformTag(scopeItem, out var result, attributeValueLengthLimit))
228-
{
229-
AddAttribute(otlpLog, result, attributeCountLimit);
230-
}
227+
AddAttribute(otlpLog, scopeItem, attributeCountLimit, attributeValueLengthLimit);
231228
}
232229
}
233230
}
@@ -241,36 +238,34 @@ void ProcessScope(LogRecordScope scope, OtlpLogs.LogRecord otlpLog)
241238
}
242239

243240
[MethodImpl(MethodImplOptions.AggressiveInlining)]
244-
private static void AddAttribute(OtlpLogs.LogRecord logRecord, OtlpCommon.KeyValue attribute, int maxAttributeCount)
241+
private static void AddAttribute(OtlpLogs.LogRecord logRecord, KeyValuePair<string, object> attribute, int maxAttributeCount, int? maxValueLength)
245242
{
246-
if (logRecord.Attributes.Count < maxAttributeCount)
243+
var logRecordAttributes = logRecord.Attributes;
244+
245+
if (logRecordAttributes.Count == maxAttributeCount)
247246
{
248-
logRecord.Attributes.Add(attribute);
247+
logRecord.DroppedAttributesCount++;
249248
}
250249
else
251250
{
252-
logRecord.DroppedAttributesCount++;
251+
OtlpTagWriter.Instance.TryWriteTag(ref logRecordAttributes, attribute, maxValueLength);
253252
}
254253
}
255254

256255
[MethodImpl(MethodImplOptions.AggressiveInlining)]
257-
private static void AddStringAttribute(OtlpLogs.LogRecord logRecord, string key, string value, int? maxValueLength, int maxAttributeCount)
256+
private static void AddStringAttribute(OtlpLogs.LogRecord logRecord, string key, string value, int maxAttributeCount, int? maxValueLength)
258257
{
259258
var attributeItem = new KeyValuePair<string, object>(key, value);
260-
if (OtlpTagTransformer.Instance.TryTransformTag(attributeItem, out var result, maxValueLength))
261-
{
262-
AddAttribute(logRecord, result, maxAttributeCount);
263-
}
259+
260+
AddAttribute(logRecord, attributeItem, maxAttributeCount, maxValueLength);
264261
}
265262

266263
[MethodImpl(MethodImplOptions.AggressiveInlining)]
267264
private static void AddIntAttribute(OtlpLogs.LogRecord logRecord, string key, int value, int maxAttributeCount)
268265
{
269266
var attributeItem = new KeyValuePair<string, object>(key, value);
270-
if (OtlpTagTransformer.Instance.TryTransformTag(attributeItem, out var result))
271-
{
272-
AddAttribute(logRecord, result, maxAttributeCount);
273-
}
267+
268+
AddAttribute(logRecord, attributeItem, maxAttributeCount, maxValueLength: null);
274269
}
275270

276271
[MethodImpl(MethodImplOptions.AggressiveInlining)]

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/OtlpTagTransformer.cs

Lines changed: 0 additions & 118 deletions
This file was deleted.

0 commit comments

Comments
 (0)