Skip to content

Commit 6faf565

Browse files
authored
refactor: Add null-handling to profiler (#5887)
1 parent 0822ad3 commit 6faf565

File tree

2 files changed

+50
-33
lines changed

2 files changed

+50
-33
lines changed

Sources/Sentry/Profiling/SentryProfiledTracerConcurrency.mm

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,12 @@
178178
_unsafe_cleanUpContinuousProfilerV2();
179179
} else if (internalTraceId != nil) {
180180
# if !SDK_V9
181-
if (sentry_isContinuousProfilingEnabled(hub.getClient)) {
181+
SentryClient *_Nullable client = hub.getClient;
182+
if (client == nil) {
183+
SENTRY_LOG_ERROR(@"No client found, skipping cleanup.");
184+
return;
185+
}
186+
if (sentry_isContinuousProfilingEnabled(SENTRY_UNWRAP_NULLABLE(SentryClient, client))) {
182187
SENTRY_LOG_ERROR(@"Tracers are not tracked with continuous profiling V1.");
183188
return;
184189
}
@@ -256,14 +261,17 @@
256261
{
257262
if (sentry_profileConfiguration != nil && sentry_profileConfiguration.isProfilingThisLaunch
258263
&& sentry_profileConfiguration.profileOptions != nil
259-
&& sentry_isTraceLifecycle(sentry_profileConfiguration.profileOptions)) {
264+
&& sentry_isTraceLifecycle(SENTRY_UNWRAP_NULLABLE(
265+
SentryProfileOptions, sentry_profileConfiguration.profileOptions))) {
260266
SENTRY_LOG_DEBUG(@"Stopping launch UI trace profile.");
261267
sentry_stopTrackingRootSpanForContinuousProfilerV2();
262268
return;
263269
}
264270

265-
if (isProfiling && sentry_isContinuousProfilingV2Enabled(hub.getClient)
266-
&& sentry_isProfilingCorrelatedToTraces(hub.getClient)) {
271+
SentryClient *_Nullable client = hub.getClient;
272+
if (isProfiling && client != nil
273+
&& sentry_isContinuousProfilingV2Enabled(SENTRY_UNWRAP_NULLABLE(SentryClient, client))
274+
&& sentry_isProfilingCorrelatedToTraces(SENTRY_UNWRAP_NULLABLE(SentryClient, client))) {
267275
SENTRY_LOG_DEBUG(@"Stopping tracking root span tracer with profilerReferenceId %@",
268276
sentry_stringFromSentryID(transaction.trace.profilerReferenceID));
269277
sentry_stopTrackingRootSpanForContinuousProfilerV2();
@@ -309,11 +317,13 @@
309317
[SentryTraceProfiler recordMetrics];
310318
transaction.endSystemTime = sentry_getSystemTime();
311319

312-
const auto profiler = sentry_profilerForFinishedTracer(transaction.trace.profilerReferenceID);
313-
if (!profiler) {
320+
SentryProfiler *_Nullable nullableProfiler
321+
= sentry_profilerForFinishedTracer(transaction.trace.profilerReferenceID);
322+
if (!nullableProfiler) {
314323
[hub captureTransaction:transaction withScope:hub.scope];
315324
return;
316325
}
326+
SentryProfiler *_Nonnull profiler = SENTRY_UNWRAP_NULLABLE(SentryProfiler, nullableProfiler);
317327

318328
// This code can run on the main thread, and the profile serialization can take a couple of
319329
// milliseconds. Therefore, we move this to a background thread to avoid potentially
@@ -329,7 +339,8 @@
329339
} else {
330340
[hub captureTransaction:transaction
331341
withScope:hub.scope
332-
additionalEnvelopeItems:@[ profileEnvelopeItem ]];
342+
additionalEnvelopeItems:@[ SENTRY_UNWRAP_NULLABLE(
343+
SentryEnvelopeItem, profileEnvelopeItem) ]];
333344
}
334345
});
335346
}
@@ -342,35 +353,38 @@
342353
// profile config file
343354
return _sentry_startContinuousProfilerV2ForTrace(
344355
sentry_profileConfiguration.profileOptions, transactionContext);
345-
} else if (sentry_isContinuousProfilingV2Enabled(hub.getClient)) {
356+
}
357+
SentryClient *_Nullable client = hub.getClient;
358+
if (client != nil
359+
&& sentry_isContinuousProfilingV2Enabled(SENTRY_UNWRAP_NULLABLE(SentryClient, client))) {
346360
// non launch profile
347361
if (sentry_getParentSpanID(transactionContext) != nil) {
348362
SENTRY_LOG_DEBUG(@"Not a root span, will not start automatically for trace lifecycle.");
349363
return nil;
350364
}
351365
return _sentry_startContinuousProfilerV2ForTrace(
352-
sentry_getProfiling(hub.getClient), transactionContext);
353-
} else {
354-
BOOL profileShouldBeSampled
355-
= configuration.profilesSamplerDecision.decision == kSentrySampleDecisionYes;
366+
sentry_getProfiling(SENTRY_UNWRAP_NULLABLE(SentryClient, client)), transactionContext);
367+
}
368+
BOOL profileShouldBeSampled
369+
= configuration.profilesSamplerDecision.decision == kSentrySampleDecisionYes;
356370
# if !SDK_V9
357-
BOOL isContinuousProfiling = sentry_isContinuousProfilingEnabled(hub.client);
358-
BOOL shouldStartNormalTraceProfile = !isContinuousProfiling && profileShouldBeSampled;
371+
BOOL isContinuousProfiling = client != nil
372+
&& sentry_isContinuousProfilingEnabled(SENTRY_UNWRAP_NULLABLE(SentryClient, client));
373+
BOOL shouldStartNormalTraceProfile = !isContinuousProfiling && profileShouldBeSampled;
359374
# else
360-
BOOL shouldStartNormalTraceProfile = profileShouldBeSampled;
375+
BOOL shouldStartNormalTraceProfile = profileShouldBeSampled;
361376
# endif // !SDK_V9
362377

