Skip to content

Commit 196a45b

Browse files
committed
s/TextMapPropagator/DistributedContextPropagator
1 parent c0da476 commit 196a45b

File tree

8 files changed

+34
-31
lines changed

8 files changed

+34
-31
lines changed

src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public GenericWebHostBuilder(IHostBuilder builder, WebHostBuilderOptions options
8787
services.TryAddSingleton(sp => new DiagnosticListener("Microsoft.AspNetCore"));
8888
services.TryAddSingleton<DiagnosticSource>(sp => sp.GetRequiredService<DiagnosticListener>());
8989
services.TryAddSingleton(sp => new ActivitySource("Microsoft.AspNetCore"));
90-
services.TryAddSingleton(sp => TextMapPropagator.Default);
90+
services.TryAddSingleton(DistributedContextPropagator.Current);
9191

9292
services.TryAddSingleton<IHttpContextFactory, DefaultHttpContextFactory>();
9393
services.TryAddScoped<IMiddlewareFactory, MiddlewareFactory>();

src/Hosting/Hosting/src/GenericHost/GenericWebHostedService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public GenericWebHostService(IOptions<GenericWebHostServiceOptions> options,
2626
ILoggerFactory loggerFactory,
2727
DiagnosticListener diagnosticListener,
2828
ActivitySource activitySource,
29-
TextMapPropagator propagator,
29+
DistributedContextPropagator propagator,
3030
IHttpContextFactory httpContextFactory,
3131
IApplicationBuilderFactory applicationBuilderFactory,
3232
IEnumerable<IStartupFilter> startupFilters,
@@ -54,7 +54,7 @@ public GenericWebHostService(IOptions<GenericWebHostServiceOptions> options,
5454
public ILogger LifetimeLogger { get; }
5555
public DiagnosticListener DiagnosticListener { get; }
5656
public ActivitySource ActivitySource { get; }
57-
public TextMapPropagator Propagator { get; }
57+
public DistributedContextPropagator Propagator { get; }
5858
public IHttpContextFactory HttpContextFactory { get; }
5959
public IApplicationBuilderFactory ApplicationBuilderFactory { get; }
6060
public IEnumerable<IStartupFilter> StartupFilters { get; }

src/Hosting/Hosting/src/Internal/HostingApplication.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public HostingApplication(
2424
ILogger logger,
2525
DiagnosticListener diagnosticSource,
2626
ActivitySource activitySource,
27-
TextMapPropagator propagator,
27+
DistributedContextPropagator propagator,
2828
IHttpContextFactory httpContextFactory)
2929
{
3030
_application = application;

src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ internal class HostingApplicationDiagnostics
2828

2929
private readonly ActivitySource _activitySource;
3030
private readonly DiagnosticListener _diagnosticListener;
31-
private readonly TextMapPropagator _propagator;
31+
private readonly DistributedContextPropagator _propagator;
3232
private readonly ILogger _logger;
3333

3434
public HostingApplicationDiagnostics(
3535
ILogger logger,
3636
DiagnosticListener diagnosticListener,
3737
ActivitySource activitySource,
38-
TextMapPropagator propagator)
38+
DistributedContextPropagator propagator)
3939
{
4040
_logger = logger;
4141
_diagnosticListener = diagnosticListener;
@@ -283,21 +283,20 @@ private static void RecordRequestStartEventLog(HttpContext httpContext)
283283
return null;
284284
}
285285
var headers = httpContext.Request.Headers;
286-
287-
_propagator.Extract(headers, static (object carrier, string fieldName, out string? value) =>
288-
{
289-
value = default;
290-
var headers = ((IHeaderDictionary)carrier);
291-
var values = headers[fieldName];
292-
if (values.Count == 0)
286+
_propagator.ExtractTraceIdAndState(headers,
287+
static (object? carrier, string fieldName, out string? fieldValue, out IEnumerable<string>? fieldValues) =>
293288
{
294-
return false;
295-
}
296-
value = values;
297-
return true;
298-
},
299-
300-
out string? requestId, out string? traceState);
289+
fieldValue = default;
290+
fieldValues = default;
291+
if (carrier is null)
292+
{
293+
return;
294+
}
295+
var headers = (IHeaderDictionary)carrier;
296+
fieldValue = headers[fieldName];
297+
},
298+
out var requestId,
299+
out var traceState);
301300

302301
if (requestId is not null)
303302
{
@@ -310,15 +309,19 @@ private static void RecordRequestStartEventLog(HttpContext httpContext)
310309

311310
if (!string.IsNullOrEmpty(requestId))
312311
{
313-
_propagator.Extract(headers, static (object carrier, string fieldName, out string? value) =>
312+
var baggage = _propagator.ExtractBaggage(headers, static (object? carrier, string fieldName, out string? fieldValue, out IEnumerable<string>? fieldValues) =>
314313
{
315-
var headers = ((IHeaderDictionary)carrier);
316-
value = headers[fieldName];
317-
return true;
318-
}, out IEnumerable<KeyValuePair<string, string?>>? baggage);
314+
fieldValue = default;
315+
fieldValues = default;
316+
if (carrier is null)
317+
{
318+
return;
319+
}
320+
var headers = (IHeaderDictionary)carrier;
321+
fieldValue = headers[fieldName];
322+
});
319323

320-
// AddBaggage adds items at the beginning of the list, so we need to add them in reverse to keep the same order as the client
321-
// An order could be important if baggage has two items with the same key (that is allowed by the contract)
324+
// Order could be important if baggage has two items with the same key (that is allowed by the contract)
322325
if (baggage is not null)
323326
{
324327
foreach (var baggageItem in baggage)

src/Hosting/Hosting/src/Internal/WebHost.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public async Task StartAsync(CancellationToken cancellationToken = default)
157157

158158
var diagnosticSource = _applicationServices.GetRequiredService<DiagnosticListener>();
159159
var activitySource = _applicationServices.GetRequiredService<ActivitySource>();
160-
var propagator = _applicationServices.GetRequiredService<TextMapPropagator>();
160+
var propagator = _applicationServices.GetRequiredService<DistributedContextPropagator>();
161161
var httpContextFactory = _applicationServices.GetRequiredService<IHttpContextFactory>();
162162
var hostingApp = new HostingApplication(application, _logger, diagnosticSource, activitySource, propagator, httpContextFactory);
163163
await Server.StartAsync(hostingApp, cancellationToken).ConfigureAwait(false);

src/Hosting/Hosting/src/WebHostBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ private IServiceCollection BuildCommonServices(out AggregateException? hostingSt
293293
services.TryAddSingleton(sp => new DiagnosticListener("Microsoft.AspNetCore"));
294294
services.TryAddSingleton<DiagnosticSource>(sp => sp.GetRequiredService<DiagnosticListener>());
295295
services.TryAddSingleton(sp => new ActivitySource("Microsoft.AspNetCore"));
296-
services.TryAddSingleton(sp => TextMapPropagator.Default);
296+
services.TryAddSingleton(DistributedContextPropagator.Current);
297297

298298
services.AddTransient<IApplicationBuilderFactory, ApplicationBuilderFactory>();
299299
services.AddTransient<IHttpContextFactory, DefaultHttpContextFactory>();

src/Hosting/Hosting/test/HostingApplicationDiagnosticsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ private static HostingApplication CreateApplication(out FeatureCollection featur
552552
logger ?? new NullScopeLogger(),
553553
diagnosticListener ?? new NoopDiagnosticListener(),
554554
activitySource ?? new ActivitySource("Microsoft.AspNetCore"),
555-
TextMapPropagator.Default,
555+
DistributedContextPropagator.CreateDefaultPropagator(),
556556
httpContextFactory.Object);
557557

558558
return hostingApplication;

src/Hosting/Hosting/test/HostingApplicationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ private static HostingApplication CreateApplication(IHttpContextFactory httpCont
196196
NullLogger.Instance,
197197
new DiagnosticListener("Microsoft.AspNetCore"),
198198
activitySource ?? new ActivitySource("Microsoft.AspNetCore"),
199-
TextMapPropagator.Default,
199+
DistributedContextPropagator.CreateDefaultPropagator(),
200200
httpContextFactory);
201201

202202
return hostingApplication;

0 commit comments

Comments
 (0)