|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OpenTelemetry\Contrib\Instrumentation\Symfony; |
| 6 | + |
| 7 | +use OpenTelemetry\API\Common\Instrumentation\CachedInstrumentation; |
| 8 | +use OpenTelemetry\API\Common\Instrumentation\Globals; |
| 9 | +use OpenTelemetry\API\Trace\Span; |
| 10 | +use OpenTelemetry\API\Trace\SpanInterface; |
| 11 | +use OpenTelemetry\API\Trace\SpanKind; |
| 12 | +use OpenTelemetry\API\Trace\StatusCode; |
| 13 | +use OpenTelemetry\Context\Context; |
| 14 | +use function OpenTelemetry\Instrumentation\hook; |
| 15 | +use OpenTelemetry\SemConv\TraceAttributes; |
| 16 | +use Symfony\Component\HttpFoundation\Request; |
| 17 | +use Symfony\Component\HttpFoundation\Response; |
| 18 | +use Symfony\Component\HttpKernel\HttpKernel; |
| 19 | +use Throwable; |
| 20 | + |
| 21 | +class SymfonyInstrumentation |
| 22 | +{ |
| 23 | + public static function register(): void |
| 24 | + { |
| 25 | + $instrumentation = new CachedInstrumentation('io.opentelemetry.contrib.php.symfony'); |
| 26 | + hook( |
| 27 | + HttpKernel::class, |
| 28 | + 'handle', |
| 29 | + pre: static function (HttpKernel $kernel, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($instrumentation) { |
| 30 | + $request = ($params[0] instanceof Request) ? $params[0] : null; |
| 31 | + /** @psalm-suppress ArgumentTypeCoercion */ |
| 32 | + $builder = $instrumentation->tracer() |
| 33 | + ->spanBuilder(sprintf('HTTP %s', $request?->getMethod() ?? 'unknown')) |
| 34 | + ->setSpanKind(SpanKind::KIND_SERVER) |
| 35 | + ->setAttribute('code.function', $function) |
| 36 | + ->setAttribute('code.namespace', $class) |
| 37 | + ->setAttribute('code.filepath', $filename) |
| 38 | + ->setAttribute('code.lineno', $lineno); |
| 39 | + $parent = Context::getCurrent(); |
| 40 | + if ($request) { |
| 41 | + $parent = Globals::propagator()->extract($request, HeadersPropagator::instance()); |
| 42 | + $span = $builder |
| 43 | + ->setParent($parent) |
| 44 | + ->setAttribute(TraceAttributes::HTTP_URL, $request->getUri()) |
| 45 | + ->setAttribute(TraceAttributes::HTTP_METHOD, $request->getMethod()) |
| 46 | + ->setAttribute(TraceAttributes::HTTP_REQUEST_CONTENT_LENGTH, $request->headers->get('Content-Length')) |
| 47 | + ->setAttribute(TraceAttributes::HTTP_SCHEME, $request->getScheme()) |
| 48 | + ->startSpan(); |
| 49 | + $request->attributes->set(SpanInterface::class, $span); |
| 50 | + } else { |
| 51 | + $span = $builder->startSpan(); |
| 52 | + } |
| 53 | + Context::storage()->attach($span->storeInContext($parent)); |
| 54 | + |
| 55 | + return [$request]; |
| 56 | + }, |
| 57 | + post: static function (HttpKernel $kernel, array $params, ?Response $response, ?Throwable $exception) { |
| 58 | + $scope = Context::storage()->scope(); |
| 59 | + if (!$scope) { |
| 60 | + return; |
| 61 | + } |
| 62 | + $scope->detach(); |
| 63 | + $span = Span::fromContext($scope->context()); |
| 64 | + if ($exception) { |
| 65 | + $span->recordException($exception, [TraceAttributes::EXCEPTION_ESCAPED => true]); |
| 66 | + $span->setStatus(StatusCode::STATUS_ERROR, $exception->getMessage()); |
| 67 | + } |
| 68 | + if ($response) { |
| 69 | + if ($response->getStatusCode() >= 400) { |
| 70 | + $span->setStatus(StatusCode::STATUS_ERROR); |
| 71 | + } |
| 72 | + $span->setAttribute(TraceAttributes::HTTP_STATUS_CODE, $response->getStatusCode()); |
| 73 | + $span->setAttribute(TraceAttributes::HTTP_FLAVOR, $response->getProtocolVersion()); |
| 74 | + $span->setAttribute(TraceAttributes::HTTP_RESPONSE_CONTENT_LENGTH, $response->headers->get('Content-Length')); |
| 75 | + } |
| 76 | + |
| 77 | + $span->end(); |
| 78 | + } |
| 79 | + ); |
| 80 | + } |
| 81 | +} |
0 commit comments