Skip to content

Commit 21ea543

Browse files
committed
Consolidated metrics handling for middleware.
1 parent 78b3166 commit 21ea543

11 files changed

Lines changed: 111 additions & 241 deletions

src/Autofac/Core/Resolving/Middleware/ActivatorErrorHandlingMiddleware.cs

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.Globalization;
55
using Autofac.Core.Resolving.Pipeline;
6-
using Autofac.Diagnostics;
76

87
namespace Autofac.Core.Resolving.Middleware;
98

@@ -29,20 +28,23 @@ private ActivatorErrorHandlingMiddleware()
2928
/// <inheritdoc />
3029
public void Execute(ResolveRequestContext context, Action<ResolveRequestContext> next)
3130
{
32-
if (!AutofacMetrics.MetricsEnabled)
31+
try
3332
{
34-
ExecuteCore(context, next);
35-
return;
36-
}
33+
next(context);
3734

38-
var timer = ValueStopwatch.StartNew();
39-
try
35+
if (context.Instance is null)
36+
{
37+
// Exited the Activation Stage without creating an instance.
38+
throw new DependencyResolutionException(MiddlewareMessages.ActivatorDidNotPopulateInstance);
39+
}
40+
}
41+
catch (ObjectDisposedException)
4042
{
41-
ExecuteCore(context, next);
43+
throw;
4244
}
43-
finally
45+
catch (Exception ex)
4446
{
45-
AutofacMetrics.RecordMiddlewareExecution(nameof(ActivatorErrorHandlingMiddleware), timer.GetElapsedTime());
47+
throw PropagateActivationException(context.Registration.Activator, ex);
4648
}
4749
}
4850

@@ -65,26 +67,4 @@ private static DependencyResolutionException PropagateActivationException(IInsta
6567
result.Data[ActivatorChainExceptionData] = activatorChain;
6668
return result;
6769
}
68-
69-
private static void ExecuteCore(ResolveRequestContext context, Action<ResolveRequestContext> next)
70-
{
71-
try
72-
{
73-
next(context);
74-
75-
if (context.Instance is null)
76-
{
77-
// Exited the Activation Stage without creating an instance.
78-
throw new DependencyResolutionException(MiddlewareMessages.ActivatorDidNotPopulateInstance);
79-
}
80-
}
81-
catch (ObjectDisposedException)
82-
{
83-
throw;
84-
}
85-
catch (Exception ex)
86-
{
87-
throw PropagateActivationException(context.Registration.Activator, ex);
88-
}
89-
}
9070
}

src/Autofac/Core/Resolving/Middleware/CircularDependencyDetectorMiddleware.cs

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Globalization;
55
using System.Runtime.CompilerServices;
66
using Autofac.Core.Resolving.Pipeline;
7-
using Autofac.Diagnostics;
87

98
namespace Autofac.Core.Resolving.Middleware;
109

@@ -39,51 +38,6 @@ public CircularDependencyDetectorMiddleware(int maxResolveDepth)
3938

4039
/// <inheritdoc/>
4140
public void Execute(ResolveRequestContext context, Action<ResolveRequestContext> next)
42-
{
43-
if (!AutofacMetrics.MetricsEnabled)
44-
{
45-
ExecuteCore(context, next);
46-
return;
47-
}
48-
49-
var timer = ValueStopwatch.StartNew();
50-
try
51-
{
52-
ExecuteCore(context, next);
53-
}
54-
finally
55-
{
56-
AutofacMetrics.RecordMiddlewareExecution(nameof(CircularDependencyDetectorMiddleware), timer.GetElapsedTime());
57-
}
58-
}
59-
60-
/// <inheritdoc/>
61-
public override string ToString() => nameof(CircularDependencyDetectorMiddleware);
62-
63-
private static string CreateDependencyGraphTo(IComponentRegistration registration, IEnumerable<ResolveRequestContext> requestStack)
64-
{
65-
if (registration == null)
66-
{
67-
throw new ArgumentNullException(nameof(registration));
68-
}
69-
70-
if (requestStack == null)
71-
{
72-
throw new ArgumentNullException(nameof(requestStack));
73-
}
74-
75-
var dependencyGraph = Display(registration);
76-
77-
return requestStack.Select(a => a.Registration)
78-
.Aggregate(dependencyGraph, (current, requestor) => Display(requestor) + " -> " + current);
79-
}
80-
81-
private static string Display(IComponentRegistration registration)
82-
{
83-
return registration.Activator.DisplayName();
84-
}
85-
86-
private void ExecuteCore(ResolveRequestContext context, Action<ResolveRequestContext> next)
8741
{
8842
if (context.Operation is not IDependencyTrackingResolveOperation dependencyTrackingResolveOperation)
8943
{
@@ -141,4 +95,30 @@ private void ExecuteCore(ResolveRequestContext context, Action<ResolveRequestCon
14195
requestStack.Pop();
14296
}
14397
}
98+
99+
/// <inheritdoc/>
100+
public override string ToString() => nameof(CircularDependencyDetectorMiddleware);
101+
102+
private static string CreateDependencyGraphTo(IComponentRegistration registration, IEnumerable<ResolveRequestContext> requestStack)
103+
{
104+
if (registration == null)
105+
{
106+
throw new ArgumentNullException(nameof(registration));
107+
}
108+
109+
if (requestStack == null)
110+
{
111+
throw new ArgumentNullException(nameof(requestStack));
112+
}
113+
114+
var dependencyGraph = Display(registration);
115+
116+
return requestStack.Select(a => a.Registration)
117+
.Aggregate(dependencyGraph, (current, requestor) => Display(requestor) + " -> " + current);
118+
}
119+
120+
private static string Display(IComponentRegistration registration)
121+
{
122+
return registration.Activator.DisplayName();
123+
}
144124
}

