Call DiagnosticListener.OnActivityImport before starting Activity to support sampling better (#9340)
* use OnActivityImport and HttpContext in the extra argument
This commit is contained in:
parent
7718671cf7
commit
4649449206
|
|
@ -273,6 +273,8 @@ namespace Microsoft.AspNetCore.Hosting.Internal
|
|||
}
|
||||
}
|
||||
|
||||
_diagnosticListener.OnActivityImport(activity, httpContext);
|
||||
|
||||
if (_diagnosticListener.IsEnabled(ActivityStartKey))
|
||||
{
|
||||
_diagnosticListener.StartActivity(activity, new { HttpContext = httpContext });
|
||||
|
|
|
|||
|
|
@ -11,8 +11,6 @@ using Microsoft.AspNetCore.Hosting.Internal;
|
|||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.Features;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.ObjectPool;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
|
|
@ -304,6 +302,34 @@ namespace Microsoft.AspNetCore.Hosting.Tests
|
|||
Assert.Contains(Activity.Current.Baggage, pair => pair.Key == "Key2" && pair.Value == "value2");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ActivityOnExportHookIsCalled()
|
||||
{
|
||||
var diagnosticSource = new DiagnosticListener("DummySource");
|
||||
var hostingApplication = CreateApplication(out var features, diagnosticSource: diagnosticSource);
|
||||
|
||||
bool onActivityImportCalled = false;
|
||||
diagnosticSource.Subscribe(
|
||||
observer: new CallbackDiagnosticListener(pair => { }),
|
||||
isEnabled: (s, o, _) => true,
|
||||
onActivityImport: (activity, context) =>
|
||||
{
|
||||
onActivityImportCalled = true;
|
||||
Assert.Null(Activity.Current);
|
||||
Assert.Equal("Microsoft.AspNetCore.Hosting.HttpRequestIn", activity.OperationName);
|
||||
Assert.NotNull(context);
|
||||
Assert.IsAssignableFrom<HttpContext>(context);
|
||||
|
||||
activity.ActivityTraceFlags = ActivityTraceFlags.Recorded;
|
||||
});
|
||||
|
||||
hostingApplication.CreateContext(features);
|
||||
|
||||
Assert.True(onActivityImportCalled);
|
||||
Assert.NotNull(Activity.Current);
|
||||
Assert.True(Activity.Current.Recorded);
|
||||
}
|
||||
|
||||
|
||||
private static void AssertProperty<T>(object o, string name)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue