Add stopwatch support for Elastica\Client

This commit is contained in:
Tim Nagel 2014-07-03 23:20:18 +10:00
parent c200e8fdfd
commit 815b836cdc
2 changed files with 30 additions and 0 deletions

View file

@ -5,6 +5,7 @@ namespace FOS\ElasticaBundle\Elastica;
use Elastica\Client as BaseClient;
use Elastica\Request;
use FOS\ElasticaBundle\Logger\ElasticaLogger;
use Symfony\Component\Stopwatch\Stopwatch;
/**
* Extends the default Elastica client to provide logging for errors that occur
@ -21,11 +22,22 @@ class Client extends BaseClient
*/
private $indexCache = array();
/**
* Symfony's debugging Stopwatch
*
* @var Stopwatch|null
*/
private $stopwatch;
/**
* {@inheritdoc}
*/
public function request($path, $method = Request::GET, $data = array(), array $query = array())
{
if ($this->stopwatch) {
$this->stopwatch->start('es_request', 'fos_elastica');
}
$start = microtime(true);
$response = parent::request($path, $method, $data, $query);
@ -44,6 +56,10 @@ class Client extends BaseClient
$this->_logger->logQuery($path, $method, $data, $time, $connection_array, $query);
}
if ($this->stopwatch) {
$this->stopwatch->stop('es_request');
}
return $response;
}
@ -55,4 +71,14 @@ class Client extends BaseClient
return $this->indexCache[$name] = new Index($this, $name);
}
/**
* Sets a stopwatch instance for debugging purposes.
*
* @param Stopwatch $stopwatch
*/
public function setStopwatch(Stopwatch $stopwatch = null)
{
$this->stopwatch = $stopwatch;
}
}

View file

@ -16,6 +16,10 @@
<service id="fos_elastica.client_prototype" class="%fos_elastica.client.class%" abstract="true">
<argument type="collection" /> <!-- configuration -->
<argument /> <!-- callback -->
<call method="setStopwatch">
<argument type="service" id="debug.stopwatch" />
</call>
</service>
<service id="fos_elastica.config_manager" class="FOS\ElasticaBundle\Configuration\ConfigManager">