Skip to content

Commit

Permalink
Cache routes
Browse files Browse the repository at this point in the history
  • Loading branch information
cataphract committed Dec 16, 2024
1 parent 2970e08 commit 644c737
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions src/DDTrace/Integrations/Symfony/SymfonyIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use DDTrace\Tag;
use DDTrace\Type;
use DDTrace\Util\Normalizer;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Contracts\Cache\ItemInterface;

class SymfonyIntegration extends Integration
{
Expand Down Expand Up @@ -419,21 +421,35 @@ function_exists('datadog\appsec\push_address')) {
}

$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_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();
}
}
if ($route_name === null) {
return;
}
if (dd_trace_env_config("DD_HTTP_SERVER_ROUTE_BASED_NAMING")) {
$rootSpan->resource = $route_name;
}
$rootSpan->meta['symfony.route.name'] = $route_name;

if ($integration->kernel === null) {
return;
}
/** @var ContainerInterface $container */
$container = $integration->kernel->getContainer();
$cache = null;
try {
$cache = $container->get('cache.app');
} catch (\Exception $e) {
return;
}

/** @var \Symfony\Bundle\FrameworkBundle\Routing\Router $router */
$router = $container->get('router');
$route = $cache->get("route.path.$route_name", function (ItemInterface $item) use ($router, $route_name) {
$item->expiresAfter(3600);
$routeCollection = $router->getRouteCollection();
return $routeCollection->get($route_name);
});
if (isset($route)) {
$rootSpan->meta[Tag::HTTP_ROUTE] = $route->getPath();
}
}
);
Expand Down

0 comments on commit 644c737

Please sign in to comment.