1- using System . Diagnostics ;
1+ // Copyright (c) .NET Foundation. All rights reserved.
2+ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+ using System . Diagnostics ;
25using System . Web ;
36
47namespace Microsoft . AspNet . TelemetryCorrelation
@@ -8,20 +11,40 @@ namespace Microsoft.AspNet.TelemetryCorrelation
811 /// </summary>
912 internal static class ActivityHelper
1013 {
14+ /// <summary>
15+ /// Listener name.
16+ /// </summary>
1117 public const string AspNetListenerName = "Microsoft.AspNet.TelemetryCorrelation" ;
18+
19+ /// <summary>
20+ /// Activity name for http request.
21+ /// </summary>
1222 public const string AspNetActivityName = "Microsoft.AspNet.HttpReqIn" ;
23+
24+ /// <summary>
25+ /// Event name for the activity start event.
26+ /// </summary>
1327 public const string AspNetActivityStartName = "Microsoft.AspNet.HttpReqIn.Start" ;
28+
29+ /// <summary>
30+ /// Event name for the activity stop event.
31+ /// </summary>
1432 public const string AspNetActivityLostStopName = "Microsoft.AspNet.HttpReqIn.ActivityLost.Stop" ;
1533
34+ /// <summary>
35+ /// Key to store the activity in HttpContext.
36+ /// </summary>
1637 public const string ActivityKey = "__AspnetActivity__" ;
17- private static readonly DiagnosticListener s_aspNetListener = new DiagnosticListener ( AspNetListenerName ) ;
38+
39+ private static readonly DiagnosticListener AspNetListener = new DiagnosticListener ( AspNetListenerName ) ;
1840
1941 /// <summary>
2042 /// It's possible that a request is executed in both native threads and managed threads,
2143 /// in such case Activity.Current will be lost during native thread and managed thread switch.
2244 /// This method is intended to restore the current activity in order to correlate the child
2345 /// activities with the root activity of the request.
2446 /// </summary>
47+ /// <param name="root">Root activity id for the current request.</param>
2548 /// <returns>If it returns an activity, it will be silently stopped with the parent activity</returns>
2649 public static Activity RestoreCurrentActivity ( Activity root )
2750 {
@@ -32,13 +55,14 @@ public static Activity RestoreCurrentActivity(Activity root)
3255 var childActivity = new Activity ( root . OperationName ) ;
3356 childActivity . SetParentId ( root . Id ) ;
3457 childActivity . SetStartTime ( root . StartTimeUtc ) ;
35- foreach ( var item in root . Baggage )
58+ foreach ( var item in root . Baggage )
3659 {
3760 childActivity . AddBaggage ( item . Key , item . Value ) ;
3861 }
62+
3963 childActivity . Start ( ) ;
4064
41- AspNetTelemetryCorrelaitonEventSource . Log . ActivityStarted ( childActivity . Id ) ;
65+ AspNetTelemetryCorrelationEventSource . Log . ActivityStarted ( childActivity . Id ) ;
4266 return childActivity ;
4367 }
4468
@@ -55,9 +79,9 @@ public static bool StopAspNetActivity(Activity activity, HttpContext context)
5579 // if activity is in the stack, stop it with Stop event
5680 if ( Activity . Current != null )
5781 {
58- s_aspNetListener . StopActivity ( Activity . Current , new { } ) ;
82+ AspNetListener . StopActivity ( Activity . Current , new { } ) ;
5983 RemoveCurrentActivity ( context ) ;
60- AspNetTelemetryCorrelaitonEventSource . Log . ActivityStopped ( activity . Id ) ;
84+ AspNetTelemetryCorrelationEventSource . Log . ActivityStopped ( activity . Id ) ;
6185 return true ;
6286 }
6387 }
@@ -69,51 +93,54 @@ public static void StopLostActivity(Activity activity, HttpContext context)
6993 {
7094 if ( activity != null )
7195 {
72- s_aspNetListener . Write ( AspNetActivityLostStopName , new { activity } ) ;
96+ AspNetListener . Write ( AspNetActivityLostStopName , new { activity } ) ;
7397 RemoveCurrentActivity ( context ) ;
74- AspNetTelemetryCorrelaitonEventSource . Log . ActivityStopped ( activity . Id , true ) ;
98+ AspNetTelemetryCorrelationEventSource . Log . ActivityStopped ( activity . Id , true ) ;
7599 }
76100 }
77101
78102 public static Activity CreateRootActivity ( HttpContext context )
79103 {
80- if ( s_aspNetListener . IsEnabled ( ) && s_aspNetListener . IsEnabled ( AspNetActivityName ) )
104+ if ( AspNetListener . IsEnabled ( ) && AspNetListener . IsEnabled ( AspNetActivityName ) )
81105 {
82106 var rootActivity = new Activity ( ActivityHelper . AspNetActivityName ) ;
83107
84108 rootActivity . Extract ( context . Request . Unvalidated . Headers ) ;
85109 if ( StartAspNetActivity ( rootActivity ) )
86110 {
87111 SaveCurrentActivity ( context , rootActivity ) ;
88- AspNetTelemetryCorrelaitonEventSource . Log . ActivityStarted ( rootActivity . Id ) ;
112+ AspNetTelemetryCorrelationEventSource . Log . ActivityStarted ( rootActivity . Id ) ;
89113 return rootActivity ;
90114 }
91115 }
116+
92117 return null ;
93118 }
94119
95120 private static bool StartAspNetActivity ( Activity activity )
96121 {
97- if ( s_aspNetListener . IsEnabled ( AspNetActivityName , activity , new { } ) )
122+ if ( AspNetListener . IsEnabled ( AspNetActivityName , activity , new { } ) )
98123 {
99- if ( s_aspNetListener . IsEnabled ( AspNetActivityStartName ) )
124+ if ( AspNetListener . IsEnabled ( AspNetActivityStartName ) )
100125 {
101- s_aspNetListener . StartActivity ( activity , new { } ) ;
126+ AspNetListener . StartActivity ( activity , new { } ) ;
102127 }
103128 else
104129 {
105130 activity . Start ( ) ;
106131 }
132+
107133 return true ;
108134 }
109135
110136 return false ;
111137 }
112138
113139 /// <summary>
114- /// This should be called after the Activity starts
115- /// and only for root activity of a request
140+ /// This should be called after the Activity starts and only for root activity of a request.
116141 /// </summary>
142+ /// <param name="context">Context to save context to.</param>
143+ /// <param name="activity">Activity to save.</param>
117144 private static void SaveCurrentActivity ( HttpContext context , Activity activity )
118145 {
119146 Debug . Assert ( context != null ) ;
0 commit comments