src/Autofac/Core/Resolving/Middleware/CoreEventMiddleware.cs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

44
using Autofac.Core.Resolving.Pipeline;
5-
using Autofac.Diagnostics;
65

76
namespace Autofac.Core.Resolving.Middleware;
87

@@ -40,20 +39,6 @@ internal CoreEventMiddleware(ResolveEventType eventType, PipelinePhase phase, Ac
4039
/// <inheritdoc/>
4140
public void Execute(ResolveRequestContext context, Action<ResolveRequestContext> next)
4241
{
43-
if (!AutofacMetrics.MetricsEnabled)
44-
{
45-
_callback(context, next);
46-
return;
47-
}
48-
49-
var timer = ValueStopwatch.StartNew();
50-
try
51-
{
52-
_callback(context, next);
53-
}
54-
finally
55-
{
56-
AutofacMetrics.RecordMiddlewareExecution(ToString(), timer.GetElapsedTime());
57-
}
42+
_callback(context, next);
5843
}
5944
}

src/Autofac/Core/Resolving/Middleware/DelegateMiddleware.cs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

44
using Autofac.Core.Resolving.Pipeline;
5-
using Autofac.Diagnostics;
65

76
namespace Autofac.Core.Resolving.Middleware;
87

@@ -33,21 +32,7 @@ public DelegateMiddleware(string descriptor, PipelinePhase phase, Action<Resolve
3332
/// <inheritdoc />
3433
public void Execute(ResolveRequestContext context, Action<ResolveRequestContext> next)
3534
{
36-
if (!AutofacMetrics.MetricsEnabled)
37-
{
38-
_callback(context, next);
39-
return;
40-
}
41-
42-
var timer = ValueStopwatch.StartNew();
43-
try
44-
{
45-
_callback(context, next);
46-
}
47-
finally
48-
{
49-
AutofacMetrics.RecordMiddlewareExecution(ToString(), timer.GetElapsedTime());
50-
}
35+
_callback(context, next);
5136
}
5237

5338
/// <inheritdoc />

src/Autofac/Core/Resolving/Middleware/DisposalTrackingMiddleware.cs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

44
using Autofac.Core.Resolving.Pipeline;
5-
using Autofac.Diagnostics;
65

76
namespace Autofac.Core.Resolving.Middleware;
87

@@ -26,28 +25,6 @@ private DisposalTrackingMiddleware()
2625

2726
/// <inheritdoc />
2827
public void Execute(ResolveRequestContext context, Action<ResolveRequestContext> next)
29-
{
30-
if (!AutofacMetrics.MetricsEnabled)
31-
{
32-
ExecuteCore(context, next);
33-
return;
34-
}
35-
36-
var timer = ValueStopwatch.StartNew();
37-
try
38-
{
39-
ExecuteCore(context, next);
40-
}
41-
finally
42-
{
43-
AutofacMetrics.RecordMiddlewareExecution(nameof(DisposalTrackingMiddleware), timer.GetElapsedTime());
44-
}
45-
}
46-
47-
/// <inheritdoc />
48-
public override string ToString() => nameof(DisposalTrackingMiddleware);
49-
50-
private static void ExecuteCore(ResolveRequestContext context, Action<ResolveRequestContext> next)
5128
{
5229
next(context);
5330

@@ -67,4 +44,7 @@ private static void ExecuteCore(ResolveRequestContext context, Action<ResolveReq
6744
}
6845
}
6946
}
47+
48+
/// <inheritdoc />
49+
public override string ToString() => nameof(DisposalTrackingMiddleware);
7050
}

src/Autofac/Core/Resolving/Middleware/RegistrationPipelineInvokeMiddleware.cs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

