Skip to content

Commit

Permalink
Merge 6c538b6 into 3db98a8
Browse files Browse the repository at this point in the history
  • Loading branch information
armcknight authored Feb 14, 2024
2 parents 3db98a8 + 6c538b6 commit 990435a
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 16 deletions.
49 changes: 34 additions & 15 deletions Sources/Sentry/Profiling/SentryLaunchProfiling.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
# import "SentrySamplerDecision.h"
# import "SentrySampling.h"
# import "SentrySamplingContext.h"
# import "SentryTraceOrigins.h"
# import "SentryTracer+Private.h"
# import "SentryTracerConfiguration.h"
# import "SentryTransactionContext.h"
# import "SentryTransactionContext+Private.h"

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -108,6 +109,29 @@
}];
}

SentryTransactionContext *
context(NSNumber *tracesRate)
{
SentryTransactionContext *context =
[[SentryTransactionContext alloc] initWithName:@"launch"
nameSource:kSentryTransactionNameSourceCustom
operation:@"app.lifecycle"
origin:SentryTraceOriginAutoAppStartProfile
sampled:kSentrySampleDecisionYes];
context.sampleRate = tracesRate;
return context;
}

SentryTracerConfiguration *
config(NSNumber *profilesRate)
{
SentryTracerConfiguration *config = [SentryTracerConfiguration defaultConfiguration];
config.profilesSamplerDecision =
[[SentrySamplerDecision alloc] initWithDecision:kSentrySampleDecisionYes
forSampleRate:profilesRate];
return config;
}

void
startLaunchProfile(void)
{
Expand All @@ -128,25 +152,19 @@
[SentryLog configure:YES diagnosticLevel:kSentryLevelDebug];
# endif // defined(DEBUG)

SENTRY_LOG_INFO(@"Starting app launch profile.");

SentryTransactionContext *context =
[[SentryTransactionContext alloc] initWithName:@"launch"
operation:@"app.lifecycle"
sampled:kSentrySampleDecisionYes];
SentryTracerConfiguration *config = [SentryTracerConfiguration defaultConfiguration];
NSDictionary<NSString *, NSNumber *> *rates = appLaunchProfileConfiguration();
NSNumber *profilesRate = rates[kSentryLaunchProfileConfigKeyProfilesSampleRate];
NSNumber *tracesRate = rates[kSentryLaunchProfileConfigKeyTracesSampleRate];
if (profilesRate != nil && tracesRate != nil) {
config.profilesSamplerDecision =
[[SentrySamplerDecision alloc] initWithDecision:kSentrySampleDecisionYes
forSampleRate:profilesRate];
context.sampleRate = tracesRate;
if (profilesRate == nil || tracesRate == nil) {
SENTRY_LOG_DEBUG(
@"Received a nil configured launch sample rate, will not trace or profile.");
return;
}
launchTracer = [[SentryTracer alloc] initWithTransactionContext:context

SENTRY_LOG_INFO(@"Starting app launch profile.");
launchTracer = [[SentryTracer alloc] initWithTransactionContext:context(tracesRate)
hub:nil
configuration:config];
configuration:config(profilesRate)];
});
}

Expand All @@ -155,6 +173,7 @@
{
if (launchTracer == nil) {
SENTRY_LOG_DEBUG(@"No launch tracer present to stop.");
return;
}

SENTRY_LOG_DEBUG(@"Finishing launch tracer.");
Expand Down
2 changes: 1 addition & 1 deletion Sources/Sentry/include/SentryLaunchProfiling.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ NS_ASSUME_NONNULL_BEGIN
SENTRY_EXTERN BOOL isTracingAppLaunch;

/** Try to start a profiled trace for this app launch, if the configuration allows. */
void startLaunchProfile(void);
SENTRY_EXTERN void startLaunchProfile(void);

/** Stop any profiled trace that may be in flight from the start of the app launch. */
void stopLaunchProfile(SentryHub *hub);
Expand Down
1 change: 1 addition & 0 deletions Sources/Sentry/include/SentryTraceOrigins.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ static NSString *const SentryTraceOriginManual = @"manual";
static NSString *const SentryTraceOriginUIEventTracker = @"auto.ui.event_tracker";

static NSString *const SentryTraceOriginAutoAppStart = @"auto.app.start";
static NSString *const SentryTraceOriginAutoAppStartProfile = @"auto.app.start.profile";
static NSString *const SentryTraceOriginAutoNSData = @"auto.file.ns_data";
static NSString *const SentryTraceOriginAutoDBCoreData = @"auto.db.core_data";
static NSString *const SentryTraceOriginAutoHttpNSURLSession = @"auto.http.ns_url_session";
Expand Down
12 changes: 12 additions & 0 deletions Tests/SentryProfilerTests/SentryAppLaunchProfilingTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#import "SentryOptions+Private.h"
#import "SentryProfilingConditionals.h"
#import "SentrySDK+Tests.h"
#import "SentryTraceOrigins.h"
#import "SentryTransactionContext.h"
#import <XCTest/XCTest.h>

#if SENTRY_TARGET_PROFILING_SUPPORTED
Expand All @@ -12,6 +14,14 @@ @interface SentryAppLaunchProfilingTests : XCTestCase

@implementation SentryAppLaunchProfilingTests

- (void)testLaunchProfileTransactionContext
{
SentryTransactionContext *actualContext = context(@1);
XCTAssertEqual(actualContext.nameSource, kSentryTransactionNameSourceCustom);
XCTAssert([actualContext.origin isEqualToString:SentryTraceOriginAutoAppStartProfile]);
XCTAssert(actualContext.sampled);
}

# define SENTRY_OPTION(name, value) \
NSStringFromSelector(@selector(name)) \
: value
Expand Down Expand Up @@ -146,6 +156,8 @@ - (void)testDisablingUIViewControllerTracingOptionDisablesAppLaunchProfiling
}
# endif // SENTRY_HAS_UIKIT

# pragma mark - Private

- (SentryOptions *)defaultLaunchProfilingOptionsWithOverrides:
(NSDictionary<NSString *, id> *)overrides
{
Expand Down
7 changes: 7 additions & 0 deletions Tests/SentryTests/SentryLaunchProfiling+Tests.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#import "SentryDefines.h"
#import "SentryLaunchProfiling.h"

#if SENTRY_TARGET_PROFILING_SUPPORTED

@class SentrySamplerDecision;
@class SentryOptions;

Expand All @@ -17,4 +19,9 @@ SENTRY_EXTERN SentryLaunchProfileConfig shouldProfileNextLaunch(SentryOptions *o
SENTRY_EXTERN NSString *const kSentryLaunchProfileConfigKeyTracesSampleRate;
SENTRY_EXTERN NSString *const kSentryLaunchProfileConfigKeyProfilesSampleRate;

SENTRY_EXTERN SentryTransactionContext *context(NSNumber *tracesRate);
SENTRY_EXTERN SentryTracerConfiguration *config(NSNumber *profilesRate);

NS_ASSUME_NONNULL_END

#endif // SENTRY_TARGET_PROFILING_SUPPORTED

0 comments on commit 990435a

Please sign in to comment.