Skip to content
Open
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
25 changes: 14 additions & 11 deletions src/State/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -576,18 +576,21 @@ public function applyToEvent(Event $event, ?EventHint $hint = null, ?Options $op
$event->setContext('trace', $traceContext);
}

if ($this->span !== null) {
// Apply the dynamic sampling context to errors if there is a Transaction on the Scope
$transaction = $this->span->getTransaction();
if ($transaction !== null) {
$event->setSdkMetadata('dynamic_sampling_context', $transaction->getDynamicSamplingContext());
}
} elseif ($externalPropagationContext === null) {
$dynamicSamplingContext = $this->propagationContext->getDynamicSamplingContext();
if ($dynamicSamplingContext === null && $options !== null) {
$dynamicSamplingContext = DynamicSamplingContext::fromOptions($options, $this);
// Do not override a dynamic sampling context already present, a transaction sets its own when it finishes
if ($event->getSdkMetadata('dynamic_sampling_context') === null) {
if ($this->span !== null) {
// Apply the dynamic sampling context to errors if there is a Transaction on the Scope
$transaction = $this->span->getTransaction();
if ($transaction !== null) {
$event->setSdkMetadata('dynamic_sampling_context', $transaction->getDynamicSamplingContext());
}
} elseif ($externalPropagationContext === null) {
$dynamicSamplingContext = $this->propagationContext->getDynamicSamplingContext();
if ($dynamicSamplingContext === null && $options !== null) {
$dynamicSamplingContext = DynamicSamplingContext::fromOptions($options, $this);
}
$event->setSdkMetadata('dynamic_sampling_context', $dynamicSamplingContext);
}
$event->setSdkMetadata('dynamic_sampling_context', $dynamicSamplingContext);
}

foreach (array_merge($this->contexts, $event->getContexts()) as $name => $data) {
Expand Down
33 changes: 33 additions & 0 deletions tests/State/ScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,39 @@ public function testApplyToEventSkipsDynamicSamplingContextWhenUsingExternalProp
Scope::clearExternalPropagationContext();
}

public function testApplyToEventKeepsDynamicSamplingContextOfEventWithoutSpanOnScope(): void
{
$dynamicSamplingContext = DynamicSamplingContext::fromHeader('sentry-trace_id=566e3688a61d4bc888951642d6f14a19,sentry-transaction=foo');

$event = Event::createTransaction();
$event->setSdkMetadata('dynamic_sampling_context', $dynamicSamplingContext);

$scope = new Scope();
$event = $scope->applyToEvent($event, null, new Options([
'dsn' => 'http://public@example.com/1',
'traces_sample_rate' => 1.0,
]));

$this->assertNotNull($event);
$this->assertSame($dynamicSamplingContext, $event->getSdkMetadata('dynamic_sampling_context'));
}

public function testApplyToEventKeepsDynamicSamplingContextOfEventWithOtherTransactionOnScope(): void
{
$dynamicSamplingContext = DynamicSamplingContext::fromHeader('sentry-trace_id=566e3688a61d4bc888951642d6f14a19,sentry-transaction=foo');

$event = Event::createTransaction();
$event->setSdkMetadata('dynamic_sampling_context', $dynamicSamplingContext);

$scope = new Scope();
$scope->setSpan(new Transaction(new TransactionContext('bar')));

$event = $scope->applyToEvent($event);

$this->assertNotNull($event);
$this->assertSame($dynamicSamplingContext, $event->getSdkMetadata('dynamic_sampling_context'));
}

/**
* @dataProvider eventWithLogCountProvider
*/
Expand Down
Loading