Skip to content

Commit 0b39168

Browse files
author
Ignacio Bonafonte
authored
Merge pull request #285 from nachoBonafonte/make-some-concurrency-tests-clearer
Xcode 13.2 adds support to Activity framework inside Tasks, so we can use ActivityContext context management to recover active Span and Baggage, we can remove TaskLocalContextManager and all related code. We will only support task related context when using Xcode 13.2 and up Add lots of test to ActivityContextManager so we can fully verify functionality
2 parents b31cb80 + 5f51256 commit 0b39168

File tree

5 files changed

+225
-429
lines changed

5 files changed

+225
-429
lines changed

Sources/OpenTelemetryApi/Context/ActivityContextManager.swift

Lines changed: 12 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,6 @@ private let OS_ACTIVITY_CURRENT = unsafeBitCast(dlsym(UnsafeMutableRawPointer(bi
1616

1717
class 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
24-
@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)
25-
static let taskLocalContextManager = TaskLocalContextManager.instance
26-
#endif
27-
#endif
2819

2920
let rlock = NSRecursiveLock()
3021

@@ -33,8 +24,6 @@ class ActivityContextManager: ContextManager {
3324
self.scope = scope
3425
}
3526

36-
deinit {}
37-
3827
var scope: os_activity_scope_state_s
3928
}
4029

@@ -46,77 +35,28 @@ class ActivityContextManager: ContextManager {
4635
var parentIdent: os_activity_id_t = 0
4736
let activityIdent = os_activity_get_identifier(OS_ACTIVITY_CURRENT, &parentIdent)
4837
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]
38+
rlock.lock()
39+
guard let context = contextMap[activityIdent] ?? contextMap[parentIdent] else {
5640
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
41+
return nil
7542
}
76-
return nil
43+
contextValue = context[key.rawValue]
44+
rlock.unlock()
45+
return contextValue
7746
}
7847

7948
func setCurrentContextValue(forKey key: OpenTelemetryContextKeys, value: AnyObject) {
8049
var parentIdent: os_activity_id_t = 0
8150
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 {
51+
rlock.lock()
52+
if contextMap[activityIdent] == nil || contextMap[activityIdent]?[key.rawValue] != nil {
9553
var scope: os_activity_scope_state_s
9654
(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-
}
55+
contextMap[activityIdent] = [String: AnyObject]()
56+
objectScope.setObject(ScopeElement(scope: scope), forKey: value)
11957
}
58+
contextMap[activityIdent]?[key.rawValue] = value
59+
rlock.unlock()
12060
}
12161

12262
func createActivityContext() -> (os_activity_id_t, os_activity_scope_state_s) {
@@ -133,26 +73,6 @@ class ActivityContextManager: ContextManager {
13373
var scope = scope.scope
13474
os_activity_scope_leave(&scope)
13575
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 = self.getCurrentContextValue(forKey: key) {
143-
ActivityContextManager.taskLocalContextManager.setCurrentContextValue(forKey: key, value: currentContext)
144-
}
145-
}
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, *) {
149-
ActivityContextManager.taskLocalContextManager.removeContextValue(forKey: key, value: value)
150-
if let currentContext = self.getCurrentContextValue(forKey: key) {
151-
ActivityContextManager.taskLocalContextManager.setCurrentContextValue(forKey: key, value: currentContext)
152-
}
153-
}
154-
#endif
155-
#endif
15676
}
15777
}
15878
}

Sources/OpenTelemetryApi/Context/TaskLocalContextManager.swift

Lines changed: 0 additions & 159 deletions
This file was deleted.

Sources/OpenTelemetryApi/Trace/PropagatedSpanBuilder.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,19 @@ class PropagatedSpanBuilder: SpanBuilder {
1010
private var tracer: Tracer
1111
private var isRootSpan: Bool = false
1212
private var spanContext: SpanContext?
13-
private var spanName: String
1413

15-
16-
init(tracer: Tracer, spanName: String = "") {
14+
init(tracer: Tracer, spanName: String) {
1715
self.tracer = tracer
18-
self.spanName = spanName
1916
}
2017

2118
@discardableResult public func startSpan() -> Span {
2219
if spanContext == nil, !isRootSpan {
2320
spanContext = OpenTelemetry.instance.contextProvider.activeSpan?.context
2421
}
25-
let span = PropagatedSpan(context: spanContext ?? SpanContext.create(traceId: TraceId.random(),
22+
return PropagatedSpan(context: spanContext ?? SpanContext.create(traceId: TraceId.random(),
2623
spanId: SpanId.random(),
2724
traceFlags: TraceFlags(),
2825
traceState: TraceState()))
29-
span.name = spanName
30-
return span
3126
}
3227

3328
@discardableResult public func setParent(_ parent: Span) -> Self {

0 commit comments

Comments
 (0)