Skip to content

Commit

Permalink
handle symfony subrequests (#262)
Browse files Browse the repository at this point in the history
- kind=internal
- name contains controller
  • Loading branch information
brettmc authored May 1, 2024
1 parent e89dc2a commit 92cf684
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"readme": "./README.md",
"license": "Apache-2.0",
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^8.0",
"ext-opentelemetry": "*",
Expand Down
10 changes: 8 additions & 2 deletions src/SymfonyInstrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;

final class SymfonyInstrumentation
{
Expand All @@ -37,11 +38,16 @@ public static function register(): void
?int $lineno,
) use ($instrumentation): array {
$request = ($params[0] instanceof Request) ? $params[0] : null;
$type = $params[1] ?? HttpKernelInterface::MAIN_REQUEST;
$method = $request?->getMethod() ?? 'unknown';
$name = ($type === HttpKernelInterface::SUB_REQUEST)
? sprintf('%s %s', $method, $request?->attributes?->get('_controller') ?? 'sub-request')
: $method;
/** @psalm-suppress ArgumentTypeCoercion */
$builder = $instrumentation
->tracer()
->spanBuilder(\sprintf('%s', $request?->getMethod() ?? 'unknown'))
->setSpanKind(SpanKind::KIND_SERVER)
->spanBuilder($name)
->setSpanKind(($type === HttpKernelInterface::SUB_REQUEST) ? SpanKind::KIND_INTERNAL : SpanKind::KIND_SERVER)
->setAttribute(TraceAttributes::CODE_FUNCTION, $function)
->setAttribute(TraceAttributes::CODE_NAMESPACE, $class)
->setAttribute(TraceAttributes::CODE_FILEPATH, $filename)
Expand Down
2 changes: 2 additions & 0 deletions tests/Integration/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use OpenTelemetry\API\Instrumentation\Configurator;
use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator;
use OpenTelemetry\Context\ScopeInterface;
use OpenTelemetry\SDK\Trace\ImmutableSpan;
use OpenTelemetry\SDK\Trace\SpanExporter\InMemoryExporter;
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
use OpenTelemetry\SDK\Trace\TracerProvider;
Expand All @@ -16,6 +17,7 @@
abstract class AbstractTest extends TestCase
{
private ScopeInterface $scope;
/** @var ArrayObject<int, ImmutableSpan> $storage */
protected ArrayObject $storage;

public function setUp(): void
Expand Down
18 changes: 18 additions & 0 deletions tests/Integration/SymfonyInstrumentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace OpenTelemetry\Tests\Instrumentation\Symfony\tests\Integration;

use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\Contrib\Propagation\ServerTiming\ServerTimingPropagator;
use OpenTelemetry\Contrib\Propagation\TraceResponse\TraceResponsePropagator;
use OpenTelemetry\SDK\Trace\ImmutableSpan;
use OpenTelemetry\SemConv\TraceAttributes;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
Expand Down Expand Up @@ -164,6 +166,22 @@ public function test_http_kernel_handle_without_route(): void
);
}

public function test_http_kernel_handle_subrequest(): void
{
$kernel = $this->getHttpKernel(new EventDispatcher());
$this->assertCount(0, $this->storage);
$request = new Request();
$request->attributes->set('_controller', 'ErrorController');

$kernel->handle($request, HttpKernelInterface::SUB_REQUEST);
$this->assertCount(1, $this->storage);

/** @var ImmutableSpan $span */
$span = $this->storage[0];
$this->assertSame('GET ErrorController', $span->getName());
$this->assertSame(SpanKind::KIND_INTERNAL, $span->getKind());
}

private function getHttpKernel(EventDispatcherInterface $eventDispatcher, $controller = null, RequestStack $requestStack = null, array $arguments = []): HttpKernel
{
$controller ??= fn () => new Response('Hello');
Expand Down

0 comments on commit 92cf684

Please sign in to comment.