Skip to content

Commit

Permalink
symfony http.route: use existing hook instead
Browse files Browse the repository at this point in the history
  • Loading branch information
cataphract committed Dec 13, 2024
1 parent b32dca7 commit 6bbae35
Show file tree
Hide file tree
Showing 20 changed files with 52 additions and 43 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,6 @@ TEST_WEB_83 := \
test_web_nette_30 \
test_web_slim_312 \
test_web_slim_4 \
test_web_symfony_52 \
test_web_symfony_62 \
test_web_symfony_70 \
test_web_wordpress_59 \
Expand Down
76 changes: 34 additions & 42 deletions src/DDTrace/Integrations/Symfony/SymfonyIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
use DDTrace\Type;
use DDTrace\Util\Normalizer;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\Route;

class SymfonyIntegration extends Integration
{
Expand All @@ -24,6 +22,8 @@ class SymfonyIntegration extends Integration
/** @var string */
public $frameworkPrefix = SymfonyIntegration::NAME;

public $kernel;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -309,42 +309,6 @@ function ($This, $scope, $args) use ($integration) {
}
);

\DDTrace\trace_method(
'Symfony\Component\EventDispatcher\EventDispatcher',
'dispatch',
function (SpanData $span, $args) {
$event = $args[0];

if (!($event instanceof ControllerEvent)) {
return;
}

$request = $event->getRequest();
$controller = $event->getController()[0];

if (!property_exists($controller, 'container')) {
return;
}

$rc = new \ReflectionClass(get_class($controller));
$container = $rc->getProperty('container');
$container->setAccessible(true);
$container = $container->getValue($controller);

$router = $container->get('router');
$routeName = $request->attributes->get('_route');

$routeCollection = $router->getRouteCollection();
/** @var Route $route */
$route = $routeCollection->get($routeName);
if (!isset($route)) {
return;
}
$root_span = \DDTrace\root_span();
$root_span->meta[Tag::HTTP_ROUTE] = $route->getPath();
}
);

$this->loadSymfony($this);

return Integration::LOADED;
Expand Down Expand Up @@ -393,6 +357,24 @@ function (SpanData $span) use ($class, $methodname) {
);
*/

\DDTrace\hook_method(
'Symfony\Component\HttpKernel\Kernel',
'getHttpKernel',
null,
function ($object) use ($integration) {
$integration->kernel = $object;
}
);

\DDTrace\hook_method(
'Drupal\Core\DrupalKernel',
'getHttpKernel',
null,
function ($object) use ($integration) {
$integration->kernel = $object;
}
);

\DDTrace\hook_method(
'Symfony\Component\HttpKernel\HttpKernel',
'__construct',
Expand Down Expand Up @@ -436,12 +418,22 @@ function_exists('datadog\appsec\push_address')) {
\datadog\appsec\push_address("server.request.path_params", $parameters);
}

