Skip to content

Commit 7f26f16

Browse files
fix: Set default trace context status to ok (#6611)
Set the default context status to ok instead of not sending it at all to avoid user confusion. Fixes GH-6230
1 parent 781f560 commit 7f26f16

File tree

5 files changed

+35
-11
lines changed

5 files changed

+35
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- Remove `uuid` and `name` of `SentryDebugMeta` (#6512) Use `debugID` instead of `uuid` and `codeFile` instead of `name`.
2525
- Enable enablePreWarmedAppStartTracing by default (#6508). With this option enabled, the SDK collects [prewarmed app starts](https://docs.sentry.io/platforms/apple/tracing/instrumentation/automatic-instrumentation/#prewarmed-app-start-tracing).
2626
- Change `value` and `type` of `SentryException` to be nullable (#6563)
27+
- Change the default trace context status to "ok" instead of "undefined" (#6611)
2728

2829
### Features
2930

Sources/Sentry/SentryScope.m

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#import "SentryPropagationContext.h"
1010
#import "SentryScope+Private.h"
1111
#import "SentryScope+PrivateSwift.h"
12-
#import "SentrySpan.h"
12+
#import "SentrySpan+Private.h"
1313
#import "SentrySwift.h"
1414
#import "SentryTracer.h"
1515
#import "SentryTransactionContext.h"
@@ -630,7 +630,21 @@ - (void)addObserver:(id<SentryScopeObserver>)observer
630630
- (NSDictionary *)buildTraceContext:(nullable id<SentrySpan>)span
631631
{
632632
if (span != nil) {
633-
return [SENTRY_UNWRAP_NULLABLE_VALUE(id<SentrySpan>, span) serialize];
633+
NSDictionary *dict = [SENTRY_UNWRAP_NULLABLE_VALUE(id<SentrySpan>, span) serialize];
634+
if (dict[kSentrySpanStatusSerializationKey] != nil) {
635+
return dict;
636+
}
637+
638+
// We set the trace context status to OK by default here if it's not set, because spans on
639+
// the scope are usually unfinished and don't have a status set. So the default trace
640+
// context status would be undefined otherwise, which would confuse users. We don't want to
641+
// set the default status for spans to OK, because when a span finishes, it sets the status
642+
// to any state other than undefined. Spans first have a default status of OK, but we don't
643+
// want to change this for the trace context status.
644+
NSMutableDictionary *mutableDict = [dict mutableCopy];
645+
mutableDict[kSentrySpanStatusSerializationKey] = kSentrySpanStatusNameOk;
646+
return mutableDict;
647+
634648
} else {
635649
return [self.propagationContext traceContextForEvent];
636650
}

Sources/Sentry/SentrySpan.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,8 @@ - (NSDictionary *)serialize
351351
}
352352

353353
if (self.status != kSentrySpanStatusUndefined) {
354-
[mutableDictionary setValue:nameForSentrySpanStatus(self.status) forKey:@"status"];
354+
[mutableDictionary setValue:nameForSentrySpanStatus(self.status)
355+
forKey:kSentrySpanStatusSerializationKey];
355356
}
356357

357358
[mutableDictionary setValue:@(self.timestamp.timeIntervalSince1970) forKey:@"timestamp"];

Sources/Sentry/include/SentrySpan+Private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#import "SentryProfilingConditionals.h"
44

5+
static NSString *_Nonnull const kSentrySpanStatusSerializationKey = @"status";
6+
57
@interface SentrySpan ()
68

79
#if SENTRY_TARGET_PROFILING_SUPPORTED

Tests/SentryTests/SentryScopeSwiftTests.swift

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -218,17 +218,18 @@ class SentryScopeSwiftTests: XCTestCase {
218218
XCTAssertEqual(event.dist, actual?.dist)
219219
}
220220

221-
func testApplyToEvent_ScopeWithSpan() {
221+
func testApplyToEvent_ScopeWithSpan() throws {
222222
let scope = fixture.scope
223223
scope.span = fixture.transaction
224224

225225
let actual = scope.applyTo(event: fixture.event, maxBreadcrumbs: 10)
226-
let trace = fixture.event.context?["trace"]
227-
226+
let trace = try XCTUnwrap(fixture.event.context?["trace"])
227+
228228
XCTAssertEqual(actual?.transaction, fixture.transactionName)
229-
XCTAssertEqual(trace?["op"] as? String, fixture.transactionOperation)
230-
XCTAssertEqual(trace?["trace_id"] as? String, fixture.transaction.traceId.sentryIdString)
231-
XCTAssertEqual(trace?["span_id"] as? String, fixture.transaction.spanId.sentrySpanIdString)
229+
XCTAssertEqual(trace["op"] as? String, fixture.transactionOperation)
230+
XCTAssertEqual(trace["trace_id"] as? String, fixture.transaction.traceId.sentryIdString)
231+
XCTAssertEqual(trace["span_id"] as? String, fixture.transaction.spanId.sentrySpanIdString)
232+
XCTAssertEqual(trace["status"] as? String, "ok")
232233
}
233234

234235
func testApplyToEvent_EventWithDist() {
@@ -603,15 +604,20 @@ class SentryScopeSwiftTests: XCTestCase {
603604
let traceContext = try XCTUnwrap(observer.traceContext)
604605
let serializedTransaction = transaction.serialize()
605606

606-
XCTAssertEqual(Set(serializedTransaction.keys), Set(traceContext.keys))
607-
607+
var expectedKeys = Set(serializedTransaction.keys)
608+
// The transaction doesn't serialize the status when it's undefined, but the trace context sets it to OK.
609+
expectedKeys.insert("status")
610+
611+
XCTAssertEqual(Set(traceContext.keys), expectedKeys)
612+
608613
XCTAssertEqual(serializedTransaction["trace_id"] as? String, traceContext["trace_id"] as? String)
609614
XCTAssertEqual(serializedTransaction["span_id"] as? String, traceContext["span_id"] as? String)
610615
XCTAssertEqual(serializedTransaction["op"] as? String, traceContext["op"] as? String)
611616
XCTAssertEqual(serializedTransaction["origin"] as? String, traceContext["origin"] as? String)
612617
XCTAssertEqual(serializedTransaction["type"] as? String, traceContext["type"] as? String)
613618
XCTAssertEqual(serializedTransaction["start_timestamp"] as? Double, traceContext["start_timestamp"] as? Double)
614619
XCTAssertEqual(serializedTransaction["timestamp"] as? Double, traceContext["timestamp"] as? Double)
620+
XCTAssertEqual(traceContext["status"] as? String, "ok")
615621
}
616622

617623
func testScopeObserver_setSpanToNil_SetsTraceContextToPropagationContext() throws {

0 commit comments

Comments
 (0)