Skip to content

Commit

Permalink
Add ability to get last request info
Browse files Browse the repository at this point in the history
At a high level, this commit allows the user to obtain info (URI, method,
status, etc) of the last request.  To accomplish this, a number of
things had to change:

- Transport visibility had to be increased to public
- Transport needs to know which connection was last used
- Connections need to save and return last request info

Resolves #45
  • Loading branch information
polyfractal committed Apr 8, 2014
1 parent ed66d23 commit f1092c5
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Elasticsearch/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Client
/**
* @var Transport
*/
protected $transport;
public $transport;

/**
* @var \Pimple
Expand Down
3 changes: 3 additions & 0 deletions src/Elasticsearch/Connections/AbstractConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ abstract public function performRequest($method, $uri, $params = null, $body = n
/** @return string */
abstract public function getTransportSchema();

/** @return array */
abstract public function getLastRequestInfo();


/**
* Constructor
Expand Down
1 change: 1 addition & 0 deletions src/Elasticsearch/Connections/ConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function markAlive();

public function markDead();

public function getLastRequestInfo();

public function performRequest($method, $uri, $params = null, $body = null);
}
24 changes: 24 additions & 0 deletions src/Elasticsearch/Connections/CurlMultiConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class CurlMultiConnection extends AbstractConnection implements ConnectionInterf

private $curlOpts;

private $lastRequest = array();


/**
* Constructor
Expand Down Expand Up @@ -137,6 +139,15 @@ public function performRequest($method, $uri, $params = null, $body = null, $opt

$this->log->debug("Curl Options:", $opts);

$this->lastRequest = array('request' =>
array(
'uri' => $uri,
'body' => $body,
'options' => $options,
'method' => $method
)
);

curl_setopt_array($curlHandle, $opts);
curl_multi_add_handle($this->multiHandle, $curlHandle);

Expand Down Expand Up @@ -197,6 +208,10 @@ public function performRequest($method, $uri, $params = null, $body = null, $opt
$this->process5xxError($method, $uri, $response);
}

$this->lastRequest['response']['body'] = $response['responseText'];
$this->lastRequest['response']['info'] = $response['requestInfo'];
$this->lastRequest['response']['status'] = $response['requestInfo']['http_code'];

$this->logRequestSuccess(
$method,
$uri,
Expand All @@ -218,6 +233,15 @@ public function performRequest($method, $uri, $params = null, $body = null, $opt
}


/**
* @return array
*/
public function getLastRequestInfo()
{
return $this->lastRequest;
}


/**
* @param $method
* @param $uri
Expand Down
47 changes: 41 additions & 6 deletions src/Elasticsearch/Connections/GuzzleConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class GuzzleConnection extends AbstractConnection implements ConnectionInterface

private $connectionOpts = array();

private $lastRequest = array();


/**
* @param array $hostDetails
Expand Down Expand Up @@ -105,6 +107,15 @@ public function performRequest($method, $uri, $params = null, $body = null, $opt
}


/**
* @return array
*/
public function getLastRequestInfo()
{
return $this->lastRequest;
}


/**
* @param string $uri
* @param array $params
Expand Down Expand Up @@ -138,8 +149,20 @@ private function buildGuzzleRequest($method, $uri, $body, $options = array())
}

if (isset($body) === true) {
$this->lastRequest = array( 'request' => array(
'uri' => $uri,
'body' => $body,
'options' => $options,
'method' => $method
));
$request = $this->guzzle->$method($uri, array(), $body, $options);
} else {
$this->lastRequest = array( 'request' => array(
'uri' => $uri,
'body' => null,
'options' => $options,
'method' => $method
));
$request = $this->guzzle->$method($uri, array(), $options);
}

Expand Down Expand Up @@ -247,8 +270,15 @@ private function process4xxError(Request $request, ClientErrorResponseException
*/
private function logErrorDueToFailure(Request $request, \Exception $exception, $body)
{
$response = $request->getResponse();
$headers = $request->getHeaders()->getAll();
$response = $request->getResponse();
$headers = $request->getHeaders()->getAll();
$info = $response->getInfo();
$responseBody = $response->getBody(true);
$status = $response->getStatusCode();

$this->lastRequest['response']['body'] = $responseBody;
$this->lastRequest['response']['info'] = $info;
$this->lastRequest['response']['status'] = $status;

$this->logRequestFail(
$request->getMethod(),
Expand Down Expand Up @@ -278,16 +308,21 @@ private function processCurlError(CurlException $exception)
*/
private function processSuccessfulRequest(Request $request, $body)
{
$response = $request->getResponse();
$headers = $request->getHeaders()->getAll();
$response = $request->getResponse();
$headers = $request->getHeaders()->getAll();
$responseBody = $response->getBody(true);
$status = $response->getStatusCode();

$this->lastRequest['response']['body'] = $responseBody;
$this->lastRequest['response']['status'] = $status;

$this->logRequestSuccess(
$request->getMethod(),
$request->getUrl(),
$body,
$headers,
$response->getStatusCode(),
$response->getBody(true),
$status,
$responseBody,
$response->getInfo('total_time')
);
}
Expand Down
20 changes: 18 additions & 2 deletions src/Elasticsearch/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class Transport
/** @var int */
private $retryAttempts;

/** @var AbstractConnection */
private $lastConnection;


/**
* Transport class is responsible for dispatching requests to the
Expand Down Expand Up @@ -146,8 +149,9 @@ public function performRequest($method, $uri, $params = null, $body = null)
throw $exception;
}

$response = array();
$caughtException = null;
$response = array();
$caughtException = null;
$this->lastConnection = $connection;

try {
if (isset($body) === true) {
Expand Down Expand Up @@ -220,6 +224,18 @@ public function shouldRetry($method, $uri, $params, $body)
}


/**
* Returns the last used connection so that it may be inspected. Mainly
* for debugging/testing purposes.
*
* @return AbstractConnection
*/
public function getLastConnection()
{
return $this->lastConnection;
}


/**
* Convert host arrays into connections
*
Expand Down
2 changes: 1 addition & 1 deletion util/elasticsearch

0 comments on commit f1092c5

Please sign in to comment.