-
Notifications
You must be signed in to change notification settings - Fork 889
[Http] Fix propagation issues #3828
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
Changes from 11 commits
f59b7a7
e3dcc58
c3526c1
888f0e5
0792c56
d9060e8
8247b24
2fb03a8
d330137
e9beee4
3dc1e57
a42447b
1ade316
2cfd787
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,7 +41,13 @@ public HttpClientTests() | |
| this.serverLifeTime = TestHttpServer.RunServer( | ||
| (ctx) => | ||
| { | ||
| if (ctx.Request.Url.PathAndQuery.Contains("500")) | ||
| string expectedTraceparent = ctx.Request.QueryString["expectedTraceparent"]; | ||
| if (!string.IsNullOrEmpty(expectedTraceparent) && ctx.Request.Headers["traceparent"] != expectedTraceparent) | ||
| { | ||
| ctx.Response.StatusCode = 500; | ||
| ctx.Response.StatusDescription = "Traceparent mismatch"; | ||
| } | ||
| else if (ctx.Request.Url.PathAndQuery.Contains("500")) | ||
| { | ||
| ctx.Response.StatusCode = 500; | ||
| } | ||
|
|
@@ -584,6 +590,105 @@ public async Task HttpClientInstrumentationDoesNotReportExceptionEventOnErrorRes | |
| Assert.Empty(exportedItems[0].Events); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(true, true)] | ||
| [InlineData(true, false)] | ||
| [InlineData(false, true)] | ||
| [InlineData(false, false)] | ||
| public async Task CustomPropagatorCalled(bool sample, bool createParentActivity) | ||
| { | ||
| ActivityContext parentContext = default; | ||
| ActivityContext contextFromPropagator = default; | ||
|
|
||
| var propagator = new Mock<TextMapPropagator>(); | ||
| propagator.Setup(m => m.Inject(It.IsAny<PropagationContext>(), It.IsAny<HttpRequestMessage>(), It.IsAny<Action<HttpRequestMessage, string, string>>())) | ||
| .Callback<PropagationContext, HttpRequestMessage, Action<HttpRequestMessage, string, string>>((context, message, action) => | ||
| { | ||
| contextFromPropagator = context.ActivityContext; | ||
|
|
||
| string traceparent = $"00/{context.ActivityContext.TraceId}/{context.ActivityContext.SpanId}/01"; | ||
|
|
||
| message.RequestUri = new Uri(message.RequestUri.OriginalString + $"?expectedTraceparent={traceparent}"); | ||
|
|
||
| action(message, "traceparent", traceparent); | ||
| action(message, "tracestate", Activity.Current.TraceStateString); | ||
|
CodeBlanch marked this conversation as resolved.
Outdated
|
||
| }); | ||
|
|
||
| Sdk.SetDefaultTextMapPropagator(propagator.Object); | ||
|
|
||
| var exportedItems = new List<Activity>(); | ||
|
|
||
| using (var traceprovider = Sdk.CreateTracerProviderBuilder() | ||
| .AddHttpClientInstrumentation() | ||
| .AddInMemoryExporter(exportedItems) | ||
| .SetSampler(sample ? new ParentBasedSampler(new AlwaysOnSampler()) : new AlwaysOffSampler()) | ||
| .Build()) | ||
| { | ||
| Activity parent = null; | ||
| if (createParentActivity) | ||
| { | ||
| parent = new Activity("parent") | ||
| .SetIdFormat(ActivityIdFormat.W3C) | ||
| .Start(); | ||
|
|
||
| parent.TraceStateString = "k1=v1,k2=v2"; | ||
| parent.ActivityTraceFlags = ActivityTraceFlags.Recorded; | ||
|
|
||
| parentContext = parent.Context; | ||
| } | ||
|
|
||
| var request = new HttpRequestMessage | ||
| { | ||
| RequestUri = new Uri(this.url), | ||
| Method = new HttpMethod("GET"), | ||
| }; | ||
|
|
||
| using var c = new HttpClient(); | ||
| await c.SendAsync(request); | ||
|
|
||
| parent?.Stop(); | ||
| } | ||
|
|
||
| if (!sample) | ||
| { | ||
| Assert.Empty(exportedItems); | ||
| } | ||
| else | ||
| { | ||
| Assert.Single(exportedItems); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For test case That is, the parent activity that is created is not sampled, right? So per the default
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It is fake sampled 🤣 parent = new Activity("parent")
.SetIdFormat(ActivityIdFormat.W3C)
.Start();
parent.TraceStateString = "k1=v1,k2=v2";
parent.ActivityTraceFlags = ActivityTraceFlags.Recorded; // <- Here |
||
| } | ||
|
|
||
| if (createParentActivity) | ||
| { | ||
| if (!sample) | ||
| { | ||
| // If we created a parent, make sure that is what was injected. | ||
| // Not the .NET 7 legacy activity. | ||
| Assert.True(parentContext == contextFromPropagator); | ||
| } | ||
| else | ||
| { | ||
| Assert.True(contextFromPropagator != parentContext); | ||
| Assert.Equal(contextFromPropagator, exportedItems[0].Context); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| // Make sure custom propagator was called. | ||
| Assert.True(contextFromPropagator != default); | ||
| if (sample) | ||
| { | ||
| Assert.Equal(contextFromPropagator, exportedItems[0].Context); | ||
| } | ||
| } | ||
|
|
||
| Sdk.SetDefaultTextMapPropagator(new CompositeTextMapPropagator(new TextMapPropagator[] | ||
|
CodeBlanch marked this conversation as resolved.
Outdated
|
||
| { | ||
| new TraceContextPropagator(), | ||
| new BaggagePropagator(), | ||
| })); | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| this.serverLifeTime?.Dispose(); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.