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

Add http.route to CodeIgniter #2379

Merged
merged 5 commits into from
Dec 6, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ function (SpanData $span, $args, $retval, $ex) use ($service, &$registered_cache
return false;
}
);

self::setHttpRoute($router, $rootSpan);
}

/**
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
$route['404_override'] = '';
$route['error'] = 'error_';

$route['parameterized/(:any)'] = "parameterized/customAction/$1";

/* End of file routes.php */
/* Location: ./application/config/routes.php */
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

class Parameterized extends CI_Controller {
function customAction($param) {
echo 'custom ' . $param;
}
}
28 changes: 28 additions & 0 deletions tests/Integrations/CodeIgniter/V2_2/CommonScenariosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function provideSpecs()
'app.endpoint' => 'Simple::index',
Tag::SPAN_KIND => 'server',
Tag::COMPONENT => 'codeigniter',
Tag::HTTP_ROUTE => 'simple',
])->withChildren([
SpanAssertion::build(
'Simple.index',
Expand All @@ -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',
Expand Down Expand Up @@ -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'])
Expand All @@ -127,6 +130,31 @@ 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',
'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',
])
]),
],
]
);
}
Expand Down
1 change: 1 addition & 0 deletions tests/Integrations/CodeIgniter/V2_2/ExitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Loading