Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
6d2c7ee
Do not create sibling activity when format is W3C
ngruson Dec 6, 2023
e516460
Formatted temp comments properly in unit tests
ngruson Dec 6, 2023
3e1af43
Conditional using statement
ngruson Dec 6, 2023
cb50735
Create sibling activity when format != W3C or trace flags is Recorded
ngruson Dec 6, 2023
a3c6d65
Leave trace flags out of comparison to create sibling activity
ngruson Dec 6, 2023
a76e470
Use WebApplicationFactory
ngruson Dec 7, 2023
0ab1901
Added Microsoft.Extensions.Features to Directory.Packages.props
ngruson Dec 7, 2023
f0c8152
Merge branch 'main' into 4466-bug-httpactivityfeature
ngruson Dec 7, 2023
d582a92
Updated trace id/span id/trace state comparison
ngruson Dec 8, 2023
0e26e0b
Merge branch 'main' into 4466-bug-httpactivityfeature
ngruson Dec 8, 2023
98c8feb
Add comment and compare Activity.Current to IHttpFeatureActivityFeatu…
ngruson Dec 8, 2023
83ce8b3
Merge branch 'main' into 4466-bug-httpactivityfeature
cijothomas Dec 8, 2023
ba7b218
Merge branch 'main' into 4466-bug-httpactivityfeature
ngruson Dec 19, 2023
f1df768
Merge branch '4466-bug-httpactivityfeature' of https://github.com/ngr…
ngruson Dec 19, 2023
462287e
Merge branch 'main' into 4466-bug-httpactivityfeature
ngruson Dec 20, 2023
2aa9fd9
Don't create ActivityContext struct for comparison
ngruson Dec 21, 2023
219ceed
Merge branch 'main' into 4466-bug-httpactivityfeature
ngruson Dec 27, 2023
b3cb1e2
Added PR #5136 to CHANGELOG.md
ngruson Jan 8, 2024
61a3bcb
Fixed trailing spaces
ngruson Jan 8, 2024
9837992
Merge branch 'main' into 4466-bug-httpactivityfeature
CodeBlanch Jan 8, 2024
e0b013e
Merge branch 'main' into 4466-bug-httpactivityfeature
alanwest Jan 16, 2024
f842030
Merge branch 'main' into 4466-bug-httpactivityfeature
alanwest Jan 16, 2024
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 @@ -112,7 +112,7 @@ public void OnStartActivity(Activity activity, object payload)
// By this time, samplers have already run and
// activity.IsAllDataRequested populated accordingly.

HttpContext context = payload as HttpContext;
var context = payload as HttpContext;
if (context == null)
{
AspNetCoreInstrumentationEventSource.Log.NullPayload(nameof(HttpInListener), nameof(this.OnStartActivity), activity.OperationName);
Expand All @@ -125,9 +125,11 @@ public void OnStartActivity(Activity activity, object payload)
if (textMapPropagator is not TraceContextPropagator)
{
var ctx = textMapPropagator.Extract(default, request, HttpRequestHeaderValuesGetter);

var activityContext = new ActivityContext(activity.TraceId, activity.ParentSpanId, activity.ActivityTraceFlags, activity.TraceStateString, true);
if (ctx.ActivityContext.IsValid()
&& ctx.ActivityContext != new ActivityContext(activity.TraceId, activity.ParentSpanId, activity.ActivityTraceFlags, activity.TraceStateString, true))
&& ((ctx.ActivityContext.TraceId != activityContext.TraceId)
|| (ctx.ActivityContext.SpanId != activityContext.SpanId)
|| (ctx.ActivityContext.TraceState != activityContext.TraceState)))
{
// Create a new activity with its parent set from the extracted context.
// This makes the new activity as a "sibling" of the activity created by
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
#if NET7_0_OR_GREATER
using Microsoft.AspNetCore.Http.Features;
#endif
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -1035,6 +1038,43 @@ public async Task DiagnosticSourceExceptionCallBackIsNotReceivedForExceptionsHan
Assert.Equal(2, numberOfSubscribedEvents);
}

#if NET7_0_OR_GREATER
Copy link
Member

Choose a reason for hiding this comment

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

This should be NET6_0_OR_GREATER? IHttpActivityFeature is available in net6.0.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had to add the Microsoft.Extensions.Features package to the test app to be able to build with net6.0.
See the project file.
Should this be made conditional in the project file?
I didn't succeed right off the bat.

Copy link
Member

Choose a reason for hiding this comment

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

Try this

context.Features.Get<IHttpActivityFeature>()?.Activity;

[Fact]
public async Task NoSiblingActivityCreatedWhenIdFormatIsW3CWithTraceFlagsNone()
{
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
.SetSampler(new AlwaysOnSampler())
.AddAspNetCoreInstrumentation()
.Build();

var builder = WebApplication.CreateBuilder();
builder.WebHost.UseUrls("http://*:0");
var app = builder.Build();

app.MapGet("/values", (HttpContext context) =>
{
var activity = context.Features.GetRequiredFeature<IHttpActivityFeature>().Activity;
var equal = Activity.Current.Id == activity.Id;

return Results.Ok(equal);
});

_ = app.RunAsync();

var url = app.Urls.ToArray()[0];
var portNumber = url.Substring(url.LastIndexOf(':') + 1);

using var client = new HttpClient();
var req = new HttpRequestMessage(HttpMethod.Get, $"http://localhost:{portNumber}/values");
req.Headers.Add("traceparent", "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-00");
var res = await client.SendAsync(req);
var result = bool.Parse(await res.Content.ReadAsStringAsync());

Assert.True(res.IsSuccessStatusCode);
Assert.True(result);
}
#endif

public void Dispose()
{
this.tracerProvider?.Dispose();
Expand Down