Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix active context is removed wrongly #448

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ class ActivityContextManager: ContextManager {
func removeContextValue(forKey key: OpenTelemetryContextKeys, value: AnyObject) {
let activityIdent = os_activity_get_identifier(OS_ACTIVITY_CURRENT, nil)
rlock.lock()
contextMap[activityIdent]?[key.rawValue] = nil
if contextMap[activityIdent]?.isEmpty ?? false {
if let currentValue = contextMap[activityIdent]?[key.rawValue],
currentValue === value
{
contextMap[activityIdent]?[key.rawValue] = nil
if contextMap[activityIdent]?.isEmpty ?? false {
contextMap[activityIdent] = nil
}
}
if let scope = objectScope.object(forKey: value) {
var scope = scope.scope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ class ActivityContextManagerTests: XCTestCase {
XCTAssert(activeSpan === parentSpan)
}

func testContextPropagationTwoSequentialChildSpans() {
let parentSpan = defaultTracer.spanBuilder(spanName: "Parent").startSpan()
OpenTelemetry.instance.contextProvider.setActiveSpan(parentSpan)

let child1 = defaultTracer.spanBuilder(spanName: "child1").startSpan()
child1.end()

let child2 = defaultTracer.spanBuilder(spanName: "child2").startSpan()
child2.end()

parentSpan.end()

XCTAssertEqual(parentSpan.context.traceId, child1.context.traceId)
XCTAssertEqual(parentSpan.context.traceId, child2.context.traceId)
}

#if swift(>=5.5.2)
@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)
func testStartAndEndSpanInAsyncTask() {
Expand Down
Loading