Skip to content
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] Export instrumentation scope attributes from ActivitySource.Tags #5897

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Notes](../../RELEASENOTES.md).

## Unreleased

*Added support for exporting instrumentation scope attributes from
`ActivitySource.Tags`.
([#5897](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5897))
CodeBlanch marked this conversation as resolved.
Show resolved Hide resolved

## 1.10.0-beta.1

Released 2024-Sep-30
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ internal static void AddBatch(
};
request.ResourceSpans.Add(resourceSpans);

var maxTags = sdkLimitOptions.AttributeCountLimit ?? int.MaxValue;

foreach (var activity in activityBatch)
{
Span? span = activity.ToOtlpSpan(sdkLimitOptions);
Expand All @@ -44,15 +46,15 @@ internal static void AddBatch(
}

var activitySourceName = activity.Source.Name;
if (!spansByLibrary.TryGetValue(activitySourceName, out var spans))
if (!spansByLibrary.TryGetValue(activitySourceName, out var scopeSpans))
{
spans = GetSpanListFromPool(activitySourceName, activity.Source.Version);
scopeSpans = GetSpanListFromPool(activity.Source, maxTags, sdkLimitOptions.AttributeValueLengthLimit);

spansByLibrary.Add(activitySourceName, spans);
resourceSpans.ScopeSpans.Add(spans);
spansByLibrary.Add(activitySourceName, scopeSpans);
resourceSpans.ScopeSpans.Add(scopeSpans);
}

spans.Spans.Add(span);
scopeSpans.Spans.Add(span);
}
}

Expand All @@ -65,34 +67,63 @@ internal static void Return(this ExportTraceServiceRequest request)
return;
}

foreach (var scope in resourceSpans.ScopeSpans)
foreach (var scopeSpan in resourceSpans.ScopeSpans)
{
scope.Spans.Clear();
SpanListPool.Add(scope);
scopeSpan.Spans.Clear();
SpanListPool.Add(scopeSpan);
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ScopeSpans GetSpanListFromPool(string name, string? version)
internal static ScopeSpans GetSpanListFromPool(ActivitySource activitySource, int maxTags, int? attributeValueLengthLimit)
{
if (!SpanListPool.TryTake(out var spans))
if (!SpanListPool.TryTake(out var scopeSpans))
{
spans = new ScopeSpans
scopeSpans = new ScopeSpans
{
Scope = new InstrumentationScope
{
Name = name, // Name is enforced to not be null, but it can be empty.
Version = version ?? string.Empty, // NRE throw by proto
Name = activitySource.Name, // Name is enforced to not be null, but it can be empty.
Version = activitySource.Version ?? string.Empty, // NRE throw by proto
},
};
}
else
{
spans.Scope.Name = name;
spans.Scope.Version = version ?? string.Empty;

if (activitySource.Tags != null)
{
var scopeAttributes = scopeSpans.Scope.Attributes;

if (activitySource.Tags is IReadOnlyList<KeyValuePair<string, object?>> activitySourceTagsList)
{
for (int i = 0; i < activitySourceTagsList.Count; i++)
{
if (scopeAttributes.Count < maxTags)
{
OtlpTagWriter.Instance.TryWriteTag(ref scopeAttributes, activitySourceTagsList[i], attributeValueLengthLimit);
}
else
{
scopeSpans.Scope.DroppedAttributesCount++;
}
}
}
else
{
foreach (var tag in activitySource.Tags ?? [])
rajkumar-rangaraj marked this conversation as resolved.
Show resolved Hide resolved
{
if (scopeAttributes.Count < maxTags)
{
OtlpTagWriter.Instance.TryWriteTag(ref scopeAttributes, tag, attributeValueLengthLimit);
}
else
{
scopeSpans.Scope.DroppedAttributesCount++;
}
}
}
}
}

return spans;
return scopeSpans;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,90 @@ void RunTest(SdkLimitOptions sdkOptions, Batch<Activity> batch)
}
}

