-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
318 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/Instrumentation/Laravel/src/ConsoleInstrumentation.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Contrib\Instrumentation\Laravel; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Contracts\Console\Kernel; | ||
use OpenTelemetry\API\Instrumentation\CachedInstrumentation; | ||
use OpenTelemetry\API\Trace\Span; | ||
use OpenTelemetry\API\Trace\SpanKind; | ||
use OpenTelemetry\API\Trace\StatusCode; | ||
use OpenTelemetry\Context\Context; | ||
use function OpenTelemetry\Instrumentation\hook; | ||
use OpenTelemetry\SemConv\TraceAttributes; | ||
use Throwable; | ||
|
||
class ConsoleInstrumentation | ||
{ | ||
public static function register(CachedInstrumentation $instrumentation): void | ||
{ | ||
hook( | ||
Kernel::class, | ||
'handle', | ||
pre: static function (Kernel $kernel, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($instrumentation) { | ||
/** @psalm-suppress ArgumentTypeCoercion */ | ||
$builder = $instrumentation->tracer() | ||
->spanBuilder('Artisan handler') | ||
->setSpanKind(SpanKind::KIND_PRODUCER) | ||
->setAttribute(TraceAttributes::CODE_FUNCTION, $function) | ||
->setAttribute(TraceAttributes::CODE_NAMESPACE, $class) | ||
->setAttribute(TraceAttributes::CODE_FILEPATH, $filename) | ||
->setAttribute(TraceAttributes::CODE_LINENO, $lineno); | ||
|
||
$parent = Context::getCurrent(); | ||
$span = $builder->startSpan(); | ||
Context::storage()->attach($span->storeInContext($parent)); | ||
|
||
return $params; | ||
}, | ||
post: static function (Kernel $kernel, array $params, ?int $exitCode, ?Throwable $exception) { | ||
$scope = Context::storage()->scope(); | ||
if (!$scope) { | ||
return; | ||
} | ||
|
||
$scope->detach(); | ||
$span = Span::fromContext($scope->context()); | ||
if ($exception) { | ||
$span->recordException($exception, [TraceAttributes::EXCEPTION_ESCAPED => true]); | ||
$span->setStatus(StatusCode::STATUS_ERROR, $exception->getMessage()); | ||
} elseif ($exitCode !== Command::SUCCESS) { | ||
$span->setStatus(StatusCode::STATUS_ERROR); | ||
} else { | ||
$span->setStatus(StatusCode::STATUS_OK); | ||
} | ||
|
||
$span->end(); | ||
} | ||
); | ||
|
||
hook( | ||
Command::class, | ||
'execute', | ||
pre: static function (Command $command, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($instrumentation) { | ||
/** @psalm-suppress ArgumentTypeCoercion */ | ||
$builder = $instrumentation->tracer() | ||
->spanBuilder(sprintf('Command %s', $command->getName() ?: 'unknown')) | ||
->setAttribute(TraceAttributes::CODE_FUNCTION, $function) | ||
->setAttribute(TraceAttributes::CODE_NAMESPACE, $class) | ||
->setAttribute(TraceAttributes::CODE_FILEPATH, $filename) | ||
->setAttribute(TraceAttributes::CODE_LINENO, $lineno); | ||
|
||
$parent = Context::getCurrent(); | ||
$span = $builder->startSpan(); | ||
Context::storage()->attach($span->storeInContext($parent)); | ||
|
||
return $params; | ||
}, | ||
post: static function (Command $command, array $params, ?int $exitCode, ?Throwable $exception) { | ||
$scope = Context::storage()->scope(); | ||
if (!$scope) { | ||
return; | ||
} | ||
|
||
$scope->detach(); | ||
$span = Span::fromContext($scope->context()); | ||
$span->addEvent('command finished', [ | ||
'exit-code' => $exitCode, | ||
]); | ||
|
||
if ($exception) { | ||
$span->recordException($exception, [TraceAttributes::EXCEPTION_ESCAPED => true]); | ||
$span->setStatus(StatusCode::STATUS_ERROR, $exception->getMessage()); | ||
} | ||
|
||
$span->end(); | ||
} | ||
); | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
src/Instrumentation/Laravel/src/HttpInstrumentation.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Contrib\Instrumentation\Laravel; | ||
|
||
use Illuminate\Contracts\Http\Kernel; | ||
use Illuminate\Http\Request; | ||
use OpenTelemetry\API\Globals; | ||
use OpenTelemetry\API\Instrumentation\CachedInstrumentation; | ||
use OpenTelemetry\API\Trace\Span; | ||
use OpenTelemetry\API\Trace\SpanInterface; | ||
use OpenTelemetry\API\Trace\SpanKind; | ||
use OpenTelemetry\API\Trace\StatusCode; | ||
use OpenTelemetry\Context\Context; | ||
use function OpenTelemetry\Instrumentation\hook; | ||
use OpenTelemetry\SemConv\TraceAttributes; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Throwable; | ||
|
||
class HttpInstrumentation | ||
{ | ||
public static function register(CachedInstrumentation $instrumentation): void | ||
{ | ||
hook( | ||
Kernel::class, | ||
'handle', | ||
pre: static function (Kernel $kernel, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($instrumentation) { | ||
$request = ($params[0] instanceof Request) ? $params[0] : null; | ||
/** @psalm-suppress ArgumentTypeCoercion */ | ||
$builder = $instrumentation->tracer() | ||
->spanBuilder(sprintf('HTTP %s', $request?->method() ?? 'unknown')) | ||
->setSpanKind(SpanKind::KIND_SERVER) | ||
->setAttribute(TraceAttributes::CODE_FUNCTION, $function) | ||
->setAttribute(TraceAttributes::CODE_NAMESPACE, $class) | ||
->setAttribute(TraceAttributes::CODE_FILEPATH, $filename) | ||
->setAttribute(TraceAttributes::CODE_LINENO, $lineno); | ||
$parent = Context::getCurrent(); | ||
if ($request) { | ||
$parent = Globals::propagator()->extract($request, HeadersPropagator::instance()); | ||
$span = $builder | ||
->setParent($parent) | ||
->setAttribute(TraceAttributes::HTTP_URL, $request->fullUrl()) | ||
->setAttribute(TraceAttributes::HTTP_METHOD, $request->method()) | ||
->setAttribute(TraceAttributes::HTTP_REQUEST_CONTENT_LENGTH, $request->header('Content-Length')) | ||
->setAttribute(TraceAttributes::HTTP_SCHEME, $request->getScheme()) | ||
->setAttribute(TraceAttributes::HTTP_FLAVOR, $request->getProtocolVersion()) | ||
->setAttribute(TraceAttributes::HTTP_CLIENT_IP, $request->ip()) | ||
->setAttribute(TraceAttributes::HTTP_TARGET, self::httpTarget($request)) | ||
->setAttribute(TraceAttributes::NET_HOST_NAME, self::httpHostName($request)) | ||
->setAttribute(TraceAttributes::NET_HOST_PORT, $request->getPort()) | ||
->setAttribute(TraceAttributes::NET_PEER_PORT, $request->server('REMOTE_PORT')) | ||
->setAttribute(TraceAttributes::USER_AGENT_ORIGINAL, $request->userAgent()) | ||
->startSpan(); | ||
$request->attributes->set(SpanInterface::class, $span); | ||
} else { | ||
$span = $builder->startSpan(); | ||
} | ||
Context::storage()->attach($span->storeInContext($parent)); | ||
|
||
return [$request]; | ||
}, | ||
post: static function (Kernel $kernel, array $params, ?Response $response, ?Throwable $exception) { | ||
$scope = Context::storage()->scope(); | ||
if (!$scope) { | ||
return; | ||
} | ||
$scope->detach(); | ||
$span = Span::fromContext($scope->context()); | ||
if ($exception) { | ||
$span->recordException($exception, [TraceAttributes::EXCEPTION_ESCAPED => true]); | ||
$span->setStatus(StatusCode::STATUS_ERROR, $exception->getMessage()); | ||
} | ||
if ($response) { | ||
if ($response->getStatusCode() >= 400) { | ||
$span->setStatus(StatusCode::STATUS_ERROR); | ||
} | ||
$span->setAttribute(TraceAttributes::HTTP_STATUS_CODE, $response->getStatusCode()); | ||
$span->setAttribute(TraceAttributes::HTTP_FLAVOR, $response->getProtocolVersion()); | ||
$span->setAttribute(TraceAttributes::HTTP_RESPONSE_CONTENT_LENGTH, $response->headers->get('Content-Length')); | ||
} | ||
|
||
$span->end(); | ||
} | ||
); | ||
} | ||
|
||
private static function httpTarget(Request $request): string | ||
{ | ||
$query = $request->getQueryString(); | ||
$question = $request->getBaseUrl() . $request->getPathInfo() === '/' ? '/?' : '?'; | ||
|
||
return $query ? $request->path() . $question . $query : $request->path(); | ||
} | ||
|
||
private static function httpHostName(Request $request): string | ||
{ | ||
if (method_exists($request, 'host')) { | ||
return $request->host(); | ||
} | ||
if (method_exists($request, 'getHost')) { | ||
return $request->getHost(); | ||
} | ||
|
||
return ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.