@@ -24,7 +24,7 @@ internal class ComponentsActivitySource
2424 public static ActivityContext CaptureHttpContext ( )
2525 {
2626 var parentActivity = Activity . Current ;
27- if ( parentActivity is not null && parentActivity . OperationName == "Microsoft.AspNetCore.Hosting.HttpRequestIn" )
27+ if ( parentActivity is not null && parentActivity . OperationName == "Microsoft.AspNetCore.Hosting.HttpRequestIn" && parentActivity . Recorded )
2828 {
2929 return parentActivity . Context ;
3030 }
@@ -34,28 +34,24 @@ public static ActivityContext CaptureHttpContext()
3434 public Activity ? StartCircuitActivity ( string circuitId , ActivityContext httpContext )
3535 {
3636 _circuitId = circuitId ;
37- IEnumerable < KeyValuePair < string , object ? > > tags =
38- [
39- new ( "circuit.id" , _circuitId ?? "unknown" ) ,
40- ] ;
4137
42- var links = new List < ActivityLink > ( ) ;
43- if ( httpContext != default )
44- {
45- _httpContext = httpContext ;
46- links . Add ( new ActivityLink ( httpContext ) ) ;
47- }
48-
49- var activity = ActivitySource . CreateActivity ( OnRouteName , ActivityKind . Server , parentId : null , tags , links ) ;
38+ var activity = ActivitySource . CreateActivity ( OnRouteName , ActivityKind . Server , parentId : null , null , null ) ;
5039 if ( activity is not null )
5140 {
52- activity . DisplayName = $ "CIRCUIT { circuitId ?? "unknown" } ";
41+ if ( activity . IsAllDataRequested )
42+ {
43+ if ( _circuitId != null )
44+ {
45+ activity . SetTag ( "circuit.id" , _circuitId ) ;
46+ }
47+ if ( httpContext != default )
48+ {
49+ activity . AddLink ( new ActivityLink ( httpContext ) ) ;
50+ }
51+ }
52+ activity . DisplayName = $ "CIRCUIT { circuitId ?? "" } ";
5353 activity . Start ( ) ;
5454 _circuitContext = activity . Context ;
55-
56- Console . WriteLine ( $ "StartCircuitActivity: { circuitId } ") ;
57- Console . WriteLine ( $ "circuitContext: { _circuitContext . TraceId } { _circuitContext . SpanId } { _circuitContext . TraceState } ") ;
58- Console . WriteLine ( $ "httpContext: { httpContext . TraceId } { httpContext . SpanId } { httpContext . TraceState } ") ;
5955 }
6056 return activity ;
6157 }
@@ -73,71 +69,82 @@ public void FailCircuitActivity(Activity activity, Exception ex)
7369
7470 public Activity ? StartRouteActivity ( string componentType , string route )
7571 {
76- IEnumerable < KeyValuePair < string , object ? > > tags =
77- [
78- new ( "circuit.id" , _circuitId ?? "unknown" ) ,
79- new ( "component.type" , componentType ?? "unknown" ) ,
80- new ( "route" , route ?? "unknown" ) ,
81- ] ;
82- var links = new List < ActivityLink > ( ) ;
8372 if ( _httpContext == default )
8473 {
8574 _httpContext = CaptureHttpContext ( ) ;
8675 }
87- if ( _httpContext != default )
88- {
89- links . Add ( new ActivityLink ( _httpContext ) ) ;
90- }
91- if ( _circuitContext != default )
92- {
93- links . Add ( new ActivityLink ( _circuitContext ) ) ;
94- }
9576
96- var activity = ActivitySource . CreateActivity ( OnRouteName , ActivityKind . Server , parentId : null , tags , links ) ;
77+ var activity = ActivitySource . CreateActivity ( OnRouteName , ActivityKind . Server , parentId : null , null , null ) ;
9778 if ( activity is not null )
9879 {
99- _routeContext = activity . Context ;
80+ if ( activity . IsAllDataRequested )
81+ {
82+ if ( _circuitId != null )
83+ {
84+ activity . SetTag ( "circuit.id" , _circuitId ) ;
85+ }
86+ if ( componentType != null )
87+ {
88+ activity . SetTag ( "component.type" , componentType ) ;
89+ }
90+ if ( route != null )
91+ {
92+ activity . SetTag ( "route" , route ) ;
93+ }
94+ if ( _httpContext != default )
95+ {
96+ activity . AddLink ( new ActivityLink ( _httpContext ) ) ;
97+ }
98+ if ( _circuitContext != default )
99+ {
100+ activity . AddLink ( new ActivityLink ( _circuitContext ) ) ;
101+ }
102+ }
103+
100104 activity . DisplayName = $ "ROUTE { route ?? "unknown" } -> { componentType ?? "unknown" } ";
101105 activity . Start ( ) ;
106+ _routeContext = activity . Context ;
102107 }
103108 return activity ;
104109 }
105110
106- public void StopRouteActivity ( Activity activity )
107- {
108- _routeContext = default ;
109- if ( ! activity . IsStopped )
110- {
111- activity . Stop ( ) ;
112- }
113- }
114-
115111 public Activity ? StartEventActivity ( string ? componentType , string ? methodName , string ? attributeName )
116112 {
117- IEnumerable < KeyValuePair < string , object ? > > tags =
118- [
119- new ( "circuit.id" , _circuitId ?? "unknown" ) ,
120- new ( "component.type" , componentType ?? "unknown" ) ,
121- new ( "component.method" , methodName ?? "unknown" ) ,
122- new ( "attribute.name" , attributeName ?? "unknown" ) ,
123- ] ;
124- var links = new List < ActivityLink > ( ) ;
125- if ( _httpContext != default )
126- {
127- links . Add ( new ActivityLink ( _httpContext ) ) ;
128- }
129- if ( _circuitContext != default )
130- {
131- links . Add ( new ActivityLink ( _circuitContext ) ) ;
132- }
133- if ( _routeContext != default )
134- {
135- links . Add ( new ActivityLink ( _routeContext ) ) ;
136- }
137-
138- var activity = ActivitySource . CreateActivity ( OnEventName , ActivityKind . Server , parentId : null , tags , links ) ;
113+ var activity = ActivitySource . CreateActivity ( OnEventName , ActivityKind . Server , parentId : null , null , null ) ;
139114 if ( activity is not null )
140115 {
116+ if ( activity . IsAllDataRequested )
117+ {
118+ if ( _circuitId != null )
119+ {
120+ activity . SetTag ( "circuit.id" , _circuitId ) ;
121+ }
122+ if ( componentType != null )
123+ {
124+ activity . SetTag ( "component.type" , componentType ) ;
125+ }
126+ if ( methodName != null )
127+ {
128+ activity . SetTag ( "component.method" , methodName ) ;
129+ }
130+ if ( attributeName != null )
131+ {
132+ activity . SetTag ( "attribute.name" , attributeName ) ;
133+ }
134+ if ( _httpContext != default )
135+ {
136+ activity . AddLink ( new ActivityLink ( _httpContext ) ) ;
137+ }
138+ if ( _circuitContext != default )
139+ {
140+ activity . AddLink ( new ActivityLink ( _circuitContext ) ) ;
141+ }
142+ if ( _routeContext != default )
143+ {
144+ activity . AddLink ( new ActivityLink ( _routeContext ) ) ;
145+ }
146+ }
147+
141148 activity . DisplayName = $ "EVENT { attributeName ?? "unknown" } -> { componentType ?? "unknown" } .{ methodName ?? "unknown" } ";
142149 activity . Start ( ) ;
143150 }
0 commit comments