Skip to content

Commit

Permalink
fix(Instrumentation/TraceableRouteLoader): undefined method named "ge…
Browse files Browse the repository at this point in the history
…tDeclaringClass" of class "ReflectionClass" (#91)
  • Loading branch information
Kaspiman authored Dec 22, 2024
1 parent 8211d43 commit 0afa9b3
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ private static function parseAttribute(Route $route): void
return;
}

$attribute = $reflection->getAttributes(Traceable::class)[0] ?? $reflection->getDeclaringClass()->getAttributes(Traceable::class)[0] ?? null;
if ($reflection instanceof \ReflectionMethod) {
$attribute = $reflection->getAttributes(Traceable::class)[0] ?? $reflection->getDeclaringClass()->getAttributes(Traceable::class)[0] ?? null;
} else {
$attribute = $reflection->getAttributes(Traceable::class)[0] ?? null;
}

if (null !== $attribute) {
$traceable = $attribute->newInstance();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\Tests\Functional\Application\Controller;

use FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\Attribute\Traceable;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

#[Traceable]
class ClassInvokeTraceableController extends AbstractController
{
#[Route('/class-invoke-traceable', methods: ['GET'])]
public function __invoke(): Response
{
return $this->json([
'status' => 'ok',
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class ClassTraceableController extends AbstractController
{
#[Route('/class-traceable', methods: ['GET'])]
public function __invoke(): Response
public function index(): Response
{
return $this->json([
'status' => 'ok',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\Tests\Functional\Application\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class NotTraceableClassController extends AbstractController
{
#[Route('/not-traceable-class', methods: ['GET'])]
public function index(): Response
{
return new Response(null, Response::HTTP_FOUND);
}
}
45 changes: 43 additions & 2 deletions tests/Functional/Instrumentation/HttpKernelTracingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,37 @@ public function testException(): void
]);
}

public function testTraceableClassInvoke(): void
{
$client = static::createClient();
$client->request('GET', '/class-invoke-traceable');

static::assertResponseIsSuccessful();
static::assertSame('{"status":"ok"}', $client->getResponse()->getContent());

self::assertSpansCount(1);

$mainSpan = self::getSpans()[0];
self::assertSpanName($mainSpan, 'friendsofopentelemetry_opentelemetry_tests_functional_application_classinvoketraceable__invoke');
self::assertSpanStatus($mainSpan, StatusData::ok());
self::assertSpanAttributes($mainSpan, [
'url.full' => 'http://localhost/class-invoke-traceable',
'http.request.method' => 'GET',
'url.path' => '/class-invoke-traceable',
'symfony.kernel.http.host' => 'localhost',
'url.scheme' => 'http',
'network.protocol.version' => '1.1',
'user_agent.original' => 'Symfony BrowserKit',
'network.peer.address' => '127.0.0.1',
'symfony.kernel.net.peer_ip' => '127.0.0.1',
'server.address' => 'localhost',
'server.port' => 80,
'http.route' => 'friendsofopentelemetry_opentelemetry_tests_functional_application_classinvoketraceable__invoke',
'http.response.status_code' => Response::HTTP_OK,
]);
self::assertSpanEventsCount($mainSpan, 0);
}

public function testTraceableClass(): void
{
$client = static::createClient();
Expand All @@ -126,7 +157,7 @@ public function testTraceableClass(): void
self::assertSpansCount(1);

$mainSpan = self::getSpans()[0];
self::assertSpanName($mainSpan, 'friendsofopentelemetry_opentelemetry_tests_functional_application_classtraceable__invoke');
self::assertSpanName($mainSpan, 'friendsofopentelemetry_opentelemetry_tests_functional_application_classtraceable_index');
self::assertSpanStatus($mainSpan, StatusData::ok());
self::assertSpanAttributes($mainSpan, [
'url.full' => 'http://localhost/class-traceable',
Expand All @@ -140,7 +171,7 @@ public function testTraceableClass(): void
'symfony.kernel.net.peer_ip' => '127.0.0.1',
'server.address' => 'localhost',
'server.port' => 80,
'http.route' => 'friendsofopentelemetry_opentelemetry_tests_functional_application_classtraceable__invoke',
'http.route' => 'friendsofopentelemetry_opentelemetry_tests_functional_application_classtraceable_index',
'http.response.status_code' => Response::HTTP_OK,
]);
self::assertSpanEventsCount($mainSpan, 0);
Expand All @@ -156,6 +187,16 @@ public function testNotTraceable(): void
self::assertSpansCount(0);
}

public function testNotTraceableClass(): void
{
$client = static::createClient();
$client->request('GET', '/not-traceable-class');

self::assertSame(Response::HTTP_FOUND, $client->getResponse()->getStatusCode());

self::assertSpansCount(0);
}

public function testTraceableFallback(): void
{
$client = static::createClient();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\Tests\Unit\Instrumentation\Symfony\Routing;

use Symfony\Component\Routing\Attribute\Route;

class NotTraceableClassInvokeController
{
#[Route('/not-traceable-class-invoke', name: 'action')]
public function __invoke(): void
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class TraceableActionController
{
#[Traceable('test')]
#[Route('/path', name: 'action')]
#[Route('/traceable-action', name: 'action')]
public function action(): void
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#[Traceable('test')]
class TraceableClassController
{
#[Route('/path', name: 'action')]
public function __invoke(): void
#[Route('/traceable-class', name: 'action')]
public function index(): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\Tests\Unit\Instrumentation\Symfony\Routing;

use FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\Attribute\Traceable;
use Symfony\Component\Routing\Attribute\Route;

#[Traceable('test')]
class TraceableClassInvokeController
{
#[Route('/traceable-class-invoke', name: 'action')]
public function __invoke(): void
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function testTraceableAction(): void
{
$routes = $this->loader->load(TraceableActionController::class);
self::assertCount(1, $routes);
self::assertEquals('/path', $routes->get('action')->getPath());
self::assertEquals('/traceable-action', $routes->get('action')->getPath());
self::assertEquals([
'_traceable' => true,
'_controller' => 'FriendsOfOpenTelemetry\OpenTelemetryBundle\Tests\Unit\Instrumentation\Symfony\Routing\TraceableActionController::action',
Expand All @@ -35,11 +35,33 @@ public function testTraceableClass(): void
{
$routes = $this->loader->load(TraceableClassController::class);
self::assertCount(1, $routes);
self::assertEquals('/path', $routes->get('action')->getPath());
self::assertEquals('/traceable-class', $routes->get('action')->getPath());
self::assertEquals([
'_traceable' => true,
'_controller' => 'FriendsOfOpenTelemetry\OpenTelemetryBundle\Tests\Unit\Instrumentation\Symfony\Routing\TraceableClassController',
'_controller' => 'FriendsOfOpenTelemetry\OpenTelemetryBundle\Tests\Unit\Instrumentation\Symfony\Routing\TraceableClassController::index',
'_tracer' => 'test',
], $routes->get('action')->getDefaults());
}

public function testTraceableClassInvoke(): void
{
$routes = $this->loader->load(TraceableClassInvokeController::class);
self::assertCount(1, $routes);
self::assertEquals('/traceable-class-invoke', $routes->get('action')->getPath());
self::assertEquals([
'_traceable' => true,
'_controller' => 'FriendsOfOpenTelemetry\OpenTelemetryBundle\Tests\Unit\Instrumentation\Symfony\Routing\TraceableClassInvokeController',
'_tracer' => 'test',
], $routes->get('action')->getDefaults());
}

public function testNotTraceableTraceableClassInvoke(): void
{
$routes = $this->loader->load(NotTraceableClassInvokeController::class);
self::assertCount(1, $routes);
self::assertEquals('/not-traceable-class-invoke', $routes->get('action')->getPath());
self::assertEquals([
'_controller' => 'FriendsOfOpenTelemetry\OpenTelemetryBundle\Tests\Unit\Instrumentation\Symfony\Routing\NotTraceableClassInvokeController',
], $routes->get('action')->getDefaults());
}
}

0 comments on commit 0afa9b3

Please sign in to comment.