HttpClientMiddleware.php in Raven: Sentry Integration 3.x
File
src/Http/HttpClientMiddleware.php
View source
<?php
namespace Drupal\raven\Http;
use GuzzleHttp\Promise\Create;
use Psr\Http\Message\RequestInterface;
use Sentry\SentrySdk;
use Sentry\Tracing\SpanContext;
use function GuzzleHttp\Promise\rejection_for;
class HttpClientMiddleware {
public function __invoke() {
return function ($handler) {
return function (RequestInterface $request, array $options) use ($handler) {
$span = NULL;
if (class_exists(SentrySdk::class) && ($parent = SentrySdk::getCurrentHub()
->getSpan())) {
$context = new SpanContext();
$context
->setOp('http.guzzle');
$context
->setDescription($request
->getMethod() . ' ' . $request
->getUri());
$span = $parent
->startChild($context);
}
$handlerPromiseCallback = static function ($responseOrException) use ($span) {
if ($span) {
$span
->finish();
}
if ($responseOrException instanceof \Throwable) {
return class_exists(Create::class) ? Create::rejectionFor($responseOrException) : rejection_for($responseOrException);
}
return $responseOrException;
};
return $handler($request, $options)
->then($handlerPromiseCallback, $handlerPromiseCallback);
};
};
}
}