363-
if (sentry_isTracingAppLaunch || shouldStartNormalTraceProfile) {
364-
SentryId *internalID = sentry_getSentryId();
365-
if ([SentryTraceProfiler startWithTracer:internalID]) {
366-
SENTRY_LOG_DEBUG(@"Started profiler for trace %@ with internal id %@",
367-
sentry_stringFromSentryID(sentry_getTraceID(transactionContext)),
368-
sentry_stringFromSentryID(internalID));
369-
return internalID;
370-
}
378+
if (sentry_isTracingAppLaunch || shouldStartNormalTraceProfile) {
379+
SentryId *internalID = sentry_getSentryId();
380+
if ([SentryTraceProfiler startWithTracer:internalID]) {
381+
SENTRY_LOG_DEBUG(@"Started profiler for trace %@ with internal id %@",
382+
sentry_stringFromSentryID(sentry_getTraceID(transactionContext)),
383+
sentry_stringFromSentryID(internalID));
384+
return internalID;
371385
}
372-
return nil;
373386
}
387+
return nil;
374388
}
375389

376390
# if defined(SENTRY_TEST) || defined(SENTRY_TEST_CI) || defined(DEBUG)

Sources/Sentry/SentryProfiler.mm

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,11 @@
8585
return;
8686
}
8787

88-
options.profiling = sentry_getSentryProfileOptions();
89-
options.configureProfiling(options.profiling);
88+
SentryProfileOptions *_Nonnull profilingOptions = sentry_getSentryProfileOptions();
89+
options.profiling = profilingOptions;
90+
options.configureProfiling(profilingOptions);
9091

91-
if (sentry_isTraceLifecycle(options.profiling) && !options.isTracingEnabled) {
92+
if (sentry_isTraceLifecycle(profilingOptions) && !options.isTracingEnabled) {
9293
SENTRY_LOG_WARN(
9394
@"Tracing must be enabled in order to configure profiling with trace lifecycle.");
9495
return;
@@ -101,16 +102,16 @@
101102
// the profile session sample rate henceforth
102103
if (sentry_profileConfiguration == nil) {
103104
sentry_profileConfiguration =
104-
[[SentryProfileConfiguration alloc] initWithProfileOptions:options.profiling];
105+
[[SentryProfileConfiguration alloc] initWithProfileOptions:profilingOptions];
105106
}
106107

107108
sentry_reevaluateSessionSampleRate();
108109

109110
SENTRY_LOG_DEBUG(@"Configured profiling options: <%@: {\n lifecycle: %@\n sessionSampleRate: "
110111
@"%.2f\n profileAppStarts: %@\n}",
111-
options.profiling, sentry_isTraceLifecycle(options.profiling) ? @"trace" : @"manual",
112-
sentry_sessionSampleRate(options.profiling),
113-
sentry_profileAppStarts(options.profiling) ? @"YES" : @"NO");
112+
options.profiling, sentry_isTraceLifecycle(profilingOptions) ? @"trace" : @"manual",
113+
sentry_sessionSampleRate(profilingOptions),
114+
sentry_profileAppStarts(profilingOptions) ? @"YES" : @"NO");
114115
}
115116

116117
void
@@ -128,11 +129,13 @@
128129

129130
const auto profileIsContinuousV2 = configurationFromLaunch.profileOptions != nil;
130131
const auto v2LifecycleIsManual = profileIsContinuousV2
131-
&& !sentry_isTraceLifecycle(configurationFromLaunch.profileOptions);
132+
&& !sentry_isTraceLifecycle(SENTRY_UNWRAP_NULLABLE(
133+
SentryProfileOptions, configurationFromLaunch.profileOptions));
132134

133135
# if SENTRY_HAS_UIKIT
134136
const auto v2LifecycleIsTrace = profileIsContinuousV2
135-
&& sentry_isTraceLifecycle(configurationFromLaunch.profileOptions);
137+
&& sentry_isTraceLifecycle(SENTRY_UNWRAP_NULLABLE(
138+
SentryProfileOptions, configurationFromLaunch.profileOptions));
136139
const auto profileIsCorrelatedToTrace = !profileIsContinuousV2 || v2LifecycleIsTrace;
137140
if (profileIsCorrelatedToTrace && configurationFromLaunch.waitForFullDisplay) {
138141
SENTRY_LOG_DEBUG(

0 commit comments

Comments
 (0)