Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add http.route tag to SymfonyIntegration.php #2710

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/DDTrace/Integrations/Symfony/SymfonyIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use DDTrace\Util\Versions;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\RouterInterface;
use function DDTrace\install_hook;

class SymfonyIntegration extends Integration
Expand Down Expand Up @@ -296,6 +297,53 @@ function ($This, $scope, $args) use ($integration) {
}
}
);

\DDTrace\trace_method(
'Symfony\Component\EventDispatcher\EventDispatcher',
'dispatch',
function (SpanData $span, $args) {

list($event) = $args;

$controllerEventClass = '\Symfony\Component\HttpKernel\Event\ControllerEvent';
if (!$event instanceof $controllerEventClass) {
return;
}

$request = $event->getRequest();
list($controller) = $event->getController();

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');
$matcher = $router->getMatcher();

// Generate URL for the current route
$parameters = $matcher->match($request->getPathInfo());
$url = $router->generate($routeName, $parameters, RouterInterface::ABSOLUTE_PATH);

// Replace route parameters in URL with placeholders
foreach ($parameters as $key => $value) {
$search = '/' . preg_quote($value, '/') . '/';
if (empty($value)) {
continue;
}
$url = preg_replace($search, '{' . $key . '}', $url, 1);
}

$urlWithoutQueryParameters = strtok($url, '?');

$span->meta[\DDTrace\Tag::HTTP_ROUTE] = $urlWithoutQueryParameters;
}
);

$this->loadSymfony($this);

Expand Down