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

[DO NOT MERGE] Draft implementation for http.route in laminas #2394

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
44 changes: 36 additions & 8 deletions src/Integrations/Integrations/Laminas/LaminasIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
use Laminas\EventManager\EventInterface;
use Laminas\Mvc\MvcEvent;
use Laminas\Router\RouteMatch;
use Laminas\Stdlib\RequestInterface;
use Laminas\Router\SimpleRouteStack;
use Laminas\View\Model\ModelInterface;
use Laminas\Router\RouteStackInterface;

use function DDTrace\active_span;
use function DDTrace\hook_method;
Expand Down Expand Up @@ -242,28 +243,29 @@ function (SpanData $span) {

// MvcEvent::EVENT_ROUTE
trace_method(
'Laminas\Router\RouteInterface',
'match',
function (SpanData $span, $args, $retval) {
'Laminas\Mvc\RouteListener',
'onRoute',
function (SpanData $span, $args, $retval) use ($integration) {
$span->name = 'laminas.route.match';
$span->resource = \get_class($this) . '@match';
$span->meta[Tag::COMPONENT] = 'laminas';

/** @var RequestInterface $request */
$request = $args[0];
/** @var MvcEvent $mvcEvent */
$mvcEvent = $args[0];

$request = $mvcEvent->getRequest();

$rootSpan = root_span();
$rootSpan->meta[Tag::HTTP_METHOD] = $request->getMethod();
$rootSpan->meta[Tag::HTTP_VERSION] = $request->getVersion();
$rootSpan->meta[Tag::HTTP_URL] = Normalizer::urlSanitize($request->getUriString());

/** @var RouteMatch $routeMatch */
$routeMatch = $retval;
if (is_null($routeMatch)) {
return;
}

/** @var RouteMatch $routeMatch */

$routeName = $routeMatch->getMatchedRouteName();
$action = $routeMatch->getParam('action');
$controller = $routeMatch->getParam('controller');
Expand All @@ -285,6 +287,7 @@ function (SpanData $span) use ($controller, $action) {
}
$rootSpan->meta['laminas.route.name'] = $routeName;
$rootSpan->meta['laminas.route.action'] = "$controller@$action";
$integration->setHttpRoute($mvcEvent->getRouter(), $routeName, $rootSpan);
}
);

Expand Down Expand Up @@ -616,4 +619,29 @@ public static function debugBacktraceToString(array $backtrace)
}
return $result;
}

public function setHttpRoute(RouteStackInterface $router, $routeName, \DDTrace\RootSpanData $rootSpan)
{
if (!$router instanceof SimpleRouteStack) {
return;
}

$routes = $router->getRoutes();
$route = '';

foreach (explode('/', $routeName) as &$segment) {
$r = $routes->get($segment);
if (isset($r->regex)){
$route.= $r->regex;
}
else if (isset($r->route)){
$route.= $r->route;
}
else {
// ...
}
}

$rootSpan->meta[Tag::HTTP_ROUTE] = $route;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@
'action' => 'error',
],
]
],
'parameterized' => [
'type' => Literal::class,
'options' => [
'route' => '/parameterized',
],
'child_routes' => [
'child' => [
'type' => Segment::class,
'options' => [
'route' => '/:paramValue',
'defaults' => [
'controller' => Controller\CommonSpecsController::class,
'action' => 'parameterized',
],
],
],
],
]
],
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ public function errorAction()
{
throw new \Exception('Controller error');
}

public function parameterizedAction()
{
$paramValue = $this->params()->fromRoute('paramValue', '');
$response = new Response();
$response->setContent('parameterized ' . $paramValue);
return $response;
}
}
12 changes: 12 additions & 0 deletions tests/Integrations/Laminas/V2_0/CommonScenariosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,16 @@ public function testScenarioGetToMissingRoute()
);
});
}

