From c5e3e334be983844ac472daaf7959fb087712998 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Marti=CC=81nez=20Bernardo?= Date: Tue, 21 Nov 2023 15:16:47 +0100 Subject: [PATCH 1/3] Add http.route to CodeIgniter --- .../CodeIgniter/V2/CodeIgniterIntegration.php | 36 +++++++++++++++++++ .../Version_2_2/application/config/routes.php | 1 + .../application/controllers/parameterized.php | 7 ++++ .../CodeIgniter/V2_2/CommonScenariosTest.php | 18 ++++++++++ .../CodeIgniter/V2_2/ExitTest.php | 1 + .../CodeIgniter/V2_2/NoCI_ControllertTest.php | 1 + 6 files changed, 64 insertions(+) create mode 100644 tests/Frameworks/CodeIgniter/Version_2_2/application/controllers/parameterized.php diff --git a/src/Integrations/Integrations/CodeIgniter/V2/CodeIgniterIntegration.php b/src/Integrations/Integrations/CodeIgniter/V2/CodeIgniterIntegration.php index 0baad99465..ff02475302 100644 --- a/src/Integrations/Integrations/CodeIgniter/V2/CodeIgniterIntegration.php +++ b/src/Integrations/Integrations/CodeIgniter/V2/CodeIgniterIntegration.php @@ -165,6 +165,8 @@ function (SpanData $span, $args, $retval, $ex) use ($service, &$registered_cache return false; } ); + + self::setHttpRoute($router, $rootSpan); } /** @@ -225,4 +227,38 @@ function (SpanData $span, $args, $retval, $ex) use ($adapter, $service) { } ); } + + /* + * Replicate CodeIgniter's route parsing, as matching key is never stored or returned in the framework. + */ + private static function setHttpRoute($router, $rootSpan) { + // Turn the segment array into a URI string + $uri = implode('/', $router->uri->segments); + + // Is there a literal match? If so we're done + if (isset($router->routes[$uri])) + { + $rootSpan->meta[Tag::HTTP_ROUTE] = $uri; + return; + } + + // Loop through the route array looking for wild-cards + foreach ($router->routes as $key => $val) + { + $origKey = $key; + // Convert wild-cards to RegEx + $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key)); + + // Does the RegEx match? + if (preg_match('#^'.$key.'$#', $uri)) + { + $rootSpan->meta[Tag::HTTP_ROUTE] = $origKey; + return; + } + } + + // If we got this far it means we didn't encounter a + // matching route so we'll set the site default route + $rootSpan->meta[Tag::HTTP_ROUTE] = $uri; + } } diff --git a/tests/Frameworks/CodeIgniter/Version_2_2/application/config/routes.php b/tests/Frameworks/CodeIgniter/Version_2_2/application/config/routes.php index 04d463059d..670b113a3d 100644 --- a/tests/Frameworks/CodeIgniter/Version_2_2/application/config/routes.php +++ b/tests/Frameworks/CodeIgniter/Version_2_2/application/config/routes.php @@ -42,6 +42,7 @@ $route['404_override'] = ''; $route['error'] = 'error_'; +$route['parameterized/(:any)'] = "parameterized/$1"; /* End of file routes.php */ /* Location: ./application/config/routes.php */ diff --git a/tests/Frameworks/CodeIgniter/Version_2_2/application/controllers/parameterized.php b/tests/Frameworks/CodeIgniter/Version_2_2/application/controllers/parameterized.php new file mode 100644 index 0000000000..18521b5cba --- /dev/null +++ b/tests/Frameworks/CodeIgniter/Version_2_2/application/controllers/parameterized.php @@ -0,0 +1,7 @@ + 'Simple::index', Tag::SPAN_KIND => 'server', Tag::COMPONENT => 'codeigniter', + Tag::HTTP_ROUTE => 'simple', ])->withChildren([ SpanAssertion::build( 'Simple.index', @@ -79,6 +80,7 @@ public function provideSpecs() 'app.endpoint' => 'Simple_View::index', Tag::SPAN_KIND => 'server', Tag::COMPONENT => 'codeigniter', + Tag::HTTP_ROUTE => 'simple_view', ])->withChildren([ SpanAssertion::build( 'Simple_View.index', @@ -113,6 +115,7 @@ public function provideSpecs() 'app.endpoint' => 'Error_::index', Tag::SPAN_KIND => 'server', Tag::COMPONENT => 'codeigniter', + Tag::HTTP_ROUTE => 'error' ]) ->setError("Exception", "Uncaught Exception: datadog in %s:%d") ->withExistingTagsNames(['error.stack']) @@ -127,6 +130,21 @@ public function provideSpecs() ])->setError('Exception', 'datadog', true), ]), ], + 'A GET request to a route with a parameter' => [ + SpanAssertion::build( + 'codeigniter.request', + 'codeigniter_test_app', + 'web', + 'GET /parameterized/paramValue' + )->withExactTags([ + Tag::HTTP_METHOD => 'GET', + Tag::HTTP_URL => 'http://localhost:9999/parameterized/paramValue', + Tag::HTTP_STATUS_CODE => '200', + Tag::SPAN_KIND => 'server', + Tag::COMPONENT => 'codeigniter', + Tag::HTTP_ROUTE => 'parameterized/(:any)', + ]), + ], ] ); } diff --git a/tests/Integrations/CodeIgniter/V2_2/ExitTest.php b/tests/Integrations/CodeIgniter/V2_2/ExitTest.php index 574e5a86ff..c243b5c8ef 100644 --- a/tests/Integrations/CodeIgniter/V2_2/ExitTest.php +++ b/tests/Integrations/CodeIgniter/V2_2/ExitTest.php @@ -43,6 +43,7 @@ public function testScenario() 'app.endpoint' => 'Exits::index', Tag::SPAN_KIND => 'server', Tag::COMPONENT => 'codeigniter', + Tag::HTTP_ROUTE => 'exits' ])->withChildren([ SpanAssertion::build( 'Exits.index', diff --git a/tests/Integrations/CodeIgniter/V2_2/NoCI_ControllertTest.php b/tests/Integrations/CodeIgniter/V2_2/NoCI_ControllertTest.php index a0314b8ee7..7f57ba39f3 100644 --- a/tests/Integrations/CodeIgniter/V2_2/NoCI_ControllertTest.php +++ b/tests/Integrations/CodeIgniter/V2_2/NoCI_ControllertTest.php @@ -42,6 +42,7 @@ public function testScenario() Tag::HTTP_STATUS_CODE => '200', Tag::SPAN_KIND => 'server', Tag::COMPONENT => 'codeigniter', + Tag::HTTP_ROUTE => 'health_check/ping', ])->withChildren([ SpanAssertion::build( 'Health_check.ping', From f21ec6c7f7a56db98f2d4c07903718a6ed603dcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Marti=CC=81nez=20Bernardo?= Date: Tue, 21 Nov 2023 15:27:32 +0100 Subject: [PATCH 2/3] String concat in php doesn't use + --- .../Version_2_2/application/controllers/parameterized.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Frameworks/CodeIgniter/Version_2_2/application/controllers/parameterized.php b/tests/Frameworks/CodeIgniter/Version_2_2/application/controllers/parameterized.php index 18521b5cba..582c8e9fa1 100644 --- a/tests/Frameworks/CodeIgniter/Version_2_2/application/controllers/parameterized.php +++ b/tests/Frameworks/CodeIgniter/Version_2_2/application/controllers/parameterized.php @@ -2,6 +2,6 @@ class Parameterized extends CI_Controller { function customAction($param) { - echo 'custom ' + $param; + echo 'custom ' . $param; } } From 6ebff7b2770c1e25541b2b13ffe632787ba774a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Marti=CC=81nez=20Bernardo?= Date: Tue, 21 Nov 2023 17:28:31 +0100 Subject: [PATCH 3/3] Use routes correctly --- .../Version_2_2/application/config/routes.php | 2 +- .../CodeIgniter/V2_2/CommonScenariosTest.php | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/Frameworks/CodeIgniter/Version_2_2/application/config/routes.php b/tests/Frameworks/CodeIgniter/Version_2_2/application/config/routes.php index 670b113a3d..fa8ba84562 100644 --- a/tests/Frameworks/CodeIgniter/Version_2_2/application/config/routes.php +++ b/tests/Frameworks/CodeIgniter/Version_2_2/application/config/routes.php @@ -42,7 +42,7 @@ $route['404_override'] = ''; $route['error'] = 'error_'; -$route['parameterized/(:any)'] = "parameterized/$1"; +$route['parameterized/(:any)'] = "parameterized/customAction/$1"; /* End of file routes.php */ /* Location: ./application/config/routes.php */ diff --git a/tests/Integrations/CodeIgniter/V2_2/CommonScenariosTest.php b/tests/Integrations/CodeIgniter/V2_2/CommonScenariosTest.php index 3d91915a9c..649e479861 100644 --- a/tests/Integrations/CodeIgniter/V2_2/CommonScenariosTest.php +++ b/tests/Integrations/CodeIgniter/V2_2/CommonScenariosTest.php @@ -140,9 +140,19 @@ public function provideSpecs() Tag::HTTP_METHOD => 'GET', Tag::HTTP_URL => 'http://localhost:9999/parameterized/paramValue', Tag::HTTP_STATUS_CODE => '200', + 'app.endpoint' => 'Parameterized::customAction', Tag::SPAN_KIND => 'server', Tag::COMPONENT => 'codeigniter', Tag::HTTP_ROUTE => 'parameterized/(:any)', + ])->withChildren([ + SpanAssertion::build( + 'Parameterized.customAction', + 'codeigniter_test_app', + Type::WEB_SERVLET, + 'Parameterized.customAction' + )->withExactTags([ + Tag::COMPONENT => 'codeigniter', + ]) ]), ], ]