-
Notifications
You must be signed in to change notification settings - Fork 843
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Wire up DiagnosticsHandler * Remove deprecated events * Fix broken tests * Add a toggle to the HttpClientOptions * Add short-circuit if not enabled * Remove existing header if we already have a parent * Fixes after rebasing * Greatly simplify DiagnosticsHandler * Further simplification * Yet even simpler * Rename to ActivityPropagationHandler Fix comments * Update src/ReverseProxy/Telemetry/ActivityPropagationHandler.cs Co-authored-by: Chris Ross <[email protected]> * Make PropagateActivityContext nullable * Fix TODO in test * Fix PR comments * Add ActivityPropagationHandler test * Add missing using * Made handler public so that it can be re-used * Moved test to appropriate folder * Fix namespace Co-authored-by: Chris Ross <[email protected]> Co-authored-by: MihaZupan <[email protected]>
- Loading branch information
1 parent
ac86b14
commit 31ca104
Showing
11 changed files
with
262 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.ReverseProxy.Telemetry | ||
{ | ||
/// <summary> | ||
/// ActivityPropagationHandler propagates the current Activity to the downstream service | ||
/// </summary> | ||
public sealed class ActivityPropagationHandler : DelegatingHandler | ||
{ | ||
private const string RequestIdHeaderName = "Request-Id"; | ||
private const string CorrelationContextHeaderName = "Correlation-Context"; | ||
|
||
private const string TraceParentHeaderName = "traceparent"; | ||
private const string TraceStateHeaderName = "tracestate"; | ||
|
||
public ActivityPropagationHandler(HttpMessageHandler innerHandler) : base(innerHandler) | ||
{ | ||
} | ||
|
||
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, | ||
CancellationToken cancellationToken) | ||
{ | ||
// This handler is conditionally inserted by the ProxyHttpClientFactory based on the configuration | ||
// If inserted it will insert the necessary headers to propagate the current activity context to | ||
// the downstream service, if there is a current activity | ||
|
||
if (request == null) | ||
{ | ||
throw new ArgumentNullException(nameof(request)); | ||
} | ||
|
||
// If we are on at all, we propagate current activity information | ||
var currentActivity = Activity.Current; | ||
if (currentActivity != null) | ||
{ | ||
InjectHeaders(currentActivity, request); | ||
} | ||
|
||
return base.SendAsync(request, cancellationToken); | ||
} | ||
|
||
private void InjectHeaders(Activity currentActivity, HttpRequestMessage request) | ||
{ | ||
if (currentActivity.IdFormat == ActivityIdFormat.W3C) | ||
{ | ||
request.Headers.Remove(TraceParentHeaderName); | ||
request.Headers.Remove(TraceStateHeaderName); | ||
|
||
request.Headers.TryAddWithoutValidation(TraceParentHeaderName, currentActivity.Id); | ||
if (currentActivity.TraceStateString != null) | ||
{ | ||
request.Headers.TryAddWithoutValidation(TraceStateHeaderName, currentActivity.TraceStateString); | ||
} | ||
} | ||
else | ||
{ | ||
request.Headers.Remove(RequestIdHeaderName); | ||
request.Headers.TryAddWithoutValidation(RequestIdHeaderName, currentActivity.Id); | ||
} | ||
|
||
// we expect baggage to be empty or contain a few items | ||
using (var e = currentActivity.Baggage.GetEnumerator()) | ||
{ | ||
if (e.MoveNext()) | ||
{ | ||
var baggage = new List<string>(); | ||
do | ||
{ | ||
var item = e.Current; | ||
baggage.Add(new NameValueHeaderValue(Uri.EscapeDataString(item.Key), Uri.EscapeDataString(item.Value)).ToString()); | ||
} | ||
while (e.MoveNext()); | ||
request.Headers.TryAddWithoutValidation(CorrelationContextHeaderName, baggage); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.ReverseProxy.Common.Tests | ||
{ | ||
internal class MockHttpHandler : HttpMessageHandler | ||
{ | ||
private readonly Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> _func; | ||
|
||
public MockHttpHandler(Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> func) | ||
{ | ||
_func = func ?? throw new ArgumentNullException(nameof(func)); | ||
} | ||
|
||
public static HttpMessageInvoker CreateClient(Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> func) | ||
{ | ||
var handler = new MockHttpHandler(func); | ||
return new HttpMessageInvoker(handler); | ||
} | ||
|
||
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
return _func(request, cancellationToken); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.