$route = $request->get('_route');
if (null !== $route && null !== $request) {
$route_name = $request->get('_route');
if (null !== $route_name && null !== $request) {
if (dd_trace_env_config("DD_HTTP_SERVER_ROUTE_BASED_NAMING")) {
$rootSpan->resource = $route;
$rootSpan->resource = $route_name;
}
$rootSpan->meta['symfony.route.name'] = $route_name;

if ($integration->kernel !== null) {
$container = $integration->kernel->getContainer();
$router = $container->get('router');
$routeCollection = $router->getRouteCollection();
$route = $routeCollection->get($route_name);
if (isset($route)) {
$rootSpan->meta[Tag::HTTP_ROUTE] = $route->getPath();
}
}
$rootSpan->meta['symfony.route.name'] = $route;
}
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"_dd.p.dm": "-0",
"component": "drupal",
"http.method": "GET",
"http.route": "/simple_view",
"http.status_code": "200",
"http.url": "http://localhost/simple_view",
"runtime-id": "cecb7d5b-eb5a-49f8-8374-29dce2d6fce6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"_dd.p.dm": "-0",
"component": "drupal",
"http.method": "GET",
"http.route": "/simple_view",
"http.status_code": "200",
"http.url": "http://localhost/simple_view",
"runtime-id": "692423cd-81b3-449e-a67e-43150df99f74",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"error.stack": "#0 [internal function]: Drupal\\datadog\\Controller\\DatadogController->error()\n#1 /home/circleci/app/tests/Frameworks/Drupal/Version_9_5/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array()\n#2 /home/circleci/app/tests/Frameworks/Drupal/Version_9_5/core/lib/Drupal/Core/Render/Renderer.php(580): Drupal\\Core\\EventSubscriber\\EarlyRenderingControllerWrapperSubscriber->Drupal\\Core\\EventSubscriber\\{closure}()\n#3 /home/circleci/app/tests/Frameworks/Drupal/Version_9_5/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(121): Drupal\\Core\\Render\\Renderer->executeInRenderContext()\n#4 /home/circleci/app/tests/Frameworks/Drupal/Version_9_5/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\\Core\\EventSubscriber\\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext()\n#5 /home/circleci/app/tests/Frameworks/Drupal/Version_9_5/vendor/symfony/http-kernel/HttpKernel.php(169): Drupal\\Core\\EventSubscriber\\EarlyRenderingControllerWrapperSubscriber->Drupal\\Core\\EventSubscriber\\{closure}()\n#6 /home/circleci/app/tests/Frameworks/Drupal/Version_9_5/vendor/symfony/http-kernel/HttpKernel.php(81): Symfony\\Component\\HttpKernel\\HttpKernel->handleRaw()\n#7 /home/circleci/app/tests/Frameworks/Drupal/Version_9_5/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Symfony\\Component\\HttpKernel\\HttpKernel->handle()\n#8 /home/circleci/app/tests/Frameworks/Drupal/Version_9_5/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\\Core\\StackMiddleware\\Session->handle()\n#9 /home/circleci/app/tests/Frameworks/Drupal/Version_9_5/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\\Core\\StackMiddleware\\KernelPreHandle->handle()\n#10 /home/circleci/app/tests/Frameworks/Drupal/Version_9_5/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\\Core\\StackMiddleware\\ReverseProxyMiddleware->handle()\n#11 /home/circleci/app/tests/Frameworks/Drupal/Version_9_5/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\\Core\\StackMiddleware\\NegotiationMiddleware->handle()\n#12 /home/circleci/app/tests/Frameworks/Drupal/Version_9_5/core/lib/Drupal/Core/DrupalKernel.php(718): Stack\\StackedHttpKernel->handle()\n#13 /home/circleci/app/tests/Frameworks/Drupal/Version_9_5/index.php(19): Drupal\\Core\\DrupalKernel->handle()\n#14 {main}",
"error.type": "Exception",
"http.method": "GET",
"http.route": "/error",
"http.status_code": "500",
"http.url": "http://localhost/error?key=value&<redacted>",
"runtime-id": "3afe4427-fcda-4ade-bdc5-572a8c5f8b12",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"_dd.p.dm": "-0",
"component": "drupal",
"http.method": "GET",
"http.route": "/simple_view",
"http.status_code": "200",
"http.url": "http://localhost/simple_view",
"runtime-id": "7a7b1e1f-04e8-41dc-82bd-81f24ef6e0a4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"_dd.p.tid": "6661b34000000000",
"component": "symfony",
"http.method": "GET",
"http.route": "/simple",
"http.status_code": "200",
"http.url": "http://localhost/app.php/simple?key=value&<redacted>",
"runtime-id": "93535b2f-0ff1-4add-8907-53304703f545",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"error.type": "Exception",
"http.method": "GET",
"http.status_code": "500",
"http.route": "/error",
"http.url": "http://localhost/app.php/error?key=value&<redacted>",
"runtime-id": "93535b2f-0ff1-4add-8907-53304703f545",
"span.kind": "server",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"_dd.p.tid": "6661bc6500000000",
"component": "symfony",
"http.method": "GET",
"http.route": "/simple_view",
"http.status_code": "200",
"http.url": "http://localhost/app.php/simple_view?key=value&<redacted>",
"runtime-id": "05d2d546-7cb6-4e46-bc10-0ff5602cdf9d",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"_dd.p.tid": "6661b35800000000",
"component": "symfony",
"http.method": "GET",
"http.route": "/",
"http.status_code": "200",
"http.url": "http://localhost/app.php?key=value&<redacted>",
"runtime-id": "7ddbbd63-c083-4257-a7f5-44101e551ef1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"_dd.p.tid": "6660700300000000",
"component": "symfony",
"http.method": "GET",
"http.route": "/simple",
"http.status_code": "200",
"http.url": "http://localhost/app.php/simple?key=value&<redacted>",
"runtime-id": "4374c14f-def1-4b62-97c0-598f46322850",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"error.stack": "#0 /home/circleci/app/tests/Frameworks/Symfony/Version_2_8/app/bootstrap.php.cache(3275): AppBundle\\Controller\\CommonScenariosController->errorAction()\n#1 /home/circleci/app/tests/Frameworks/Symfony/Version_2_8/app/bootstrap.php.cache(3234): Symfony\\Component\\HttpKernel\\HttpKernel->handleRaw()\n#2 /home/circleci/app/tests/Frameworks/Symfony/Version_2_8/app/bootstrap.php.cache(3388): Symfony\\Component\\HttpKernel\\HttpKernel->handle()\n#3 /home/circleci/app/tests/Frameworks/Symfony/Version_2_8/app/bootstrap.php.cache(2594): Symfony\\Component\\HttpKernel\\DependencyInjection\\ContainerAwareHttpKernel->handle()\n#4 /home/circleci/app/tests/Frameworks/Symfony/Version_2_8/web/app.php(15): Symfony\\Component\\HttpKernel\\Kernel->handle()\n#5 {main}",
"error.type": "Exception",
"http.method": "GET",
"http.route": "/error",
"http.status_code": "500",
"http.url": "http://localhost/app.php/error?key=value&<redacted>",
"runtime-id": "4374c14f-def1-4b62-97c0-598f46322850",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"_dd.p.tid": "6660709f00000000",
"component": "symfony",
"http.method": "GET",
"http.route": "/simple_view",
"http.status_code": "200",
"http.url": "http://localhost/app.php/simple_view?key=value&<redacted>",
"runtime-id": "a908b910-f481-44e7-bf7b-8d48954a4639",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"_dd.p.tid": "6660aaff00000000",
"component": "symfony",
"http.method": "GET",
"http.route": "/",
"http.status_code": "200",
"http.url": "http://localhost/app.php?key=value&<redacted>",
"runtime-id": "4b578cb2-1f1d-441e-aacb-a4aaff240713",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"_dd.p.tid": "67376acd00000000",
"component": "symfony",
"http.method": "GET",
"http.route": "/lucky/fail",
"http.status_code": "200",
"http.url": "http://localhost/lucky/fail",
"runtime-id": "ec06d36e-1a6e-48f5-a627-c724d35f765b",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"_dd.p.tid": "6737688900000000",
"component": "symfony",
"http.method": "GET",
"http.route": "/lucky/number",
"http.status_code": "200",
"http.url": "http://localhost/lucky/number",
"runtime-id": "5fe9f332-5cdf-4438-a2c1-35f9245e4015",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"_dd.p.tid": "6737607d00000000",
"component": "symfony",
"http.method": "GET",
"http.route": "/lucky/number",
"http.status_code": "200",
"http.url": "http://localhost/lucky/number",
"runtime-id": "f05a51bb-8120-4e60-8705-ccc45d60b1c3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"_dd.p.tid": "67376d6700000000",
"component": "symfony",
"http.method": "GET",
"http.route": "/lucky/fail",
"http.status_code": "200",
"http.url": "http://localhost/lucky/fail",
"runtime-id": "34c72fce-60c5-4a66-b562-44064d3104ba",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"_dd.p.tid": "67376c0a00000000",
"component": "symfony",
"http.method": "GET",
"http.route": "/lucky/number",
"http.status_code": "200",
"http.url": "http://localhost/lucky/number",
"runtime-id": "1bbc4547-ce88-419f-b529-961bc1522710",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"_dd.p.tid": "673760aa00000000",
"component": "symfony",
"http.method": "GET",
"http.route": "/lucky/number",
"http.status_code": "200",
"http.url": "http://localhost/lucky/number",
"runtime-id": "99fe2e1b-f085-435b-8e4a-909cfe1eeb25",
Expand Down

0 comments on commit 6bbae35

Please sign in to comment.