client = $client; $this->logger = $logger; } /** * Accepts a PSR-7 request and an array of options and returns a PSR-7 response. * * @param RequestInterface $request * @param array $options * @return ResponseInterface */ public function __invoke(RequestInterface $request, array $options = []) { $requestEvent = null; if ($this->logger) { $requestEvent = $this->requestLog($request, $options); } $response = $this->client->send($request, $options); if ($this->logger) { $this->responseLog($response, $requestEvent); } return $response; } /** * Accepts a PSR-7 request and an array of options and returns a PromiseInterface * * @param RequestInterface $request * @param array $options * * @return \GuzzleHttp\Promise\PromiseInterface */ public function async(RequestInterface $request, array $options = []) { $requestEvent = null; if ($this->logger) { $requestEvent = $this->requestLog($request, $options); } $promise = $this->client->sendAsync($request, $options); if ($this->logger) { $promise->then(function (ResponseInterface $response) use ($requestEvent) { $this->responseLog($response, $requestEvent); return $response; }); } return $promise; } /** * @internal * @param RequestInterface $request * @param array $options */ public function requestLog(RequestInterface $request, array $options): RpcLogEvent { $requestEvent = new RpcLogEvent(); $requestEvent->method = $request->getMethod(); $requestEvent->url = (string) $request->getUri(); $requestEvent->headers = $request->getHeaders(); $requestEvent->payload = $request->getBody()->getContents(); $requestEvent->retryAttempt = $options['retryAttempt'] ?? null; $requestEvent->serviceName = $options['serviceName'] ?? null; $requestEvent->processId = (int) getmypid(); $requestEvent->requestId = $options['requestId'] ?? crc32((string) spl_object_id($request) . getmypid()); $this->logRequest($requestEvent); return $requestEvent; } /** * @internal */ public function responseLog(ResponseInterface $response, RpcLogEvent $requestEvent): void { $responseEvent = new RpcLogEvent($requestEvent->milliseconds); $responseEvent->headers = $response->getHeaders(); $responseEvent->payload = $response->getBody()->getContents(); $responseEvent->status = $response->getStatusCode(); $responseEvent->processId = $requestEvent->processId; $responseEvent->requestId = $requestEvent->requestId; $this->logResponse($responseEvent); } }