@@ -16,6 +16,10 @@ private let OS_ACTIVITY_CURRENT = unsafeBitCast(dlsym(UnsafeMutableRawPointer(bi
1616
1717class ActivityContextManager: ContextManager {
1818 static let instance = ActivityContextManager ( )
19+ #if swift(>=5.5)
20+ @available ( macOS 12 . 0 , iOS 15 . 0 , tvOS 15 . 0 , * )
21+ static let taskLocalContextManager = TaskLocalContextManager . instance
22+ #endif
1923
2024 let rlock = NSRecursiveLock ( )
2125
@@ -24,8 +28,7 @@ class ActivityContextManager: ContextManager {
2428 self . scope = scope
2529 }
2630
27- deinit {
28- }
31+ deinit { }
2932
3033 var scope : os_activity_scope_state_s
3134 }
@@ -34,7 +37,15 @@ class ActivityContextManager: ContextManager {
3437
3538 var contextMap = [ os_activity_id_t: [ String: AnyObject] ] ( )
3639
37- func getCurrentContextValue( forKey key: String ) -> AnyObject ? {
40+ func getCurrentContextValue( forKey key: OpenTelemetryContextKeys ) -> AnyObject ? {
41+ #if swift(>=5.5)
42+ if #available( macOS 12 . 0 , iOS 15 . 0 , tvOS 15 . 0 , * ) {
43+ // If running with task local, use first its stored value
44+ if let contextValue = ActivityContextManager . taskLocalContextManager. getCurrentContextValue ( forKey: key) {
45+ return contextValue
46+ }
47+ }
48+ #endif
3849 var parentIdent : os_activity_id_t = 0
3950 let activityIdent = os_activity_get_identifier ( OS_ACTIVITY_CURRENT, & parentIdent)
4051 var contextValue : AnyObject ?
@@ -43,22 +54,28 @@ class ActivityContextManager: ContextManager {
4354 rlock. unlock ( )
4455 return nil
4556 }
46- contextValue = context [ key]
57+ contextValue = context [ key. rawValue ]
4758 rlock. unlock ( )
4859 return contextValue
4960 }
5061
51- func setCurrentContextValue( forKey key: String , value: AnyObject ) {
62+ func setCurrentContextValue( forKey key: OpenTelemetryContextKeys , value: AnyObject ) {
5263 var parentIdent : os_activity_id_t = 0
5364 var activityIdent = os_activity_get_identifier ( OS_ACTIVITY_CURRENT, & parentIdent)
5465 rlock. lock ( )
55- if contextMap [ activityIdent] == nil || contextMap [ activityIdent] ? [ key] != nil {
66+ if contextMap [ activityIdent] == nil || contextMap [ activityIdent] ? [ key. rawValue ] != nil {
5667 var scope : os_activity_scope_state_s
5768 ( activityIdent, scope) = createActivityContext ( )
5869 contextMap [ activityIdent] = [ String: AnyObject] ( )
5970 objectScope. setObject ( ScopeElement ( scope: scope) , forKey: value)
6071 }
61- contextMap [ activityIdent] ? [ key] = value
72+ contextMap [ activityIdent] ? [ key. rawValue] = value
73+ #if swift(>=5.5)
74+ if #available( macOS 12 . 0 , iOS 15 . 0 , tvOS 15 . 0 , * ) {
75+ // If running with task local, set the value after the activity, so activity is not empty
76+ ActivityContextManager . taskLocalContextManager. setCurrentContextValue ( forKey: key, value: value)
77+ }
78+ #endif
6279 rlock. unlock ( )
6380 }
6481
@@ -71,11 +88,20 @@ class ActivityContextManager: ContextManager {
7188 return ( currentActivityId, activityState)
7289 }
7390
74- func removeContextValue( forKey key: String , value: AnyObject ) {
91+ func removeContextValue( forKey key: OpenTelemetryContextKeys , value: AnyObject ) {
7592 if let scope = objectScope. object ( forKey: value) {
7693 var scope = scope. scope
7794 os_activity_scope_leave ( & scope)
7895 objectScope. removeObject ( forKey: value)
7996 }
97+ #if swift(>=5.5)
98+ if #available( macOS 12 . 0 , iOS 15 . 0 , tvOS 15 . 0 , * ) {
99+ // If there is a parent activity, set its content as the task local
100+ ActivityContextManager . taskLocalContextManager. removeContextValue ( forKey: key, value: value)
101+ if let currentContext = self . getCurrentContextValue ( forKey: key) {
102+ ActivityContextManager . taskLocalContextManager. setCurrentContextValue ( forKey: key, value: currentContext)
103+ }
104+ }
105+ #endif
80106 }
81107}
0 commit comments