Skip to content

Commit 4a2c1a8

Browse files
committed
wip
1 parent 4bd3e25 commit 4a2c1a8

File tree

2 files changed

+53
-18
lines changed

2 files changed

+53
-18
lines changed

src/State/Hub.php

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -323,16 +323,7 @@ public function startTransaction(TransactionContext $context, array $customSampl
323323

324324
$transaction->initSpanRecorder();
325325

326-
$profilesSampleRate = $options->getProfilesSampleRate();
327-
if ($profilesSampleRate === null) {
328-
$logger->info(\sprintf('Transaction [%s] is not profiling because `profiles_sample_rate` option is not set.', (string) $transaction->getTraceId()));
329-
} elseif ($this->sample($profilesSampleRate)) {
330-
$logger->info(\sprintf('Transaction [%s] started profiling because it was sampled.', (string) $transaction->getTraceId()));
331-
332-
$transaction->initProfiler()->start();
333-
} else {
334-
$logger->info(\sprintf('Transaction [%s] is not profiling because it was not sampled.', (string) $transaction->getTraceId()));
335-
}
326+
$this->initProfiler($transaction, $options, $logger);
336327

337328
return $transaction;
338329
}
@@ -423,4 +414,34 @@ private function isValidSampleRate($sampleRate): bool
423414

424415
return true;
425416
}
417+
418+
private function initProfiler(Transaction $transaction, $options, $logger): void
419+
{
420+
$profileSessionSampleRate = $options->getProfilesSessionSampleRate();
421+
$profilesLifecycle = $options->getProfilesLifecycle();
422+
423+
// profileSessionSampleRate takes priority
424+
if ($profilesLifecycle === 'trace' && $profileSessionSampleRate !== null) {
425+
if ($this->sample($profileSessionSampleRate)) {
426+
$logger->info(\sprintf('Transaction [%s] started profiling because it was sampled by profiles_session_sample_rate.', (string) $transaction->getTraceId()));
427+
$transaction->initProfiler()->start();
428+
} else {
429+
$logger->info(\sprintf('Transaction [%s] is not profiling because it was not sampled by profiles_session_sample_rate.', (string) $transaction->getTraceId()));
430+
}
431+
432+
return;
433+
}
434+
435+
// Fall back to profilesSampleRate
436+
$profilesSampleRate = $options->getProfilesSampleRate();
437+
438+
if ($profilesSampleRate === null) {
439+
$logger->info(\sprintf('Transaction [%s] is not profiling because `profiles_sample_rate` option is not set.', (string) $transaction->getTraceId()));
440+
} elseif ($this->sample($profilesSampleRate)) {
441+
$logger->info(\sprintf('Transaction [%s] started profiling because it was sampled.', (string) $transaction->getTraceId()));
442+
$transaction->initProfiler()->start();
443+
} else {
444+
$logger->info(\sprintf('Transaction [%s] is not profiling because it was not sampled.', (string) $transaction->getTraceId()));
445+
}
446+
}
426447
}

src/Tracing/Transaction.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Sentry\Event;
88
use Sentry\EventId;
99
use Sentry\Profiles\ProfileChunk;
10-
use Sentry\Profiles\Profiles;
10+
use Sentry\Profiles\Profiler as ContinuousProfiler;
1111
use Sentry\Profiling\Profiler;
1212
use Sentry\SentrySdk;
1313
use Sentry\State\HubInterface;
@@ -38,7 +38,7 @@ final class Transaction extends Span
3838
protected $metadata;
3939

4040
/**
41-
* @var Profiler|null Reference instance to the {@see Profiler}
41+
* @var Profiler|ContinuousProfiler|null Reference instance to the {@see Profiler or @see ContinuousProfiler}
4242
*/
4343
protected $profiler;
4444

@@ -121,19 +121,31 @@ public function initSpanRecorder(int $maxSpans = 1000): self
121121
return $this;
122122
}
123123

124-
public function initProfiler(): Profiler
124+
/**
125+
* @return Profiler|ContinuousProfiler|null
126+
*/
127+
public function initProfiler()
125128
{
126129
if ($this->profiler === null) {
127130
$client = $this->hub->getClient();
128131
$options = $client !== null ? $client->getOptions() : null;
129132

130-
$this->profiler = new Profiler($options);
133+
if ($options !== null) {
134+
if ($options->getProfilesLifecycle() === 'trace' && $options->getProfilesSessionSampleRate() !== null) {
135+
$this->profiler = new ContinuousProfiler($options);
136+
} else {
137+
$this->profiler = new Profiler($options);
138+
}
139+
}
131140
}
132141

133142
return $this->profiler;
134143
}
135144

136-
public function getProfiler(): ?Profiler
145+
/**
146+
* @return Profiler|ContinuousProfiler|null
147+
*/
148+
public function getProfiler()
137149
{
138150
return $this->profiler;
139151
}
@@ -185,16 +197,18 @@ public function finish(?float $endTimestamp = null): ?EventId
185197
$event->setSdkMetadata('dynamic_sampling_context', $this->getDynamicSamplingContext());
186198
$event->setSdkMetadata('transaction_metadata', $this->getMetadata());
187199

188-
if ($this->profiler !== null) {
200+
// Legacy Profiler
201+
if ($this->profiler !== null && $this->profiler instanceof Profiler) {
189202
$profile = $this->profiler->getProfile();
190203
if ($profile !== null) {
191204
$event->setSdkMetadata('profile', $profile);
192205
}
193206
}
194207

195-
if (Profiles::getInstance()->getProfiler()->getProfilerId() !== null) {
208+
// Continuous Profiler
209+
if ($this->profiler !== null && $this->profiler instanceof ContinuousProfiler) {
196210
$event->setContext('profile', [
197-
'profiler_id' => Profiles::getInstance()->getProfiler()->getProfilerId(),
211+
'profiler_id' => $this->profiler->getProfilerId(),
198212
]);
199213

200214
$traceContext = $this->getTraceContext();

0 commit comments

Comments
 (0)