|
27 | 27 | #import "SentryTraceContext.h" |
28 | 28 | #import "SentryTraceHeader.h" |
29 | 29 | #import "SentryTraceOrigin.h" |
| 30 | +#import "SentryTracePropagation.h" |
30 | 31 | #import "SentryTracer.h" |
31 | 32 | #import "SentryUser.h" |
32 | 33 | #import <objc/runtime.h> |
@@ -107,35 +108,6 @@ - (void)disable SENTRY_DISABLE_THREAD_SANITIZER(SentryNetworkTrackerThreadSaniti |
107 | 108 | _isGraphQLOperationTrackingEnabled = NO; |
108 | 109 | } |
109 | 110 |
|
110 | | -- (BOOL)isTargetMatch:(NSURL *)URL withTargets:(NSArray *)targets |
111 | | -{ |
112 | | - for (id targetCheck in targets) { |
113 | | - if ([targetCheck isKindOfClass:[NSRegularExpression class]]) { |
114 | | - NSString *string = URL.absoluteString; |
115 | | - NSUInteger numberOfMatches = |
116 | | - [targetCheck numberOfMatchesInString:string |
117 | | - options:0 |
118 | | - range:NSMakeRange(0, [string length])]; |
119 | | - if (numberOfMatches > 0) { |
120 | | - return YES; |
121 | | - } |
122 | | - } else if ([targetCheck isKindOfClass:[NSString class]]) { |
123 | | - if ([URL.absoluteString containsString:targetCheck]) { |
124 | | - return YES; |
125 | | - } |
126 | | - } |
127 | | - } |
128 | | - |
129 | | - return NO; |
130 | | -} |
131 | | - |
132 | | -- (BOOL)sessionTaskRequiresPropagation:(NSURLSessionTask *)sessionTask |
133 | | -{ |
134 | | - return sessionTask.currentRequest != nil && |
135 | | - [self isTargetMatch:sessionTask.currentRequest.URL |
136 | | - withTargets:SentrySDKInternal.options.tracePropagationTargets]; |
137 | | -} |
138 | | - |
139 | 111 | - (void)urlSessionTaskResume:(NSURLSessionTask *)sessionTask |
140 | 112 | { |
141 | 113 | NSURLSessionTaskState sessionState = sessionTask.state; |
@@ -217,7 +189,9 @@ - (void)urlSessionTaskResume:(NSURLSessionTask *)sessionTask |
217 | 189 | } |
218 | 190 |
|
219 | 191 | SentryBaggage *baggage = [[[SentryTracer getTracer:span] traceContext] toBaggage]; |
220 | | - [self addBaggageHeader:baggage traceHeader:[netSpan toTraceHeader] toRequest:sessionTask]; |
| 192 | + [SentryTracePropagation addBaggageHeader:baggage |
| 193 | + traceHeader:[netSpan toTraceHeader] |
| 194 | + toRequest:sessionTask]; |
221 | 195 |
|
222 | 196 | SENTRY_LOG_DEBUG( |
223 | 197 | @"SentryNetworkTracker automatically started HTTP span for sessionTask: %@", |
@@ -249,66 +223,9 @@ - (void)addTraceWithoutTransactionToTask:(NSURLSessionTask *)sessionTask |
249 | 223 | #endif |
250 | 224 | replayId:SentrySDKInternal.currentHub.scope.replayId]; |
251 | 225 |
|
252 | | - [self addBaggageHeader:[traceContext toBaggage] |
253 | | - traceHeader:[propagationContext traceHeader] |
254 | | - toRequest:sessionTask]; |
255 | | -} |
256 | | - |
257 | | -- (void)addBaggageHeader:(SentryBaggage *)baggage |
258 | | - traceHeader:(SentryTraceHeader *)traceHeader |
259 | | - toRequest:(NSURLSessionTask *)sessionTask |
260 | | -{ |
261 | | - if (![self sessionTaskRequiresPropagation:sessionTask]) { |
262 | | - SENTRY_LOG_DEBUG(@"Not adding trace_id and baggage headers for %@", |
263 | | - sessionTask.currentRequest.URL.absoluteString); |
264 | | - return; |
265 | | - } |
266 | | - NSString *baggageHeader = @""; |
267 | | - |
268 | | - if (baggage != nil) { |
269 | | - NSDictionary *originalBaggage = [SentryBaggageSerialization |
270 | | - decode:sessionTask.currentRequest.allHTTPHeaderFields[SENTRY_BAGGAGE_HEADER]]; |
271 | | - |
272 | | - if (originalBaggage[@"sentry-trace_id"] == nil) { |
273 | | - baggageHeader = [baggage toHTTPHeaderWithOriginalBaggage:originalBaggage]; |
274 | | - } |
275 | | - } |
276 | | - |
277 | | - // First we check if the current request is mutable, so we could easily add a new |
278 | | - // header. Otherwise we try to change the current request for a new one with the extra |
279 | | - // header. |
280 | | - if ([sessionTask.currentRequest isKindOfClass:[NSMutableURLRequest class]]) { |
281 | | - NSMutableURLRequest *currentRequest = (NSMutableURLRequest *)sessionTask.currentRequest; |
282 | | - |
283 | | - if ([currentRequest valueForHTTPHeaderField:SENTRY_TRACE_HEADER] == nil) { |
284 | | - [currentRequest setValue:traceHeader.value forHTTPHeaderField:SENTRY_TRACE_HEADER]; |
285 | | - } |
286 | | - |
287 | | - if (baggageHeader.length > 0) { |
288 | | - [currentRequest setValue:baggageHeader forHTTPHeaderField:SENTRY_BAGGAGE_HEADER]; |
289 | | - } |
290 | | - } else { |
291 | | - // Even though NSURLSessionTask doesn't have 'setCurrentRequest', some subclasses |
292 | | - // do. For those subclasses we replace the currentRequest with a mutable one with |
293 | | - // the additional trace header. Since NSURLSessionTask is a public class and can be |
294 | | - // override, we believe this is not considered a private api. |
295 | | - SEL setCurrentRequestSelector = NSSelectorFromString(@"setCurrentRequest:"); |
296 | | - if ([sessionTask respondsToSelector:setCurrentRequestSelector]) { |
297 | | - NSMutableURLRequest *newRequest = [sessionTask.currentRequest mutableCopy]; |
298 | | - |
299 | | - if ([newRequest valueForHTTPHeaderField:SENTRY_TRACE_HEADER] == nil) { |
300 | | - [newRequest setValue:traceHeader.value forHTTPHeaderField:SENTRY_TRACE_HEADER]; |
301 | | - } |
302 | | - |
303 | | - if (baggageHeader.length > 0) { |
304 | | - [newRequest setValue:baggageHeader forHTTPHeaderField:SENTRY_BAGGAGE_HEADER]; |
305 | | - } |
306 | | - |
307 | | - void (*func)(id, SEL, id param) |
308 | | - = (void *)[sessionTask methodForSelector:setCurrentRequestSelector]; |
309 | | - func(sessionTask, setCurrentRequestSelector, newRequest); |
310 | | - } |
311 | | - } |
| 226 | + [SentryTracePropagation addBaggageHeader:[traceContext toBaggage] |
| 227 | + traceHeader:[propagationContext traceHeader] |
| 228 | + toRequest:sessionTask]; |
312 | 229 | } |
313 | 230 |
|
314 | 231 | - (void)urlSessionTask:(NSURLSessionTask *)sessionTask setState:(NSURLSessionTaskState)newState |
@@ -401,8 +318,8 @@ - (void)captureFailedRequests:(NSURLSessionTask *)sessionTask |
401 | 318 | return; |
402 | 319 | } |
403 | 320 |
|
404 | | - if (![self isTargetMatch:myRequest.URL |
405 | | - withTargets:SentrySDKInternal.options.failedRequestTargets]) { |
| 321 | + if (![SentryTracePropagation isTargetMatch:myRequest.URL |
| 322 | + withTargets:SentrySDKInternal.options.failedRequestTargets]) { |
406 | 323 | SENTRY_LOG_DEBUG( |
407 | 324 | @"Request url isn't within the request targets, not capturing HTTP Client errors."); |
408 | 325 | return; |
|
0 commit comments