1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
+ using System . Diagnostics ;
5
+ using System . Diagnostics . Metrics ;
4
6
using System . Globalization ;
5
7
using System . Linq ;
6
8
using System . Security . Claims ;
@@ -23,11 +25,13 @@ internal partial class CircuitHost : IAsyncDisposable
23
25
private readonly CircuitOptions _options ;
24
26
private readonly RemoteNavigationManager _navigationManager ;
25
27
private readonly ILogger _logger ;
28
+ private readonly CircuitMetrics ? _circuitMetrics ;
26
29
private Func < Func < Task > , Task > _dispatchInboundActivity ;
27
30
private CircuitHandler [ ] _circuitHandlers ;
28
31
private bool _initialized ;
29
32
private bool _isFirstUpdate = true ;
30
33
private bool _disposed ;
34
+ private long _startTime ;
31
35
32
36
// This event is fired when there's an unrecoverable exception coming from the circuit, and
33
37
// it need so be torn down. The registry listens to this even so that the circuit can
@@ -47,6 +51,7 @@ public CircuitHost(
47
51
RemoteJSRuntime jsRuntime ,
48
52
RemoteNavigationManager navigationManager ,
49
53
CircuitHandler [ ] circuitHandlers ,
54
+ CircuitMetrics circuitMetrics ,
50
55
ILogger logger )
51
56
{
52
57
CircuitId = circuitId ;
@@ -64,6 +69,7 @@ public CircuitHost(
64
69
JSRuntime = jsRuntime ?? throw new ArgumentNullException ( nameof ( jsRuntime ) ) ;
65
70
_navigationManager = navigationManager ?? throw new ArgumentNullException ( nameof ( navigationManager ) ) ;
66
71
_circuitHandlers = circuitHandlers ?? throw new ArgumentNullException ( nameof ( circuitHandlers ) ) ;
72
+ _circuitMetrics = circuitMetrics ;
67
73
_logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
68
74
69
75
Services = scope . ServiceProvider ;
@@ -230,6 +236,8 @@ await Renderer.Dispatcher.InvokeAsync(async () =>
230
236
private async Task OnCircuitOpenedAsync ( CancellationToken cancellationToken )
231
237
{
232
238
Log . CircuitOpened ( _logger , CircuitId ) ;
239
+ _startTime = ( ( bool ) _circuitMetrics ? . IsDurationEnabled ( ) ) ? Stopwatch . GetTimestamp ( ) : 0 ;
240
+ _circuitMetrics ? . OnCircuitOpened ( ) ;
233
241
234
242
Renderer . Dispatcher . AssertAccess ( ) ;
235
243
@@ -259,6 +267,7 @@ private async Task OnCircuitOpenedAsync(CancellationToken cancellationToken)
259
267
public async Task OnConnectionUpAsync ( CancellationToken cancellationToken )
260
268
{
261
269
Log . ConnectionUp ( _logger , CircuitId , Client . ConnectionId ) ;
270
+ _circuitMetrics ? . OnConnectionUp ( ) ;
262
271
263
272
Renderer . Dispatcher . AssertAccess ( ) ;
264
273
@@ -288,6 +297,7 @@ public async Task OnConnectionUpAsync(CancellationToken cancellationToken)
288
297
public async Task OnConnectionDownAsync ( CancellationToken cancellationToken )
289
298
{
290
299
Log . ConnectionDown ( _logger , CircuitId , Client . ConnectionId ) ;
300
+ _circuitMetrics ? . OnConnectionDown ( ) ;
291
301
292
302
Renderer . Dispatcher . AssertAccess ( ) ;
293
303
@@ -317,6 +327,7 @@ public async Task OnConnectionDownAsync(CancellationToken cancellationToken)
317
327
private async Task OnCircuitDownAsync ( CancellationToken cancellationToken )
318
328
{
319
329
Log . CircuitClosed ( _logger , CircuitId ) ;
330
+ _circuitMetrics ? . OnCircuitDown ( _startTime , Stopwatch . GetTimestamp ( ) ) ;
320
331
321
332
List < Exception > exceptions = null ;
322
333
0 commit comments