[Fact(Skip = "Troubleshoot failing test.")]
public void ScopeAttributesRemainConsistentAcrossMultipleBatches()
{
var activitySourceTags = new TagList
{
new("k0", "v0"),
};

using var activitySource = new ActivitySource(nameof(this.ScopeAttributesRemainConsistentAcrossMultipleBatches), "1.1.1.3", activitySourceTags);
using var rootActivity = activitySource.StartActivity("root", ActivityKind.Server, default(ActivityContext));
using var childActivity = activitySource.StartActivity("child", ActivityKind.Client);

Batch<Activity> batch = new Batch<Activity>(new[] { rootActivity!, childActivity! }, 2);

var request = new OtlpCollector.ExportTraceServiceRequest();
request.AddBatch(DefaultSdkLimitOptions, ResourceBuilder.CreateDefault().Build().ToOtlpResource(), batch);

var resourceSpans = request.ResourceSpans.First();
Assert.NotNull(request.ResourceSpans.First());

var scopeSpans = resourceSpans.ScopeSpans.First();
Assert.NotNull(scopeSpans);

var scope = scopeSpans.Scope;
Assert.Single(scope.Attributes);
Assert.Equal(nameof(this.ScopeAttributesRemainConsistentAcrossMultipleBatches), scope.Name);
Assert.Equal("1.1.1.3", scope.Version);
Assert.Contains(scope.Attributes, (kvp) => kvp.Key == "k0" && kvp.Value.StringValue == "v0");

// Return and re-add batch to simulate reuse
request.Return();
request.AddBatch(DefaultSdkLimitOptions, ResourceBuilder.CreateDefault().Build().ToOtlpResource(), batch);

resourceSpans = request.ResourceSpans.First();
scopeSpans = resourceSpans.ScopeSpans.First();
scope = scopeSpans.Scope;

Assert.Single(scope.Attributes);
Assert.Equal(nameof(this.ScopeAttributesRemainConsistentAcrossMultipleBatches), scope.Name);
Assert.Equal("1.1.1.3", scope.Version);
Assert.Contains(scope.Attributes, (kvp) => kvp.Key == "k0" && kvp.Value.StringValue == "v0");
}

[Fact]
public void ScopeAttributesLimitsTest()
{
var sdkOptions = new SdkLimitOptions()
{
AttributeValueLengthLimit = 4,
AttributeCountLimit = 3,
};

// ActivitySource Tags are sorted in .NET.
var activitySourceTags = new TagList
{
new("1_TruncatedSourceTag", "12345"),
new("2_TruncatedSourceStringArray", new string?[] { "12345", "1234", string.Empty, null }),
new("3_TruncatedSourceObjectTag", new object()),
new("4_OneSourceTagTooMany", 1),
};

using var activitySource = new ActivitySource(name: nameof(this.ScopeAttributesLimitsTest), tags: activitySourceTags);
using var activity = activitySource.StartActivity("root", ActivityKind.Server, default(ActivityContext));
Batch<Activity> batch = new Batch<Activity>(new[] { activity! }, 1);

var request = new OtlpCollector.ExportTraceServiceRequest();
request.AddBatch(sdkOptions, ResourceBuilder.CreateDefault().Build().ToOtlpResource(), batch);

var resourceSpans = request.ResourceSpans.First();
Assert.NotNull(request.ResourceSpans.First());

var scopeSpans = resourceSpans.ScopeSpans.First();
Assert.NotNull(scopeSpans);

var scope = scopeSpans.Scope;
Assert.NotNull(scope);

Assert.Equal(3, scope.Attributes.Count);
Assert.Equal(1u, scope.DroppedAttributesCount);
Assert.Equal("1234", scope.Attributes[0].Value.StringValue);
this.ArrayValueAsserts(scope.Attributes[1].Value.ArrayValue.Values);
Assert.Equal(new object().ToString()!.Substring(0, 4), scope.Attributes[2].Value.StringValue);
}

