-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
456 additions
and
30 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
src/DependencyInjection/Compiler/HttpKernelTracerLocatorPass.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,25 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
class HttpKernelTracerLocatorPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if ($container->has('open_telemetry.instrumentation.http_kernel.trace.event_subscriber')) { | ||
$tracers = $container->findTaggedServiceIds('open_telemetry.tracer'); | ||
|
||
if (0 !== count($tracers)) { | ||
$traceableHttpKernelEventSubscriber = $container->getDefinition('open_telemetry.instrumentation.http_kernel.trace.event_subscriber'); | ||
$traceableHttpKernelEventSubscriber->setArgument( | ||
'$tracerLocator', | ||
ServiceLocatorTagPass::register($container, $tracers), | ||
); | ||
} | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/DependencyInjection/Compiler/SetInstrumentationTypePass.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,32 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
class SetInstrumentationTypePass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if ($container->hasParameter('open_telemetry.instrumentation.console.type')) { | ||
$consoleInstrumentationType = $container->getParameter('open_telemetry.instrumentation.console.type'); | ||
if ($container->hasDefinition('open_telemetry.instrumentation.console.trace.event_subscriber')) { | ||
} | ||
} | ||
|
||
if ($container->hasParameter('open_telemetry.instrumentation.http_kernel.type')) { | ||
$httpKernelInstrumentationType = $container->getParameter('open_telemetry.instrumentation.http_kernel.type'); | ||
if ($container->hasDefinition('open_telemetry.instrumentation.http_kernel.trace.event_subscriber')) { | ||
$container->getDefinition('open_telemetry.instrumentation.http_kernel.trace.event_subscriber') | ||
->addMethodCall('setInstrumentationType', [$httpKernelInstrumentationType]); | ||
} | ||
} | ||
|
||
if ($container->hasParameter('open_telemetry.instrumentation.messenger.type')) { | ||
$messengerInstrumentationType = $container->getParameter('open_telemetry.instrumentation.messenger.type'); | ||
if ($container->hasDefinition('open_telemetry.instrumentation.http_kernel.trace.event_subscriber')) { | ||
} | ||
} | ||
} | ||
} |
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
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,12 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\Attribute; | ||
|
||
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)] | ||
class Traceable | ||
{ | ||
public function __construct( | ||
public readonly ?string $tracer = null, | ||
) { | ||
} | ||
} |
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,9 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation; | ||
|
||
enum InstrumentationTypeEnum: string | ||
{ | ||
case Auto = 'auto'; | ||
case Attribute = 'attribute'; | ||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation; | ||
|
||
interface InstrumentationTypeInterface | ||
{ | ||
public function setInstrumentationType(InstrumentationTypeEnum $type): void; | ||
} |
79 changes: 79 additions & 0 deletions
79
src/Instrumentation/Symfony/Framework/Routing/TraceableRouteLoader.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,79 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\Symfony\Framework\Routing; | ||
|
||
use FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\Attribute\Traceable; | ||
use Symfony\Component\Config\Loader\LoaderInterface; | ||
use Symfony\Component\Config\Loader\LoaderResolverInterface; | ||
use Symfony\Component\Routing\Route; | ||
use Symfony\Component\Routing\RouteCollection; | ||
|
||
class TraceableRouteLoader implements LoaderInterface | ||
{ | ||
public const DEFAULT_KEY = '_traceable'; | ||
public const TRACER_KEY = '_tracer'; | ||
public const OPTION_KEY = 'traceable'; | ||
|
||
public function __construct(private LoaderInterface $loader) | ||
{ | ||
} | ||
|
||
public function load(mixed $resource, ?string $type = null): RouteCollection | ||
{ | ||
$routes = $this->loader->load($resource, $type); | ||
|
||
/** @var Route $route */ | ||
foreach ($routes as $route) { | ||
self::parseAttribute($route); | ||
|
||
$traceable = $route->getOption(self::OPTION_KEY); | ||
if (null !== $traceable) { | ||
$route->addDefaults([ | ||
self::DEFAULT_KEY => $traceable, | ||
self::TRACER_KEY => $route->getOption(self::TRACER_KEY), | ||
]); | ||
} | ||
} | ||
|
||
return $routes; | ||
} | ||
|
||
public function supports(mixed $resource, ?string $type = null): bool | ||
{ | ||
return $this->loader->supports($resource, $type); | ||
} | ||
|
||
public function getResolver(): LoaderResolverInterface | ||
{ | ||
return $this->loader->getResolver(); | ||
} | ||
|
||
public function setResolver(LoaderResolverInterface $resolver): void | ||
{ | ||
$this->loader->setResolver($resolver); | ||
} | ||
|
||
private static function parseAttribute(Route $route): void | ||
{ | ||
try { | ||
$controller = $route->getDefault('_controller'); | ||
if (true === str_contains($controller, '::')) { | ||
$reflection = new \ReflectionMethod($controller); | ||
} else { | ||
$reflection = new \ReflectionClass($controller); | ||
} | ||
} catch (\ReflectionException) { | ||
return; | ||
} | ||
|
||
$attribute = $reflection->getAttributes(Traceable::class)[0] ?? $reflection->getDeclaringClass()->getAttributes(Traceable::class)[0] ?? null; | ||
|
||
if (null !== $attribute) { | ||
$traceable = $attribute->newInstance(); | ||
$route->addOptions([ | ||
self::OPTION_KEY => true, | ||
self::TRACER_KEY => $traceable->tracer ?? null, | ||
]); | ||
} | ||
} | ||
} |
Oops, something went wrong.