From 387a651f7c2e4cd3f0f8529eeed5fba9417d149a Mon Sep 17 00:00:00 2001 From: Gustavo Lopes Date: Thu, 12 Dec 2024 08:05:03 -0300 Subject: [PATCH] symfony http.route: use existing hook instead --- Makefile | 1 - .../Symfony/SymfonyIntegration.php | 76 +++++++++---------- ...rios_test.test_scenario_get_with_view.json | 1 + ...rios_test.test_scenario_get_with_view.json | 1 + ...rios_test.test_scenario_get_with_view.json | 1 + ...6_2.messenger_test.test_async_failure.json | 1 + ...6_2.messenger_test.test_async_success.json | 1 + ...async_with_tracer_disabled_on_consume.json | 1 + 8 files changed, 40 insertions(+), 43 deletions(-) diff --git a/Makefile b/Makefile index 1037ea3bf2c..665e450dff0 100644 --- a/Makefile +++ b/Makefile @@ -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 \ diff --git a/src/DDTrace/Integrations/Symfony/SymfonyIntegration.php b/src/DDTrace/Integrations/Symfony/SymfonyIntegration.php index 80668f9d9cd..c28219d83ea 100644 --- a/src/DDTrace/Integrations/Symfony/SymfonyIntegration.php +++ b/src/DDTrace/Integrations/Symfony/SymfonyIntegration.php @@ -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 { @@ -24,6 +22,8 @@ class SymfonyIntegration extends Integration /** @var string */ public $frameworkPrefix = SymfonyIntegration::NAME; + public $kernel; + /** * {@inheritdoc} */ @@ -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; @@ -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', @@ -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; } } ); diff --git a/tests/snapshots/tests.integrations.drupal.v10_1.common_scenarios_test.test_scenario_get_with_view.json b/tests/snapshots/tests.integrations.drupal.v10_1.common_scenarios_test.test_scenario_get_with_view.json index 16afc5508e4..53ccf7e10f8 100644 --- a/tests/snapshots/tests.integrations.drupal.v10_1.common_scenarios_test.test_scenario_get_with_view.json +++ b/tests/snapshots/tests.integrations.drupal.v10_1.common_scenarios_test.test_scenario_get_with_view.json @@ -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", diff --git a/tests/snapshots/tests.integrations.drupal.v8_9.common_scenarios_test.test_scenario_get_with_view.json b/tests/snapshots/tests.integrations.drupal.v8_9.common_scenarios_test.test_scenario_get_with_view.json index 42eaba32f3e..50d551be747 100644 --- a/tests/snapshots/tests.integrations.drupal.v8_9.common_scenarios_test.test_scenario_get_with_view.json +++ b/tests/snapshots/tests.integrations.drupal.v8_9.common_scenarios_test.test_scenario_get_with_view.json @@ -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", diff --git a/tests/snapshots/tests.integrations.drupal.v9_5.common_scenarios_test.test_scenario_get_with_view.json b/tests/snapshots/tests.integrations.drupal.v9_5.common_scenarios_test.test_scenario_get_with_view.json index fbcc57a8a7f..870ca01550a 100644 --- a/tests/snapshots/tests.integrations.drupal.v9_5.common_scenarios_test.test_scenario_get_with_view.json +++ b/tests/snapshots/tests.integrations.drupal.v9_5.common_scenarios_test.test_scenario_get_with_view.json @@ -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", diff --git a/tests/snapshots/tests.integrations.symfony.v6_2.messenger_test.test_async_failure.json b/tests/snapshots/tests.integrations.symfony.v6_2.messenger_test.test_async_failure.json index bea7a584f0b..43414641614 100644 --- a/tests/snapshots/tests.integrations.symfony.v6_2.messenger_test.test_async_failure.json +++ b/tests/snapshots/tests.integrations.symfony.v6_2.messenger_test.test_async_failure.json @@ -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", diff --git a/tests/snapshots/tests.integrations.symfony.v6_2.messenger_test.test_async_success.json b/tests/snapshots/tests.integrations.symfony.v6_2.messenger_test.test_async_success.json index 611c4546745..5769a83c099 100644 --- a/tests/snapshots/tests.integrations.symfony.v6_2.messenger_test.test_async_success.json +++ b/tests/snapshots/tests.integrations.symfony.v6_2.messenger_test.test_async_success.json @@ -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", diff --git a/tests/snapshots/tests.integrations.symfony.v6_2.messenger_test.test_async_with_tracer_disabled_on_consume.json b/tests/snapshots/tests.integrations.symfony.v6_2.messenger_test.test_async_with_tracer_disabled_on_consume.json index fc9a24837c6..d9c5955b2e1 100644 --- a/tests/snapshots/tests.integrations.symfony.v6_2.messenger_test.test_async_with_tracer_disabled_on_consume.json +++ b/tests/snapshots/tests.integrations.symfony.v6_2.messenger_test.test_async_with_tracer_disabled_on_consume.json @@ -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",