Skip to content

Commit

Permalink
FRW-9744 addded Guzzle instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
gechetspr committed Jan 15, 2025
1 parent ccb868f commit 9b40127
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 7 deletions.
2 changes: 2 additions & 0 deletions _register.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,4 +26,5 @@
PropelInstrumentation::register();
RabbitMqInstrumentation::register();
RedisInstrumentation::register();
GuzzleInstrumentation::register();
SprykerInstrumentationBootstrap::register();
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace Spryker\Service\Opentelemetry\Instrumentation;

use GuzzleHttp\Client;
use GuzzleHttp\Promise\PromiseInterface;
use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\Context\Context;
use OpenTelemetry\SDK\Sdk;
use OpenTelemetry\SemConv\TraceAttributes;
use Psr\Http\Message\ResponseInterface;
use Spryker\Service\Opentelemetry\Instrumentation\Sampler\CriticalSpanRatioSampler;
use Spryker\Service\Opentelemetry\Instrumentation\Sampler\TraceSampleResult;
use Spryker\Service\Opentelemetry\Instrumentation\Span\Span;
use Spryker\Shared\Opentelemetry\Instrumentation\CachedInstrumentation;
use Throwable;
use function OpenTelemetry\Instrumentation\hook;

class GuzzleInstrumentation
{
/**
* @var string
*/
protected const NAME = 'spryker_otel_guzzle';

/**
* @var string
*/
protected const HEADER_USER_AGENT = 'User-Agent';

/**
* @return void
*/
public static function register(): void
{
//BC check
if (class_exists('\OpenTelemetry\Contrib\Instrumentation\Guzzle\GuzzleInstrumentation')) {
return;
}

if (Sdk::isInstrumentationDisabled(static::NAME) === true) {
return;
}

$instrumentation = CachedInstrumentation::getCachedInstrumentation();

hook(
Client::class,
'transfer',
pre: static function (Client $guzzleClient, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($instrumentation): void {
if (TraceSampleResult::shouldSkipTraceBody()) {
return;
}
$context = Context::getCurrent();
/** @var \Psr\Http\Message\RequestInterface $request */
$request = $params[0];
$uriObject = $request->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;
}
);
},
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 9b40127

Please sign in to comment.