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
12 changes: 11 additions & 1 deletion src/ReverseProxy/Forwarder/ForwarderMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Connections;
samsp-msft marked this conversation as resolved.
Show resolved Hide resolved
using Microsoft.Extensions.Logging;
using Yarp.ReverseProxy.Model;
using Yarp.ReverseProxy.Utilities;
Expand Down Expand Up @@ -64,11 +65,19 @@ public async Task Invoke(HttpContext context)
throw new InvalidOperationException($"Chosen destination has no model set: '{destination.DestinationId}'");
}

var activityForTracing = reverseProxyFeature.ActivityForTracing;
if (activityForTracing != null)
{
activityForTracing.AddTag("Route", route.Config.RouteId);
activityForTracing.AddTag("Cluster", cluster.ClusterId);
activityForTracing.AddTag("Destination", destination.DestinationId);
}

try
{
cluster.ConcurrencyCounter.Increment();
destination.ConcurrencyCounter.Increment();

activityForTracing?.Start();
ForwarderTelemetry.Log.ForwarderInvoke(cluster.ClusterId, route.Config.RouteId, destination.DestinationId);

var clusterConfig = reverseProxyFeature.Cluster;
Expand All @@ -79,6 +88,7 @@ await _forwarder.SendAsync(context, destinationModel.Config.Address, clusterConf
{
destination.ConcurrencyCounter.Decrement();
cluster.ConcurrencyCounter.Decrement();
activityForTracing?.Stop();
samsp-msft marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
17 changes: 15 additions & 2 deletions src/ReverseProxy/Health/ActiveHealthCheckMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.Extensions.Options;
using Yarp.ReverseProxy.Model;
using Yarp.ReverseProxy.Utilities;
using System.Diagnostics;
samsp-msft marked this conversation as resolved.
Show resolved Hide resolved

namespace Yarp.ReverseProxy.Health;

Expand Down Expand Up @@ -108,6 +109,8 @@ private async Task ProbeCluster(ClusterState cluster)
return;
}

var activity = Observability.YarpActivitySource.CreateActivity("Yarp.ActiveHealthCheck", ActivityKind.Internal);

Log.StartingActiveHealthProbingOnCluster(_logger, cluster.ClusterId);

var allDestinations = cluster.DestinationsState.AllDestinations;
Expand All @@ -118,7 +121,7 @@ private async Task ProbeCluster(ClusterState cluster)

for (var i = 0; i < probeTasks.Length; i++)
{
probeTasks[i] = ProbeDestinationAsync(cluster, allDestinations[i], timeout);
probeTasks[i] = ProbeDestinationAsync(cluster, allDestinations[i], timeout, activity);
}

for (var i = 0; i < probeResults.Length; i++)
Expand Down Expand Up @@ -150,10 +153,11 @@ private async Task ProbeCluster(ClusterState cluster)
}

Log.StoppedActiveHealthProbingOnCluster(_logger, cluster.ClusterId);
activity?.Stop();
}
}

private async Task<DestinationProbingResult> ProbeDestinationAsync(ClusterState cluster, DestinationState destination, TimeSpan timeout)
private async Task<DestinationProbingResult> ProbeDestinationAsync(ClusterState cluster, DestinationState destination, TimeSpan timeout, Activity? healthCheckActivity)
samsp-msft marked this conversation as resolved.
Show resolved Hide resolved
{
HttpRequestMessage request;
try
Expand All @@ -167,19 +171,28 @@ private async Task<DestinationProbingResult> ProbeDestinationAsync(ClusterState
return new DestinationProbingResult(destination, null, ex);
}

var probeActivity = Observability.YarpActivitySource.StartActivity("Probe Destination", ActivityKind.Internal);
probeActivity?.AddTag("Cluster", cluster.ClusterId);
probeActivity?.AddTag("Destination", destination.DestinationId);
var cts = new CancellationTokenSource(timeout);
try
{
Log.SendingHealthProbeToEndpointOfDestination(_logger, request.RequestUri, destination.DestinationId, cluster.ClusterId);
var response = await cluster.Model.HttpClient.SendAsync(request, cts.Token);
Log.DestinationProbingCompleted(_logger, destination.DestinationId, cluster.ClusterId, (int)response.StatusCode);

probeActivity?.SetStatus(ActivityStatusCode.Ok);
probeActivity?.Stop();

return new DestinationProbingResult(destination, response, null);
}
catch (Exception ex)
{
Log.DestinationProbingFailed(_logger, destination.DestinationId, cluster.ClusterId, ex);

probeActivity?.SetStatus(ActivityStatusCode.Error);
probeActivity?.Stop();

return new DestinationProbingResult(destination, null, ex);
}
finally
Expand Down
3 changes: 3 additions & 0 deletions src/ReverseProxy/Model/IReverseProxyFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Diagnostics;

namespace Yarp.ReverseProxy.Model;

Expand Down Expand Up @@ -35,4 +36,6 @@ public interface IReverseProxyFeature
/// The actual destination that the request was proxied to.
/// </summary>
DestinationState? ProxiedDestination { get; set; }

samsp-msft marked this conversation as resolved.
Show resolved Hide resolved
Activity? ActivityForTracing { get; }
samsp-msft marked this conversation as resolved.
Show resolved Hide resolved
}
10 changes: 10 additions & 0 deletions src/ReverseProxy/Model/ReverseProxyFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using Yarp.ReverseProxy.Utilities;
samsp-msft marked this conversation as resolved.
Show resolved Hide resolved

namespace Yarp.ReverseProxy.Model;

Expand Down Expand Up @@ -31,4 +33,12 @@ public IReadOnlyList<DestinationState> AvailableDestinations

/// <inheritdoc/>
public DestinationState? ProxiedDestination { get; set; }

public Activity? ActivityForTracing { get; init; }
MihaZupan marked this conversation as resolved.
Show resolved Hide resolved

public ReverseProxyFeature()
{
if (Observability.IsListening)
ActivityForTracing = (Observability.IsListening) ? Observability.YarpActivitySource.CreateActivity("Forward", ActivityKind.Server) : null;
}
}
18 changes: 18 additions & 0 deletions src/ReverseProxy/Utilities/Observability.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
samsp-msft marked this conversation as resolved.
Show resolved Hide resolved
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace Yarp.ReverseProxy.Utilities
samsp-msft marked this conversation as resolved.
Show resolved Hide resolved
{
samsp-msft marked this conversation as resolved.
Show resolved Hide resolved
internal class Observability
samsp-msft marked this conversation as resolved.
Show resolved Hide resolved
{
private static ActivitySource _activitySource = new ActivitySource("Yarp.ReverseProxy");
samsp-msft marked this conversation as resolved.
Show resolved Hide resolved

public static ActivitySource YarpActivitySource => _activitySource;
samsp-msft marked this conversation as resolved.
Show resolved Hide resolved

public static bool IsListening => _activitySource.HasListeners();
}
}