[Fact]
public void SpanLimitsTest()
{
Expand Down Expand Up @@ -263,43 +347,24 @@ public void SpanLimitsTest()
Assert.Equal(3, otlpSpan.Attributes.Count);
Assert.Equal(1u, otlpSpan.DroppedAttributesCount);
Assert.Equal("1234", otlpSpan.Attributes[0].Value.StringValue);
ArrayValueAsserts(otlpSpan.Attributes[1].Value.ArrayValue.Values);
this.ArrayValueAsserts(otlpSpan.Attributes[1].Value.ArrayValue.Values);
Assert.Equal(new object().ToString()!.Substring(0, 4), otlpSpan.Attributes[2].Value.StringValue);

Assert.Single(otlpSpan.Events);
Assert.Equal(1u, otlpSpan.DroppedEventsCount);
Assert.Equal(3, otlpSpan.Events[0].Attributes.Count);
Assert.Equal(1u, otlpSpan.Events[0].DroppedAttributesCount);
Assert.Equal("1234", otlpSpan.Events[0].Attributes[0].Value.StringValue);
ArrayValueAsserts(otlpSpan.Events[0].Attributes[1].Value.ArrayValue.Values);
this.ArrayValueAsserts(otlpSpan.Events[0].Attributes[1].Value.ArrayValue.Values);
Assert.Equal(new object().ToString()!.Substring(0, 4), otlpSpan.Events[0].Attributes[2].Value.StringValue);

Assert.Single(otlpSpan.Links);
Assert.Equal(1u, otlpSpan.DroppedLinksCount);
Assert.Equal(3, otlpSpan.Links[0].Attributes.Count);
Assert.Equal(1u, otlpSpan.Links[0].DroppedAttributesCount);
Assert.Equal("1234", otlpSpan.Links[0].Attributes[0].Value.StringValue);
ArrayValueAsserts(otlpSpan.Links[0].Attributes[1].Value.ArrayValue.Values);
this.ArrayValueAsserts(otlpSpan.Links[0].Attributes[1].Value.ArrayValue.Values);
Assert.Equal(new object().ToString()!.Substring(0, 4), otlpSpan.Links[0].Attributes[2].Value.StringValue);

void ArrayValueAsserts(RepeatedField<OtlpCommon.AnyValue> values)
{
var expectedStringArray = new string?[] { "1234", "1234", string.Empty, null };
for (var i = 0; i < expectedStringArray.Length; ++i)
{
var expectedValue = expectedStringArray[i];
var expectedValueCase = expectedValue != null
? OtlpCommon.AnyValue.ValueOneofCase.StringValue
: OtlpCommon.AnyValue.ValueOneofCase.None;

var actual = values[i];
Assert.Equal(expectedValueCase, actual.ValueCase);
if (expectedValueCase != OtlpCommon.AnyValue.ValueOneofCase.None)
{
Assert.Equal(expectedValue, actual.StringValue);
}
}
}
}

[Fact]
Expand Down Expand Up @@ -859,4 +924,23 @@ public void SpanLinkFlagsTest(bool isRecorded, bool isRemote)
Assert.False(flags.HasFlag(OtlpTrace.SpanFlags.ContextIsRemoteMask));
}
}

private void ArrayValueAsserts(RepeatedField<OtlpCommon.AnyValue> values)
{
var expectedStringArray = new string?[] { "1234", "1234", string.Empty, null };
for (var i = 0; i < expectedStringArray.Length; ++i)
{
var expectedValue = expectedStringArray[i];
var expectedValueCase = expectedValue != null
? OtlpCommon.AnyValue.ValueOneofCase.StringValue
: OtlpCommon.AnyValue.ValueOneofCase.None;

var actual = values[i];
Assert.Equal(expectedValueCase, actual.ValueCase);
if (expectedValueCase != OtlpCommon.AnyValue.ValueOneofCase.None)
{
Assert.Equal(expectedValue, actual.StringValue);
}
}
}
}
Loading