Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 38 additions & 24 deletions Sources/Sentry/Profiling/SentryProfiledTracerConcurrency.mm
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,12 @@
_unsafe_cleanUpContinuousProfilerV2();
} else if (internalTraceId != nil) {
# if !SDK_V9
if (sentry_isContinuousProfilingEnabled(hub.getClient)) {
SentryClient *_Nullable client = hub.getClient;
if (client == nil) {
SENTRY_LOG_ERROR(@"No client found, skipping cleanup.");
return;
}
if (sentry_isContinuousProfilingEnabled(SENTRY_UNWRAP_NULLABLE(SentryClient, client))) {
SENTRY_LOG_ERROR(@"Tracers are not tracked with continuous profiling V1.");
return;
}
Expand Down Expand Up @@ -256,14 +261,17 @@
{
if (sentry_profileConfiguration != nil && sentry_profileConfiguration.isProfilingThisLaunch
&& sentry_profileConfiguration.profileOptions != nil
&& sentry_isTraceLifecycle(sentry_profileConfiguration.profileOptions)) {
&& sentry_isTraceLifecycle(SENTRY_UNWRAP_NULLABLE(
SentryProfileOptions, sentry_profileConfiguration.profileOptions))) {
SENTRY_LOG_DEBUG(@"Stopping launch UI trace profile.");
sentry_stopTrackingRootSpanForContinuousProfilerV2();
return;
}

if (isProfiling && sentry_isContinuousProfilingV2Enabled(hub.getClient)
&& sentry_isProfilingCorrelatedToTraces(hub.getClient)) {
SentryClient *_Nullable client = hub.getClient;
if (isProfiling && client != nil
&& sentry_isContinuousProfilingV2Enabled(SENTRY_UNWRAP_NULLABLE(SentryClient, client))
&& sentry_isProfilingCorrelatedToTraces(SENTRY_UNWRAP_NULLABLE(SentryClient, client))) {
SENTRY_LOG_DEBUG(@"Stopping tracking root span tracer with profilerReferenceId %@",
sentry_stringFromSentryID(transaction.trace.profilerReferenceID));
sentry_stopTrackingRootSpanForContinuousProfilerV2();
Expand Down Expand Up @@ -309,11 +317,13 @@
[SentryTraceProfiler recordMetrics];
transaction.endSystemTime = sentry_getSystemTime();

const auto profiler = sentry_profilerForFinishedTracer(transaction.trace.profilerReferenceID);
if (!profiler) {
SentryProfiler *_Nullable nullableProfiler
= sentry_profilerForFinishedTracer(transaction.trace.profilerReferenceID);
if (!nullableProfiler) {
[hub captureTransaction:transaction withScope:hub.scope];
return;
}
SentryProfiler *_Nonnull profiler = SENTRY_UNWRAP_NULLABLE(SentryProfiler, nullableProfiler);

// This code can run on the main thread, and the profile serialization can take a couple of
// milliseconds. Therefore, we move this to a background thread to avoid potentially
Expand All @@ -329,7 +339,8 @@
} else {
[hub captureTransaction:transaction
withScope:hub.scope
additionalEnvelopeItems:@[ profileEnvelopeItem ]];
additionalEnvelopeItems:@[ SENTRY_UNWRAP_NULLABLE(
SentryEnvelopeItem, profileEnvelopeItem) ]];
}
});
}
Expand All @@ -342,35 +353,38 @@
// profile config file
return _sentry_startContinuousProfilerV2ForTrace(
sentry_profileConfiguration.profileOptions, transactionContext);
} else if (sentry_isContinuousProfilingV2Enabled(hub.getClient)) {
}
SentryClient *_Nullable client = hub.getClient;
if (client != nil
&& sentry_isContinuousProfilingV2Enabled(SENTRY_UNWRAP_NULLABLE(SentryClient, client))) {
// non launch profile
if (sentry_getParentSpanID(transactionContext) != nil) {
SENTRY_LOG_DEBUG(@"Not a root span, will not start automatically for trace lifecycle.");
return nil;
}
return _sentry_startContinuousProfilerV2ForTrace(
sentry_getProfiling(hub.getClient), transactionContext);
} else {
BOOL profileShouldBeSampled
= configuration.profilesSamplerDecision.decision == kSentrySampleDecisionYes;
sentry_getProfiling(SENTRY_UNWRAP_NULLABLE(SentryClient, client)), transactionContext);
}
BOOL profileShouldBeSampled
= configuration.profilesSamplerDecision.decision == kSentrySampleDecisionYes;
# if !SDK_V9
BOOL isContinuousProfiling = sentry_isContinuousProfilingEnabled(hub.client);
BOOL shouldStartNormalTraceProfile = !isContinuousProfiling && profileShouldBeSampled;
BOOL isContinuousProfiling = client != nil
&& sentry_isContinuousProfilingEnabled(SENTRY_UNWRAP_NULLABLE(SentryClient, client));
BOOL shouldStartNormalTraceProfile = !isContinuousProfiling && profileShouldBeSampled;
# else
BOOL shouldStartNormalTraceProfile = profileShouldBeSampled;
BOOL shouldStartNormalTraceProfile = profileShouldBeSampled;
# endif // !SDK_V9

