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 #665

Merged
merged 4 commits into from Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- feat: Add support for Dynamic Sampling (#665)

## 4.3.1 (2022-10-10)

fix: Update span ops (#655)
Expand Down
4 changes: 2 additions & 2 deletions composer.json
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": "^3.4.44||^4.4.20||^5.0.11||^6.0",
"symfony/console": "^3.4.44||^4.4.20||^5.0.11||^6.0",
Expand Down Expand Up @@ -97,7 +97,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "4.3.x-dev",
"dev-master": "4.4.x-dev",
"releases/3.2.x": "3.2.x-dev",
"releases/2.x": "2.x-dev",
"releases/1.x": "1.x-dev"
Expand Down
2 changes: 2 additions & 0 deletions src/EventListener/TracingConsoleListener.php
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
Expand Up @@ -6,6 +6,7 @@

use Sentry\Tracing\Transaction;
use Sentry\Tracing\TransactionContext;
use Sentry\Tracing\TransactionSource;
use Symfony\Component\HttpFoundation\Request;

/**
Expand Down Expand Up @@ -34,9 +35,13 @@ public function handleKernelRequestEvent(RequestListenerRequestEvent $event): vo
/** @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
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
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
Expand Up @@ -13,11 +13,13 @@
use Sentry\SentryBundle\EventListener\RequestListenerTerminateEvent;
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
37 changes: 37 additions & 0 deletions tests/Twig/SentryExtensionTest.php
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