Skip to content

Commit 061882b

Browse files
Implement http.route for Slim (#2402)
Implement http.route for Slim
1 parent b032b1e commit 061882b

File tree

5 files changed

+92
-9
lines changed

5 files changed

+92
-9
lines changed

Diff for: src/Integrations/Integrations/Slim/SlimIntegration.php

+16-3
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,28 @@ function ($errorMiddleware, $self, $args) use ($rootSpan, $integration) {
9191
'lookupRoute',
9292
null,
9393
function ($router, $scope, $args, $return) use ($rootSpan) {
94+
/** @var \Slim\Interfaces\RouteInterface $return */
95+
$rootSpan->meta[Tag::HTTP_ROUTE] = $return->getPattern();
96+
9497
if (PHP_VERSION_ID < 70000 || dd_trace_env_config("DD_HTTP_SERVER_ROUTE_BASED_NAMING")) {
95-
/** @var \Slim\Interfaces\RouteInterface $route */
96-
$route = $return;
9798
$rootSpan->resource =
98-
$_SERVER['REQUEST_METHOD'] . ' ' . ($route->getName() ?: $route->getPattern());
99+
$_SERVER['REQUEST_METHOD'] . ' ' . ($return->getName() ?: $return->getPattern());
99100
}
100101
}
101102
);
102103
}
104+
else if ('4' === $majorVersion) {
105+
\DDTrace\hook_method(
106+
'Slim\\Routing\\RouteCollector',
107+
'lookupRoute',
108+
null,
109+
function ($router, $scope, $args, $return) use ($rootSpan) {
110+
/** @var \Slim\Interfaces\RouteInterface $route */
111+
$route = $return;
112+
$rootSpan->meta[Tag::HTTP_ROUTE] = $route->getPattern();
113+
}
114+
);
115+
}
103116

104117
// Providing info about the controller
105118
$traceControllers = function (SpanData $span, $args) use ($rootSpan, $appName, $majorVersion) {

Diff for: tests/Frameworks/Slim/Version_3_12/src/routes.php

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
throw new \Exception('Foo error');
1818
});
1919

20+
$app->get('/parameterized/{value}', function (Request $request, Response $response, array $args) {
21+
$value = $args['value'];
22+
$response->getBody()->write("Hello, $value");
23+
return $response;
24+
});
25+
2026
$app->get('/[{name}]', function (Request $request, Response $response, array $args) use ($container) {
2127
// Sample log message
2228
$container->get('logger')->info("Slim-Skeleton '/' route");

Diff for: tests/Frameworks/Slim/Version_4/app/routes.php

+6
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,10 @@
4545
$app->get('/error', function (Request $request, Response $response, $args) {
4646
throw new \Exception('Foo error');
4747
});
48+
49+
$app->get('/parameterized/{value}', function (Request $request, Response $response, $args) {
50+
$value = $args['value'];
51+
$response->getBody()->write("Hello, $value");
52+
return $response;
53+
});
4854
};

Diff for: tests/Integrations/Slim/V3_12/CommonScenariosTest.php

+31-3
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ public function provideSpecs()
112112
'http.url' => 'http://localhost:9999/simple?key=value&<redacted>',
113113
'http.status_code' => '200',
114114
Tag::SPAN_KIND => 'server',
115-
Tag::COMPONENT => 'slim'
115+
Tag::COMPONENT => 'slim',
116+
Tag::HTTP_ROUTE => '/simple',
116117
])->withChildren([
117118
SpanAssertion::build(
118119
'slim.route.controller',
@@ -136,7 +137,8 @@ public function provideSpecs()
136137
'http.url' => 'http://localhost:9999/simple_view?key=value&<redacted>',
137138
'http.status_code' => '200',
138139
Tag::SPAN_KIND => 'server',
139-
Tag::COMPONENT => 'slim'
140+
Tag::COMPONENT => 'slim',
141+
Tag::HTTP_ROUTE => '/simple_view',
140142
])->withChildren([
141143
SpanAssertion::build(
142144
'slim.route.controller',
@@ -170,7 +172,8 @@ public function provideSpecs()
170172
'http.url' => 'http://localhost:9999/error?key=value&<redacted>',
171173
'http.status_code' => '500',
172174
Tag::SPAN_KIND => 'server',
173-
Tag::COMPONENT => 'slim'
175+
Tag::COMPONENT => 'slim',
176+
Tag::HTTP_ROUTE => '/error',
174177
])->setError(null, null)
175178
->withChildren([
176179
SpanAssertion::build(
@@ -185,6 +188,31 @@ public function provideSpecs()
185188
])->setError(null, 'Foo error')
186189
]),
187190
],
191+
'A GET request to a route with a parameter' => [
192+
SpanAssertion::build(
193+
'slim.request',
194+
'slim_test_app',
195+
'web',
196+
'GET /parameterized/{value}'
197+
)->withExactTags([
198+
'slim.route.controller' => 'Closure::__invoke',
199+
'http.method' => 'GET',
200+
'http.url' => 'http://localhost:9999/parameterized/paramValue',
201+
'http.status_code' => '200',
202+
Tag::SPAN_KIND => 'server',
203+
Tag::COMPONENT => 'slim',
204+
Tag::HTTP_ROUTE => '/parameterized/{value}',
205+
])->withChildren([
206+
SpanAssertion::build(
207+
'slim.route.controller',
208+
'slim_test_app',
209+
'web',
210+
'Closure::__invoke'
211+
)->withExactTags([
212+
Tag::COMPONENT => 'slim'
213+
])
214+
]),
215+
],
188216
]
189217
);
190218
}

