Baggage + CorrelationContext improvements by Eddy & Mike#1048
Baggage + CorrelationContext improvements by Eddy & Mike#1048cijothomas merged 42 commits intoopen-telemetry:masterfrom
Conversation
src/OpenTelemetry.Instrumentation.Http/HttpWebRequestInstrumentationOptions.netfx.cs
Outdated
Show resolved
Hide resolved
| /// Gets the correlation values. | ||
| /// </summary> | ||
| public static CorrelationContext Empty { get; } = new CorrelationContext(EmptyList); | ||
| public IEnumerable<KeyValuePair<string, string>> Correlations => this.activity?.Baggage ?? EmptyBaggage; |
There was a problem hiding this comment.
Couple questions:
- Would there be memory allocation concern?
- What would happen if a consumer gets the IEnumerable and tries to iterate through it, while the underlying baggage got modified?
- Does this enforce immutability? https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/correlationcontext/api.md#get-correlations
There was a problem hiding this comment.
Would there be memory allocation concern?
Check out the underlying Activity.Baggage enumeration:
https://github.com/dotnet/runtime/blob/ead2cd50799ecc523e23f50e50b54d8a30d0aa4f/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs#L290
Right below that method is the get.
The memory won't be too bad. An allocation for the state thing generated by the compiler due to the yield. The internal storage is a LinkedList, not great for searching. O(N)?
What would happen if a consumer gets the IEnumerable and tries to iterate through it, while the underlying baggage got modified?
I think the way the LinkedList works under the hood, it would actually be OK. User can only add to the back of the list. So if you were enumerating you would also consume that new one when you got to the tail.
Does this enforce immutability?
I think so. The enumeration is returning the KVPs. They are structs, so you essentially get a copy?
reyang
left a comment
There was a problem hiding this comment.
LGTM, love the fact that we've removed more lines than we added 👍
|
I think this is ready for a proper review. There seems to be a couple of flaky tests. Don't seem to be related to the changes. I'll keep digging on those. @cijothomas Anything jump out at you about them? |
| /// <param name="key">Correlation item key.</param> | ||
| /// <param name="value">Correlation item value.</param> | ||
| /// <returns>The <see cref="CorrelationContext"/> instance for chaining.</returns> | ||
| public CorrelationContext AddCorrelation(string key, string value) |
There was a problem hiding this comment.
This API will not match the spec, right? As spec wants "Set" behavior, but we have "add" behavior, and also we won't have "Remove" feature.
There was a problem hiding this comment.
Correct. The current version is built on top of Activity.Baggage, which is very limiting. Let's discuss this. The other option is we could make CorrelationContext independent of Baggage which would allow us to fully implement the spec. If we do that, let's remove "AllChildren" from #969. CorrelationContext could be used for "AllChildren" behavior and then EnrichmentScope is just responsible for "FirstChild" behavior?
src/OpenTelemetry.Instrumentation.AspNet/Implementation/HttpInListener.cs
Show resolved
Hide resolved
| /// <summary> | ||
| /// TextFormat context. | ||
| /// </summary> | ||
| public readonly struct TextFormatContext : IEquatable<TextFormatContext> |
There was a problem hiding this comment.
This represents both SpanContext and Baggage. Not sure if the name TextFormatContext is aptly capturing the purpose.
There was a problem hiding this comment.
Shooting down a name without a better suggestion isn't fair 🤣 I went with PropagationContext. Probably some further improvements needed. Let's do a follow-up PR? Our ITextFormat name doesn't really mirror what the other libraries are doing. All I have checked have HttpTextFormat type of things, which is what is called out in the spec. Some chain like TraceContextHttpPropagationFormat is IHttpPropagationFormat is IPropagationFormat?
There was a problem hiding this comment.
I like IPropagationFormat :)
cijothomas
left a comment
There was a problem hiding this comment.
Will follow up for the following
- Should we make CorrelationContext as spec compliant and build it outside Activity.Baggage or just use the Baggage from Activity.
- Potential perf issue as .net (asp.net core) and OTel is potentially extracting the same context in the hot path before sampling.
Changes:
CompositePropagator(TraceContextFormat, BaggageFormat)by default.IsInjectedfromITextFormat. It wasn't part of the spec and didn't really make sense for something optional like Baggage.TODO: