-
-
Notifications
You must be signed in to change notification settings - Fork 340
/
Copy pathSentryPerformanceTracker.m
247 lines (211 loc) · 7.43 KB
/
SentryPerformanceTracker.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#import "SentryPerformanceTracker.h"
#import "SentryHub+Private.h"
#import "SentryLog.h"
#import "SentrySDK+Private.h"
#import "SentryScope.h"
#import "SentrySpan.h"
#import "SentrySpanId.h"
#import "SentrySpanProtocol.h"
#import "SentryTracer.h"
#import "SentryTransactionContext+Private.h"
#if SENTRY_HAS_UIKIT
# import "SentryUIEventTracker.h"
#endif // SENTRY_HAS_UIKIT
NS_ASSUME_NONNULL_BEGIN
@interface
SentryPerformanceTracker () <SentryTracerDelegate>
@property (nonatomic, strong) NSMutableDictionary<SentrySpanId *, id<SentrySpan>> *spans;
@property (nonatomic, strong) NSMutableArray<id<SentrySpan>> *activeSpanStack;
@end
@implementation SentryPerformanceTracker
+ (SentryPerformanceTracker *)shared
{
static SentryPerformanceTracker *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ instance = [[self alloc] init]; });
return instance;
}
- (instancetype)init
{
if (self = [super init]) {
self.spans = [[NSMutableDictionary alloc] init];
self.activeSpanStack = [[NSMutableArray alloc] init];
}
return self;
}
- (SentrySpanId *)startSpanWithName:(NSString *)name
nameSource:(SentryTransactionNameSource)source
operation:(NSString *)operation
origin:(NSString *)origin
{
id<SentrySpan> activeSpan;
@synchronized(self.activeSpanStack) {
activeSpan = [self.activeSpanStack lastObject];
}
__block id<SentrySpan> newSpan;
if (activeSpan != nil) {
newSpan = [activeSpan startChildWithOperation:operation description:name];
newSpan.origin = origin;
} else {
SentryTransactionContext *context = [[SentryTransactionContext alloc] initWithName:name
nameSource:source
operation:operation
origin:origin];
[SentrySDK.currentHub.scope useSpan:^(id<SentrySpan> span) {
BOOL bindToScope = NO;
if (span == nil) {
bindToScope = YES;
}
#if SENTRY_HAS_UIKIT
else {
if ([SentryUIEventTracker isUIEventOperation:span.operation]) {
SENTRY_LOG_DEBUG(
@"Cancelling previous UI event span %@", span.spanId.sentrySpanIdString);
[span finishWithStatus:kSentrySpanStatusCancelled];
bindToScope = YES;
}
}
#endif // SENTRY_HAS_UIKIT
SENTRY_LOG_DEBUG(@"Creating new transaction bound to scope: %d", bindToScope);
newSpan = [SentrySDK.currentHub
startTransactionWithContext:context
bindToScope:bindToScope
customSamplingContext:@{}
configuration:[SentryTracerConfiguration configurationWithBlock:^(
SentryTracerConfiguration *configuration) {
configuration.waitForChildren = YES;
}]];
[(SentryTracer *)newSpan setDelegate:self];
}];
}
SentrySpanId *spanId = newSpan.spanId;
if (spanId != nil) {
@synchronized(self.spans) {
self.spans[spanId] = newSpan;
}
} else {
SENTRY_LOG_ERROR(@"startSpanWithName:operation: spanId is nil.");
return [SentrySpanId empty];
}
return spanId;
}
- (void)measureSpanWithDescription:(NSString *)description
nameSource:(SentryTransactionNameSource)source
operation:(NSString *)operation
origin:(NSString *)origin
inBlock:(void (^)(void))block
{
SentrySpanId *spanId = [self startSpanWithName:description
nameSource:source
operation:operation
origin:origin];
SENTRY_LOG_DEBUG(@"Measuring span %@; description %@; operation: %@", spanId.sentrySpanIdString,
description, operation);
[self pushActiveSpan:spanId];
block();
[self popActiveSpan];
[self finishSpan:spanId];
}
- (void)measureSpanWithDescription:(NSString *)description
nameSource:(SentryTransactionNameSource)source
operation:(NSString *)operation
origin:(NSString *)origin
parentSpanId:(SentrySpanId *)parentSpanId
inBlock:(void (^)(void))block
{
[self activateSpan:parentSpanId
duringBlock:^{
[self measureSpanWithDescription:description
nameSource:source
operation:operation
origin:origin
inBlock:block];
}];
}
- (void)activateSpan:(SentrySpanId *)spanId duringBlock:(void (^)(void))block
{
if ([self pushActiveSpan:spanId]) {
block();
[self popActiveSpan];
} else {
block();
}
}
- (nullable SentrySpanId *)activeSpanId
{
@synchronized(self.activeSpanStack) {
return [self.activeSpanStack lastObject].spanId;
}
}
- (BOOL)pushActiveSpan:(SentrySpanId *)spanId
{
SENTRY_LOG_DEBUG(@"Pushing active span %@", spanId.sentrySpanIdString);
id<SentrySpan> toActiveSpan;
@synchronized(self.spans) {
toActiveSpan = self.spans[spanId];
}
if (toActiveSpan == nil) {
SENTRY_LOG_DEBUG(@"No span found with ID %@", spanId.sentrySpanIdString);
return NO;
}
@synchronized(self.activeSpanStack) {
[self.activeSpanStack addObject:toActiveSpan];
}
return YES;
}
- (void)popActiveSpan
{
@synchronized(self.activeSpanStack) {
[self.activeSpanStack removeLastObject];
}
}
- (void)finishSpan:(SentrySpanId *)spanId
{
SENTRY_LOG_DEBUG(@"Finishing performance span %@", spanId.sentrySpanIdString);
[self finishSpan:spanId withStatus:kSentrySpanStatusOk];
}
- (void)finishSpan:(SentrySpanId *)spanId withStatus:(SentrySpanStatus)status
{
id<SentrySpan> spanTracker;
@synchronized(self.spans) {
spanTracker = self.spans[spanId];
// Hold reference for tracer until the tracer finishes because automatic
// tracers aren't referenced by anything else.
// callback to `tracerDidFinish` will release it.
if (![spanTracker isKindOfClass:SentryTracer.self]) {
[self.spans removeObjectForKey:spanId];
}
}
[spanTracker finishWithStatus:status];
}
- (BOOL)isSpanAlive:(SentrySpanId *)spanId
{
@synchronized(self.spans) {
return self.spans[spanId] != nil;
}
}
- (nullable id<SentrySpan>)getSpan:(SentrySpanId *)spanId
{
@synchronized(self.spans) {
return self.spans[spanId];
}
}
- (nullable id<SentrySpan>)activeSpanForTracer:(SentryTracer *)tracer
{
@synchronized(self.activeSpanStack) {
return [self.activeSpanStack lastObject];
}
}
- (void)clear
{
[self.activeSpanStack removeAllObjects];
[self.spans removeAllObjects];
}
- (void)tracerDidFinish:(SentryTracer *)tracer
{
@synchronized(self.spans) {
[self.spans removeObjectForKey:tracer.spanId];
}
}
@end
NS_ASSUME_NONNULL_END