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

Implement http.route for Slim #2402

Merged
merged 9 commits into from
Jan 15, 2024
18 changes: 16 additions & 2 deletions src/Integrations/Integrations/Slim/SlimIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,29 @@ function ($errorMiddleware, $self, $args) use ($rootSpan, $integration) {
'lookupRoute',
null,
function ($router, $scope, $args, $return) use ($rootSpan) {
/** @var \Slim\Interfaces\RouteInterface $route */
$route = $return;
pablomartinezbernardo marked this conversation as resolved.
Show resolved Hide resolved
$rootSpan->meta[Tag::HTTP_ROUTE] = $route->getPattern();

if (PHP_VERSION_ID < 70000 || dd_trace_env_config("DD_HTTP_SERVER_ROUTE_BASED_NAMING")) {
/** @var \Slim\Interfaces\RouteInterface $route */
$route = $return;
$rootSpan->resource =
$_SERVER['REQUEST_METHOD'] . ' ' . ($route->getName() ?: $route->getPattern());
}
}
);
}
else if ('4' === $majorVersion) {
\DDTrace\hook_method(
'Slim\\Routing\\RouteCollector',
'lookupRoute',
null,
function ($router, $scope, $args, $return) use ($rootSpan) {
/** @var \Slim\Interfaces\RouteInterface $route */
$route = $return;
$rootSpan->meta[Tag::HTTP_ROUTE] = $route->getPattern();
}
);
}

// Providing info about the controller
$traceControllers = function (SpanData $span, $args) use ($rootSpan, $appName, $majorVersion) {
Expand Down
6 changes: 6 additions & 0 deletions tests/Frameworks/Slim/Version_3_12/src/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
throw new \Exception('Foo error');
});

$app->get('/parameterized/{value}', function (Request $request, Response $response, array $args) {
$value = $args['value'];
$response->getBody()->write("Hello, $value");
return $response;
});

$app->get('/[{name}]', function (Request $request, Response $response, array $args) use ($container) {
// Sample log message
$container->get('logger')->info("Slim-Skeleton '/' route");
Expand Down
6 changes: 6 additions & 0 deletions tests/Frameworks/Slim/Version_4/app/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,10 @@
$app->get('/error', function (Request $request, Response $response, $args) {
throw new \Exception('Foo error');
});

$app->get('/parameterized/{value}', function (Request $request, Response $response, $args) {
$value = $args['value'];
$response->getBody()->write("Hello, $value");
return $response;
});
};
34 changes: 31 additions & 3 deletions tests/Integrations/Slim/V3_12/CommonScenariosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ public function provideSpecs()
'http.url' => 'http://localhost:9999/simple?key=value&<redacted>',
'http.status_code' => '200',
Tag::SPAN_KIND => 'server',
Tag::COMPONENT => 'slim'
Tag::COMPONENT => 'slim',
Tag::HTTP_ROUTE => '/simple',
])->withChildren([
SpanAssertion::build(
'slim.route.controller',
Expand All @@ -136,7 +137,8 @@ public function provideSpecs()
'http.url' => 'http://localhost:9999/simple_view?key=value&<redacted>',
'http.status_code' => '200',
Tag::SPAN_KIND => 'server',
Tag::COMPONENT => 'slim'
Tag::COMPONENT => 'slim',
Tag::HTTP_ROUTE => '/simple_view',
])->withChildren([
SpanAssertion::build(
'slim.route.controller',
Expand Down Expand Up @@ -170,7 +172,8 @@ public function provideSpecs()
'http.url' => 'http://localhost:9999/error?key=value&<redacted>',
'http.status_code' => '500',
Tag::SPAN_KIND => 'server',
Tag::COMPONENT => 'slim'
Tag::COMPONENT => 'slim',
Tag::HTTP_ROUTE => '/error',
])->setError(null, null)
->withChildren([
SpanAssertion::build(
Expand All @@ -185,6 +188,31 @@ public function provideSpecs()
])->setError(null, 'Foo error')
]),
],
'A GET request to a route with a parameter' => [
SpanAssertion::build(
'slim.request',
'slim_test_app',
'web',
'GET /parameterized/{value}'
)->withExactTags([
'slim.route.controller' => 'Closure::__invoke',
'http.method' => 'GET',
'http.url' => 'http://localhost:9999/parameterized/paramValue',
'http.status_code' => '200',
Tag::SPAN_KIND => 'server',
Tag::COMPONENT => 'slim',
Tag::HTTP_ROUTE => '/parameterized/{value}',
])->withChildren([
SpanAssertion::build(
'slim.route.controller',
'slim_test_app',
'web',
'Closure::__invoke'
)->withExactTags([
Tag::COMPONENT => 'slim'
])
]),
],
]
);
}
Expand Down
36 changes: 33 additions & 3 deletions tests/Integrations/Slim/V4/CommonScenariosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ public function provideSpecs()
'http.url' => 'http://localhost:9999/simple?key=value&<redacted>',
'http.status_code' => '200',
Tag::SPAN_KIND => 'server',
Tag::COMPONENT => 'slim'
Tag::COMPONENT => 'slim',
Tag::HTTP_ROUTE => '/simple',
])->withChildren([
$this->wrapMiddleware([
SpanAssertion::build(
Expand All @@ -142,7 +143,8 @@ public function provideSpecs()
'http.url' => 'http://localhost:9999/simple_view?key=value&<redacted>',
'http.status_code' => '200',
Tag::SPAN_KIND => 'server',
Tag::COMPONENT => 'slim'
Tag::COMPONENT => 'slim',
Tag::HTTP_ROUTE => '/simple_view',
])->withChildren([
$this->wrapMiddleware([
SpanAssertion::build(
Expand Down Expand Up @@ -178,7 +180,8 @@ public function provideSpecs()
'http.url' => 'http://localhost:9999/error?key=value&<redacted>',
'http.status_code' => '500',
Tag::SPAN_KIND => 'server',
Tag::COMPONENT => 'slim'
Tag::COMPONENT => 'slim',
Tag::HTTP_ROUTE => '/error',
])
->setError(null, null)
->withChildren([
Expand All @@ -199,6 +202,33 @@ public function provideSpecs()
)
]),
],
'A GET request to a route with a parameter' => [
SpanAssertion::build(
'web.request',
'slim_test_app',
'web',
'GET /parameterized/paramValue'
)->withExactTags([
'slim.route.handler' => 'Closure::__invoke',
'http.method' => 'GET',
'http.url' => 'http://localhost:9999/parameterized/paramValue',
'http.status_code' => '200',
Tag::SPAN_KIND => 'server',
Tag::COMPONENT => 'slim',
Tag::HTTP_ROUTE => '/parameterized/{value}',
])->withChildren([
$this->wrapMiddleware([
SpanAssertion::build(
'slim.route',
'slim_test_app',
'web',
'Closure::__invoke'
)->withExactTags([
Tag::COMPONENT => 'slim',
])
]),
]),
],
]
);
}
Expand Down
Loading