-
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
- Loading branch information
Katya Sokolova
committed
Jan 26, 2022
1 parent
a323abe
commit 2f67321
Showing
5 changed files
with
61 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// 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; | ||
|
||
public sealed class ReverseProxyPropagator : DistributedContextPropagator | ||
{ | ||
private readonly DistributedContextPropagator _innerPropagator; | ||
private readonly string[] _headersToRemove; | ||
|
||
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