Skip to content

Commit

Permalink
fix(profiling): prefix some functions with "sentry_" (#3862)
Browse files Browse the repository at this point in the history
  • Loading branch information
armcknight authored Apr 19, 2024
1 parent f801098 commit 94e1968
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 84 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

- Crash due to a background call to -[UIApplication applicationState] (#3855)
- Save framework without UIKit/AppKit as Github Asset for releases (#3858)
- Fix crash associated with runtime collision in global C function names (#3862)

## 8.24.0

Expand Down
3 changes: 1 addition & 2 deletions Sentry.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1857,9 +1857,9 @@
D8751FA4274743710032F4DE /* SentryNSURLSessionTaskSearchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryNSURLSessionTaskSearchTests.swift; sourceTree = "<group>"; };
D8757D142A209F7300BFEFCC /* SentrySampleDecision+Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "SentrySampleDecision+Private.h"; path = "include/SentrySampleDecision+Private.h"; sourceTree = "<group>"; };
D875ED0A276CC84700422FAC /* SentryNSDataTrackerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SentryNSDataTrackerTests.swift; sourceTree = "<group>"; };
D878C6C02BC8048A0039D6A3 /* SentryPrivate.podspec */ = {isa = PBXFileReference; explicitFileType = text.script.ruby; path = SentryPrivate.podspec; sourceTree = "<group>"; };
D87C89022BC43C9C0086C7DF /* SentryRedactOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryRedactOptions.swift; sourceTree = "<group>"; };
D87C892A2BC67BC20086C7DF /* SentryExperimentalOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryExperimentalOptions.swift; sourceTree = "<group>"; };
D878C6C02BC8048A0039D6A3 /* SentryPrivate.podspec */ = {isa = PBXFileReference; explicitFileType = text.script.ruby; path = SentryPrivate.podspec; sourceTree = "<group>"; };
D880E3A628573E87008A90DB /* SentryBaggageTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryBaggageTests.swift; sourceTree = "<group>"; };
D880E3B02860A5A0008A90DB /* SentryEvent+Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "SentryEvent+Private.h"; path = "include/SentryEvent+Private.h"; sourceTree = "<group>"; };
D884A20327C80F2700074664 /* SentryCoreDataTrackerTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryCoreDataTrackerTest.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -4325,7 +4325,6 @@
7BCFBD6F2681D0EE00BC27D8 /* SentryCrashScopeObserver.m in Sources */,
7BD86ED1264A7CF6005439DB /* SentryAppStartMeasurement.m in Sources */,
7DC27EC723997EB7006998B5 /* SentryAutoBreadcrumbTrackingIntegration.m in Sources */,
D820CE142BB2F13C00BA339D /* SentryCoreGraphicsHelper.m in Sources */,
63FE717B20DA4C1100CDBAE8 /* SentryCrashReport.c in Sources */,
7B7A599726B692F00060A676 /* SentryScreenFrames.m in Sources */,
7B3398652459C15200BD9C96 /* SentryEnvelopeRateLimit.m in Sources */,
Expand Down
96 changes: 51 additions & 45 deletions Sources/Sentry/Profiling/SentryLaunchProfiling.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# import "SentryDispatchQueueWrapper.h"
# import "SentryFileManager.h"
# import "SentryInternalDefines.h"
# import "SentryLaunchProfiling.h"
# import "SentryLog.h"
# import "SentryOptions.h"
# import "SentryProfiler+Private.h"
Expand All @@ -22,21 +23,33 @@

NS_ASSUME_NONNULL_BEGIN

BOOL isTracingAppLaunch;
NSString *const kSentryLaunchProfileConfigKeyTracesSampleRate = @"traces";
NSString *const kSentryLaunchProfileConfigKeyProfilesSampleRate = @"profiles";
static SentryTracer *_Nullable launchTracer;

# pragma mark - Private

static SentryTracer *_Nullable sentry_launchTracer;

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

# pragma mark - Package

typedef struct {
BOOL shouldProfile;
SentrySamplerDecision *_Nullable tracesDecision;
SentrySamplerDecision *_Nullable profilesDecision;
} SentryLaunchProfileConfig;

NSString *const kSentryLaunchProfileConfigKeyTracesSampleRate = @"traces";
NSString *const kSentryLaunchProfileConfigKeyProfilesSampleRate = @"profiles";

SentryLaunchProfileConfig
shouldProfileNextLaunch(SentryOptions *options)
sentry_shouldProfileNextLaunch(SentryOptions *options)
{
BOOL shouldProfileNextLaunch = options.enableAppLaunchProfiling && options.enableTracing;
if (!shouldProfileNextLaunch) {
Expand All @@ -52,14 +65,14 @@
SentrySamplingContext *context =
[[SentrySamplingContext alloc] initWithTransactionContext:transactionContext];

SentrySamplerDecision *tracesSamplerDecision = sampleTrace(context, options);
SentrySamplerDecision *tracesSamplerDecision = sentry_sampleTrace(context, options);
if (tracesSamplerDecision.decision != kSentrySampleDecisionYes) {
SENTRY_LOG_DEBUG(@"Sampling out the launch trace.");
return (SentryLaunchProfileConfig) { NO, nil, nil };
}

SentrySamplerDecision *profilesSamplerDecision
= sampleProfile(context, tracesSamplerDecision, options);
= sentry_sampleProfile(context, tracesSamplerDecision, options);
if (profilesSamplerDecision.decision != kSentrySampleDecisionYes) {
SENTRY_LOG_DEBUG(@"Sampling out the launch profile.");
return (SentryLaunchProfileConfig) { NO, nil, nil };
Expand All @@ -69,13 +82,28 @@
return (SentryLaunchProfileConfig) { YES, tracesSamplerDecision, profilesSamplerDecision };
}

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

# pragma mark - Public

BOOL sentry_isTracingAppLaunch;

void
configureLaunchProfiling(SentryOptions *options)
sentry_configureLaunchProfiling(SentryOptions *options)
{
[SentryDependencyContainer.sharedInstance.dispatchQueueWrapper dispatchAsyncWithBlock:^{
SentryLaunchProfileConfig config = shouldProfileNextLaunch(options);
SentryLaunchProfileConfig config = sentry_shouldProfileNextLaunch(options);
if (!config.shouldProfile) {
removeAppLaunchProfilingConfigFile();
return;
Expand All @@ -91,40 +119,17 @@
}];
}

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)
sentry_startLaunchProfile(void)
{

static dispatch_once_t onceToken;
// this function is called from SentryTracer.load but in the future we may expose access
// directly to customers, and we'll want to ensure it only runs once. dispatch_once is an
// efficient operation so it's fine to leave this in the launch path in any case.
dispatch_once(&onceToken, ^{
isTracingAppLaunch = appLaunchProfileConfigFileExists();
if (!isTracingAppLaunch) {
sentry_isTracingAppLaunch = appLaunchProfileConfigFileExists();
if (!sentry_isTracingAppLaunch) {
return;
}

Expand All @@ -145,29 +150,30 @@
}

SENTRY_LOG_INFO(@"Starting app launch profile at %llu.", getAbsoluteTime());
launchTracer = [[SentryTracer alloc] initWithTransactionContext:context(tracesRate)
hub:nil
configuration:config(profilesRate)];
sentry_launchTracer =
[[SentryTracer alloc] initWithTransactionContext:sentry_context(tracesRate)
hub:nil
configuration:sentry_config(profilesRate)];
});
}

void
stopAndTransmitLaunchProfile(SentryHub *hub)
sentry_stopAndTransmitLaunchProfile(SentryHub *hub)
{
if (launchTracer == nil) {
if (sentry_launchTracer == nil) {
SENTRY_LOG_DEBUG(@"No launch tracer present to stop.");
return;
}

launchTracer.hub = hub;
stopAndDiscardLaunchProfileTracer();
sentry_launchTracer.hub = hub;
sentry_stopAndDiscardLaunchProfileTracer();
}

void
stopAndDiscardLaunchProfileTracer(void)
sentry_stopAndDiscardLaunchProfileTracer(void)
{
SENTRY_LOG_DEBUG(@"Finishing launch tracer.");
[launchTracer finish];
[sentry_launchTracer finish];
}

NS_ASSUME_NONNULL_END
Expand Down
4 changes: 2 additions & 2 deletions Sources/Sentry/SentryHub.m
Original file line number Diff line number Diff line change
Expand Up @@ -400,14 +400,14 @@ - (SentryTracer *)startTransactionWithContext:(SentryTransactionContext *)transa
customSamplingContext:customSamplingContext];

SentrySamplerDecision *tracesSamplerDecision
= sampleTrace(samplingContext, self.client.options);
= sentry_sampleTrace(samplingContext, self.client.options);
transactionContext = [self transactionContext:transactionContext
withSampled:tracesSamplerDecision.decision];
transactionContext.sampleRate = tracesSamplerDecision.sampleRate;

#if SENTRY_TARGET_PROFILING_SUPPORTED
SentrySamplerDecision *profilesSamplerDecision
= sampleProfile(samplingContext, tracesSamplerDecision, self.client.options);
= sentry_sampleProfile(samplingContext, tracesSamplerDecision, self.client.options);

configuration.profilesSamplerDecision = profilesSamplerDecision;
#endif // SENTRY_TARGET_PROFILING_SUPPORTED"
Expand Down
6 changes: 3 additions & 3 deletions Sources/Sentry/SentryProfiler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ + (nullable SentryEnvelopeItem *)createEnvelopeItemForProfilePayload:
}

NSString *pathToWrite;
if (isTracingAppLaunch) {
if (sentry_isTracingAppLaunch) {
SENTRY_LOG_DEBUG(@"Writing app launch profile.");
pathToWrite = [appSupportDirPath stringByAppendingPathComponent:@"launchProfile"];
} else {
Expand All @@ -448,11 +448,11 @@ + (nullable SentryEnvelopeItem *)createEnvelopeItemForProfilePayload:
SENTRY_LOG_DEBUG(@"Already a %@ profile file present; make sure to remove them right after "
@"using them, and that tests clean state in between so there isn't "
@"leftover config producing one when it isn't expected.",
isTracingAppLaunch ? @" launch" : @"");
sentry_isTracingAppLaunch ? @" launch" : @"");
return;
}

SENTRY_LOG_DEBUG(@"Writing%@ profile to file.", isTracingAppLaunch ? @" launch" : @"");
SENTRY_LOG_DEBUG(@"Writing%@ profile to file.", sentry_isTracingAppLaunch ? @" launch" : @"");

NSError *error;
if (![data writeToFile:pathToWrite options:NSDataWritingAtomic error:&error]) {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Sentry/SentrySDK.m
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@ + (void)startWithOptions:(SentryOptions *)options
if (shouldstopAndTransmitLaunchProfile) {
SENTRY_LOG_DEBUG(@"Stopping launch profile in SentrySDK.start because there will "
@"be no automatic trace to attach it to.");
stopAndTransmitLaunchProfile(hub);
sentry_stopAndTransmitLaunchProfile(hub);
}
configureLaunchProfiling(options);
sentry_configureLaunchProfiling(options);
}];
#endif // SENTRY_TARGET_PROFILING_SUPPORTED
}];
Expand Down
4 changes: 2 additions & 2 deletions Sources/Sentry/SentrySampling.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
#pragma mark - Public

SentrySamplerDecision *
sampleTrace(SentrySamplingContext *context, SentryOptions *options)
sentry_sampleTrace(SentrySamplingContext *context, SentryOptions *options)
{
// check this transaction's sampling decision, if already decided
if (context.transactionContext.sampled != kSentrySampleDecisionUndecided) {
Expand Down Expand Up @@ -83,7 +83,7 @@
#if SENTRY_TARGET_PROFILING_SUPPORTED

SentrySamplerDecision *
sampleProfile(SentrySamplingContext *context, SentrySamplerDecision *tracesSamplerDecision,
sentry_sampleProfile(SentrySamplingContext *context, SentrySamplerDecision *tracesSamplerDecision,
SentryOptions *options)
{
// Profiles are always undersampled with respect to traces. If the trace is not sampled,
Expand Down
4 changes: 2 additions & 2 deletions Sources/Sentry/SentryTimeToDisplayTracker.m
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ - (void)framesTrackerHasNewFrame:(NSDate *)newFrameDate
if (!_waitForFullDisplay) {
[SentryDependencyContainer.sharedInstance.framesTracker removeListener:self];
# if SENTRY_TARGET_PROFILING_SUPPORTED
stopAndDiscardLaunchProfileTracer();
sentry_stopAndDiscardLaunchProfileTracer();
# endif // SENTRY_TARGET_PROFILING_SUPPORTED
}
}
Expand All @@ -144,7 +144,7 @@ - (void)framesTrackerHasNewFrame:(NSDate *)newFrameDate
self.fullDisplaySpan.timestamp = newFrameDate;
[self.fullDisplaySpan finish];
# if SENTRY_TARGET_PROFILING_SUPPORTED
stopAndDiscardLaunchProfileTracer();
sentry_stopAndDiscardLaunchProfileTracer();
# endif // SENTRY_TARGET_PROFILING_SUPPORTED
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/Sentry/SentryTracer.m
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ @implementation SentryTracer {
#if SENTRY_TARGET_PROFILING_SUPPORTED
+ (void)load
{
startLaunchProfile();
sentry_startLaunchProfile();
}
#endif // SENTRY_TARGET_PROFILING_SUPPORTED

Expand Down Expand Up @@ -190,7 +190,7 @@ - (instancetype)initWithTransactionContext:(SentryTransactionContext *)transacti

#if SENTRY_TARGET_PROFILING_SUPPORTED
if (_configuration.profilesSamplerDecision.decision == kSentrySampleDecisionYes
|| isTracingAppLaunch) {
|| sentry_isTracingAppLaunch) {
_internalID = [[SentryId alloc] init];
if ((_isProfiling = [SentryProfiler startWithTracer:_internalID])) {
SENTRY_LOG_DEBUG(@"Started profiler for trace %@ with internal id %@",
Expand Down
15 changes: 8 additions & 7 deletions Sources/Sentry/include/SentryLaunchProfiling.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#import "SentryDefines.h"
#import "SentryProfilingConditionals.h"
#import <Foundation/Foundation.h>

#if SENTRY_TARGET_PROFILING_SUPPORTED

# import "SentryDefines.h"
# import <Foundation/Foundation.h>

@class SentryHub;
@class SentryId;
@class SentryOptions;
Expand All @@ -12,31 +13,31 @@

NS_ASSUME_NONNULL_BEGIN

SENTRY_EXTERN BOOL isTracingAppLaunch;
SENTRY_EXTERN BOOL sentry_isTracingAppLaunch;

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

/**
* Stop any profiled trace that may be in flight from the start of the app launch, and transmit the
* dedicated transaction with the profiling data attached.
*/
void stopAndTransmitLaunchProfile(SentryHub *hub);
void sentry_stopAndTransmitLaunchProfile(SentryHub *hub);

/**
* Stop the tracer that started the launch profiler. Use when the profiler will be attached to an
* app start transaction and doesn't need to be attached to a dedicated tracer. The tracer managing
* the profiler will be discarded in this case.
*/
void stopAndDiscardLaunchProfileTracer(void);
void sentry_stopAndDiscardLaunchProfileTracer(void);

/**
* Write a file to disk containing sample rates for profiles and traces. The presence of this file
* will let the profiler know to start on the app launch, and the sample rates contained will help
* thread sampling decisions through to SentryHub later when it needs to start a transaction for the
* profile to be attached to.
*/
void configureLaunchProfiling(SentryOptions *options);
void sentry_configureLaunchProfiling(SentryOptions *options);

NS_ASSUME_NONNULL_END

Expand Down
6 changes: 4 additions & 2 deletions Sources/Sentry/include/SentrySampling.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#import "SentryDefines.h"
#import "SentryProfilingConditionals.h"
#import <Foundation/Foundation.h>

Expand All @@ -10,14 +11,15 @@ NS_ASSUME_NONNULL_BEGIN
/**
* Determines whether a trace should be sampled based on the context and options.
*/
SentrySamplerDecision *sampleTrace(SentrySamplingContext *context, SentryOptions *options);
SENTRY_EXTERN SentrySamplerDecision *sentry_sampleTrace(
SentrySamplingContext *context, SentryOptions *options);

#if SENTRY_TARGET_PROFILING_SUPPORTED
/**
* Determines whether a profile should be sampled based on the context, options, and
* whether the trace corresponding to the profile was sampled.
*/
SentrySamplerDecision *sampleProfile(SentrySamplingContext *context,
SENTRY_EXTERN SentrySamplerDecision *sentry_sampleProfile(SentrySamplingContext *context,
SentrySamplerDecision *tracesSamplerDecision, SentryOptions *options);
#endif // SENTRY_TARGET_PROFILING_SUPPORTED

Expand Down
Loading

0 comments on commit 94e1968

Please sign in to comment.