44
using Autofac.Core.Resolving.Pipeline;
5-
using Autofac.Diagnostics;
65

76
namespace Autofac.Core.Resolving.Middleware;
87

@@ -26,21 +25,7 @@ private RegistrationPipelineInvokeMiddleware()
2625
/// <inheritdoc/>
2726
public void Execute(ResolveRequestContext context, Action<ResolveRequestContext> next)
2827
{
29-
if (!AutofacMetrics.MetricsEnabled)
30-
{
31-
context.Registration.ResolvePipeline.Invoke(context);
32-
return;
33-
}
34-
35-
var timer = ValueStopwatch.StartNew();
36-
try
37-
{
38-
context.Registration.ResolvePipeline.Invoke(context);
39-
}
40-
finally
41-
{
42-
AutofacMetrics.RecordMiddlewareExecution(nameof(RegistrationPipelineInvokeMiddleware), timer.GetElapsedTime());
43-
}
28+
context.Registration.ResolvePipeline.Invoke(context);
4429
}
4530

4631
/// <inheritdoc/>

src/Autofac/Core/Resolving/Middleware/ScopeSelectionMiddleware.cs

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Globalization;
55
using System.Text;
66
using Autofac.Core.Resolving.Pipeline;
7-
using Autofac.Diagnostics;
87

98
namespace Autofac.Core.Resolving.Middleware;
109

@@ -21,35 +20,13 @@ private ScopeSelectionMiddleware()
2120
/// <summary>
2221
/// Gets the singleton instance of the <see cref="ScopeSelectionMiddleware"/>.
2322
/// </summary>
24-
public static ScopeSelectionMiddleware Instance => new();
23+
public static ScopeSelectionMiddleware Instance { get; } = new ScopeSelectionMiddleware();
2524

2625
/// <inheritdoc/>
2726
public PipelinePhase Phase => PipelinePhase.ScopeSelection;
2827

2928
/// <inheritdoc/>
3029
public void Execute(ResolveRequestContext context, Action<ResolveRequestContext> next)
31-
{
32-
if (!AutofacMetrics.MetricsEnabled)
33-
{
34-
ExecuteCore(context, next);
35-
return;
36-
}
37-
38-
var timer = ValueStopwatch.StartNew();
39-
try
40-
{
41-
ExecuteCore(context, next);
42-
}
43-
finally
44-
{
45-
AutofacMetrics.RecordMiddlewareExecution(nameof(ScopeSelectionMiddleware), timer.GetElapsedTime());
46-
}
47-
}
48-
49-
/// <inheritdoc/>
50-
public override string ToString() => nameof(ScopeSelectionMiddleware);
51-
52-
private static void ExecuteCore(ResolveRequestContext context, Action<ResolveRequestContext> next)
5330
{
5431
try
5532
{
@@ -70,4 +47,7 @@ private static void ExecuteCore(ResolveRequestContext context, Action<ResolveReq
7047

7148
next(context);
7249
}
50+
51+
/// <inheritdoc/>
52+
public override string ToString() => nameof(ScopeSelectionMiddleware);
7353
}

src/Autofac/Core/Resolving/Middleware/SharingMiddleware.cs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

44
using Autofac.Core.Resolving.Pipeline;
5-
using Autofac.Diagnostics;
65

76
namespace Autofac.Core.Resolving.Middleware;
87

@@ -21,28 +20,6 @@ internal class SharingMiddleware : IResolveMiddleware
2120

2221
/// <inheritdoc />
2322
public void Execute(ResolveRequestContext context, Action<ResolveRequestContext> next)
24-
{
25-
if (!AutofacMetrics.MetricsEnabled)
26-
{
27-
ExecuteCore(context, next);
28-
return;
29-
}
30-
31-
var timer = ValueStopwatch.StartNew();
32-
try
33-
{
34-
ExecuteCore(context, next);
35-
}
36-
finally
37-
{
38-
AutofacMetrics.RecordMiddlewareExecution(nameof(SharingMiddleware), timer.GetElapsedTime());
39-
}
40-
}
41-
42-
/// <inheritdoc />
43-
public override string ToString() => nameof(SharingMiddleware);
44-
45-
private static void ExecuteCore(ResolveRequestContext context, Action<ResolveRequestContext> next)
4623
{
4724
var registration = context.Registration;
4825
var decoratorRegistration = context.DecoratorTarget;
@@ -78,4 +55,7 @@ private static void ExecuteCore(ResolveRequestContext context, Action<ResolveReq
7855
}
7956
}
8057
}
58+
59+
/// <inheritdoc />
60+
public override string ToString() => nameof(SharingMiddleware);
8161
}

0 commit comments

Comments
 (0)