-
Notifications
You must be signed in to change notification settings - Fork 854
[api] Optimise TraceContextPropagator.Extract
#5749
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
Merged
CodeBlanch
merged 5 commits into
open-telemetry:main
from
stevejgordon:perf/tracecontextpropagator
Aug 12, 2024
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d01fa96
Optimise TraceContextPropagator.Extract
stevejgordon 538592e
Update changelog
stevejgordon a24b5a9
Reduce initial stackalloc buffer size and prefer try/finally
stevejgordon 35af52d
Fix typo
stevejgordon d43e26f
Merge from main.
CodeBlanch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
test/Benchmarks/Context/Propagation/TraceContextPropagatorBenchmarks.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using BenchmarkDotNet.Attributes; | ||
| using OpenTelemetry.Context.Propagation; | ||
|
|
||
| namespace Benchmarks.Context.Propagation; | ||
|
|
||
| public class TraceContextPropagatorBenchmarks | ||
| { | ||
| private const string TraceParent = "traceparent"; | ||
| private const string TraceState = "tracestate"; | ||
| private const string TraceId = "0af7651916cd43dd8448eb211c80319c"; | ||
| private const string SpanId = "b9c7c989f97918e1"; | ||
|
|
||
| private static readonly Random Random = new(455946); | ||
| private static readonly TraceContextPropagator TraceContextPropagator = new(); | ||
|
|
||
| private static readonly Func<IReadOnlyDictionary<string, string>, string, IEnumerable<string>> Getter = (headers, name) => | ||
| { | ||
| if (headers.TryGetValue(name, out var value)) | ||
| { | ||
| return [value]; | ||
| } | ||
|
|
||
| return []; | ||
| }; | ||
|
|
||
| private Dictionary<string, string> headers; | ||
|
|
||
| [Params(true, false)] | ||
| public bool LongListMember { get; set; } | ||
|
|
||
| [Params(0, 4, 32)] | ||
| public int MembersCount { get; set; } | ||
|
|
||
| public Dictionary<string, string> Headers => this.headers; | ||
|
|
||
| [GlobalSetup] | ||
| public void Setup() | ||
| { | ||
| var length = this.LongListMember ? 256 : 20; | ||
|
|
||
| var value = new string('a', length); | ||
|
|
||
| Span<char> keyBuffer = stackalloc char[length - 2]; | ||
|
|
||
| string traceState = string.Empty; | ||
| for (var i = 0; i < this.MembersCount; i++) | ||
| { | ||
| // We want a unique key for each member | ||
| for (var j = 0; j < length - 2; j++) | ||
| { | ||
| keyBuffer[j] = (char)('a' + Random.Next(0, 26)); | ||
| } | ||
|
|
||
| var key = keyBuffer.ToString(); | ||
|
|
||
| var listMember = $"{key}{i:00}={value}"; | ||
| traceState += i < this.MembersCount - 1 ? $"{listMember}," : listMember; | ||
| } | ||
|
|
||
| this.headers = new Dictionary<string, string> | ||
| { | ||
| { TraceParent, $"00-{TraceId}-{SpanId}-01" }, | ||
| { TraceState, traceState }, | ||
| }; | ||
| } | ||
|
|
||
| [Benchmark(Baseline = true)] | ||
| public void Extract() => _ = TraceContextPropagator!.Extract(default, this.headers, Getter); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love what this PR is doing but I have some concern here with allocating 704 bytes on the stack
((256 + 96) * 2).System.Text.Json for example will at max
stackalloc256 bytes (128chars): https://github.com/dotnet/runtime/blob/a86987ccab917433d065fe5dc8870fc261f79d14/src/libraries/System.Text.Json/Common/JsonConstants.cs#L12-L13Not sure why that number, but I'm sure a lot of thought went into it 😄 /cc @stephentoub
I think this pattern for "
stackallocwith fallback to rented array" is solid but the version here seems a bit off from what I have seen. IMO it more commonly looks like this:Would something like that work/help simplify things here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bit squishy. We almost never go above 1K. We typically use 256 or 512 bytes, but it varies case-to-case based on knowledge of that particular location and how likely longer buffers are expected to be needed.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@CodeBlanch I think I'd seen somewhere that 1K was a "reasonable" max value. I went for 256 chars as, realistically, that should cover most trace state scenarios. We could drop to 128 (256B) instead, though, and still cover most shorter trace state strings. Combined with the 96 chars (192B) for the duplicate lookup, that might be more reasonable.
I can switch to the try/finally here. The
Returnmethod initially had some extra logic, but I refactored that before the PR. I left it in that form because I preferred avoiding one extra level of indentation introduced with the extra blocks.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the pattern the
try-finallycan be ommited. In case of exceptions the buffer (if rented) will just be dropped instead of returned to the pool, but that isn't a problem to the pool.In first incarnations of this pattern try-finally got used, but later on that pattern evolved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't care so much about the
try-finallywe could drop that. Just want to make sure thestackallocis good.We can't really control where
Extractruns so it is probably a good idea to be conservative. For incoming request (think AspNetCore instrumentation) it will probably be ~early. But something like processing messages from a queue, who knows! What I would really like to see is us avoid thestackallocif we know there is a lot of tracestate but I guess hard to do because there could be multiple headers needing to be processed?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@CodeBlanch We could just be safe and go with
ArrayPool. I preferredstackallocsince we might reasonably expect the state to be small, and we can avoid a small amount of overhead that we incur by renting.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gfoidl I think we need the
finallyhere, though, as there are multiple branches where this code could return from this method on the non-exception path, and we need to ensure that the array is returned in those cases, too.