public function testScenarioParameterized()
{
$this->tracesFromWebRequestSnapshot(function () {
$this->call(
GetSpec::create(
'A GET request to a route with a parameter',
'/parameterized/value'
)->expectStatusCode(200)
);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
"resource": "Application\\Controller\\CommonSpecsController@simple simple",
"trace_id": 0,
"span_id": 1,
"parent_id": 2883696377808074518,
"parent_id": 15784643431827456268,
"type": "web",
"meta": {
"_dd.p.dm": "-0",
"_dd.p.tid": "65645caa00000000",
"component": "laminas",
"http.method": "GET",
"laminas.route.name": "simple",
"http.status_code": "200",
"http.url": "http://localhost:9999/simple?key=value&<redacted>",
"http.version": "1.1",
"laminas.route.action": "Application\\Controller\\CommonSpecsController@simple",
"laminas.route.name": "simple",
"runtime-id": "05e49e0a-5ce8-4a67-9151-78ffaf427ea6",
"span.kind": "server"
},
"metrics": {
Expand Down Expand Up @@ -131,9 +133,9 @@
}
},
{
"name": "laminas.mvcEventListener",
"name": "laminas.route.match",
"service": "test_laminas_20",
"resource": "Laminas\\Mvc\\RouteListener@onRoute",
"resource": "Laminas\\Mvc\\RouteListener@match",
"trace_id": 0,
"span_id": 12,
"parent_id": 7,
Expand All @@ -142,18 +144,6 @@
"component": "laminas"
}
},
{
"name": "laminas.route.match",
"service": "test_laminas_20",
"resource": "Laminas\\Router\\Http\\TreeRouteStack@match",
"trace_id": 0,
"span_id": 17,
"parent_id": 12,
"type": "web",
"meta": {
"component": "laminas"
}
},
{
"name": "laminas.event.dispatch",
"service": "test_laminas_20",
Expand Down Expand Up @@ -195,7 +185,7 @@
"service": "test_laminas_20",
"resource": "Application\\Controller\\CommonSpecsController",
"trace_id": 0,
"span_id": 18,
"span_id": 17,
"parent_id": 14,
"type": "web",
"meta": {
Expand All @@ -207,8 +197,8 @@
"service": "test_laminas_20",
"resource": "Application\\Controller\\CommonSpecsController@onDispatch",
"trace_id": 0,
"span_id": 19,
"parent_id": 18,
"span_id": 18,
"parent_id": 17,
"type": "web",
"meta": {
"component": "laminas"
Expand All @@ -219,8 +209,8 @@
"service": "test_laminas_20",
"resource": "Application\\Controller\\CommonSpecsController@simpleAction",
"trace_id": 0,
"span_id": 20,
"parent_id": 19,
"span_id": 19,
"parent_id": 18,
"type": "web",
"meta": {
"component": "laminas"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
"resource": "GET /does_not_exist",
"trace_id": 0,
"span_id": 1,
"parent_id": 2747341357639439789,
"parent_id": 18444003753463499472,
"type": "web",
"meta": {
"_dd.p.dm": "-0",
"_dd.p.tid": "656460f800000000",
"component": "laminas",
"http.method": "GET",
"http.status_code": "404",
"http.url": "http://localhost:9999/does_not_exist?key=value&<redacted>",
"http.version": "1.1",
"runtime-id": "55b00f79-3f44-47f4-87d4-5b131249c8cd",
"span.kind": "server"
},
"metrics": {
Expand Down Expand Up @@ -129,9 +131,9 @@
}
},
{
"name": "laminas.mvcEventListener",
"name": "laminas.route.match",
"service": "test_laminas_20",
"resource": "Laminas\\Mvc\\RouteListener@onRoute",
"resource": "Laminas\\Mvc\\RouteListener@match",
"trace_id": 0,
"span_id": 11,
"parent_id": 7,
Expand All @@ -140,24 +142,12 @@
"component": "laminas"
}
},
{
"name": "laminas.route.match",
"service": "test_laminas_20",
"resource": "Laminas\\Router\\Http\\TreeRouteStack@match",
"trace_id": 0,
"span_id": 15,
"parent_id": 11,
"type": "web",
"meta": {
"component": "laminas"
}
},
{
"name": "laminas.mvcEvent.setError",
"service": "test_laminas_20",
"resource": "error-router-no-match",
"trace_id": 0,
"span_id": 16,
"span_id": 15,
"parent_id": 11,
"type": "web",
"meta": {
Expand All @@ -169,7 +159,7 @@
"service": "test_laminas_20",
"resource": "Laminas\\Mvc\\View\\Http\\RouteNotFoundStrategy@detectNotFoundError",
"trace_id": 0,
"span_id": 17,
"span_id": 16,
"parent_id": 11,
"type": "web",
"meta": {
Expand All @@ -181,7 +171,7 @@
"service": "test_laminas_20",
"resource": "Laminas\\Mvc\\View\\Http\\RouteNotFoundStrategy@prepareNotFoundViewModel",
"trace_id": 0,
"span_id": 18,
"span_id": 17,
"parent_id": 11,
"type": "web",
"meta": {
Expand All @@ -193,7 +183,7 @@
"service": "test_laminas_20",
"resource": "Laminas\\Mvc\\View\\Http\\ExceptionStrategy@prepareExceptionViewModel",
"trace_id": 0,
"span_id": 19,
"span_id": 18,
"parent_id": 11,
"type": "web",
"meta": {
Expand All @@ -205,7 +195,7 @@
"service": "test_laminas_20",
"resource": "Laminas\\Mvc\\View\\Http\\InjectViewModelListener@injectViewModel",
"trace_id": 0,
"span_id": 20,
"span_id": 19,
"parent_id": 11,
"type": "web",
"meta": {
Expand Down Expand Up @@ -241,7 +231,7 @@
"service": "test_laminas_20",
"resource": "Laminas\\Mvc\\View\\Http\\DefaultRenderingStrategy@render",
"trace_id": 0,
"span_id": 21,
"span_id": 20,
"parent_id": 12,
"type": "web",
"meta": {
Expand All @@ -253,8 +243,8 @@
"service": "test_laminas_20",
"resource": "laminas.view.render",
"trace_id": 0,
"span_id": 23,
"parent_id": 21,
"span_id": 22,
"parent_id": 20,
"type": "web",
"meta": {
"component": "laminas"
Expand All @@ -265,8 +255,8 @@
"service": "test_laminas_20",
"resource": "error/404",
"trace_id": 0,
"span_id": 24,
"parent_id": 23,
"span_id": 23,
"parent_id": 22,
"type": "web",
"meta": {
"component": "laminas"
Expand All @@ -277,8 +267,8 @@
"service": "test_laminas_20",
"resource": "layout/layout",
"trace_id": 0,
"span_id": 25,
"parent_id": 23,
"span_id": 24,
"parent_id": 22,
"type": "web",
"meta": {
"component": "laminas"
Expand All @@ -301,7 +291,7 @@
"service": "test_laminas_20",
"resource": "Laminas\\Mvc\\SendResponseListener@sendResponse",
"trace_id": 0,
"span_id": 22,
"span_id": 21,
"parent_id": 13,
"type": "web",
"meta": {
Expand Down
Loading
Loading