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

Span attribute count and length limits #3231

Closed

Conversation

alanwest
Copy link
Member

Implements attribute limits for span data when using the shim API.

Follow on PR will implement additional span limits for events and links.

I've opened dotnet/runtime#68528 to propose a similar API for ActivitySource.

@alanwest alanwest requested a review from a team April 26, 2022 00:32
/// <param name="spanLimits">The configured <see cref="SpanLimits" />.</param>
public static void SetSpanLimits(SpanLimits spanLimits)
{
SpanAttributeCountLimit = spanLimits.AttributeCountLimit;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How to handle error when the value is out of range?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. My thought was that if set to negative then it it won't hurt anything - i.e., the setting would have no effect and nothing would be limited. At least that was my intent... though just realized that setting SpanAttributeValueLengthLimit to a negative value will cause an exception upon calling String.Substring. Will fix.

I'm open to other opinions, but since this is part of the API and not the SDK, I figured an error here was not appropriate.

if (attributeCountLimit-- != 0)
{
this.AddInternal(kvp.Key, kvp.Value);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to exit the loop once the limit is reached (or it will underflow to negative)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If negative then it will loop over all attributes and then stop. attributeCountLimit is initialized to -1 for this purpose when the user set TracerProvider.SpanAttributeCountLimit = null.

Though, for efficiency, this loop could break when the limit has been reached. Also will add tests to demonstrate when limits are not set.

/// <summary>
/// Gets or sets the maximum allowed number of attributes on spans.
/// </summary>
public int? AttributeCountLimit { get; set; } = 128;
Copy link
Member Author

@alanwest alanwest Apr 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

128 is the default identified by the spec. Changing the defaults would be a breaking change, so right now the defaults only apply if someone explicitly calls SetSpanLimits(new SpanLimits()).

@codecov
Copy link

codecov bot commented Apr 26, 2022

Codecov Report

Merging #3231 (96b6f8c) into main (f9602e2) will increase coverage by 0.04%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3231      +/-   ##
==========================================
+ Coverage   85.55%   85.60%   +0.04%     
==========================================
  Files         261      262       +1     
  Lines        9390     9420      +30     
==========================================
+ Hits         8034     8064      +30     
  Misses       1356     1356              
Impacted Files Coverage Δ
src/OpenTelemetry.Api/Trace/SpanAttributes.cs 100.00% <100.00%> (ø)
src/OpenTelemetry.Api/Trace/SpanLimits.cs 100.00% <100.00%> (ø)
src/OpenTelemetry.Api/Trace/TracerProvider.cs 100.00% <100.00%> (ø)

Copy link
Member

@reyang reyang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The actual code logic looks good to me.

I have a design question regarding the goal of these limits. By reading the specification I kind of feel that these limits are intended to provide protection against unbounded memory usage - e.g. if a user keeps adding new attributes, at certain moment the API would become a simple no-op that doesn't has memory cost.

Given .NET is not putting such limits on the Activity APIs, I feel the limits are becoming less useful. Perhaps such logic can be applied to the exporter logic?

@alanwest
Copy link
Member Author

By reading the specification I kind of feel that these limits are intended to provide protection against unbounded memory usage - e.g. if a user keeps adding new attributes, at certain moment the API would become a simple no-op that doesn't has memory cost.

Yes, I agree this is one of the primary goals. Another place this concern was raised was in the context of unbounded events being added to a span here #1276 (comment).

Another thing this feature offers is support to backends that have their own limitations on the number of attributes, attribute length, etc. that spans can have.

Given .NET is not putting such limits on the Activity APIs

My hope was that .NET would consider implementing configurable limits: dotnet/runtime#68528

Perhaps such logic can be applied to the exporter logic?

I think I'm liking this idea. I'm going to make this PR a draft for now and try this out.

@alanwest alanwest marked this pull request as draft April 26, 2022 17:43
@github-actions
Copy link
Contributor

github-actions bot commented May 4, 2022

This PR was marked stale due to lack of activity and will be closed in 7 days. Commenting or Pushing will instruct the bot to automatically remove the label. This bot runs once per day.

@github-actions github-actions bot added the Stale Issues and pull requests which have been flagged for closing due to inactivity label May 4, 2022
@alanwest
Copy link
Member Author

Closing this will follow up with the plan to handle these limits in the exporters.

@alanwest alanwest closed this May 11, 2022
@alanwest alanwest deleted the alanwest/attribute-limits branch May 11, 2022 22:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Stale Issues and pull requests which have been flagged for closing due to inactivity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants