Skip to content

feat: Add support for Dynamic Sampling #661

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

Merged
merged 7 commits into from
Oct 14, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- feat: Add support for Dynamic Sampling (#661)
- Add support for tracing of the Symfony HTTP client requests (#606)
- Support logging the impersonator user, if any (#647)
- ref: Use constant for the SDK version (#662)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"php": "^7.2||^8.0",
"jean85/pretty-package-versions": "^1.5 || ^2.0",
"php-http/discovery": "^1.11",
"sentry/sdk": "^3.2",
"sentry/sdk": "^3.3",
"symfony/cache-contracts": "^1.1||^2.4||^3.0",
"symfony/config": "^4.4.20||^5.0.11||^6.0",
"symfony/console": "^4.4.20||^5.0.11||^6.0",
Expand Down
2 changes: 2 additions & 0 deletions src/EventListener/TracingConsoleListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Sentry\Tracing\SpanContext;
use Sentry\Tracing\Transaction;
use Sentry\Tracing\TransactionContext;
use Sentry\Tracing\TransactionSource;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
Expand Down Expand Up @@ -61,6 +62,7 @@ public function handleConsoleCommandEvent(ConsoleCommandEvent $event): void
$transactionContext = new TransactionContext();
$transactionContext->setOp('console.command');
$transactionContext->setName($this->getSpanName($command));
$transactionContext->setSource(TransactionSource::task());

$span = $this->hub->startTransaction($transactionContext);
} else {
Expand Down
7 changes: 6 additions & 1 deletion src/EventListener/TracingRequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Sentry\Tracing\Transaction;
use Sentry\Tracing\TransactionContext;
use Sentry\Tracing\TransactionSource;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\TerminateEvent;
Expand Down Expand Up @@ -36,9 +37,13 @@ public function handleKernelRequestEvent(RequestEvent $event): void
/** @var float $requestStartTime */
$requestStartTime = $request->server->get('REQUEST_TIME_FLOAT', microtime(true));

$context = TransactionContext::fromSentryTrace($request->headers->get('sentry-trace', ''));
$context = TransactionContext::fromHeaders(
$request->headers->get('sentry-trace', ''),
$request->headers->get('baggage', '')
);
$context->setOp('http.server');
$context->setName(sprintf('%s %s%s%s', $request->getMethod(), $request->getSchemeAndHttpHost(), $request->getBaseUrl(), $request->getPathInfo()));
$context->setSource(TransactionSource::url());
$context->setStartTimestamp($requestStartTime);
$context->setTags($this->getTags($request));

Expand Down
11 changes: 11 additions & 0 deletions src/Twig/SentryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function getFunctions(): array
{
return [
new TwigFunction('sentry_trace_meta', [$this, 'getTraceMeta'], ['is_safe' => ['html']]),
new TwigFunction('sentry_baggage_meta', [$this, 'getBaggageMeta'], ['is_safe' => ['html']]),
];
}

Expand All @@ -42,4 +43,14 @@ public function getTraceMeta(): string

return sprintf('<meta name="sentry-trace" content="%s" />', null !== $span ? $span->toTraceparent() : '');
}

/**
* Returns an HTML meta tag named `baggage`.
*/
public function getBaggageMeta(): string
{
$span = $this->hub->getSpan();

return sprintf('<meta name="baggage" content="%s" />', null !== $span ? $span->toBaggage() : '');
}
}
3 changes: 3 additions & 0 deletions tests/EventListener/TracingConsoleListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Sentry\Tracing\Span;
use Sentry\Tracing\Transaction;
use Sentry\Tracing\TransactionContext;
use Sentry\Tracing\TransactionSource;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
Expand Down Expand Up @@ -71,6 +72,7 @@ public function handleConsoleCommandEventStartsTransactionIfNoSpanIsSetOnHubData
$transactionContext = new TransactionContext();
$transactionContext->setOp('console.command');
$transactionContext->setName('<unnamed command>');
$transactionContext->setSource(TransactionSource::task());

yield [
new Command(),
Expand All @@ -80,6 +82,7 @@ public function handleConsoleCommandEventStartsTransactionIfNoSpanIsSetOnHubData
$transactionContext = new TransactionContext();
$transactionContext->setOp('console.command');
$transactionContext->setName('app:command');
$transactionContext->setSource(TransactionSource::task());

yield [
new Command('app:command'),
Expand Down
55 changes: 55 additions & 0 deletions tests/EventListener/TracingRequestListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
use Sentry\Options;
use Sentry\SentryBundle\EventListener\TracingRequestListener;
use Sentry\State\HubInterface;
use Sentry\Tracing\DynamicSamplingContext;
use Sentry\Tracing\SpanId;
use Sentry\Tracing\SpanStatus;
use Sentry\Tracing\TraceId;
use Sentry\Tracing\Transaction;
use Sentry\Tracing\TransactionContext;
use Sentry\Tracing\TransactionSource;
use Symfony\Bridge\PhpUnit\ClockMock;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -85,11 +87,15 @@ public function testHandleKernelRequestEvent(Options $options, Request $request,
*/
public function handleKernelRequestEventDataProvider(): \Generator
{
$samplingContext = DynamicSamplingContext::fromHeader('');
$samplingContext->freeze();

$transactionContext = new TransactionContext();
$transactionContext->setTraceId(new TraceId('566e3688a61d4bc888951642d6f14a19'));
$transactionContext->setParentSpanId(new SpanId('566e3688a61d4bc8'));
$transactionContext->setParentSampled(true);
$transactionContext->setName('GET http://www.example.com/');
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setTags([
Expand All @@ -100,6 +106,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
'route' => '<unknown>',
'net.host.name' => 'www.example.com',
]);
$transactionContext->getMetadata()->setDynamicSamplingContext($samplingContext);

yield 'request.headers.sentry-trace EXISTS' => [
new Options(),
Expand All @@ -117,8 +124,47 @@ public function handleKernelRequestEventDataProvider(): \Generator
$transactionContext,
];

$samplingContext = DynamicSamplingContext::fromHeader('sentry-trace_id=566e3688a61d4bc888951642d6f14a19,sentry-public_key=public,sentry-sample_rate=1');
$samplingContext->freeze();

$transactionContext = new TransactionContext();
$transactionContext->setTraceId(new TraceId('566e3688a61d4bc888951642d6f14a19'));
$transactionContext->setParentSpanId(new SpanId('566e3688a61d4bc8'));
$transactionContext->setParentSampled(true);
$transactionContext->setName('GET http://www.example.com/');
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setTags([
'net.host.port' => '80',
'http.method' => 'GET',
'http.url' => 'http://www.example.com/',
'http.flavor' => '1.1',
'route' => '<unknown>',
'net.host.name' => 'www.example.com',
]);
$transactionContext->getMetadata()->setDynamicSamplingContext($samplingContext);

yield 'request.headers.sentry-trace and headers.baggage EXISTS' => [
new Options(),
Request::create(
'http://www.example.com',
'GET',
[],
[],
[],
[
'REQUEST_TIME_FLOAT' => 1613493597.010275,
'HTTP_sentry-trace' => '566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8-1',
'HTTP_baggage' => 'sentry-trace_id=566e3688a61d4bc888951642d6f14a19,sentry-public_key=public,sentry-sample_rate=1',
]
),
$transactionContext,
];

$transactionContext = new TransactionContext();
$transactionContext->setName('GET http://www.example.com/');
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setTags([
Expand All @@ -141,6 +187,7 @@ public function handleKernelRequestEventDataProvider(): \Generator

$transactionContext = new TransactionContext();
$transactionContext->setName('GET http://127.0.0.1/');
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setTags([
Expand Down Expand Up @@ -171,6 +218,7 @@ public function handleKernelRequestEventDataProvider(): \Generator

$transactionContext = new TransactionContext();
$transactionContext->setName('GET http://www.example.com/path');
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setTags([
Expand All @@ -194,6 +242,7 @@ public function handleKernelRequestEventDataProvider(): \Generator

$transactionContext = new TransactionContext();
$transactionContext->setName('GET http://www.example.com/path');
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setTags([
Expand All @@ -217,6 +266,7 @@ public function handleKernelRequestEventDataProvider(): \Generator

$transactionContext = new TransactionContext();
$transactionContext->setName('GET http://www.example.com/');
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setTags([
Expand All @@ -240,6 +290,7 @@ public function handleKernelRequestEventDataProvider(): \Generator

$transactionContext = new TransactionContext();
$transactionContext->setName('GET http://www.example.com/');
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setTags([
Expand All @@ -263,6 +314,7 @@ public function handleKernelRequestEventDataProvider(): \Generator

$transactionContext = new TransactionContext();
$transactionContext->setName('GET http://www.example.com/');
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setTags([
Expand All @@ -286,6 +338,7 @@ public function handleKernelRequestEventDataProvider(): \Generator

$transactionContext = new TransactionContext();
$transactionContext->setName('GET http://www.example.com/');
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setTags([
Expand All @@ -309,6 +362,7 @@ public function handleKernelRequestEventDataProvider(): \Generator

$transactionContext = new TransactionContext();
$transactionContext->setName('GET http://www.example.com/');
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setTags([
Expand All @@ -332,6 +386,7 @@ public function handleKernelRequestEventDataProvider(): \Generator

$transactionContext = new TransactionContext();
$transactionContext->setName('GET http://:/');
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setTags([
Expand Down
8 changes: 7 additions & 1 deletion tests/Tracing/HttpClient/TraceableHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Sentry\SentryBundle\Tracing\HttpClient\AbstractTraceableResponse;
use Sentry\SentryBundle\Tracing\HttpClient\TraceableHttpClient;
use Sentry\State\HubInterface;
use Sentry\Tracing\DynamicSamplingContext;
use Sentry\Tracing\Transaction;
use Sentry\Tracing\TransactionContext;
use Symfony\Component\HttpClient\MockHttpClient;
Expand Down Expand Up @@ -51,7 +52,12 @@ protected function setUp(): void

public function testRequest(): void
{
$transaction = new Transaction(new TransactionContext());
$samplingContext = DynamicSamplingContext::fromHeader('sentry-trace_id=566e3688a61d4bc888951642d6f14a19,sentry-public_key=public,sentry-sample_rate=1');

$transactionContext = new TransactionContext();
$transactionContext->getMetadata()->setDynamicSamplingContext($samplingContext);

$transaction = new Transaction($transactionContext);
$transaction->initSpanRecorder();

$this->hub->expects($this->once())
Expand Down
37 changes: 37 additions & 0 deletions tests/Twig/SentryExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPUnit\Framework\TestCase;
use Sentry\SentryBundle\Twig\SentryExtension;
use Sentry\State\HubInterface;
use Sentry\Tracing\DynamicSamplingContext;
use Sentry\Tracing\Span;
use Sentry\Tracing\SpanId;
use Sentry\Tracing\TraceId;
Expand Down Expand Up @@ -71,6 +72,42 @@ public function traceMetaFunctionDataProvider(): \Generator
];
}

/**
* @dataProvider baggageMetaFunctionDataProvider
*/
public function testBaggageMetaFunction(?Span $span, string $expectedTemplate): void
{
$this->hub->expects($this->once())
->method('getSpan')
->willReturn($span);

$environment = new Environment(new ArrayLoader(['foo.twig' => '{{ sentry_baggage_meta() }}']));
$environment->addExtension(new SentryExtension($this->hub));

$this->assertSame($expectedTemplate, $environment->render('foo.twig'));
}

/**
* @return \Generator<mixed>
*/
public function baggageMetaFunctionDataProvider(): \Generator
{
yield [
null,
'<meta name="baggage" content="" />',
];

$samplingContext = DynamicSamplingContext::fromHeader('sentry-trace_id=566e3688a61d4bc888951642d6f14a19,sentry-public_key=public,sentry-sample_rate=1');
$transactionContext = new TransactionContext();
$transactionContext->getMetadata()->setDynamicSamplingContext($samplingContext);
$transaction = new Transaction($transactionContext);

yield [
$transaction,
'<meta name="baggage" content="sentry-trace_id=566e3688a61d4bc888951642d6f14a19,sentry-public_key=public,sentry-sample_rate=1" />',
];
}

private static function isTwigBundlePackageInstalled(): bool
{
return class_exists(TwigBundle::class);
Expand Down