diff --git a/src/Instrumentation/Yii/src/YiiInstrumentation.php b/src/Instrumentation/Yii/src/YiiInstrumentation.php index a5d5ed62..91759aa9 100644 --- a/src/Instrumentation/Yii/src/YiiInstrumentation.php +++ b/src/Instrumentation/Yii/src/YiiInstrumentation.php @@ -12,6 +12,7 @@ use OpenTelemetry\Context\Context; use function OpenTelemetry\Instrumentation\hook; use OpenTelemetry\SemConv\TraceAttributes; +use yii\base\InlineAction; use yii\web\Application; use yii\web\Controller; use yii\web\Response; @@ -134,7 +135,8 @@ public static function register(): void } $span = Span::fromContext($scope->context()); - $route = YiiInstrumentation::normalizeRouteName(get_class($controller), $action->actionMethod); + $actionName = $action instanceof InlineAction ? $action->actionMethod : $action->id; + $route = YiiInstrumentation::normalizeRouteName(get_class($controller), $actionName); /** @psalm-suppress ArgumentTypeCoercion */ $span->updateName($route); $span->setAttribute(TraceAttributes::HTTP_ROUTE, $route); @@ -157,14 +159,14 @@ protected static function getResponseLength(Response $response): ?string return null; } - protected static function normalizeRouteName(string $controllerClassName, string $controllerMethod): string + protected static function normalizeRouteName(string $controllerClassName, string $actionName): string { $lastSegment = strrchr($controllerClassName, '\\'); if ($lastSegment === false) { - return $controllerClassName . '.' . $controllerMethod; + return $controllerClassName . '.' . $actionName; } - return substr($lastSegment, 1) . '.' . $controllerMethod; + return substr($lastSegment, 1) . '.' . $actionName; } } diff --git a/src/Instrumentation/Yii/tests/Integration/YiiInstrumentationTest.php b/src/Instrumentation/Yii/tests/Integration/YiiInstrumentationTest.php index cb76b9e7..243c17c5 100644 --- a/src/Instrumentation/Yii/tests/Integration/YiiInstrumentationTest.php +++ b/src/Instrumentation/Yii/tests/Integration/YiiInstrumentationTest.php @@ -30,6 +30,23 @@ public function test_success() $this->assertGreaterThan(0, $attributes->get(TraceAttributes::HTTP_RESPONSE_BODY_SIZE)); } + public function test_non_inline_action() + { + $exception = $this->runRequest('/site/error'); + + // This is thrown from ErrorAction that does not extend InlineAction as it is not a controller method + $this->assertNotNull($exception); + $this->assertEquals('yii\base\ViewNotFoundException', get_class($exception)); + + $attributes = $this->storage[0]->getAttributes(); + $this->assertCount(1, $this->storage); + $this->assertEquals('SiteController.error', $this->storage[0]->getName()); + $this->assertEquals('http://example.com/site/error', $attributes->get(TraceAttributes::URL_FULL)); + $this->assertEquals('GET', $attributes->get(TraceAttributes::HTTP_REQUEST_METHOD)); + $this->assertEquals('http', $attributes->get(TraceAttributes::URL_SCHEME)); + $this->assertEquals('SiteController.error', $attributes->get(TraceAttributes::HTTP_ROUTE)); + } + public function test_exception() { $exception = $this->runRequest('/site/throw'); @@ -157,6 +174,15 @@ public function actionIndex() ]); } + public function actions() + { + return [ + 'error' => [ + 'class' => 'yii\web\ErrorAction', + ], + ]; + } + public function actionThrow() { throw new \Exception('Threw');