You are here

class HttpClientMiddleware in Raven: Sentry Integration 3.x

Instrument Guzzle HTTP requests.

Hierarchy

Expanded class hierarchy of HttpClientMiddleware

1 string reference to 'HttpClientMiddleware'
raven.services.yml in ./raven.services.yml
raven.services.yml
1 service uses HttpClientMiddleware
http_client_middleware.raven in ./raven.services.yml
Drupal\raven\Http\HttpClientMiddleware

File

src/Http/HttpClientMiddleware.php, line 15

Namespace

Drupal\raven\Http
View source
class HttpClientMiddleware {

  /**
   * {@inheritdoc}
   */
  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) {

            // @phpstan-ignore-next-line for compatibility with older Guzzle versions.
            return class_exists(Create::class) ? Create::rejectionFor($responseOrException) : rejection_for($responseOrException);
          }
          return $responseOrException;
        };
        return $handler($request, $options)
          ->then($handlerPromiseCallback, $handlerPromiseCallback);
      };
    };
  }

}

Members