@@ -17,15 +17,17 @@ private let OS_ACTIVITY_CURRENT = unsafeBitCast(dlsym(UnsafeMutableRawPointer(bi
1717class ActivityContextManager: ContextManager {
1818 static let instance = ActivityContextManager ( )
1919
20+ @available ( macOS 12 . 0 , iOS 15 . 0 , tvOS 15 . 0 , * )
21+ static let taskLocalContextManager = TaskLocalContextManager . instance
22+
2023 let rlock = NSRecursiveLock ( )
2124
2225 class ScopeElement {
2326 init ( scope: os_activity_scope_state_s ) {
2427 self . scope = scope
2528 }
2629
27- deinit {
28- }
30+ deinit { }
2931
3032 var scope : os_activity_scope_state_s
3133 }
@@ -34,7 +36,13 @@ class ActivityContextManager: ContextManager {
3436
3537 var contextMap = [ os_activity_id_t: [ String: AnyObject] ] ( )
3638
37- func getCurrentContextValue( forKey key: String ) -> AnyObject ? {
39+ func getCurrentContextValue( forKey key: OpenTelemetryContextKeys ) -> AnyObject ? {
40+ if #available( macOS 12 . 0 , iOS 15 . 0 , tvOS 15 . 0 , * ) {
41+ // If running with task local, use first its stored value
42+ if let contextValue = ActivityContextManager . taskLocalContextManager. getCurrentContextValue ( forKey: key) {
43+ return contextValue
44+ }
45+ }
3846 var parentIdent : os_activity_id_t = 0
3947 let activityIdent = os_activity_get_identifier ( OS_ACTIVITY_CURRENT, & parentIdent)
4048 var contextValue : AnyObject ?
@@ -43,22 +51,26 @@ class ActivityContextManager: ContextManager {
4351 rlock. unlock ( )
4452 return nil
4553 }
46- contextValue = context [ key]
54+ contextValue = context [ key. rawValue ]
4755 rlock. unlock ( )
4856 return contextValue
4957 }
5058
51- func setCurrentContextValue( forKey key: String , value: AnyObject ) {
59+ func setCurrentContextValue( forKey key: OpenTelemetryContextKeys , value: AnyObject ) {
5260 var parentIdent : os_activity_id_t = 0
5361 var activityIdent = os_activity_get_identifier ( OS_ACTIVITY_CURRENT, & parentIdent)
5462 rlock. lock ( )
55- if contextMap [ activityIdent] == nil || contextMap [ activityIdent] ? [ key] != nil {
63+ if contextMap [ activityIdent] == nil || contextMap [ activityIdent] ? [ key. rawValue ] != nil {
5664 var scope : os_activity_scope_state_s
5765 ( activityIdent, scope) = createActivityContext ( )
5866 contextMap [ activityIdent] = [ String: AnyObject] ( )
5967 objectScope. setObject ( ScopeElement ( scope: scope) , forKey: value)
6068 }
61- contextMap [ activityIdent] ? [ key] = value
69+ contextMap [ activityIdent] ? [ key. rawValue] = value
70+ if #available( macOS 12 . 0 , iOS 15 . 0 , tvOS 15 . 0 , * ) {
71+ // If running with task local, set the value after the activity, so activity is not empty
72+ ActivityContextManager . taskLocalContextManager. setCurrentContextValue ( forKey: key, value: value)
73+ }
6274 rlock. unlock ( )
6375 }
6476
@@ -71,11 +83,18 @@ class ActivityContextManager: ContextManager {
7183 return ( currentActivityId, activityState)
7284 }
7385
74- func removeContextValue( forKey key: String , value: AnyObject ) {
86+ func removeContextValue( forKey key: OpenTelemetryContextKeys , value: AnyObject ) {
7587 if let scope = objectScope. object ( forKey: value) {
7688 var scope = scope. scope
7789 os_activity_scope_leave ( & scope)
7890 objectScope. removeObject ( forKey: value)
7991 }
92+ if #available( macOS 12 . 0 , iOS 15 . 0 , tvOS 15 . 0 , * ) {
93+ // If there is a parent activity, set its content as the task local
94+ ActivityContextManager . taskLocalContextManager. removeContextValue ( forKey: key, value: value)
95+ if let currentContext = self . getCurrentContextValue ( forKey: key) {
96+ ActivityContextManager . taskLocalContextManager. setCurrentContextValue ( forKey: key, value: currentContext)
97+ }
98+ }
8099 }
81100}
0 commit comments