Skip to content

Commit 5c4ed14

Browse files
Yii: Fix span name to include {http.method} {http.route} (#421)
* chore: update dependabot configuration [skip ci] * Updated span name as per otel spec to include http.route * Reverted change in yaml * Fixed CI psalm error --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 9d513a6 commit 5c4ed14

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

src/Instrumentation/Yii/src/YiiInstrumentation.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,14 @@ public static function register(): void
140140
$span = Span::fromContext($scope->context());
141141
$actionName = $action instanceof InlineAction ? $action->actionMethod : $action->id;
142142
$route = YiiInstrumentation::normalizeRouteName(get_class($controller), $actionName);
143+
144+
// Get the HTTP method from the request
145+
$request = $controller->request;
146+
$method = $request->getMethod();
147+
143148
/** @psalm-suppress ArgumentTypeCoercion */
144-
$span->updateName($route);
149+
// Update span name to follow OpenTelemetry HTTP naming convention: {http.method} {http.route}
150+
$span->updateName(sprintf('%s %s', $method, $route));
145151
$span->setAttribute(TraceAttributes::HTTP_ROUTE, $route);
146152
},
147153
post: null

src/Instrumentation/Yii/tests/Integration/YiiInstrumentationTest.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function test_success()
2020

2121
$attributes = $this->storage[0]->getAttributes();
2222
$this->assertCount(1, $this->storage);
23-
$this->assertEquals('SiteController.actionIndex', $this->storage[0]->getName());
23+
$this->assertEquals('GET SiteController.actionIndex', $this->storage[0]->getName());
2424
$this->assertEquals('http://example.com/site/index', $attributes->get(TraceAttributes::URL_FULL));
2525
$this->assertEquals('GET', $attributes->get(TraceAttributes::HTTP_REQUEST_METHOD));
2626
$this->assertEquals('http', $attributes->get(TraceAttributes::URL_SCHEME));
@@ -40,7 +40,7 @@ public function test_non_inline_action()
4040

4141
$attributes = $this->storage[0]->getAttributes();
4242
$this->assertCount(1, $this->storage);
43-
$this->assertEquals('SiteController.error', $this->storage[0]->getName());
43+
$this->assertEquals('GET SiteController.error', $this->storage[0]->getName());
4444
$this->assertEquals('http://example.com/site/error', $attributes->get(TraceAttributes::URL_FULL));
4545
$this->assertEquals('GET', $attributes->get(TraceAttributes::HTTP_REQUEST_METHOD));
4646
$this->assertEquals('http', $attributes->get(TraceAttributes::URL_SCHEME));
@@ -56,7 +56,7 @@ public function test_exception()
5656

5757
$attributes = $this->storage[0]->getAttributes();
5858
$this->assertCount(1, $this->storage);
59-
$this->assertEquals('SiteController.actionThrow', $this->storage[0]->getName());
59+
$this->assertEquals('GET SiteController.actionThrow', $this->storage[0]->getName());
6060
$this->assertEquals('http://example.com/site/throw', $attributes->get(TraceAttributes::URL_FULL));
6161
$this->assertEquals('GET', $attributes->get(TraceAttributes::HTTP_REQUEST_METHOD));
6262
$this->assertEquals('http', $attributes->get(TraceAttributes::URL_SCHEME));
@@ -82,7 +82,7 @@ public function test_parent()
8282

8383
$attributes = $span->getAttributes();
8484
$this->assertCount(1, $this->storage);
85-
$this->assertEquals('SiteController.actionIndex', $this->storage[0]->getName());
85+
$this->assertEquals('GET SiteController.actionIndex', $this->storage[0]->getName());
8686
$this->assertEquals('http://example.com/site/index', $attributes->get(TraceAttributes::URL_FULL));
8787
$this->assertEquals('GET', $attributes->get(TraceAttributes::HTTP_REQUEST_METHOD));
8888
$this->assertEquals('http', $attributes->get(TraceAttributes::URL_SCHEME));
@@ -165,17 +165,25 @@ private function runRequest($path)
165165

166166
class SiteController extends Controller
167167
{
168+
/**
169+
* @return mixed
170+
*/
168171
public function actionIndex()
169172
{
170-
return \Yii::createObject([
173+
/** @var array{class: class-string, format: string, content: string} $config */
174+
$config = [
171175
'class' => 'yii\web\Response',
172176
'format' => \yii\web\Response::FORMAT_RAW,
173177
'content' => 'hello',
174-
]);
178+
];
179+
180+
return \Yii::createObject($config);
175181
}
176182

183+
/** @psalm-suppress MoreSpecificReturnType */
177184
public function actions()
178185
{
186+
/** @psalm-suppress LessSpecificReturnStatement */
179187
return [
180188
'error' => [
181189
'class' => 'yii\web\ErrorAction',

0 commit comments

Comments
 (0)