if (sentry_isTracingAppLaunch || shouldStartNormalTraceProfile) {
SentryId *internalID = sentry_getSentryId();
if ([SentryTraceProfiler startWithTracer:internalID]) {
SENTRY_LOG_DEBUG(@"Started profiler for trace %@ with internal id %@",
sentry_stringFromSentryID(sentry_getTraceID(transactionContext)),
sentry_stringFromSentryID(internalID));
return internalID;
}
if (sentry_isTracingAppLaunch || shouldStartNormalTraceProfile) {
SentryId *internalID = sentry_getSentryId();
if ([SentryTraceProfiler startWithTracer:internalID]) {
SENTRY_LOG_DEBUG(@"Started profiler for trace %@ with internal id %@",
sentry_stringFromSentryID(sentry_getTraceID(transactionContext)),
sentry_stringFromSentryID(internalID));
return internalID;
}
return nil;
}
return nil;
}

# if defined(SENTRY_TEST) || defined(SENTRY_TEST_CI) || defined(DEBUG)
Expand Down
21 changes: 12 additions & 9 deletions Sources/Sentry/SentryProfiler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@
return;
}

options.profiling = sentry_getSentryProfileOptions();
options.configureProfiling(options.profiling);
SentryProfileOptions *_Nonnull profilingOptions = sentry_getSentryProfileOptions();
options.profiling = profilingOptions;
options.configureProfiling(profilingOptions);

if (sentry_isTraceLifecycle(options.profiling) && !options.isTracingEnabled) {
if (sentry_isTraceLifecycle(profilingOptions) && !options.isTracingEnabled) {
SENTRY_LOG_WARN(
@"Tracing must be enabled in order to configure profiling with trace lifecycle.");
return;
Expand All @@ -101,16 +102,16 @@
// the profile session sample rate henceforth
if (sentry_profileConfiguration == nil) {
sentry_profileConfiguration =
[[SentryProfileConfiguration alloc] initWithProfileOptions:options.profiling];
[[SentryProfileConfiguration alloc] initWithProfileOptions:profilingOptions];
}

sentry_reevaluateSessionSampleRate();

SENTRY_LOG_DEBUG(@"Configured profiling options: <%@: {\n lifecycle: %@\n sessionSampleRate: "
@"%.2f\n profileAppStarts: %@\n}",
options.profiling, sentry_isTraceLifecycle(options.profiling) ? @"trace" : @"manual",
sentry_sessionSampleRate(options.profiling),
sentry_profileAppStarts(options.profiling) ? @"YES" : @"NO");
options.profiling, sentry_isTraceLifecycle(profilingOptions) ? @"trace" : @"manual",
sentry_sessionSampleRate(profilingOptions),
sentry_profileAppStarts(profilingOptions) ? @"YES" : @"NO");
}

void
Expand All @@ -128,11 +129,13 @@

const auto profileIsContinuousV2 = configurationFromLaunch.profileOptions != nil;
const auto v2LifecycleIsManual = profileIsContinuousV2
&& !sentry_isTraceLifecycle(configurationFromLaunch.profileOptions);
&& !sentry_isTraceLifecycle(SENTRY_UNWRAP_NULLABLE(
SentryProfileOptions, configurationFromLaunch.profileOptions));

# if SENTRY_HAS_UIKIT
const auto v2LifecycleIsTrace = profileIsContinuousV2
&& sentry_isTraceLifecycle(configurationFromLaunch.profileOptions);
&& sentry_isTraceLifecycle(SENTRY_UNWRAP_NULLABLE(
SentryProfileOptions, configurationFromLaunch.profileOptions));
const auto profileIsCorrelatedToTrace = !profileIsContinuousV2 || v2LifecycleIsTrace;
if (profileIsCorrelatedToTrace && configurationFromLaunch.waitForFullDisplay) {
SENTRY_LOG_DEBUG(
Expand Down
Loading