-
Notifications
You must be signed in to change notification settings - Fork 838
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle distributed context headers in custom propagator (#1533)
* Handle distributed context headers in custom propagator * Update src/ReverseProxy/Forwarder/ForwarderHttpClientFactory.cs Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com> * Addressing review feedback * Improve docs * Apply suggestions from code review Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com> * Apply suggestions from code review Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com> * Add ActivityHeadersPropagator into Direct.Sample Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com>
- Loading branch information
Showing
8 changed files
with
81 additions
and
29 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,53 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
#if NET6_0_OR_GREATER | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Net.Http; | ||
|
||
namespace Yarp.ReverseProxy.Forwarder; | ||
|
||
/// <summary> | ||
/// Removes existing headers and then delegates to the inner propagator. | ||
/// </summary> | ||
public sealed class ReverseProxyPropagator : DistributedContextPropagator | ||
{ | ||
private readonly DistributedContextPropagator _innerPropagator; | ||
private readonly string[] _headersToRemove; | ||
|
||
/// <summary> | ||
/// ReverseProxyPropagator removes headers pointed out in innerPropagator. | ||
/// </summary> | ||
public ReverseProxyPropagator(DistributedContextPropagator innerPropagator) | ||
{ | ||
_innerPropagator = innerPropagator ?? throw new ArgumentNullException(nameof(innerPropagator)); | ||
_headersToRemove = _innerPropagator.Fields.ToArray(); | ||
} | ||
|
||
public override void Inject(Activity? activity, object? carrier, PropagatorSetterCallback? setter) | ||
{ | ||
if (carrier is HttpRequestMessage message) | ||
{ | ||
var headers = message.Headers; | ||
|
||
foreach (var header in _headersToRemove) | ||
{ | ||
headers.Remove(header); | ||
} | ||
} | ||
|
||
_innerPropagator.Inject(activity, carrier, setter); | ||
} | ||
|
||
public override void ExtractTraceIdAndState(object? carrier, PropagatorGetterCallback? getter, out string? traceId, out string? traceState) => | ||
_innerPropagator.ExtractTraceIdAndState(carrier, getter, out traceId, out traceState); | ||
|
||
public override IEnumerable<KeyValuePair<string, string?>>? ExtractBaggage(object? carrier, PropagatorGetterCallback? getter) => | ||
_innerPropagator.ExtractBaggage(carrier, getter); | ||
|
||
public override IReadOnlyCollection<string> Fields => _innerPropagator.Fields; | ||
} | ||
#endif |
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