Skip to content

Commit

Permalink
Add http.route tag to SymfonyIntegration.php
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian-Louis authored and cataphract committed Dec 12, 2024
1 parent b928df9 commit b32dca7
Show file tree
Hide file tree
Showing 33 changed files with 122 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion appsec/tests/integration/src/docker/php/build_dev_php.sh
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ EOD
chmod +x /tmp/apxs_wrapper
}

function run_dsymutil {
if [[ $(uname) != Darwin ]] then
return
fi
local readonly dir=$1 exe=
find "$dir" -type f -exec test -x '{}' \; -print | while read -r exe; do
if ! grep -q '^#!' "$exe"; then
local readonly dSYM_DIR="${exe}.dSYM"
if [[ ! -d $dSYM_DIR ]]; then
dsymutil "$exe"
fi
fi
done
}

function get_xdebug_version {
local -r version=$1
local readonly version_id=$(php_version_id $version)
Expand Down Expand Up @@ -294,6 +309,7 @@ function build_php {
make install-sapi || true
make install-binaries install-headers install-modules install-programs install-build

run_dsymutil "$prefix_dir"
rm -rf "$build_dir"
cd -
}
Expand Down Expand Up @@ -462,11 +478,12 @@ function install_xdebug {
"$php_prefix/bin/phpize"
mkdir -p "$build_dir"
cd "$build_dir"
"$xdebug_source_dir/configure" "--with-php-config=$php_prefix/bin/php-config"
CFLAGS="$CFLAGS -ggdb" "$xdebug_source_dir/configure" "--with-php-config=$php_prefix/bin/php-config"
make -j
make install
cd -

run_dsymutil "$php_prefix/lib"
rm -rf "$build_dir"
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
framework:
test: true
session:
storage_id: session.storage.mock_file
storage_factory_id: session.storage.factory.mock_file
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ public function homeAction(Request $request)
);
}