Diff for: tests/Integrations/Slim/V4/CommonScenariosTest.php

+33-3
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ public function provideSpecs()
115115
'http.url' => 'http://localhost:9999/simple?key=value&<redacted>',
116116
'http.status_code' => '200',
117117
Tag::SPAN_KIND => 'server',
118-
Tag::COMPONENT => 'slim'
118+
Tag::COMPONENT => 'slim',
119+
Tag::HTTP_ROUTE => '/simple',
119120
])->withChildren([
120121
$this->wrapMiddleware([
121122
SpanAssertion::build(
@@ -142,7 +143,8 @@ public function provideSpecs()
142143
'http.url' => 'http://localhost:9999/simple_view?key=value&<redacted>',
143144
'http.status_code' => '200',
144145
Tag::SPAN_KIND => 'server',
145-
Tag::COMPONENT => 'slim'
146+
Tag::COMPONENT => 'slim',
147+
Tag::HTTP_ROUTE => '/simple_view',
146148
])->withChildren([
147149
$this->wrapMiddleware([
148150
SpanAssertion::build(
@@ -178,7 +180,8 @@ public function provideSpecs()
178180
'http.url' => 'http://localhost:9999/error?key=value&<redacted>',
179181
'http.status_code' => '500',
180182
Tag::SPAN_KIND => 'server',
181-
Tag::COMPONENT => 'slim'
183+
Tag::COMPONENT => 'slim',
184+
Tag::HTTP_ROUTE => '/error',
182185
])
183186
->setError(null, null)
184187
->withChildren([
@@ -199,6 +202,33 @@ public function provideSpecs()
199202
)
200203
]),
201204
],
205+
'A GET request to a route with a parameter' => [
206+
SpanAssertion::build(
207+
'web.request',
208+
'slim_test_app',
209+
'web',
210+
'GET /parameterized/paramValue'
211+
)->withExactTags([
212+
'slim.route.handler' => 'Closure::__invoke',
213+
'http.method' => 'GET',
214+
'http.url' => 'http://localhost:9999/parameterized/paramValue',
215+
'http.status_code' => '200',
216+
Tag::SPAN_KIND => 'server',
217+
Tag::COMPONENT => 'slim',
218+
Tag::HTTP_ROUTE => '/parameterized/{value}',
219+
])->withChildren([
220+
$this->wrapMiddleware([
221+
SpanAssertion::build(
222+
'slim.route',
223+
'slim_test_app',
224+
'web',
225+
'Closure::__invoke'
226+
)->withExactTags([
227+
Tag::COMPONENT => 'slim',
228+
])
229+
]),
230+
]),
231+
],
202232
]
203233
);
204234
}

0 commit comments

Comments
 (0)