Skip to content

Commit

Permalink
Merge 1fc2ad4 into ec9a3d7
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto authored Oct 21, 2022
2 parents ec9a3d7 + 1fc2ad4 commit d34a8cf
Show file tree
Hide file tree
Showing 24 changed files with 258 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1340"
LastUpgradeVersion = "1400"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1320"
LastUpgradeVersion = "1400"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1250"
LastUpgradeVersion = "1400"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1340"
LastUpgradeVersion = "1400"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1200"
LastUpgradeVersion = "1400"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1310"
LastUpgradeVersion = "1400"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1340"
LastUpgradeVersion = "1400"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
1 change: 1 addition & 0 deletions Samples/iOS-Swift/iOS-Swift/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
// because we run CPU for 15 seconds at full throttle, we trigger ANR issues being sent. disable such during benchmarks.
options.enableAppHangTracking = !isBenchmarking
options.appHangTimeoutInterval = 2
options.enableCaptureFailedRequests = true
}

return true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1310"
LastUpgradeVersion = "1400"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1200"
LastUpgradeVersion = "1400"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1250"
LastUpgradeVersion = "1400"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
10 changes: 9 additions & 1 deletion Sentry.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,8 @@
A811D867248E2770008A41EA /* SentrySystemEventBreadcrumbsTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = A811D866248E2770008A41EA /* SentrySystemEventBreadcrumbsTest.swift */; };
A839D89824864B80003B7AFD /* SentrySystemEventBreadcrumbs.h in Headers */ = {isa = PBXBuildFile; fileRef = A839D89724864B80003B7AFD /* SentrySystemEventBreadcrumbs.h */; };
A839D89A24864BA8003B7AFD /* SentrySystemEventBreadcrumbs.m in Sources */ = {isa = PBXBuildFile; fileRef = A839D89924864BA8003B7AFD /* SentrySystemEventBreadcrumbs.m */; };
A8F17B2C29016BD100990B25 /* SentryRequest.h in Headers */ = {isa = PBXBuildFile; fileRef = A8F17B2B29016BD100990B25 /* SentryRequest.h */; };
A8F17B2E2901765900990B25 /* SentryRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = A8F17B2D2901765900990B25 /* SentryRequest.m */; };
D8019910286B089000C277F0 /* SentryCrashReportSinkTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = D801990F286B089000C277F0 /* SentryCrashReportSinkTest.swift */; };
D808FB88281AB33C009A2A33 /* SentryUIEventTrackerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D808FB86281AB31D009A2A33 /* SentryUIEventTrackerTests.swift */; };
D808FB8B281BCE96009A2A33 /* TestSentrySwizzleWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = D808FB89281BCE46009A2A33 /* TestSentrySwizzleWrapper.swift */; };
Expand Down Expand Up @@ -1444,6 +1446,8 @@
A811D866248E2770008A41EA /* SentrySystemEventBreadcrumbsTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentrySystemEventBreadcrumbsTest.swift; sourceTree = "<group>"; };
A839D89724864B80003B7AFD /* SentrySystemEventBreadcrumbs.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentrySystemEventBreadcrumbs.h; path = include/SentrySystemEventBreadcrumbs.h; sourceTree = "<group>"; };
A839D89924864BA8003B7AFD /* SentrySystemEventBreadcrumbs.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentrySystemEventBreadcrumbs.m; sourceTree = "<group>"; };
A8F17B2B29016BD100990B25 /* SentryRequest.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SentryRequest.h; sourceTree = "<group>"; };
A8F17B2D2901765900990B25 /* SentryRequest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryRequest.m; sourceTree = "<group>"; };
D801990F286B089000C277F0 /* SentryCrashReportSinkTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryCrashReportSinkTest.swift; sourceTree = "<group>"; };
D808FB86281AB31D009A2A33 /* SentryUIEventTrackerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryUIEventTrackerTests.swift; sourceTree = "<group>"; };
D808FB89281BCE46009A2A33 /* TestSentrySwizzleWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestSentrySwizzleWrapper.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1640,6 +1644,8 @@
7BD4BD4827EB2A5D0071F4FF /* SentryDiscardedEvent.m */,
7BC9A20128F41350001E7C4C /* SentryMeasurementUnit.h */,
7BC9A20528F41781001E7C4C /* SentryMeasurementUnit.m */,
A8F17B2B29016BD100990B25 /* SentryRequest.h */,
A8F17B2D2901765900990B25 /* SentryRequest.m */,
);
name = Protocol;
sourceTree = "<group>";
Expand Down Expand Up @@ -3008,6 +3014,7 @@
63FE711F20DA4C1000CDBAE8 /* SentryCrashObjC.h in Headers */,
7BC3936825B1AB3E004F03D3 /* SentryLevelMapper.h in Headers */,
8E4E7C6E25DAAAFE006AB9E2 /* SentrySpan.h in Headers */,
A8F17B2C29016BD100990B25 /* SentryRequest.h in Headers */,
D8ACE3CE2762187D00F5A213 /* SentryNSDataTracker.h in Headers */,
03F84D2427DD414C008FE43F /* SentryCompiler.h in Headers */,
631E6D331EBC679C00712345 /* SentryQueueableRequestManager.h in Headers */,
Expand Down Expand Up @@ -3215,7 +3222,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 1250;
LastUpgradeCheck = 1200;
LastUpgradeCheck = 1400;
ORGANIZATIONNAME = Sentry;
TargetAttributes = {
63AA759A1EB8AEF500D153DE = {
Expand Down Expand Up @@ -3392,6 +3399,7 @@
6344DDBA1EC3115C00D9160D /* SentryCrashReportConverter.m in Sources */,
63FE70FD20DA4C1000CDBAE8 /* SentryCrashCachedData.c in Sources */,
8EE32518261FE27B00DC3FF2 /* SentryUIViewControllerSanitizer.m in Sources */,
A8F17B2E2901765900990B25 /* SentryRequest.m in Sources */,
7BE1E33424F7E3CB009D3AD0 /* SentryMigrateSessionInit.m in Sources */,
15E0A8F22411A45A00F044E3 /* SentrySession.m in Sources */,
7B6D1261265F784000C9BE4B /* PrivateSentrySDKOnly.m in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Sentry.xcodeproj/xcshareddata/xcschemes/Sentry.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1200"
LastUpgradeVersion = "1400"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
7 changes: 6 additions & 1 deletion Sources/Sentry/Public/SentryEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
NS_ASSUME_NONNULL_BEGIN

@class SentryThread, SentryException, SentryStacktrace, SentryUser, SentryDebugMeta, SentryContext,
SentryBreadcrumb, SentryId, SentryMessage;
SentryBreadcrumb, SentryId, SentryMessage, SentryRequest;

NS_SWIFT_NAME(Event)
@interface SentryEvent : NSObject <SentrySerializable>
Expand Down Expand Up @@ -159,6 +159,11 @@ NS_SWIFT_NAME(Event)
*/
@property (nonatomic, strong) NSArray<SentryBreadcrumb *> *_Nullable breadcrumbs;

/**
* Set the Http request information.
*/
@property (nonatomic, strong) SentryRequest *_Nullable request;

/**
* Init an SentryEvent will set all needed fields by default
* @return SentryEvent
Expand Down
5 changes: 5 additions & 0 deletions Sources/Sentry/Public/SentryOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,11 @@ NS_SWIFT_NAME(Options)
*/
@property (nonatomic, retain) NSArray *tracePropagationTargets;

/**
* When enabled, the SDK captures HTTP Client errors. Default value is NO.
*/
@property (nonatomic, assign) BOOL enableCaptureFailedRequests;

@end

NS_ASSUME_NONNULL_END
5 changes: 5 additions & 0 deletions Sources/Sentry/Public/SentryStacktrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ SENTRY_NO_INIT
*/
@property (nonatomic, strong) NSDictionary<NSString *, NSString *> *registers;

/**
* Indicates that this stack trace is a snapshot triggered by an external signal.
*/
@property (nonatomic, copy) NSNumber *_Nullable snapshot;

/**
* Initialize a SentryStacktrace with frames and registers
* @param frames NSArray
Expand Down
5 changes: 5 additions & 0 deletions Sources/Sentry/SentryEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "SentryLevelMapper.h"
#import "SentryMessage.h"
#import "SentryMeta.h"
#import "SentryRequest.h"
#import "SentryStacktrace.h"
#import "SentryThread.h"
#import "SentryUser.h"
Expand Down Expand Up @@ -157,6 +158,10 @@ - (void)addSimpleProperties:(NSMutableDictionary *)serializedData
forKey:@"start_timestamp"];
}
}

if (nil != self.request) {
[serializedData setValue:[self.request serialize] forKey:@"request"];
}
}

- (NSArray *_Nullable)serializeBreadcrumbs
Expand Down
102 changes: 101 additions & 1 deletion Sources/Sentry/SentryNetworkTracker.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
#import "SentryNetworkTracker.h"
#import "SentryBaggage.h"
#import "SentryBreadcrumb.h"
#import "SentryClient+Private.h"
#import "SentryEvent.h"
#import "SentryException.h"
#import "SentryHub+Private.h"
#import "SentryLog.h"
#import "SentryMechanism.h"
#import "SentryRequest.h"
#import "SentrySDK+Private.h"
#import "SentryScope+Private.h"
#import "SentrySerialization.h"
#import "SentryStacktrace.h"
#import "SentryThread.h"
#import "SentryThreadInspector.h"
#import "SentryTraceContext.h"
#import "SentryTraceHeader.h"
#import "SentryTracer.h"
Expand All @@ -16,6 +24,7 @@

@property (nonatomic, assign) BOOL isNetworkTrackingEnabled;
@property (nonatomic, assign) BOOL isNetworkBreadcrumbEnabled;
@property (nonatomic, assign) BOOL isCaptureFailedRequests;

@end

Expand All @@ -34,6 +43,7 @@ - (instancetype)init
if (self = [super init]) {
_isNetworkTrackingEnabled = NO;
_isNetworkBreadcrumbEnabled = NO;
_isCaptureFailedRequests = NO;
}
return self;
}
Expand All @@ -52,11 +62,19 @@ - (void)enableNetworkBreadcrumbs
}
}

- (void)enableCaptureFailedRequests
{
@synchronized(self) {
_isCaptureFailedRequests = YES;
}
}

- (void)disable
{
@synchronized(self) {
_isNetworkBreadcrumbEnabled = NO;
_isNetworkTrackingEnabled = NO;
_isCaptureFailedRequests = NO;
}
}

Expand Down Expand Up @@ -206,7 +224,8 @@ - (void)urlSessionTaskResume:(NSURLSessionTask *)sessionTask

- (void)urlSessionTask:(NSURLSessionTask *)sessionTask setState:(NSURLSessionTaskState)newState
{
if (!self.isNetworkTrackingEnabled && !self.isNetworkBreadcrumbEnabled) {
if (!self.isNetworkTrackingEnabled && !self.isNetworkBreadcrumbEnabled
&& !self.isCaptureFailedRequests) {
return;
}

Expand Down Expand Up @@ -239,6 +258,8 @@ - (void)urlSessionTask:(NSURLSessionTask *)sessionTask setState:(NSURLSessionTas
}

if (sessionTask.state == NSURLSessionTaskStateRunning) {
[self captureEvent:sessionTask];

[self addBreadcrumbForSessionTask:sessionTask];

NSInteger responseStatusCode = [self urlResponseStatusCode:sessionTask.response];
Expand All @@ -265,6 +286,85 @@ - (void)urlSessionTask:(NSURLSessionTask *)sessionTask setState:(NSURLSessionTas
SENTRY_LOG_DEBUG(@"SentryNetworkTracker finished HTTP span for sessionTask");
}

- (void)captureEvent:(NSURLSessionTask *)sessionTask
{
NSInteger responseStatusCode = [self urlResponseStatusCode:sessionTask.response];

// TODO: check the string contains and regex
if (!self.isCaptureFailedRequests) {
return;
}

// TODO: check the range
if (responseStatusCode == 201) {
return;
}

NSString *message = [NSString
stringWithFormat:@"HTTP Client Error with status code: %li", (long)(responseStatusCode)];

SentryEvent *event = [[SentryEvent alloc] initWithLevel:kSentryLevelError];

SentryThreadInspector *threadInspector = SentrySDK.currentHub.getClient.threadInspector;
NSArray<SentryThread *> *threads = [threadInspector getCurrentThreadsWithStackTrace];

SentryException *sentryException = [[SentryException alloc] initWithValue:message
type:@"HTTP-ClientError"];
sentryException.mechanism = [[SentryMechanism alloc] initWithType:@"SentryNetworkTrackingIntegration"];

if (threads.count > 0) {
SentryStacktrace *sentryStacktrace = [threads[0] stacktrace];
sentryStacktrace.snapshot = @(YES);

sentryException.stacktrace = sentryStacktrace;
// TODO: do I need this?
// [threads enumerateObjectsUsingBlock:^(SentryThread *_Nonnull obj, NSUInteger idx,
// BOOL *_Nonnull stop) { obj.current = [NSNumber numberWithBool:idx == 0]; }];
}

SentryRequest *request = [[SentryRequest alloc] init];

NSURLRequest *myRequest = (NSURLRequest *)sessionTask.currentRequest;

NSURL *url = [[sessionTask currentRequest] URL];
request.url = url.absoluteString;

request.fragment = url.fragment;
request.queryString = url.query;
request.method = myRequest.HTTPMethod;
if (sessionTask.countOfBytesSent != 0) {
request.bodySize = [NSNumber numberWithLongLong:sessionTask.countOfBytesSent];
}
if (nil != myRequest.allHTTPHeaderFields) {
NSDictionary<NSString *, NSString *> *headers = myRequest.allHTTPHeaderFields.copy;
request.headers = headers;
request.cookies = headers[@"Cookie"];
}

event.exceptions = @[ sentryException ];
event.request = request;

NSHTTPURLResponse *myResponse = (NSHTTPURLResponse *)sessionTask.response;

NSMutableDictionary<NSString *, id> *context = [[NSMutableDictionary alloc] init];;
NSMutableDictionary<NSString *, id> *response = [[NSMutableDictionary alloc] init];

[response setValue:[NSNumber numberWithLongLong:responseStatusCode] forKey:@"status_code"];
if (nil != myResponse.allHeaderFields) {
NSDictionary<NSString *, NSString *> *headers = myResponse.allHeaderFields.copy;
[response setValue:headers forKey:@"headers"];
[response setValue:headers[@"Cookie"] forKey:@"cookies"];
}
if (sessionTask.countOfBytesReceived != 0) {
[response setValue:[NSNumber numberWithLongLong:sessionTask.countOfBytesReceived] forKey:@"body_size"];
}

context[@"response"] = response;
event.context = context;

[SentrySDK captureEvent:event];
}

- (void)addBreadcrumbForSessionTask:(NSURLSessionTask *)sessionTask
{
if (!self.isNetworkBreadcrumbEnabled) {
Expand Down
7 changes: 6 additions & 1 deletion Sources/Sentry/SentryNetworkTrackingIntegration.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ - (BOOL)installWithOptions:(SentryOptions *)options
[SentryNetworkTracker.sharedInstance enableNetworkBreadcrumbs];
}

if (shouldEnableNetworkTracking || options.enableNetworkBreadcrumbs) {
if (options.enableCaptureFailedRequests) {
[SentryNetworkTracker.sharedInstance enableCaptureFailedRequests];
}

if (shouldEnableNetworkTracking || options.enableNetworkBreadcrumbs
|| options.enableCaptureFailedRequests) {
[SentryNetworkTrackingIntegration swizzleURLSessionTask];
return YES;
} else {
Expand Down
Loading

0 comments on commit d34a8cf

Please sign in to comment.