Skip to content

Commit

Permalink
Fix http.route for non-InlineAction (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
agoallikmaa authored Feb 5, 2024
1 parent 324ecfd commit 5e8357b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Instrumentation/Yii/src/YiiInstrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -157,6 +174,15 @@ public function actionIndex()
]);
}

public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
];
}

public function actionThrow()
{
throw new \Exception('Threw');
Expand Down

0 comments on commit 5e8357b

Please sign in to comment.