diff --git a/_register.php b/_register.php index afbf15d..92fb6d1 100644 --- a/_register.php +++ b/_register.php @@ -4,6 +4,7 @@ use OpenTelemetry\SDK\Sdk; use Spryker\Service\Opentelemetry\Instrumentation\ElasticaInstrumentation; +use Spryker\Service\Opentelemetry\Instrumentation\GuzzleInstrumentation; use Spryker\Service\Opentelemetry\Instrumentation\PropelInstrumentation; use Spryker\Service\Opentelemetry\Instrumentation\RabbitMqInstrumentation; use Spryker\Service\Opentelemetry\Instrumentation\RedisInstrumentation; @@ -25,4 +26,5 @@ PropelInstrumentation::register(); RabbitMqInstrumentation::register(); RedisInstrumentation::register(); +GuzzleInstrumentation::register(); SprykerInstrumentationBootstrap::register(); diff --git a/src/Spryker/Service/Opentelemetry/Instrumentation/GuzzleInstrumentation.php b/src/Spryker/Service/Opentelemetry/Instrumentation/GuzzleInstrumentation.php new file mode 100644 index 0000000..021bc81 --- /dev/null +++ b/src/Spryker/Service/Opentelemetry/Instrumentation/GuzzleInstrumentation.php @@ -0,0 +1,131 @@ +getUri(); + $method = $request->getMethod(); + $url = (string)$uriObject; + + $span = $instrumentation->tracer() + ->spanBuilder(sprintf('Guzzle %s %s', $method, $url)) + ->setSpanKind(SpanKind::KIND_CLIENT) + ->setParent($context) + ->setAttribute(CriticalSpanRatioSampler::IS_CRITICAL_ATTRIBUTE, true) + ->setAttribute(TraceAttributes::CODE_FUNCTION, $function) + ->setAttribute(TraceAttributes::CODE_NAMESPACE, $class) + ->setAttribute(TraceAttributes::CODE_FILEPATH, $filename) + ->setAttribute(TraceAttributes::CODE_LINENO, $lineno) + ->setAttribute(TraceAttributes::SERVER_ADDRESS, $uriObject->getHost()) + ->setAttribute(TraceAttributes::SERVER_PORT, $uriObject->getPort()) + ->setAttribute(TraceAttributes::URL_PATH, $uriObject->getPath()) + ->setAttribute(TraceAttributes::URL_FULL, $url) + ->setAttribute(TraceAttributes::HTTP_REQUEST_METHOD, $method) + ->setAttribute(TraceAttributes::NETWORK_PROTOCOL_VERSION, $request->getProtocolVersion()) + ->setAttribute(TraceAttributes::USER_AGENT_ORIGINAL, $request->getHeaderLine(static::HEADER_USER_AGENT)) + ->startSpan(); + + Context::storage()->attach($span->storeInContext($context)); + }, + post: static function (Client $guzzleClient, array $params, PromiseInterface $promise, ?Throwable $exception): void { + if (TraceSampleResult::shouldSkipTraceBody()) { + return; + } + $scope = Context::storage()->scope(); + + if ($scope === null) { + return; + } + + $scope->detach(); + + $span = Span::fromContext($scope->context()); + + if ($exception) { + $span->recordException($exception); + $span->setStatus(StatusCode::STATUS_ERROR); + $span->end(); + + return; + } + + $promise->then( + onFulfilled: function (ResponseInterface $response) use ($span) { + $span->setAttribute(TraceAttributes::HTTP_RESPONSE_STATUS_CODE, $response->getStatusCode()); + + if ($response->getStatusCode() >= 400 && $response->getStatusCode() < 600) { + $span->setStatus(StatusCode::STATUS_ERROR); + } + $span->end(); + + return $response; + }, + onRejected: function (Throwable $exception) use ($span) { + $span->recordException($exception); + $span->setStatus(StatusCode::STATUS_ERROR); + $span->end(); + + throw $exception; + } + ); + }, + ); + } +} diff --git a/src/Spryker/Service/Opentelemetry/Instrumentation/RabbitMqInstrumentation.php b/src/Spryker/Service/Opentelemetry/Instrumentation/RabbitMqInstrumentation.php index fe2fbbb..79a66a2 100644 --- a/src/Spryker/Service/Opentelemetry/Instrumentation/RabbitMqInstrumentation.php +++ b/src/Spryker/Service/Opentelemetry/Instrumentation/RabbitMqInstrumentation.php @@ -135,7 +135,15 @@ function: $functionName, if (TraceSampleResult::shouldSkipTraceBody()) { return; } - $span = Span::fromContext(Context::getCurrent()); + $scope = Context::storage()->scope(); + + if ($scope === null) { + return; + } + + $scope->detach(); + + $span = Span::fromContext($scope->context()); if ($exception !== null) { $span->recordException($exception); diff --git a/src/Spryker/Service/Opentelemetry/Instrumentation/RedisInstrumentation.php b/src/Spryker/Service/Opentelemetry/Instrumentation/RedisInstrumentation.php index ff1453d..8f24068 100644 --- a/src/Spryker/Service/Opentelemetry/Instrumentation/RedisInstrumentation.php +++ b/src/Spryker/Service/Opentelemetry/Instrumentation/RedisInstrumentation.php @@ -72,7 +72,15 @@ public static function register(): void if (TraceSampleResult::shouldSkipTraceBody()) { return; } - $span = Span::fromContext(Context::getCurrent()); + $scope = Context::storage()->scope(); + + if ($scope === null) { + return; + } + + $scope->detach(); + + $span = Span::fromContext($scope->context()); if ($exception !== null) { $span->recordException($exception); @@ -108,7 +116,15 @@ public static function register(): void if (TraceSampleResult::shouldSkipTraceBody()) { return; } - $span = Span::fromContext(Context::getCurrent()); + $scope = Context::storage()->scope(); + + if ($scope === null) { + return; + } + + $scope->detach(); + + $span = Span::fromContext($scope->context()); if ($exception !== null) { $span->recordException($exception); @@ -144,7 +160,15 @@ public static function register(): void if (TraceSampleResult::shouldSkipTraceBody()) { return; } - $span = Span::fromContext(Context::getCurrent()); + $scope = Context::storage()->scope(); + + if ($scope === null) { + return; + } + + $scope->detach(); + + $span = Span::fromContext($scope->context()); if ($exception !== null) { $span->recordException($exception); @@ -180,7 +204,15 @@ public static function register(): void if (TraceSampleResult::shouldSkipTraceBody()) { return; } - $span = Span::fromContext(Context::getCurrent()); + $scope = Context::storage()->scope(); + + if ($scope === null) { + return; + } + + $scope->detach(); + + $span = Span::fromContext($scope->context()); if ($exception !== null) { $span->recordException($exception); @@ -216,7 +248,15 @@ public static function register(): void if (TraceSampleResult::shouldSkipTraceBody()) { return; } - $span = Span::fromContext(Context::getCurrent()); + $scope = Context::storage()->scope(); + + if ($scope === null) { + return; + } + + $scope->detach(); + + $span = Span::fromContext($scope->context()); if ($exception !== null) { $span->recordException($exception); @@ -254,7 +294,15 @@ public static function register(): void if (TraceSampleResult::shouldSkipTraceBody()) { return; } - $span = Span::fromContext(Context::getCurrent()); + $scope = Context::storage()->scope(); + + if ($scope === null) { + return; + } + + $scope->detach(); + + $span = Span::fromContext($scope->context()); if ($exception !== null) { $span->recordException($exception);