/**
* @Route("/dynamic-path/{param01}", name="dynamic-path")
*/
public function dynamicAction(Request $request)
#[Route("/dynamic-path/{param01}")]
public function dynamicAction(Request $request, string $param01)
{
return new Response(
'Hi!'
"Hi $param01!"
);
}
}
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ services:
test-agent:
image: ghcr.io/datadog/dd-apm-test-agent/ddapm-test-agent:latest
ports:
- "127.0.0.1:9126:8126"
- "9126:9126"
volumes:
- ./tests/snapshots:/snapshots
environment:
Expand Down
40 changes: 39 additions & 1 deletion src/DDTrace/Integrations/Symfony/SymfonyIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
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 Down Expand Up @@ -307,6 +309,42 @@ 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 @@ -394,7 +432,7 @@ function (SpanData $span, $args, $response) use ($integration) {
$parameters = $request->get('_route_params');
if (!empty($parameters) &&
is_array($parameters) &&
function_exists('\datadog\appsec\push_address')) {
function_exists('datadog\appsec\push_address')) {
\datadog\appsec\push_address("server.request.path_params", $parameters);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Common/TracerTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public function tracesFromWebRequest($fn, $tracer = null, callable $until = null
// Clearing existing dumped file
$this->resetRequestDumper();

// The we server has to be configured to send traces to the provided requests dumper.
// The web server has to be configured to send traces to the provided requests dumper.
$fn($tracer);

self::putEnv('DD_TRACE_SHUTDOWN_TIMEOUT');
Expand Down
9 changes: 9 additions & 0 deletions tests/Common/WebFrameworkTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,18 @@ protected static function getInis()
// The following values should be made configurable from the outside. I could not get env XDEBUG_CONFIG
// to work setting it both in docker-compose.yml and in `getEnvs()` above, but that should be the best
// option.
'xdebug.start_with_request' => 'yes',

// 2
'xdebug.remote_enable' => 1,
'xdebug.remote_host' => 'host.docker.internal',
'xdebug.remote_autostart' => 1,
// 'xdebug.remote_log' => '/tmp/xdebug.log',

// 3
'xdebug.mode' => 'develop,debug',
'xdebug.client_host' => 'host.docker.internal',
// 'xdebug.log' => '/tmp/xdebug.log',
] + ($enableOpcache ? ["zend_extension" => "opcache.so"] : []);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
class CommonScenariosController extends AbstractController
{

/**
* @Route("/simple", name="simple")
*/
#[Route("/simple", name:"simple")]
public function simpleAction(Request $request)
{
// replace this example code with whatever you need
Expand All @@ -21,9 +19,7 @@ public function simpleAction(Request $request)
);
}

/**
* @Route("/simple_view", name="simple_view")
*/
#[Route("/simple_view", name:"simple_view")]
public function simpleViewAction(Request $request)
{
// replace this example code with whatever you need
Expand All @@ -43,9 +39,9 @@ public function dynamicWithOptionalsAction($param01, $param02)
}

/**
* @Route("/error", name="error")
* @throws \Exception
*/
#[Route("/error", name:"error")]
public function errorAction(Request $request)
{
throw new \Exception('An exception occurred');
Expand Down
3 changes: 3 additions & 0 deletions tests/Integrations/Symfony/V3_0/CommonScenariosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function provideSpecs()
)->withExactTags([
'symfony.route.action' => 'AppBundle\Controller\CommonScenariosController@simpleAction',
'symfony.route.name' => 'simple',
'http.route' => '/simple',
'http.method' => 'GET',
'http.url' => 'http://localhost/simple?key=value&<redacted>',
'http.status_code' => '200',
Expand Down Expand Up @@ -79,6 +80,7 @@ public function provideSpecs()
)->withExactTags([
'symfony.route.action' => 'AppBundle\Controller\CommonScenariosController@simpleViewAction',
'symfony.route.name' => 'simple_view',
'http.route' => '/simple_view',
'http.method' => 'GET',
'http.url' => 'http://localhost/simple_view?key=value&<redacted>',
'http.status_code' => '200',
Expand Down Expand Up @@ -125,6 +127,7 @@ public function provideSpecs()
)->withExactTags([
'symfony.route.action' => 'AppBundle\Controller\CommonScenariosController@errorAction',
'symfony.route.name' => 'error',
'http.route' => '/error',
'http.method' => 'GET',
'http.url' => 'http://localhost/error?key=value&<redacted>',
'http.status_code' => '500',
Expand Down
1 change: 1 addition & 0 deletions tests/Integrations/Symfony/V3_0/TraceSearchConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function testScenario()
)->withExactTags([
'symfony.route.action' => 'AppBundle\Controller\CommonScenariosController@simpleAction',
'symfony.route.name' => 'simple',
'http.route' => '/simple',
'http.method' => 'GET',
'http.url' => 'http://localhost/simple',
'http.status_code' => '200',
Expand Down
3 changes: 3 additions & 0 deletions tests/Integrations/Symfony/V3_3/CommonScenariosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function provideSpecs()
)->withExactTags([
'symfony.route.action' => 'AppBundle\Controller\CommonScenariosController@simpleAction',
'symfony.route.name' => 'simple',
'http.route' => '/simple',
'http.method' => 'GET',
'http.url' => 'http://localhost/simple?key=value&<redacted>',
'http.status_code' => '200',
Expand Down Expand Up @@ -80,6 +81,7 @@ public function provideSpecs()
)->withExactTags([
'symfony.route.action' => 'AppBundle\Controller\CommonScenariosController@simpleViewAction',
'symfony.route.name' => 'simple_view',
'http.route' => '/simple_view',
'http.method' => 'GET',
'http.url' => 'http://localhost/simple_view?key=value&<redacted>',
'http.status_code' => '200',
Expand Down Expand Up @@ -127,6 +129,7 @@ public function provideSpecs()
)->withExactTags([
'symfony.route.action' => 'AppBundle\Controller\CommonScenariosController@errorAction',
'symfony.route.name' => 'error',
'http.route' => '/error',
'http.method' => 'GET',
'http.url' => 'http://localhost/error?key=value&<redacted>',
'http.status_code' => '500',
Expand Down
1 change: 1 addition & 0 deletions tests/Integrations/Symfony/V3_3/TraceSearchConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function testScenario()
)->withExactTags([
'symfony.route.action' => 'AppBundle\Controller\CommonScenariosController@simpleAction',
'symfony.route.name' => 'simple',
'http.route' => '/simple',
'http.method' => 'GET',
'http.url' => 'http://localhost/simple',
'http.status_code' => '200',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function testEndpointThatExitsWithNoProcess()
)->withExactTags([
'symfony.route.action' => 'AppBundle\Controller\HomeController@actionBeingTerminatedByExit',
'symfony.route.name' => 'terminated_by_exit',
'http.route' => '/terminated_by_exit',
'http.method' => 'GET',
'http.url' => 'http://localhost/terminated_by_exit',
'http.status_code' => '200',
Expand Down
3 changes: 3 additions & 0 deletions tests/Integrations/Symfony/V3_4/CommonScenariosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function provideSpecs()
)->withExactTags([
'symfony.route.action' => 'AppBundle\Controller\CommonScenariosController@simpleAction',
'symfony.route.name' => 'simple',
'http.route' => '/simple',
'http.method' => 'GET',
'http.url' => 'http://localhost/simple?key=value&<redacted>',
'http.status_code' => '200',
Expand Down Expand Up @@ -87,6 +88,7 @@ public function provideSpecs()
)->withExactTags([
'symfony.route.action' => 'AppBundle\Controller\CommonScenariosController@simpleViewAction',
'symfony.route.name' => 'simple_view',
'http.route' => '/simple_view',
'http.method' => 'GET',
'http.url' => 'http://localhost/simple_view?key=value&<redacted>',
'http.status_code' => '200',
Expand Down Expand Up @@ -136,6 +138,7 @@ public function provideSpecs()
->withExactTags([
'symfony.route.action' => 'AppBundle\Controller\CommonScenariosController@errorAction',
'symfony.route.name' => 'error',
'http.route' => '/error',
'http.method' => 'GET',
'http.url' => 'http://localhost/error?key=value&<redacted>',
'http.status_code' => '500',
Expand Down
1 change: 1 addition & 0 deletions tests/Integrations/Symfony/V3_4/TemplateEnginesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function testAlternateTemplatingEngine()
)->withExactTags([
'symfony.route.action' => 'AppBundle\Controller\HomeController@indexAction',
'symfony.route.name' => 'alternate_templating',
'http.route' => '/alternate_templating',
'http.method' => 'GET',
'http.url' => 'http://localhost/alternate_templating',
'http.status_code' => '200',
Expand Down
1 change: 1 addition & 0 deletions tests/Integrations/Symfony/V3_4/TraceSearchConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function testScenario()
)->withExactTags([
'symfony.route.action' => 'AppBundle\Controller\CommonScenariosController@simpleAction',
'symfony.route.name' => 'simple',
'http.route' => '/simple',
'http.method' => 'GET',
'http.url' => 'http://localhost/simple',
'http.status_code' => '200',
Expand Down
3 changes: 3 additions & 0 deletions tests/Integrations/Symfony/V4_0/CommonScenariosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function provideSpecs()
)->withExactTags([
'symfony.route.action' => 'App\Controller\CommonScenariosController@simpleAction',
'symfony.route.name' => 'simple',
'http.route' => '/simple',
'http.method' => 'GET',
'http.url' => 'http://localhost/simple?key=value&<redacted>',
'http.status_code' => '200',
Expand Down Expand Up @@ -88,6 +89,7 @@ public function provideSpecs()
)->withExactTags([
'symfony.route.action' => 'App\Controller\CommonScenariosController@simpleViewAction',
'symfony.route.name' => 'simple_view',
'http.route' => '/simple_view',
'http.method' => 'GET',
'http.url' => 'http://localhost/simple_view?key=value&<redacted>',
'http.status_code' => '200',
Expand Down Expand Up @@ -135,6 +137,7 @@ public function provideSpecs()
)->withExactTags([
'symfony.route.action' => 'App\Controller\CommonScenariosController@errorAction',
'symfony.route.name' => 'error',
'http.route' => '/error',
'http.method' => 'GET',
'http.url' => 'http://localhost/error?key=value&<redacted>',
'http.status_code' => '500',
Expand Down
1 change: 1 addition & 0 deletions tests/Integrations/Symfony/V4_0/TraceSearchConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function testScenario()
)->withExactTags([
'symfony.route.action' => 'App\Controller\CommonScenariosController@simpleAction',
'symfony.route.name' => 'simple',
'http.route' => '/simple',
'http.method' => 'GET',
'http.url' => 'http://localhost/simple',
'http.status_code' => '200',
Expand Down
3 changes: 3 additions & 0 deletions tests/Integrations/Symfony/V4_2/CommonScenariosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function provideSpecs()
)->withExactTags([
'symfony.route.action' => 'App\Controller\CommonScenariosController@simpleAction',
'symfony.route.name' => 'simple',
'http.route' => '/simple',
'http.method' => 'GET',
'http.url' => 'http://localhost/simple?key=value&<redacted>',
'http.status_code' => '200',
Expand Down Expand Up @@ -88,6 +89,7 @@ public function provideSpecs()
)->withExactTags([
'symfony.route.action' => 'App\Controller\CommonScenariosController@simpleViewAction',
'symfony.route.name' => 'simple_view',
'http.route' => '/simple_view',
'http.method' => 'GET',
'http.url' => 'http://localhost/simple_view?key=value&<redacted>',
'http.status_code' => '200',
Expand Down Expand Up @@ -135,6 +137,7 @@ public function provideSpecs()
)->withExactTags([
'symfony.route.action' => 'App\Controller\CommonScenariosController@errorAction',
'symfony.route.name' => 'error',
'http.route' => '/error',
'http.method' => 'GET',
'http.url' => 'http://localhost/error?key=value&<redacted>',
'http.status_code' => '500',
Expand Down
1 change: 1 addition & 0 deletions tests/Integrations/Symfony/V4_2/TraceSearchConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function testScenario()
)->withExactTags([
'symfony.route.action' => 'App\Controller\CommonScenariosController@simpleAction',
'symfony.route.name' => 'simple',
'http.route' => '/simple',
'http.method' => 'GET',
'http.url' => 'http://localhost/simple',
'http.status_code' => '200',
Expand Down
Loading

0 comments on commit b32dca7

Please sign in to comment.