Skip to content

Add the sentry_trace_meta() Twig function to print the sentry-trace HTML meta tag #510

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
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 @@ -3,6 +3,7 @@
## Unreleased

- Make the transport factory configurable in the bundle's config (#504)
- Add the `sentry_trace_meta()` Twig function to print the `sentry-trace` HTML meta tag (#510)

## 4.1.0 (2021-04-19)

Expand Down
6 changes: 6 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,11 @@
<argument type="service" id="Symfony\Component\HttpFoundation\RequestStack" />
<argument type="service" id="Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface" on-invalid="null" />
</service>

<service id="Sentry\SentryBundle\Twig\SentryExtension" class="Sentry\SentryBundle\Twig\SentryExtension">
<argument type="service" id="Sentry\State\HubInterface" />

<tag name="twig.extension" />
</service>
</services>
</container>
45 changes: 45 additions & 0 deletions src/Twig/SentryExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Twig;

use Sentry\State\HubInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

final class SentryExtension extends AbstractExtension
{
/**
* @var HubInterface The current hub
*/
private $hub;

/**
* @param HubInterface $hub The current hub
*/
public function __construct(HubInterface $hub)
{
$this->hub = $hub;
}

/**
* {@inheritdoc}
*/
public function getFunctions(): array
{
return [
new TwigFunction('sentry_trace_meta', \Closure::fromCallable([$this, 'getTraceMeta']), ['is_safe' => ['html']]),
];
}

/**
* Returns an HTML meta tag named `sentry-trace`.
*/
private function getTraceMeta(): string
{
$span = $this->hub->getSpan();

return sprintf('<meta name="sentry-trace" content="%s" />', null !== $span ? $span->toTraceparent() : '');
}
}
78 changes: 78 additions & 0 deletions tests/Twig/SentryExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Tests\Twig;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sentry\SentryBundle\Twig\SentryExtension;
use Sentry\State\HubInterface;
use Sentry\Tracing\Span;
use Sentry\Tracing\SpanId;
use Sentry\Tracing\TraceId;
use Sentry\Tracing\Transaction;
use Sentry\Tracing\TransactionContext;
use Symfony\Bundle\TwigBundle\TwigBundle;
use Twig\Environment;
use Twig\Loader\ArrayLoader;

final class SentryExtensionTest extends TestCase
{
/**
* @var MockObject&HubInterface
*/
private $hub;

public static function setUpBeforeClass(): void
{
if (!self::isTwigBundlePackageInstalled()) {
self::markTestSkipped('This test requires the "symfony/twig-bundle" Composer package to be installed.');
}
}

protected function setUp(): void
{
$this->hub = $this->createMock(HubInterface::class);
}

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

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

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

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

$transaction = new Transaction(new TransactionContext());
$transaction->setTraceId(new TraceId('a3c01c41d7b94b90aee23edac90f4319'));
$transaction->setSpanId(new SpanId('e69c2aef0ec34f2a'));

yield [
$transaction,
'<meta name="sentry-trace" content="a3c01c41d7b94b90aee23edac90f4319-e69c2aef0ec34f2a" />',
];
}

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