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

feat: Add support for Dynamic Sampling #572

Merged
merged 5 commits into from Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Sentry/Laravel/Console/TestCommand.php
Expand Up @@ -140,6 +140,7 @@ public function log($level, $message, array $context = []): void
$transactionContext = new TransactionContext();
$transactionContext->setSampled(true);
$transactionContext->setName('Sentry Test Transaction');
$transactionContext->setSource(TransactionSource::custom());
$transactionContext->setOp('sentry.test');

$transaction = $hub->startTransaction($transactionContext);
Expand Down
20 changes: 19 additions & 1 deletion src/Sentry/Laravel/Integration.php
Expand Up @@ -247,7 +247,25 @@ public static function sentryTracingMeta(): string
}

$content = sprintf('<meta name="sentry-trace" content="%s"/>', $span->toTraceparent());
// $content .= sprintf('<meta name="sentry-trace-data" content="%s"/>', $span->getDescription());

return $content;
}

/**
* Retrieve the meta tags with baggage information to link this request to front-end requests.
* This propagates the Dynamic Sampling Context.
*
* @return string
*/
public static function sentryBaggageMeta(): string
{
$span = self::currentTracingSpan();

if ($span === null) {
return '';
}

$content = sprintf('<meta name="baggage" content="%s"/>', $span->toBaggage());

return $content;
}
Expand Down
8 changes: 7 additions & 1 deletion src/Sentry/Laravel/Tracing/EventHandler.php
Expand Up @@ -16,11 +16,14 @@
use Sentry\Tracing\SpanContext;
use Sentry\Tracing\SpanStatus;
use Sentry\Tracing\TransactionContext;
use Sentry\Tracing\TransactionSource;

class EventHandler
{
public const QUEUE_PAYLOAD_TRACE_PARENT_DATA = 'sentry_trace_parent_data';

public const QUEUE_PAYLOAD_BAGGAGE_DATA = 'sentry_baggage_data';

/**
* Map event handlers to events.
*
Expand Down Expand Up @@ -153,6 +156,7 @@ public function subscribeQueueEvents(QueueManager $queue): void

if ($currentSpan !== null && $payload !== null) {
$payload[self::QUEUE_PAYLOAD_TRACE_PARENT_DATA] = $currentSpan->toTraceparent();
$payload[self::QUEUE_PAYLOAD_BAGGAGE_DATA] = $currentSpan->toBaggage();
}

return $payload;
Expand Down Expand Up @@ -294,10 +298,11 @@ protected function queueJobProcessingHandler(QueueEvents\JobProcessing $event)

if ($parentSpan === null) {
$traceParent = $event->job->payload()[self::QUEUE_PAYLOAD_TRACE_PARENT_DATA] ?? null;
$baggage = $event->job->payload()[self::QUEUE_PAYLOAD_BAGGAGE_DATA] ?? null;

$context = $traceParent === null
? new TransactionContext
: TransactionContext::fromSentryTrace($traceParent);
: TransactionContext::fromHeaders($traceParent, $baggage);

// If the parent transaction was not sampled we also stop the queue job from being recorded
if ($context->getParentSampled() === false) {
Expand Down Expand Up @@ -325,6 +330,7 @@ protected function queueJobProcessingHandler(QueueEvents\JobProcessing $event)

if ($context instanceof TransactionContext) {
$context->setName($resolvedJobName ?? $event->job->getName());
$context->setSource(TransactionSource::task());
}

$context->setOp('queue.process');
Expand Down
5 changes: 4 additions & 1 deletion src/Sentry/Laravel/Tracing/Middleware.php
Expand Up @@ -12,6 +12,7 @@
use Sentry\Tracing\Span;
use Sentry\Tracing\SpanContext;
use Sentry\Tracing\TransactionContext;
use Sentry\Tracing\TransactionSource;

class Middleware
{
Expand Down Expand Up @@ -103,12 +104,14 @@ private function startTransaction(Request $request, HubInterface $sentry): void
{
$requestStartTime = $request->server('REQUEST_TIME_FLOAT', microtime(true));
$sentryTraceHeader = $request->header('sentry-trace');
$baggageHeader = $request->header('baggage');

$context = $sentryTraceHeader
? TransactionContext::fromSentryTrace($sentryTraceHeader)
? TransactionContext::fromHeaders($sentryTraceHeader, $baggageHeader)
: new TransactionContext;

$context->setOp('http.server');
$context->setSource(TransactionSource::url());
cleptric marked this conversation as resolved.
Show resolved Hide resolved
$context->setData([
'url' => '/' . ltrim($request->path(), '/'),
'method' => strtoupper($request->method()),
Expand Down