Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: Attach the DSC to error events #1522

Merged
merged 4 commits into from
May 3, 2023
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
8 changes: 7 additions & 1 deletion src/State/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,15 @@ public function applyToEvent(Event $event, ?EventHint $hint = null): ?Event
$event->setUser($user);
}

// We do this here to also apply the trace context to errors if there is a Span on the Scope
// Apply the trace context to errors if there is a Span on the Scope
if (null !== $this->span) {
$event->setContext('trace', $this->span->getTraceContext());

// Apply the dynamic sampling context to errors if there is a Transaction on the Scope
$transaction = $this->span->getTransaction();
if (null !== $transaction) {
$event->setSdkMetadata('dynamic_sampling_context', $transaction->getDynamicSamplingContext());
}
}

foreach (array_merge($this->contexts, $event->getContexts()) as $name => $data) {
Expand Down
6 changes: 5 additions & 1 deletion src/Tracing/DynamicSamplingContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ public static function fromTransaction(Transaction $transaction, HubInterface $h
{
$samplingContext = new self();
$samplingContext->set('trace_id', (string) $transaction->getTraceId());
$samplingContext->set('sample_rate', (string) $transaction->getMetaData()->getSamplingRate());

$sampleRate = $transaction->getMetaData()->getSamplingRate();
if (null !== $sampleRate) {
$samplingContext->set('sample_rate', (string) $sampleRate);
}

// Only include the transaction name if it has good quality
if ($transaction->getMetadata()->getSource() !== TransactionSource::url()) {
Expand Down
20 changes: 17 additions & 3 deletions tests/State/ScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
use Sentry\EventHint;
use Sentry\Severity;
use Sentry\State\Scope;
use Sentry\Tracing\Span;
use Sentry\Tracing\DynamicSamplingContext;
use Sentry\Tracing\SpanContext;
use Sentry\Tracing\SpanId;
use Sentry\Tracing\TraceId;
use Sentry\Tracing\Transaction;
use Sentry\Tracing\TransactionContext;
use Sentry\UserDataBag;

final class ScopeTest extends TestCase
Expand Down Expand Up @@ -357,9 +360,13 @@ public function testApplyToEvent(): void
$event = Event::createEvent();
$event->setContext('foocontext', ['foo' => 'foo', 'bar' => 'bar']);

$span = new Span();
$transactionContext = new TransactionContext('foo');
$transaction = new Transaction($transactionContext);
$transaction->setSpanId(new SpanId('8c2df92a922b4efe'));
$transaction->setTraceId(new TraceId('566e3688a61d4bc888951642d6f14a19'));

$span = $transaction->startChild(new SpanContext());
$span->setSpanId(new SpanId('566e3688a61d4bc8'));
$span->setTraceId(new TraceId('566e3688a61d4bc888951642d6f14a19'));

$scope = new Scope();
$scope->setLevel(Severity::warning());
Expand Down Expand Up @@ -387,10 +394,17 @@ public function testApplyToEvent(): void
'trace' => [
'span_id' => '566e3688a61d4bc8',
'trace_id' => '566e3688a61d4bc888951642d6f14a19',
'parent_span_id' => '8c2df92a922b4efe',
],
'barcontext' => [
'bar' => 'foo',
],
], $event->getContexts());

$dynamicSamplingContext = $event->getSdkMetadata('dynamic_sampling_context');

$this->assertInstanceOf(DynamicSamplingContext::class, $dynamicSamplingContext);
$this->assertSame('foo', $dynamicSamplingContext->get('transaction'));
$this->assertSame('566e3688a61d4bc888951642d6f14a19', $dynamicSamplingContext->get('trace_id'));
}
}
1 change: 1 addition & 0 deletions tests/Tracing/DynamicSamplingContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public function testFromTransaction(): void
$transactionContext->setName('foo');

$transaction = new Transaction($transactionContext, $hub);
$transaction->getMetadata()->setSamplingRate(1.0);

$samplingContext = DynamicSamplingContext::fromTransaction($transaction, $hub);

Expand Down