55
66namespace  Microsoft . AspNetCore . Components ; 
77
8- /// <summary> 
9- /// Named tuple for restoring the previous activity after stopping the current one. 
10- /// </summary> 
11- internal  struct  ComponentsActivityHandle 
12- { 
13-     public  Activity ?  Previous ; 
14-     public  Activity ?  Activity ; 
15- } 
16- 
178/// <summary> 
189/// This is instance scoped per renderer 
1910/// </summary> 
@@ -23,13 +14,15 @@ internal class ComponentsActivitySource
2314    internal  const  string  OnRouteName  =  $ "{ Name } .RouteChange"; 
2415    internal  const  string  OnEventName  =  $ "{ Name } .HandleEvent"; 
2516
26-     internal  ActivityContext  _httpActivityContext ; 
27-     internal  ActivityContext  _routeContext ; 
28-     internal  ActivityContext  _circuitActivityContext ; 
29-     internal  string ?  _circuitId ; 
17+     private  readonly  IComponentsActivityLinkStore  _activityLinkStore ; 
3018
3119    private  ActivitySource  ActivitySource  {  get ;  }  =  new  ActivitySource ( Name ) ; 
3220
21+     public  ComponentsActivitySource ( IComponentsActivityLinkStore  activityLinkStore ) 
22+     { 
23+         _activityLinkStore  =  activityLinkStore  ??  throw  new  ArgumentNullException ( nameof ( activityLinkStore ) ) ; 
24+     } 
25+ 
3326    public  ComponentsActivityHandle  StartRouteActivity ( string  componentType ,  string  route ) 
3427    { 
3528        var  activity  =  ActivitySource . CreateActivity ( OnRouteName ,  ActivityKind . Internal ,  parentId :  null ,  null ,  null ) ; 
@@ -38,10 +31,6 @@ public ComponentsActivityHandle StartRouteActivity(string componentType, string
3831            var  previousActivity  =  Activity . Current ; 
3932            if  ( activity . IsAllDataRequested ) 
4033            { 
41-                 if  ( _circuitId  !=  null ) 
42-                 { 
43-                     activity . SetTag ( "aspnetcore.components.circuit.id" ,  _circuitId ) ; 
44-                 } 
4534                if  ( componentType  !=  null ) 
4635                { 
4736                    activity . SetTag ( "aspnetcore.components.type" ,  componentType ) ; 
@@ -54,28 +43,32 @@ public ComponentsActivityHandle StartRouteActivity(string componentType, string
5443                { 
5544                    activity . AddLink ( new  ActivityLink ( previousActivity . Context ) ) ; 
5645                } 
46+ 
47+                 // store the link 
48+                 _activityLinkStore . SetActivityContext ( ComponentsActivityCategory . Route ,  activity . Context , 
49+                     new  KeyValuePair < string ,  object ? > ( "aspnetcore.components.route" ,  route ) ) ; 
5750            } 
5851
5952            activity . DisplayName  =  $ "Route { route  ??  "[unknown path]" }  -> { componentType  ??  "[unknown component]" } "; 
6053            Activity . Current  =  null ;  // do not inherit the parent activity 
6154            activity . Start ( ) ; 
62-             _routeContext  =  activity . Context ; 
6355            return  new  ComponentsActivityHandle  {  Activity  =  activity ,  Previous  =  previousActivity  } ; 
6456        } 
6557        return  default ; 
6658    } 
6759
60+     public  void  StopRouteActivity ( ComponentsActivityHandle  activityHandle ,  Exception ?  ex ) 
61+     { 
62+         StopComponentActivity ( ComponentsActivityCategory . Route ,  activityHandle ,  ex ) ; 
63+     } 
64+ 
6865    public  ComponentsActivityHandle  StartEventActivity ( string ?  componentType ,  string ?  methodName ,  string ?  attributeName ) 
6966    { 
7067        var  activity  =  ActivitySource . CreateActivity ( OnEventName ,  ActivityKind . Internal ,  parentId :  null ,  null ,  null ) ; 
7168        if  ( activity  is  not null ) 
7269        { 
7370            if  ( activity . IsAllDataRequested ) 
7471            { 
75-                 if  ( _circuitId  !=  null ) 
76-                 { 
77-                     activity . SetTag ( "aspnetcore.components.circuit.id" ,  _circuitId ) ; 
78-                 } 
7972                if  ( componentType  !=  null ) 
8073                { 
8174                    activity . SetTag ( "aspnetcore.components.type" ,  componentType ) ; 
@@ -99,35 +92,38 @@ public ComponentsActivityHandle StartEventActivity(string? componentType, string
9992        return  default ; 
10093    } 
10194
102-     public  void  StopComponentActivity ( ComponentsActivityHandle  activityHandle ,  Exception ?  ex ) 
95+     public  void  StopEventActivity ( ComponentsActivityHandle  activityHandle ,  Exception ?  ex ) 
96+     { 
97+         StopComponentActivity ( ComponentsActivityCategory . Event ,  activityHandle ,  ex ) ; 
98+     } 
99+ 
100+     public  async  Task  CaptureEventStopAsync ( Task  task ,  ComponentsActivityHandle  activityHandle ) 
101+     { 
102+         try 
103+         { 
104+             await  task ; 
105+             StopEventActivity ( activityHandle ,  null ) ; 
106+         } 
107+         catch  ( Exception  ex ) 
108+         { 
109+             StopEventActivity ( activityHandle ,  ex ) ; 
110+         } 
111+     } 
112+ 
113+     private  void  StopComponentActivity ( int  category ,  ComponentsActivityHandle  activityHandle ,  Exception ?  ex ) 
103114    { 
104115        var  activity  =  activityHandle . Activity ; 
105116        if  ( activity  !=  null  &&  ! activity . IsStopped ) 
106117        { 
107-             if  ( activity . IsAllDataRequested ) 
108-             { 
109-                 if  ( _circuitId  !=  null ) 
110-                 { 
111-                     activity . SetTag ( "aspnetcore.components.circuit.id" ,  _circuitId ) ; 
112-                 } 
113-                 if  ( _httpActivityContext  !=  default ) 
114-                 { 
115-                     activity . AddLink ( new  ActivityLink ( _httpActivityContext ) ) ; 
116-                 } 
117-                 if  ( _circuitActivityContext  !=  default ) 
118-                 { 
119-                     activity . AddLink ( new  ActivityLink ( _circuitActivityContext ) ) ; 
120-                 } 
121-                 if  ( _routeContext  !=  default  &&  activity . Context  !=  _routeContext ) 
122-                 { 
123-                     activity . AddLink ( new  ActivityLink ( _routeContext ) ) ; 
124-                 } 
125-             } 
126118            if  ( ex  !=  null ) 
127119            { 
128120                activity . SetTag ( "error.type" ,  ex . GetType ( ) . FullName ) ; 
129121                activity . SetStatus ( ActivityStatusCode . Error ) ; 
130122            } 
123+             if  ( activity . IsAllDataRequested ) 
124+             { 
125+                 _activityLinkStore . AddActivityContexts ( category ,  activity ) ; 
126+             } 
131127            activity . Stop ( ) ; 
132128
133129            if  ( Activity . Current  ==  null  &&  activityHandle . Previous  !=  null  &&  ! activityHandle . Previous . IsStopped ) 
@@ -136,17 +132,13 @@ public void StopComponentActivity(ComponentsActivityHandle activityHandle, Excep
136132            } 
137133        } 
138134    } 
135+ } 
139136
140-     public  async  Task  CaptureEventStopAsync ( Task  task ,  ComponentsActivityHandle  activityHandle ) 
141-     { 
142-         try 
143-         { 
144-             await  task ; 
145-             StopComponentActivity ( activityHandle ,  null ) ; 
146-         } 
147-         catch  ( Exception  ex ) 
148-         { 
149-             StopComponentActivity ( activityHandle ,  ex ) ; 
150-         } 
151-     } 
137+ /// <summary> 
138+ /// Named tuple for restoring the previous activity after stopping the current one. 
139+ /// </summary> 
140+ internal  struct  ComponentsActivityHandle 
141+ { 
142+     public  Activity ?  Previous ; 
143+     public  Activity ?  Activity ; 
152144} 
0 commit comments