Skip to content

Commit be6ffdf

Browse files
CodeIgniter: Fix span name to include {http.method} {http.route} (open-telemetry#422)
* Fix CodeIgniter spanName as per Otel spec * Update src/Instrumentation/CodeIgniter/src/CodeIgniterInstrumentation.php --------- Co-authored-by: Chris Lightfoot-Wild <[email protected]>
1 parent 9992837 commit be6ffdf

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/Instrumentation/CodeIgniter/src/CodeIgniterInstrumentation.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
class CodeIgniterInstrumentation
2121
{
2222
public const NAME = 'codeigniter';
23-
23+
24+
// Store the HTTP method for use in the post hook
25+
private static $httpMethod = 'unknown';
26+
2427
/** @psalm-api */
2528
public static function register(): void
2629
{
@@ -58,12 +61,14 @@ public static function register(): void
5861
) use ($instrumentation, $requestProperty): void {
5962
$extractedRequest = $requestProperty->getValue($igniter);
6063
$request = ($extractedRequest instanceof RequestInterface) ? $extractedRequest : null;
61-
64+
65+
// Get the HTTP method from the request and store it for later use
66+
self::$httpMethod = $request?->getMethod() ?? $_SERVER['REQUEST_METHOD'] ?? 'unknown';
6267
/** @psalm-suppress ArgumentTypeCoercion,DeprecatedMethod */
6368
$spanBuilder = $instrumentation
6469
->tracer()
6570
/** @phan-suppress-next-line PhanDeprecatedFunction */
66-
->spanBuilder(\sprintf('%s', $request?->getMethod() ?? 'unknown'))
71+
->spanBuilder(\sprintf('%s', self::$httpMethod))
6772
->setSpanKind(SpanKind::KIND_SERVER)
6873
->setAttribute(TraceAttributes::CODE_FUNCTION_NAME, sprintf('%s::%s', $class, $function))
6974
->setAttribute(TraceAttributes::CODE_FILE_PATH, $filename)
@@ -147,6 +152,7 @@ public static function register(): void
147152
if ($controllerClassName !== null && is_string($controllerMethod)) {
148153
$routeName = CodeIgniterInstrumentation::normalizeRouteName($controllerClassName, $controllerMethod);
149154
$span->setAttribute(TraceAttributes::HTTP_ROUTE, $routeName);
155+
$span->updateName(sprintf('%s %s', self::$httpMethod, $routeName));
150156
}
151157

152158
if ($exception) {

src/Instrumentation/CodeIgniter/tests/Integration/CodeIgniterInstrumentationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function test_success()
2525

2626
$attributes = $this->storage[0]->getAttributes();
2727
$this->assertCount(1, $this->storage);
28-
$this->assertEqualsIgnoringCase('GET', $this->storage[0]->getName());
28+
$this->assertEqualsIgnoringCase('GET Home.index', $this->storage[0]->getName());
2929
$this->assertStringMatchesFormat('http://%s/home', $attributes->get(TraceAttributes::URL_FULL));
3030
$this->assertEqualsIgnoringCase('GET', $attributes->get(TraceAttributes::HTTP_REQUEST_METHOD));
3131
$this->assertEquals('http', $attributes->get(TraceAttributes::URL_SCHEME));
@@ -55,7 +55,7 @@ public function test_exception()
5555

5656
$attributes = $this->storage[0]->getAttributes();
5757
$this->assertCount(1, $this->storage);
58-
$this->assertEqualsIgnoringCase('GET', $this->storage[0]->getName());
58+
$this->assertEqualsIgnoringCase('GET Closure.index', $this->storage[0]->getName());
5959
$this->assertStringMatchesFormat('http://%s/exception', $attributes->get(TraceAttributes::URL_FULL));
6060
$this->assertEqualsIgnoringCase('GET', $attributes->get(TraceAttributes::HTTP_REQUEST_METHOD));
6161
$this->assertEquals('http', $attributes->get(TraceAttributes::URL_SCHEME));

0 commit comments

Comments
 (0)