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

Adding distinct activity for distributed tracing to YARP #2098

Merged
merged 10 commits into from
May 5, 2023

Conversation

samsp-msft
Copy link
Contributor

@samsp-msft samsp-msft commented Apr 12, 2023

Adding distinct tracing objects to YARP.

These can then be picked up via monitoring middleware such as Open Telemetry.

using Azure.Monitor.OpenTelemetry.AspNetCore;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddReverseProxy().LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));

var otel = builder.Services.AddOpenTelemetry();
otel.UseAzureMonitor(o =>
{
    o.ConnectionString = builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"];
});
otel.WithTracing(t =>
{
    t.AddSource("Yarp.*");
});

var app = builder.Build();

app.MapReverseProxy();

app.Run();

The activity source is only active if something is listening to it, such as t.AddSource("Yarp.*");.

@samsp-msft
Copy link
Contributor Author

Result when seen in App Insights
image

@MihaZupan
Copy link
Member

If you add .AddHttpClientInstrumentation() to the builder, you should see another dependency node at about the same time you see Forward now (assuming things aren't broken). That means you don't gain new timing information from the new activity.

Re: Tags like routeId, is there prior art in adding such information to existing Activities?
E.g. to the root one ASP.NET will create, or to the one HttpClient creates internally?

Things like that should be trivial to do now that ActivityListener is a thing

ActivitySource.AddActivityListener(new ActivityListener()
{
    ShouldListenTo = source => source.Name == "System.Net.Http",
    ActivityStarted = activity =>
    {
        string routeId = "..."; // Get that somehow (IHttpContextAccessor?)
        activity.AddTag("YarpRoute", routeId);
    }
});

@samsp-msft samsp-msft marked this pull request as ready for review April 13, 2023 16:14
@samsp-msft
Copy link
Contributor Author

If you add .AddHttpClientInstrumentation() to the builder, you should see another dependency node at about the same time you see Forward now (assuming things aren't broken). That means you don't gain new timing information from the new activity.

HttpClientInstrumentation is added as part of the AzureMonitor helper.

Adding to the existing activity verses adding another is an interesting question. You don't even need to get to the listener, you can probably just do Activity.Current.SetTag(...)

src/ReverseProxy/Health/ActiveHealthCheckMonitor.cs Outdated Show resolved Hide resolved
src/ReverseProxy/Health/ActiveHealthCheckMonitor.cs Outdated Show resolved Hide resolved
src/ReverseProxy/Forwarder/ForwarderMiddleware.cs Outdated Show resolved Hide resolved
src/ReverseProxy/Utilities/Observability.cs Outdated Show resolved Hide resolved
src/ReverseProxy/Utilities/Observability.cs Outdated Show resolved Hide resolved
src/ReverseProxy/Utilities/Observability.cs Outdated Show resolved Hide resolved
src/ReverseProxy/Model/ReverseProxyFeature.cs Outdated Show resolved Hide resolved
@samsp-msft samsp-msft changed the title Prototype of Adding distinct activity for distributed tracing to YARP Adding distinct activity for distributed tracing to YARP Apr 14, 2023
Copy link
Member

@Tratcher Tratcher left a comment

Choose a reason for hiding this comment

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

Great, just needs some cleaup.

src/ReverseProxy/Health/ActiveHealthCheckMonitor.cs Outdated Show resolved Hide resolved
src/ReverseProxy/Model/HttpContextFeaturesExtensions.cs Outdated Show resolved Hide resolved
src/ReverseProxy/Model/IReverseProxyFeature.cs Outdated Show resolved Hide resolved
src/ReverseProxy/Model/ReverseProxyFeature.cs Outdated Show resolved Hide resolved
src/ReverseProxy/Utilities/Observability.cs Outdated Show resolved Hide resolved
docs/docfx/articles/distributed-tracing.md Show resolved Hide resolved
var activity = Observability.YarpActivitySource.StartActivity("Proxy Forwarder", ActivityKind.Server);
context.SetYarpActivity(activity);

await _next(context);
Copy link
Member

Choose a reason for hiding this comment

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

This will now allocate an extra async state machine even if the request isn't being sampled.
Can you please change the logic to something along the lines of what I shared here: #2098 (comment)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This will now allocate an extra async state machine even if the request isn't being sampled. Can you please change the logic to something along the lines of what I shared here: #2098 (comment)?

It only adds it if the activity is created, which depends on the listener. If the activity is null, nothing gets set.

Copy link
Member

Choose a reason for hiding this comment

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

The issue is that the method is now async, which means you get an extra state machine allocation even if you don't have the activity.

Copy link
Member

Choose a reason for hiding this comment

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

If you want you can keep it as-is and I can clean it up in a follow up PR

@samsp-msft samsp-msft merged commit 2463910 into main May 5, 2023
6 checks passed
@samsp-msft samsp-msft deleted the samsp/diagnostics branch May 5, 2023 17:59
MihaZupan added a commit that referenced this pull request May 9, 2023
* Followup after #2098

* Include CreateRequest under destination_health_check activity
@MihaZupan MihaZupan added this to the YARP 2.x milestone May 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants