Skip to content

Commit b41dc56

Browse files
committed
Add the sentry_trace_meta() Twig function to print the sentry-trace HTML meta tag
1 parent 8cd97db commit b41dc56

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

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

78
## 4.1.0 (2021-04-19)
89

src/Resources/config/services.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,11 @@
116116
<argument type="service" id="Symfony\Component\HttpFoundation\RequestStack" />
117117
<argument type="service" id="Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface" on-invalid="null" />
118118
</service>
119+
120+
<service id="Sentry\SentryBundle\Twig\SentryExtension" class="Sentry\SentryBundle\Twig\SentryExtension">
121+
<argument type="service" id="Sentry\State\HubInterface" />
122+
123+
<tag name="twig.extension" />
124+
</service>
119125
</services>
120126
</container>

src/Twig/SentryExtension.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sentry\SentryBundle\Twig;
6+
7+
use Sentry\State\HubInterface;
8+
use Twig\Extension\AbstractExtension;
9+
use Twig\TwigFunction;
10+
11+
final class SentryExtension extends AbstractExtension
12+
{
13+
/**
14+
* @var HubInterface The current hub
15+
*/
16+
private $hub;
17+
18+
/**
19+
* @param HubInterface $hub The current hub
20+
*/
21+
public function __construct(HubInterface $hub)
22+
{
23+
$this->hub = $hub;
24+
}
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function getFunctions(): array
30+
{
31+
return [
32+
new TwigFunction('sentry_trace_meta', \Closure::fromCallable([$this, 'getTraceMeta']), ['is_safe' => ['html']]),
33+
];
34+
}
35+
36+
/**
37+
* Returns an HTML meta tag named `sentry-trace`.
38+
*/
39+
private function getTraceMeta(): string
40+
{
41+
$span = $this->hub->getSpan();
42+
43+
return sprintf('<meta name="sentry-trace" content="%s" />', null !== $span ? $span->toTraceparent() : '');
44+
}
45+
}

tests/Twig/SentryExtensionTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sentry\SentryBundle\Tests\Twig;
6+
7+
use PHPUnit\Framework\MockObject\MockObject;
8+
use PHPUnit\Framework\TestCase;
9+
use Sentry\SentryBundle\Twig\SentryExtension;
10+
use Sentry\State\HubInterface;
11+
use Sentry\Tracing\Span;
12+
use Sentry\Tracing\SpanId;
13+
use Sentry\Tracing\TraceId;
14+
use Sentry\Tracing\Transaction;
15+
use Sentry\Tracing\TransactionContext;
16+
use Symfony\Bundle\TwigBundle\TwigBundle;
17+
use Twig\Environment;
18+
use Twig\Loader\ArrayLoader;
19+
20+
final class SentryExtensionTest extends TestCase
21+
{
22+
/**
23+
* @var MockObject&HubInterface
24+
*/
25+
private $hub;
26+
27+
public static function setUpBeforeClass(): void
28+
{
29+
if (!self::isTwigBundlePackageInstalled()) {
30+
self::markTestSkipped('This test requires the "symfony/twig-bundle" Composer package to be installed.');
31+
}
32+
}
33+
34+
protected function setUp(): void
35+
{
36+
$this->hub = $this->createMock(HubInterface::class);
37+
}
38+
39+
/**
40+
* @dataProvider traceMetaFunctionDataProvider
41+
*/
42+
public function testTraceMetaFunction(?Span $span, string $expectedTemplate): void
43+
{
44+
$this->hub->expects($this->once())
45+
->method('getSpan')
46+
->willReturn($span);
47+
48+
$environment = new Environment(new ArrayLoader(['foo.twig' => '{{ sentry_trace_meta() }}']));
49+
$environment->addExtension(new SentryExtension($this->hub));
50+
51+
$this->assertSame($expectedTemplate, $environment->render('foo.twig'));
52+
}
53+
54+
/**
55+
* @return \Generator<mixed>
56+
*/
57+
public function traceMetaFunctionDataProvider(): \Generator
58+
{
59+
yield [
60+
null,
61+
'<meta name="sentry-trace" content="" />',
62+
];
63+
64+
$transaction = new Transaction(new TransactionContext());
65+
$transaction->setTraceId(new TraceId('a3c01c41d7b94b90aee23edac90f4319'));
66+
$transaction->setSpanId(new SpanId('e69c2aef0ec34f2a'));
67+
68+
yield [
69+
$transaction,
70+
'<meta name="sentry-trace" content="a3c01c41d7b94b90aee23edac90f4319-e69c2aef0ec34f2a" />',
71+
];
72+
}
73+
74+
private static function isTwigBundlePackageInstalled(): bool
75+
{
76+
return class_exists(TwigBundle::class);
77+
}
78+
}

0 commit comments

Comments
 (0)