Skip to content

Commit

Permalink
feat: Add enableTracing option (#2693)
Browse files Browse the repository at this point in the history
Added a enableTracing as a new property to SentryOption.
Co-authored-by: Philipp Hofmann <[email protected]>
  • Loading branch information
brustolin authored Feb 24, 2023
1 parent cb10da6 commit ce4cfaf
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

### Features

- Combine UIKit and SwiftUI transactions (#2681)
- Add enableTracing option (#2693)
- Add isMain thread to SentryThread (#2692)
- Add `in_foreground` to App Context (#2692)
- Combine UIKit and SwiftUI transactions (#2681)

### Fixes

Expand Down
14 changes: 12 additions & 2 deletions Sources/Sentry/Public/SentryOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,16 @@ NS_SWIFT_NAME(Options)
*/
@property (nonatomic, assign) BOOL enableFileIOTracing;

/**
* Indicates whether tracing should be enabled.
* Enabling this sets `tracesSampleRate` to 1 if both
* `tracesSampleRate` and `tracesSampler` are nil.
*
* Changing either `tracesSampleRate` or `tracesSampler` to
* a value other then nil will enable this in case this was never changed before.
*/
@property (nonatomic) BOOL enableTracing;

/**
* Indicates the percentage of the tracing data that is collected. Setting this to 0 or NIL discards
* all trace data, 1.0 collects all trace data, 0.01 collects 1% of all trace data. The default is
Expand All @@ -280,8 +290,8 @@ NS_SWIFT_NAME(Options)
@property (nullable, nonatomic) SentryTracesSamplerCallback tracesSampler;

/**
* If tracing should be enabled or not. Returns YES if either a tracesSampleRate > 0 and <=1 or a
* tracesSampler is set otherwise NO.
* If tracing is enabled or not. Returns YES if enabledTracing is YES and either a tracesSampleRate
* > 0 and <=1 or a tracesSampler is set otherwise NO.
*/
@property (nonatomic, assign, readonly) BOOL isTracingEnabled;

Expand Down
40 changes: 37 additions & 3 deletions Sources/Sentry/SentryOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

NSString *const kSentryDefaultEnvironment = @"production";

@implementation SentryOptions
@implementation SentryOptions {
BOOL _enableTracingManual;
}

- (void)setMeasurement:(SentryMeasurementValue *)measurement
{
Expand Down Expand Up @@ -74,6 +76,9 @@ - (instancetype)init
self.enableAutoPerformanceTracing = YES;
self.enableCaptureFailedRequests = YES;
self.environment = kSentryDefaultEnvironment;

_enableTracing = NO;
_enableTracingManual = NO;
#if SENTRY_HAS_UIKIT
self.enableUIViewControllerTracing = YES;
self.attachScreenshot = NO;
Expand Down Expand Up @@ -366,6 +371,10 @@ - (BOOL)validateOptions:(NSDictionary<NSString *, id> *)options
self.tracesSampler = options[@"tracesSampler"];
}

if ([options[@"enableTracing"] isKindOfClass:NSNumber.self]) {
self.enableTracing = [options[@"enableTracing"] boolValue];
}

if ([options[@"inAppIncludes"] isKindOfClass:[NSArray class]]) {
NSArray<NSString *> *inAppIncludes =
[options[@"inAppIncludes"] filteredArrayUsingPredicate:isNSString];
Expand Down Expand Up @@ -466,17 +475,41 @@ - (BOOL)isValidSampleRate:(NSNumber *)sampleRate
return [self isValidTracesSampleRate:sampleRate];
}

- (void)setEnableTracing:(BOOL)enableTracing
{
//`enableTracing` is basically an alias to tracesSampleRate
// by enabling it we set tracesSampleRate to maximum
// if the user did not configured other ways to enable tracing
if ((_enableTracing = enableTracing)) {
if (_tracesSampleRate == nil && _tracesSampler == nil && _enableTracing) {
_tracesSampleRate = @1;
}
}
_enableTracingManual = YES;
}

- (void)setTracesSampleRate:(NSNumber *)tracesSampleRate
{
if (tracesSampleRate == nil) {
_tracesSampleRate = nil;
} else if ([self isValidTracesSampleRate:tracesSampleRate]) {
_tracesSampleRate = tracesSampleRate;
if (!_enableTracingManual) {
_enableTracing = YES;
}
} else {
_tracesSampleRate = _defaultTracesSampleRate;
}
}

- (void)setTracesSampler:(SentryTracesSamplerCallback)tracesSampler
{
_tracesSampler = tracesSampler;
if (_tracesSampler != nil && !_enableTracingManual) {
_enableTracing = YES;
}
}

- (BOOL)isValidTracesSampleRate:(NSNumber *)tracesSampleRate
{
double rate = [tracesSampleRate doubleValue];
Expand All @@ -485,8 +518,9 @@ - (BOOL)isValidTracesSampleRate:(NSNumber *)tracesSampleRate

- (BOOL)isTracingEnabled
{
return (_tracesSampleRate != nil && [_tracesSampleRate doubleValue] > 0)
|| _tracesSampler != nil;
return _enableTracing
&& ((_tracesSampleRate != nil && [_tracesSampleRate doubleValue] > 0)
|| _tracesSampler != nil);
}

#if SENTRY_TARGET_PROFILING_SUPPORTED
Expand Down
66 changes: 66 additions & 0 deletions Tests/SentryTests/SentryOptionsTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ - (void)testNSNull_SetsDefaultValue
@"sdk" : [NSNull null],
@"enableCaptureFailedRequests" : [NSNull null],
@"failedRequestStatusCodes" : [NSNull null],
@"enableTracing" : [NSNull null]
}
didFailWithError:nil];

Expand Down Expand Up @@ -567,6 +568,7 @@ - (void)assertDefaultValues:(SentryOptions *)options
XCTAssertEqual(options.enablePreWarmedAppStartTracing, NO);
XCTAssertEqual(options.attachViewHierarchy, NO);
#endif
XCTAssertFalse(options.enableTracing);
XCTAssertTrue(options.enableAppHangTracking);
XCTAssertEqual(options.appHangTimeoutInterval, 2);
XCTAssertEqual(YES, options.enableNetworkTracking);
Expand Down Expand Up @@ -763,11 +765,74 @@ - (void)testEnableSwizzling
[self testBooleanField:@"enableSwizzling"];
}

- (void)testEnableTracing
{
SentryOptions *options = [self getValidOptions:@{ @"enableTracing" : @YES }];
XCTAssertTrue(options.enableTracing);
XCTAssertEqual(options.tracesSampleRate.doubleValue, 1);
}

- (void)testChanging_enableTracing_afterSetting_tracesSampleRate
{
SentryOptions *options = [[SentryOptions alloc] init];
options.tracesSampleRate = @0.5;
options.enableTracing = NO;
XCTAssertEqual(options.tracesSampleRate.doubleValue, 0.5);
options.enableTracing = YES;
XCTAssertEqual(options.tracesSampleRate.doubleValue, 0.5);
}

- (void)testChanging_enableTracing_afterSetting_tracesSampler
{
SentryOptions *options = [[SentryOptions alloc] init];
options.tracesSampler = ^NSNumber *(SentrySamplingContext *__unused samplingContext)
{
return @0.1;
};
options.enableTracing = NO;
XCTAssertNil(options.tracesSampleRate);
options.enableTracing = FALSE;
XCTAssertNil(options.tracesSampleRate);
}

- (void)testChanging_tracesSampleRate_afterSetting_enableTracing
{
SentryOptions *options = [[SentryOptions alloc] init];
options.enableTracing = YES;
options.tracesSampleRate = @0;
XCTAssertTrue(options.enableTracing);
options.tracesSampleRate = @1;
XCTAssertTrue(options.enableTracing);

options.enableTracing = NO;
options.tracesSampleRate = @0.5;
XCTAssertFalse(options.enableTracing);
XCTAssertEqual(options.tracesSampleRate.doubleValue, 0.5);
}

- (void)testChanging_tracesSampler_afterSetting_enableTracing
{
SentryTracesSamplerCallback sampler
= ^(__unused SentrySamplingContext *context) { return @1.0; };

SentryOptions *options = [[SentryOptions alloc] init];
options.enableTracing = YES;
options.tracesSampler = sampler;
XCTAssertTrue(options.enableTracing);
options.tracesSampleRate = nil;
XCTAssertTrue(options.enableTracing);

options.enableTracing = NO;
options.tracesSampler = sampler;
XCTAssertFalse(options.enableTracing);
}

- (void)testTracesSampleRate
{
SentryOptions *options = [self getValidOptions:@{ @"tracesSampleRate" : @0.1 }];

XCTAssertEqual(options.tracesSampleRate.doubleValue, 0.1);
XCTAssertTrue(options.enableTracing);
}

- (void)testDefaultTracesSampleRate
Expand Down Expand Up @@ -832,6 +897,7 @@ - (void)testTracesSampler

SentrySamplingContext *context = [[SentrySamplingContext alloc] init];
XCTAssertEqual(options.tracesSampler(context), @1.0);
XCTAssertTrue(options.enableTracing);
}

- (void)testDefaultTracesSampler
Expand Down

0 comments on commit ce4cfaf

Please sign in to comment.