Skip to content

Commit ce2eef8

Browse files
authored
Merge 6e59a87 into 12e5006
2 parents 12e5006 + 6e59a87 commit ce2eef8

File tree

8 files changed

+105
-1
lines changed

8 files changed

+105
-1
lines changed

Sources/Sentry/PrivateSentrySDKOnly.mm

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#import "SentryInternalDefines.h"
1010
#import "SentryMeta.h"
1111
#import "SentryOptions+Private.h"
12+
#import "SentryPropagationContext.h"
1213
#import "SentrySDK+Private.h"
1314
#import "SentrySerialization.h"
1415
#import "SentrySessionReplayIntegration+Private.h"
@@ -199,6 +200,14 @@ + (NSDictionary *)getExtraContext
199200
return [SentryDependencyContainer.sharedInstance.extraContextProvider getExtraContext];
200201
}
201202

203+
+ (void)setTrace:(SentryId *)traceId spanId:(SentrySpanId *)spanId
204+
{
205+
[SentrySDK.currentHub configureScope:^(SentryScope *scope) {
206+
scope.propagationContext = [[SentryPropagationContext alloc] initWithTraceId:traceId
207+
spanId:spanId];
208+
}];
209+
}
210+
202211
#if SENTRY_TARGET_PROFILING_SUPPORTED
203212
+ (uint64_t)startProfilerForTrace:(SentryId *)traceId;
204213
{

Sources/Sentry/SentryPropagationContext.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ NS_ASSUME_NONNULL_BEGIN
1212
@property (nonatomic, strong, readonly) SentrySpanId *spanId;
1313
@property (nonatomic, readonly) SentryTraceHeader *traceHeader;
1414

15+
- (instancetype)initWithTraceId:(SentryId *)traceId spanId:(SentrySpanId *)spanId;
16+
1517
- (NSDictionary<NSString *, NSString *> *)traceContextForEvent;
1618

1719
@end

Sources/Sentry/SentryPropagationContext.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ - (instancetype)init
1414
return self;
1515
}
1616

17+
- (instancetype)initWithTraceId:(SentryId *)traceId spanId:(SentrySpanId *)spanId
18+
{
19+
if (self = [super init]) {
20+
_traceId = traceId;
21+
_spanId = spanId;
22+
}
23+
return self;
24+
}
25+
1726
- (SentryTraceHeader *)traceHeader
1827
{
1928
return [[SentryTraceHeader alloc] initWithTraceId:self.traceId

Sources/Sentry/SentryScope.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,20 @@ - (void)setSpan:(nullable id<SentrySpan>)span
154154
}
155155
}
156156

157+
- (void)setPropagationContext:(SentryPropagationContext *)propagationContext
158+
{
159+
@synchronized(_propagationContext) {
160+
_propagationContext = propagationContext;
161+
162+
if (self.observers.count > 0) {
163+
NSDictionary *traceContext = [self.propagationContext traceContextForEvent];
164+
for (id<SentryScopeObserver> observer in self.observers) {
165+
[observer setTraceContext:traceContext];
166+
}
167+
}
168+
}
169+
}
170+
157171
- (nullable id<SentrySpan>)span
158172
{
159173
@synchronized(_spanLock) {

Sources/Sentry/include/HybridPublic/PrivateSentrySDKOnly.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
@class SentryUser;
1919
@class SentryEnvelope;
2020
@class SentryId;
21+
@class SentrySpanId;
2122
@class SentrySessionReplayIntegration;
2223
@class UIView;
2324

@@ -103,6 +104,11 @@ typedef void (^SentryOnAppStartMeasurementAvailable)(
103104
*/
104105
+ (NSDictionary *)getExtraContext;
105106

107+
/**
108+
* Allows Hybrids SDKs to thread-safe set the current trace.
109+
*/
110+
+ (void)setTrace:(SentryId *)traceId spanId:(SentrySpanId *)spanId;
111+
106112
#if SENTRY_TARGET_PROFILING_SUPPORTED
107113
/**
108114
* Start a profiler session associated with the given @c SentryId.

Sources/Sentry/include/SentryScope+Private.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ NS_ASSUME_NONNULL_BEGIN
2020
*/
2121
@property (atomic, strong) SentryUser *_Nullable userObject;
2222

23-
@property (atomic, strong) SentryPropagationContext *propagationContext;
23+
/**
24+
* The propagation context has a setter, requiring it to be nonatomic
25+
*/
26+
@property (nonatomic, strong) SentryPropagationContext *propagationContext;
2427

2528
@property (nonatomic, nullable, copy) NSString *currentScreen;
2629

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
@testable import Sentry
2+
import XCTest
3+
4+
class SentryPropagationContextTests: XCTestCase {
5+
6+
func testInitWithTraceIdSpanId() {
7+
let traceId = SentryId()
8+
let spanId = SpanId()
9+
10+
let context = SentryPropagationContext(traceId: traceId, spanId: spanId)
11+
12+
XCTAssertEqual(traceId, context.traceId)
13+
XCTAssertEqual(spanId, context.spanId)
14+
}
15+
16+
func testTraceContextForEvent() {
17+
let traceId = SentryId()
18+
let spanId = SpanId()
19+
20+
let context = SentryPropagationContext(traceId: traceId, spanId: spanId)
21+
22+
let traceContext = context.traceContextForEvent()
23+
24+
XCTAssertEqual(traceContext.count, 2)
25+
XCTAssertEqual(traceContext["trace_id"], traceId.sentryIdString)
26+
XCTAssertEqual(traceContext["span_id"], spanId.sentrySpanIdString)
27+
}
28+
29+
func testTraceHeader() {
30+
let traceId = SentryId()
31+
let spanId = SpanId()
32+
33+
let context = SentryPropagationContext(traceId: traceId, spanId: spanId)
34+
35+
let traceHeader = context.traceHeader
36+
37+
XCTAssertNotNil(traceHeader)
38+
XCTAssertEqual(traceHeader.traceId, traceId)
39+
XCTAssertEqual(traceHeader.spanId, spanId)
40+
}
41+
}

Tests/SentryTests/SentryScopeSwiftTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,26 @@ class SentryScopeSwiftTests: XCTestCase {
843843
XCTAssertEqual(observer2.context as? NSDictionary, expected)
844844
}
845845

846+
func testScopeObserver_setPropagationContext_UpdatesTraceContext() throws {
847+
// -- Arrange --
848+
let sut = Scope()
849+
let observer = fixture.observer
850+
sut.add(observer)
851+
852+
let traceId = SentryId(uuidString: "12345678123456781234567812345678")
853+
let spanId = SpanId(value: "1234567812345678")
854+
let propagationContext = SentryPropagationContext(trace: traceId, spanId: spanId)
855+
856+
// -- Act --
857+
sut.propagationContext = propagationContext
858+
859+
// -- Assert --
860+
let traceContext = try XCTUnwrap(observer.traceContext)
861+
XCTAssertEqual(2, traceContext.count)
862+
XCTAssertEqual(traceId.sentryIdString, traceContext["trace_id"] as? String)
863+
XCTAssertEqual(spanId.sentrySpanIdString, traceContext["span_id"] as? String)
864+
}
865+
846866
private class TestScopeObserver: NSObject, SentryScopeObserver {
847867
var tags: [String: String]?
848868
func setTags(_ tags: [String: String]?) {

0 commit comments

Comments
 (0)