From 1056e1bb07912c4acf15a5fdbc8b93624ea20be7 Mon Sep 17 00:00:00 2001 From: Andrew McKnight Date: Wed, 14 Feb 2024 13:39:57 +0800 Subject: [PATCH 1/2] set a new trace origin for app launch profiles --- Sources/Sentry/Profiling/SentryLaunchProfiling.m | 5 ++++- Sources/Sentry/include/SentryTraceOrigins.h | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Sources/Sentry/Profiling/SentryLaunchProfiling.m b/Sources/Sentry/Profiling/SentryLaunchProfiling.m index 4b5003d41c..9925c57648 100644 --- a/Sources/Sentry/Profiling/SentryLaunchProfiling.m +++ b/Sources/Sentry/Profiling/SentryLaunchProfiling.m @@ -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" BOOL isTracingAppLaunch; NSString *const kSentryLaunchProfileConfigKeyTracesSampleRate = @"traces"; @@ -130,7 +131,9 @@ SentryTransactionContext *context = [[SentryTransactionContext alloc] initWithName:@"launch" + nameSource:kSentryTransactionNameSourceCustom operation:@"app.lifecycle" + origin:SentryTraceOriginAutoAppStartProfile sampled:kSentrySampleDecisionYes]; SentryTracerConfiguration *config = [SentryTracerConfiguration defaultConfiguration]; NSDictionary *rates = appLaunchProfileConfiguration(); diff --git a/Sources/Sentry/include/SentryTraceOrigins.h b/Sources/Sentry/include/SentryTraceOrigins.h index 9be87b76a7..7922cdb9a7 100644 --- a/Sources/Sentry/include/SentryTraceOrigins.h +++ b/Sources/Sentry/include/SentryTraceOrigins.h @@ -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"; From 58a447b59c0b78dd37b5270bcc2af5ee89afc984 Mon Sep 17 00:00:00 2001 From: Andrew McKnight Date: Wed, 14 Feb 2024 18:59:32 +0800 Subject: [PATCH 2/2] add a test for transaction context origin/nameSource --- .../Sentry/Profiling/SentryLaunchProfiling.m | 48 ++++++++++++------- .../Sentry/include/SentryLaunchProfiling.h | 2 +- .../SentryAppLaunchProfilingTests.m | 12 +++++ .../SentryTests/SentryLaunchProfiling+Tests.h | 3 ++ 4 files changed, 48 insertions(+), 17 deletions(-) diff --git a/Sources/Sentry/Profiling/SentryLaunchProfiling.m b/Sources/Sentry/Profiling/SentryLaunchProfiling.m index 9925c57648..a0748cbbf5 100644 --- a/Sources/Sentry/Profiling/SentryLaunchProfiling.m +++ b/Sources/Sentry/Profiling/SentryLaunchProfiling.m @@ -107,6 +107,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) { @@ -127,27 +150,19 @@ [SentryLog configure:YES diagnosticLevel:kSentryLevelDebug]; # endif // defined(DEBUG) - SENTRY_LOG_INFO(@"Starting app launch profile."); - - SentryTransactionContext *context = - [[SentryTransactionContext alloc] initWithName:@"launch" - nameSource:kSentryTransactionNameSourceCustom - operation:@"app.lifecycle" - origin:SentryTraceOriginAutoAppStartProfile - sampled:kSentrySampleDecisionYes]; - SentryTracerConfiguration *config = [SentryTracerConfiguration defaultConfiguration]; NSDictionary *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)]; }); } @@ -156,6 +171,7 @@ { if (launchTracer == nil) { SENTRY_LOG_DEBUG(@"No launch tracer present to stop."); + return; } SENTRY_LOG_DEBUG(@"Finishing launch tracer."); diff --git a/Sources/Sentry/include/SentryLaunchProfiling.h b/Sources/Sentry/include/SentryLaunchProfiling.h index ce594eb15e..d7d9863439 100644 --- a/Sources/Sentry/include/SentryLaunchProfiling.h +++ b/Sources/Sentry/include/SentryLaunchProfiling.h @@ -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); diff --git a/Tests/SentryProfilerTests/SentryAppLaunchProfilingTests.m b/Tests/SentryProfilerTests/SentryAppLaunchProfilingTests.m index f622f9a0c9..85064c55ae 100644 --- a/Tests/SentryProfilerTests/SentryAppLaunchProfilingTests.m +++ b/Tests/SentryProfilerTests/SentryAppLaunchProfilingTests.m @@ -3,6 +3,8 @@ #import "SentryOptions+Private.h" #import "SentryProfilingConditionals.h" #import "SentrySDK+Tests.h" +#import "SentryTraceOrigins.h" +#import "SentryTransactionContext.h" #import #if SENTRY_TARGET_PROFILING_SUPPORTED @@ -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 @@ -146,6 +156,8 @@ - (void)testDisablingUIViewControllerTracingOptionDisablesAppLaunchProfiling } # endif // SENTRY_HAS_UIKIT +# pragma mark - Private + - (SentryOptions *)defaultLaunchProfilingOptionsWithOverrides: (NSDictionary *)overrides { diff --git a/Tests/SentryTests/SentryLaunchProfiling+Tests.h b/Tests/SentryTests/SentryLaunchProfiling+Tests.h index 92cbff552e..81f436c28d 100644 --- a/Tests/SentryTests/SentryLaunchProfiling+Tests.h +++ b/Tests/SentryTests/SentryLaunchProfiling+Tests.h @@ -17,4 +17,7 @@ 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