@@ -16,14 +16,9 @@ private let OS_ACTIVITY_CURRENT = unsafeBitCast(dlsym(UnsafeMutableRawPointer(bi
1616
1717class ActivityContextManager: ContextManager {
1818 static let instance = ActivityContextManager ( )
19- #if canImport(_Concurrency)
20- #if swift(<5.5.2)
21- @available ( macOS 12 . 0 , iOS 15 . 0 , tvOS 15 . 0 , * )
22- static let taskLocalContextManager = TaskLocalContextManager . instance
23- #else
19+ #if swift(>=5.5.2)
2420 @available ( macOS 10 . 15 , iOS 13 . 0 , tvOS 13 . 0 , * )
2521 static let taskLocalContextManager = TaskLocalContextManager . instance
26- #endif
2722#endif
2823
2924 let rlock = NSRecursiveLock ( )
@@ -43,80 +38,52 @@ class ActivityContextManager: ContextManager {
4338 var contextMap = [ os_activity_id_t: [ String: AnyObject] ] ( )
4439
4540 func getCurrentContextValue( forKey key: OpenTelemetryContextKeys ) -> AnyObject ? {
41+ #if swift(>=5.5.2)
42+ if #available( macOS 10 . 15 , iOS 13 . 0 , tvOS 13 . 0 , * ) {
43+ if let contextValue = ActivityContextManager . taskLocalContextManager. getCurrentContextValue ( forKey: key) {
44+ return contextValue
45+ }
46+ }
47+ #endif
4648 var parentIdent : os_activity_id_t = 0
4749 let activityIdent = os_activity_get_identifier ( OS_ACTIVITY_CURRENT, & parentIdent)
4850 var contextValue : AnyObject ?
49- if activityIdent != 0 {
50- rlock. lock ( )
51- guard let context = contextMap [ activityIdent] ?? contextMap [ parentIdent] else {
52- rlock. unlock ( )
53- return nil
54- }
55- contextValue = context [ key. rawValue]
51+ rlock. lock ( )
52+ guard let context = contextMap [ activityIdent] ?? contextMap [ parentIdent] else {
5653 rlock. unlock ( )
57- return contextValue
58- } else {
59- // If activityIdent == 0, it means no active Span or we are inside an Task
60- #if canImport(_Concurrency)
61- #if swift(<5.5.2)
62- if #available( macOS 12 . 0 , iOS 15 . 0 , tvOS 15 . 0 , * ) {
63- if let contextValue = ActivityContextManager . taskLocalContextManager. getCurrentContextValue ( forKey: key) {
64- return contextValue
65- }
66- }
67- #else
68- if #available( macOS 10 . 15 , iOS 13 . 0 , tvOS 13 . 0 , * ) {
69- if let contextValue = ActivityContextManager . taskLocalContextManager. getCurrentContextValue ( forKey: key) {
70- return contextValue
71- }
72- }
73- #endif
74- #endif
54+ return nil
7555 }
76- return nil
56+ contextValue = context [ key. rawValue]
57+ rlock. unlock ( )
58+ return contextValue
7759 }
7860
7961 func setCurrentContextValue( forKey key: OpenTelemetryContextKeys , value: AnyObject ) {
62+ #if swift(>=5.5.2)
63+ if #available( macOS 10 . 15 , iOS 13 . 0 , tvOS 13 . 0 , * ) {
64+ var insideTask = false
65+ withUnsafeCurrentTask { task in
66+ if task != nil {
67+ insideTask = true
68+ }
69+ }
70+ if insideTask {
71+ ActivityContextManager . taskLocalContextManager. setCurrentContextValue ( forKey: key, value: value)
72+ return
73+ }
74+ }
75+ #endif
8076 var parentIdent : os_activity_id_t = 0
8177 var activityIdent = os_activity_get_identifier ( OS_ACTIVITY_CURRENT, & parentIdent)
82- if activityIdent != 0 {
83- // We are inside an activity, it can be an activity created by us for a span context or another independent activty
84- // We are surely not inside a Task
85- rlock. lock ( )
86- if contextMap [ activityIdent] == nil || contextMap [ activityIdent] ? [ key. rawValue] != nil {
87- var scope : os_activity_scope_state_s
88- ( activityIdent, scope) = createActivityContext ( )
89- contextMap [ activityIdent] = [ String: AnyObject] ( )
90- objectScope. setObject ( ScopeElement ( scope: scope) , forKey: value)
91- }
92- contextMap [ activityIdent] ? [ key. rawValue] = value
93- rlock. unlock ( )
94- } else {
78+ rlock. lock ( )
79+ if contextMap [ activityIdent] == nil || contextMap [ activityIdent] ? [ key. rawValue] != nil {
9580 var scope : os_activity_scope_state_s
9681 ( activityIdent, scope) = createActivityContext ( )
97- if activityIdent == 0 {
98- // If activityIdent == 0, means we are inside a Task, because we cannot create an activity, set the context inside the task
99- #if canImport(_Concurrency)
100- #if swift(<5.5.2)
101- if #available( macOS 12 . 0 , iOS 15 . 0 , tvOS 15 . 0 , * ) {
102- ActivityContextManager . taskLocalContextManager. setCurrentContextValue ( forKey: key, value: value)
103- }
104- #else
105- if #available( macOS 10 . 15 , iOS 13 . 0 , tvOS 13 . 0 , * ) {
106- ActivityContextManager . taskLocalContextManager. setCurrentContextValue ( forKey: key, value: value)
107- }
108-
109- #endif
110- #endif
111- } else {
112- // We could create the activity so we store the context in the activity map
113- rlock. lock ( )
114- contextMap [ activityIdent] = [ String: AnyObject] ( )
115- objectScope. setObject ( ScopeElement ( scope: scope) , forKey: value)
116- contextMap [ activityIdent] ? [ key. rawValue] = value
117- rlock. unlock ( )
118- }
82+ contextMap [ activityIdent] = [ String: AnyObject] ( )
83+ objectScope. setObject ( ScopeElement ( scope: scope) , forKey: value)
11984 }
85+ contextMap [ activityIdent] ? [ key. rawValue] = value
86+ rlock. unlock ( )
12087 }
12188
12289 func createActivityContext( ) -> ( os_activity_id_t , os_activity_scope_state_s ) {
@@ -129,30 +96,24 @@ class ActivityContextManager: ContextManager {
12996 }
13097
13198 func removeContextValue( forKey key: OpenTelemetryContextKeys , value: AnyObject ) {
132- if let scope = objectScope. object ( forKey: value) {
133- var scope = scope. scope
134- os_activity_scope_leave ( & scope)
135- objectScope. removeObject ( forKey: value)
136- } else {
137- #if canImport(_Concurrency)
138- #if swift(<5.5.2)
139- if #available( macOS 12 . 0 , iOS 15 . 0 , tvOS 15 . 0 , * ) {
140- // If there is a parent activity, set its content as the task local
141- ActivityContextManager . taskLocalContextManager. removeContextValue ( forKey: key, value: value)
142- if let currentContext = ActivityContextManager . taskLocalContextManager. getCurrentContextValue ( forKey: key) {
143- ActivityContextManager . taskLocalContextManager. setCurrentContextValue ( forKey: key, value: currentContext)
99+ #if swift(>=5.5.2)
100+ if #available( macOS 10 . 15 , iOS 13 . 0 , tvOS 13 . 0 , * ) {
101+ var insideTask = false
102+ withUnsafeCurrentTask { task in
103+ if task != nil {
104+ insideTask = true
144105 }
145106 }
146- #else
147- // If there is a parent activity, set its content as the task local
148- if #available( macOS 10 . 15 , iOS 13 . 0 , tvOS 13 . 0 , * ) {
107+ if insideTask {
149108 ActivityContextManager . taskLocalContextManager. removeContextValue ( forKey: key, value: value)
150- if let currentContext = ActivityContextManager . taskLocalContextManager. getCurrentContextValue ( forKey: key) {
151- ActivityContextManager . taskLocalContextManager. setCurrentContextValue ( forKey: key, value: currentContext)
152- }
109+ return
153110 }
111+ }
154112#endif
155- #endif
113+ if let scope = objectScope. object ( forKey: value) {
114+ var scope = scope. scope
115+ os_activity_scope_leave ( & scope)
116+ objectScope. removeObject ( forKey: value)
156117 }
157118 }
158119}
0 commit comments