-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(extension/trace): implement twig instrumentation
- Loading branch information
1 parent
5a978ab
commit 48200c5
Showing
18 changed files
with
324 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...ependencyInjection/ExtensionLoader/Instrumentation/TwigInstrumentationExtensionLoader.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\ExtensionLoader\Instrumentation; | ||
|
||
use Symfony\Bundle\TwigBundle\TwigBundle; | ||
|
||
final class TwigInstrumentationExtensionLoader extends AbstractInstrumentationExtensionLoader | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct('twig'); | ||
} | ||
|
||
protected function assertInstrumentationCanHappen(): void | ||
{ | ||
if (!class_exists(TwigBundle::class)) { | ||
throw new \LogicException('To configure the Twig instrumentation, you must first install the symfony/twig-bundle package.'); | ||
} | ||
} | ||
|
||
protected function setTracingDefinitions(): void | ||
{ | ||
$this->container | ||
->getDefinition('open_telemetry.instrumentation.twig.trace.extension') | ||
->addTag('twig.extension') | ||
->setArgument('$tracer', $this->getInstrumentationTracerOrDefaultTracer()); | ||
} | ||
|
||
protected function setMeteringDefinitions(): void | ||
{ | ||
return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\EventListener\Doctrine\Trace; | ||
|
||
use Doctrine\ORM\Event\OnClearEventArgs; | ||
use OpenTelemetry\API\Trace\TracerInterface; | ||
|
||
class OnClearListener | ||
{ | ||
public function __construct( | ||
private readonly TracerInterface $tracer, | ||
) { | ||
} | ||
|
||
public function onClear(OnClearEventArgs $args): void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\EventListener\Doctrine\Trace; | ||
|
||
use Doctrine\ORM\Event\OnFlushEventArgs; | ||
use OpenTelemetry\API\Trace\TracerInterface; | ||
|
||
class OnFlushListener | ||
{ | ||
public function __construct( | ||
private readonly TracerInterface $tracer, | ||
) { | ||
} | ||
|
||
public function onFlush(OnFlushEventArgs $args): void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\EventListener\Doctrine\Trace; | ||
|
||
use Doctrine\ORM\Event\PostFlushEventArgs; | ||
use OpenTelemetry\API\Trace\TracerInterface; | ||
|
||
class PostFlushListener | ||
{ | ||
public function __construct( | ||
private readonly TracerInterface $tracer, | ||
) { | ||
} | ||
|
||
public function postFlush(PostFlushEventArgs $args): void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\EventListener\Doctrine\Trace; | ||
|
||
use Doctrine\ORM\Event\PostLoadEventArgs; | ||
use OpenTelemetry\API\Trace\TracerInterface; | ||
|
||
class PostLoadListener | ||
{ | ||
public function __construct( | ||
private readonly TracerInterface $tracer, | ||
) { | ||
} | ||
|
||
public function postLoad(PostLoadEventArgs $args): void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\EventListener\Doctrine\Trace; | ||
|
||
use Doctrine\ORM\Event\PostPersistEventArgs; | ||
use OpenTelemetry\API\Trace\TracerInterface; | ||
|
||
class PostPersistListener | ||
{ | ||
public function __construct( | ||
private readonly TracerInterface $tracer, | ||
) { | ||
} | ||
|
||
public function postPersist(PostPersistEventArgs $args): void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\EventListener\Doctrine\Trace; | ||
|
||
use Doctrine\ORM\Event\PostRemoveEventArgs; | ||
use OpenTelemetry\API\Trace\TracerInterface; | ||
|
||
class PostRemoveListener | ||
{ | ||
public function __construct( | ||
private readonly TracerInterface $tracer, | ||
) { | ||
} | ||
|
||
public function postRemove(PostRemoveEventArgs $args): void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\EventListener\Doctrine\Trace; | ||
|
||
use Doctrine\ORM\Event\PostUpdateEventArgs; | ||
use OpenTelemetry\API\Trace\TracerInterface; | ||
|
||
class PostUpdateListener | ||
{ | ||
public function __construct( | ||
private readonly TracerInterface $tracer, | ||
) { | ||
} | ||
|
||
public function postUpdate(PostUpdateEventArgs $args): void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\EventListener\Doctrine\Trace; | ||
|
||
use Doctrine\ORM\Event\PreFlushEventArgs; | ||
use OpenTelemetry\API\Trace\TracerInterface; | ||
|
||
class PreFlushListener | ||
{ | ||
public function __construct( | ||
private readonly TracerInterface $tracer, | ||
) { | ||
} | ||
|
||
public function preFlush(PreFlushEventArgs $args): void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\EventListener\Doctrine\Trace; | ||
|
||
use Doctrine\ORM\Event\PrePersistEventArgs; | ||
use OpenTelemetry\API\Trace\TracerInterface; | ||
|
||
class PrePersistListener | ||
{ | ||
public function __construct( | ||
private readonly TracerInterface $tracer, | ||
) { | ||
} | ||
|
||
public function prePersist(PrePersistEventArgs $args): void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\EventListener\Doctrine\Trace; | ||
|
||
use Doctrine\ORM\Event\PreRemoveEventArgs; | ||
use OpenTelemetry\API\Trace\TracerInterface; | ||
|
||
class PreRemoveListener | ||
{ | ||
public function __construct( | ||
private readonly TracerInterface $tracer, | ||
) { | ||
} | ||
|
||
public function preRemove(PreRemoveEventArgs $args): void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\EventListener\Doctrine\Trace; | ||
|
||
use Doctrine\ORM\Event\PreUpdateEventArgs; | ||
use OpenTelemetry\API\Trace\TracerInterface; | ||
|
||
class PreUpdateListener | ||
{ | ||
public function __construct( | ||
private readonly TracerInterface $tracer, | ||
) { | ||
} | ||
|
||
public function preRemove(PreUpdateEventArgs $args): void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\Extension\Twig\Trace; | ||
|
||
use OpenTelemetry\API\Trace\Span; | ||
use OpenTelemetry\API\Trace\SpanKind; | ||
use OpenTelemetry\API\Trace\TracerInterface; | ||
use OpenTelemetry\Context\Context; | ||
use Twig\Extension\AbstractExtension; | ||
use Twig\Profiler\NodeVisitor\ProfilerNodeVisitor; | ||
use Twig\Profiler\Profile; | ||
|
||
class TwigTraceExtension extends AbstractExtension | ||
{ | ||
/** | ||
* @var \SplObjectStorage<Profile, Span> | ||
*/ | ||
private \SplObjectStorage $spans; | ||
|
||
public function __construct( | ||
private readonly TracerInterface $tracer, | ||
) { | ||
$this->spans = new \SplObjectStorage(); | ||
} | ||
|
||
public function enter(Profile $profile): void | ||
{ | ||
$spanBuilder = $this->tracer | ||
->spanBuilder($this->getSpanName($profile)) | ||
->setSpanKind(SpanKind::KIND_SERVER) | ||
; | ||
|
||
$parent = Context::getCurrent(); | ||
|
||
$span = $spanBuilder->setParent($parent)->startSpan(); | ||
|
||
$this->spans[$profile] = $span; | ||
} | ||
|
||
public function leave(Profile $profile): void | ||
{ | ||
if (!isset($this->spans[$profile])) { | ||
return; | ||
} | ||
$this->spans[$profile]->end(); | ||
unset($this->spans[$profile]); | ||
} | ||
|
||
public function getNodeVisitors(): array | ||
{ | ||
return [ | ||
new ProfilerNodeVisitor(self::class), | ||
]; | ||
} | ||
|
||
private function getSpanName(Profile $profile): string | ||
{ | ||
switch (true) { | ||
case $profile->isRoot(): | ||
return $profile->getName(); | ||
|
||
case $profile->isTemplate(): | ||
return $profile->getTemplate(); | ||
|
||
default: | ||
return sprintf('%s::%s(%s)', $profile->getTemplate(), $profile->getType(), $profile->getName()); | ||
} | ||
} | ||
} |
Oops, something went wrong.