private void TestPassThroughPropagatorUsingW3CActivity(DistributedContextPropagator propagator, string state, IEnumerable <KeyValuePair <string, string> > baggage) { using Activity a = CreateW3CActivity("PassThroughW3C", "PassThroughW3CState=1", baggage); propagator.Inject(a, null, (object carrier, string fieldName, string value) => { if (fieldName == TraceParent) { Assert.Equal(a.Id, value); return; } if (fieldName == TraceState) { Assert.Equal(a.TraceStateString, value); return; } if (fieldName == CorrelationContext) { Assert.Equal(GetFormattedBaggage(a.Baggage), value); return; } Assert.False(true, $"Encountered wrong header name '{fieldName}'"); }); TestDefaultExtraction(propagator, a); TestBaggageExtraction(propagator, a); }
private void TestDefaultPropagatorUsing(Activity a, DistributedContextPropagator propagator, string state, IEnumerable <KeyValuePair <string, string> > baggage) { // Test with non-current propagator.Inject(a, null, (object carrier, string fieldName, string value) => { if (fieldName == TraceParent && a.IdFormat == ActivityIdFormat.W3C) { Assert.Equal(a.Id, value); return; } if (fieldName == RequestId && a.IdFormat != ActivityIdFormat.W3C) { Assert.Equal(a.Id, value); return; } if (fieldName == TraceState) { Assert.Equal(a.TraceStateString, value); return; } if (fieldName == CorrelationContext) { Assert.Equal(GetFormattedBaggage(a.Baggage), value); return; } Assert.False(true, $"Encountered wrong header name '{fieldName}'"); }); TestDefaultExtraction(propagator, a); TestBaggageExtraction(propagator, a); }
public GenericWebHostService(IOptions <GenericWebHostServiceOptions> options, IServer server, ILoggerFactory loggerFactory, DiagnosticListener diagnosticListener, ActivitySource activitySource, DistributedContextPropagator propagator, IHttpContextFactory httpContextFactory, IApplicationBuilderFactory applicationBuilderFactory, IEnumerable <IStartupFilter> startupFilters, IConfiguration configuration, IWebHostEnvironment hostingEnvironment) { Options = options.Value; Server = server; Logger = loggerFactory.CreateLogger("Microsoft.AspNetCore.Hosting.Diagnostics"); LifetimeLogger = loggerFactory.CreateLogger("Microsoft.Hosting.Lifetime"); DiagnosticListener = diagnosticListener; ActivitySource = activitySource; Propagator = propagator; HttpContextFactory = httpContextFactory; ApplicationBuilderFactory = applicationBuilderFactory; StartupFilters = startupFilters; Configuration = configuration; HostingEnvironment = hostingEnvironment; }
private void TestFields(DistributedContextPropagator propagator) { Assert.True(propagator.Fields.Contains(TraceParent)); Assert.True(propagator.Fields.Contains(RequestId)); Assert.True(propagator.Fields.Contains(TraceState)); Assert.True(propagator.Fields.Contains(Baggage)); Assert.True(propagator.Fields.Contains(CorrelationContext)); }
public HostingApplicationDiagnostics( ILogger logger, DiagnosticListener diagnosticListener, ActivitySource activitySource, DistributedContextPropagator propagator) { _logger = logger; _diagnosticListener = diagnosticListener; _activitySource = activitySource; _propagator = propagator; }
private void TestDefaultPropagatorUsingHierarchicalActivity(DistributedContextPropagator propagator, string state, IEnumerable <KeyValuePair <string, string> > baggage) { using Activity a = CreateHierarchicalActivity("LegacyHierarchical1", null, "LegacyHierarchicalState=1", baggage); using Activity b = CreateHierarchicalActivity("LegacyHierarchical2", null, "LegacyHierarchicalState=2", baggage); Assert.NotSame(Activity.Current, a); TestDefaultPropagatorUsing(a, propagator, state, baggage); Assert.Same(Activity.Current, b); TestDefaultPropagatorUsing(Activity.Current, propagator, state, baggage); }
private void TestNoOutputPropagatorUsingW3CActivity(DistributedContextPropagator propagator, string state, IEnumerable <KeyValuePair <string, string> > baggage) { using Activity a = CreateW3CActivity("NoOutputW3C", state, baggage); propagator.Inject(a, null, (object carrier, string fieldName, string value) => { Assert.False(true, $"Not expected to have the setter callback be called in the NoOutput propgator."); }); TestDefaultExtraction(propagator, a); TestBaggageExtraction(propagator, a); }
private void TestDefaultExtraction(DistributedContextPropagator propagator, Activity a) { bool traceParentEncountered = false; propagator.ExtractTraceIdAndState(null, (object carrier, string fieldName, out string?fieldValue, out IEnumerable <string>?fieldValues) => { Assert.Null(carrier); fieldValues = null; fieldValue = null; if (fieldName == TraceParent) { if (a.IdFormat == ActivityIdFormat.W3C) { fieldValue = a.Id; } else { traceParentEncountered = true; } return; } if (fieldName == RequestId) { if (a.IdFormat == ActivityIdFormat.W3C) { Assert.True(false, $"Not expected to get RequestId as we expect the request handled using TraceParenet."); } else { Assert.True(traceParentEncountered, $"Expected to get TraceParent request before getting RequestId."); fieldValue = a.Id; } return; } if (fieldName == TraceState) { fieldValue = a.TraceStateString; return; } Assert.False(true, $"Encountered wrong header name '{fieldName}'"); }, out string?traceId, out string?traceState); Assert.Equal(a.Id, traceId); Assert.Equal(a.TraceStateString, traceState); }
private void TestPassThroughPropagatorUsingHierarchicalActivityWithParentId(DistributedContextPropagator propagator, string state, IEnumerable <KeyValuePair <string, string> > baggage) { using Activity a = CreateHierarchicalActivity("PassThrough", "Parent1", state, baggage); using Activity b = CreateHierarchicalActivity("PassThroughChild1", "Parent2", state + "1", new List <KeyValuePair <string, string> >() { new KeyValuePair <string, string>("Child1Key", "Child1Value") }); using Activity c = CreateHierarchicalActivity("PassThroughChild2", "Parent3", state + "2", new List <KeyValuePair <string, string> >() { new KeyValuePair <string, string>("Child2Key", "Child2Value") }); propagator.Inject(a, null, (object carrier, string fieldName, string value) => { if (fieldName == TraceParent) { Assert.False(true, $"Unexpected to inject a TraceParent with Hierarchical Activity."); return; } if (fieldName == RequestId) { Assert.Equal(c.ParentId, value); return; } if (fieldName == TraceState) { Assert.Equal(c.TraceStateString, value); return; } if (fieldName == CorrelationContext) { Assert.Equal(GetFormattedBaggage(c.Baggage), value); return; } Assert.False(true, $"Encountered wrong header name '{fieldName}'"); }); TestDefaultExtraction(propagator, a); TestDefaultExtraction(propagator, b); TestDefaultExtraction(propagator, c); TestBaggageExtraction(propagator, a); TestBaggageExtraction(propagator, b); TestBaggageExtraction(propagator, c); }
public HostingApplication( RequestDelegate application, ILogger logger, DiagnosticListener diagnosticSource, ActivitySource activitySource, DistributedContextPropagator propagator, IHttpContextFactory httpContextFactory) { _application = application; _diagnostics = new HostingApplicationDiagnostics(logger, diagnosticSource, activitySource, propagator); if (httpContextFactory is DefaultHttpContextFactory factory) { _defaultHttpContextFactory = factory; } else { _httpContextFactory = httpContextFactory; } }
public DiagnosticsHandler(HttpMessageHandler innerHandler, DistributedContextPropagator propagator, bool autoRedirect = false) { Debug.Assert(IsGloballyEnabled()); Debug.Assert(innerHandler is not null && propagator is not null); _innerHandler = innerHandler; _propagator = propagator; // Prepare HeaderDescriptors for fields we need to clear when following redirects if (autoRedirect && _propagator.Fields is IReadOnlyCollection <string> fields && fields.Count > 0) { var fieldDescriptors = new List <HeaderDescriptor>(fields.Count); foreach (string field in fields) { if (field is not null && HeaderDescriptor.TryGet(field, out HeaderDescriptor descriptor)) { fieldDescriptors.Add(descriptor); } } _propagatorFields = fieldDescriptors.ToArray(); } }
private void TestPassThroughPropagatorWithNullCurrent(DistributedContextPropagator propagator) { Activity.Current = null; propagator.Inject(null, null, (object carrier, string fieldName, string value) => { Assert.False(true, $"PassThroughPropagator shouldn't inject anything if the Activity.Current is null"); }); using Activity a = CreateW3CActivity("PassThroughNotNull", "", null); propagator.Inject(a, null, (object carrier, string fieldName, string value) => { if (fieldName == TraceParent) { Assert.Equal(a.Id, value); return; } Assert.False(true, $"Encountered wrong header name '{fieldName}'"); }); }
private static HostingApplication CreateApplication(IHttpContextFactory httpContextFactory = null, bool useHttpContextAccessor = false, ActivitySource activitySource = null) { var services = new ServiceCollection(); services.AddOptions(); if (useHttpContextAccessor) { services.AddHttpContextAccessor(); } httpContextFactory ??= new DefaultHttpContextFactory(services.BuildServiceProvider()); var hostingApplication = new HostingApplication( ctx => Task.CompletedTask, NullLogger.Instance, new DiagnosticListener("Microsoft.AspNetCore"), activitySource ?? new ActivitySource("Microsoft.AspNetCore"), DistributedContextPropagator.CreateDefaultPropagator(), httpContextFactory); return(hostingApplication); }
private static HostingApplication CreateApplication(out FeatureCollection features, DiagnosticListener diagnosticListener = null, ActivitySource activitySource = null, ILogger logger = null, Action <DefaultHttpContext> configure = null) { var httpContextFactory = new Mock <IHttpContextFactory>(); features = new FeatureCollection(); features.Set <IHttpRequestFeature>(new HttpRequestFeature()); var context = new DefaultHttpContext(features); configure?.Invoke(context); httpContextFactory.Setup(s => s.Create(It.IsAny <IFeatureCollection>())).Returns(context); httpContextFactory.Setup(s => s.Dispose(It.IsAny <HttpContext>())); var hostingApplication = new HostingApplication( ctx => Task.CompletedTask, logger ?? new NullScopeLogger(), diagnosticListener ?? new NoopDiagnosticListener(), activitySource ?? new ActivitySource("Microsoft.AspNetCore"), DistributedContextPropagator.CreateDefaultPropagator(), httpContextFactory.Object); return(hostingApplication); }
private void TestBaggageExtraction(DistributedContextPropagator propagator, Activity a) { bool baggageEncountered = false; IEnumerable <KeyValuePair <string, string?> >?b = propagator.ExtractBaggage(null, (object carrier, string fieldName, out string?fieldValue, out IEnumerable <string>?fieldValues) => { Assert.Null(carrier); fieldValue = null; fieldValues = null; if (fieldName == Baggage) { if (a.IdFormat == ActivityIdFormat.W3C) { fieldValue = GetFormattedBaggage(a.Baggage); } else { baggageEncountered = true; } return; } if (fieldName == CorrelationContext && a.IdFormat != ActivityIdFormat.W3C) { Assert.True(baggageEncountered, $"Expected to get Baggage request before getting Correlation-Context."); fieldValue = GetFormattedBaggage(a.Baggage); return; } Assert.False(true, $"Encountered wrong header name '{fieldName}'"); }); Assert.Equal(GetFormattedBaggage(a.Baggage, false, true), GetFormattedBaggage(b, true)); }
/// <summary> /// Initializes a new instance of the <see cref="ActivityPropagationOutgoingGrainCallFilter"/> class. /// </summary> /// <param name="propagator">The context propagator.</param> public ActivityPropagationOutgoingGrainCallFilter(DistributedContextPropagator propagator) { this.propagator = propagator; }
/// <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 void TestBuiltInPropagatorsAreCached() { Assert.Same(DistributedContextPropagator.CreateDefaultPropagator(), DistributedContextPropagator.CreateDefaultPropagator()); Assert.Same(DistributedContextPropagator.CreateNoOutputPropagator(), DistributedContextPropagator.CreateNoOutputPropagator()); Assert.Same(DistributedContextPropagator.CreatePassThroughPropagator(), DistributedContextPropagator.CreatePassThroughPropagator()); }
public void TestAllPropagators() { RemoteExecutor.Invoke(() => { Assert.NotNull(DistributedContextPropagator.Current); // // Default Propagator // Assert.Same(DistributedContextPropagator.CreateDefaultPropagator(), DistributedContextPropagator.Current); TestDefaultPropagatorUsingW3CActivity( DistributedContextPropagator.Current, "Legacy1=true", new List <KeyValuePair <string, string> >() { new KeyValuePair <string, string>(" LegacyKey1 ", " LegacyValue1 ") }); TestDefaultPropagatorUsingHierarchicalActivity( DistributedContextPropagator.Current, "Legacy2=true", new List <KeyValuePair <string, string> >() { new KeyValuePair <string, string>("LegacyKey2", "LegacyValue2") }); TestFields(DistributedContextPropagator.Current); // // NoOutput Propagator // DistributedContextPropagator.Current = DistributedContextPropagator.CreateNoOutputPropagator(); Assert.NotNull(DistributedContextPropagator.Current); TestNoOutputPropagatorUsingHierarchicalActivity( DistributedContextPropagator.Current, "ActivityState=1", new List <KeyValuePair <string, string> >() { new KeyValuePair <string, string>("B1", "V1"), new KeyValuePair <string, string>(" B2 ", " V2 ") }); TestNoOutputPropagatorUsingHierarchicalActivity( DistributedContextPropagator.Current, "ActivityState=2", null); TestNoOutputPropagatorUsingW3CActivity( DistributedContextPropagator.Current, "ActivityState=1", new List <KeyValuePair <string, string> >() { new KeyValuePair <string, string>(" B3 ", " V3"), new KeyValuePair <string, string>(" B4 ", " V4 "), new KeyValuePair <string, string>("B5", "V5") }); TestNoOutputPropagatorUsingW3CActivity( DistributedContextPropagator.Current, "ActivityState=2", null); TestFields(DistributedContextPropagator.Current); // // Pass Through Propagator // DistributedContextPropagator.Current = DistributedContextPropagator.CreatePassThroughPropagator(); Assert.NotNull(DistributedContextPropagator.Current); TestPassThroughPropagatorUsingHierarchicalActivityWithParentChain( DistributedContextPropagator.Current, "PassThrough=true", new List <KeyValuePair <string, string> >() { new KeyValuePair <string, string>("PassThroughKey1", "PassThroughValue1"), new KeyValuePair <string, string>("PassThroughKey2", "PassThroughValue2") }); TestPassThroughPropagatorUsingHierarchicalActivityWithParentId( DistributedContextPropagator.Current, "PassThrough1=true", new List <KeyValuePair <string, string> >() { new KeyValuePair <string, string>("PassThroughKey3", "PassThroughValue3"), new KeyValuePair <string, string>(" PassThroughKey4 ", " PassThroughValue4 ") }); TestPassThroughPropagatorUsingW3CActivity( DistributedContextPropagator.Current, "PassThrough2=1", new List <KeyValuePair <string, string> >() { new KeyValuePair <string, string>(" PassThroughKey4 ", " PassThroughValue4 ") }); TestPassThroughPropagatorWithNullCurrent(DistributedContextPropagator.Current); TestFields(DistributedContextPropagator.Current); // // Test Current // Assert.Throws <ArgumentNullException>(() => DistributedContextPropagator.Current = null); }).Dispose(); }