Store connection infos for all transports, display infos in debug

This commit is contained in:
Damien Alexandre 2013-12-09 14:46:06 +01:00
parent ca507a5e34
commit 1ddd7c0e0c
4 changed files with 15 additions and 17 deletions

View file

@ -4,8 +4,6 @@ namespace FOS\ElasticaBundle;
use Elastica\Client as ElasticaClient; use Elastica\Client as ElasticaClient;
use Elastica\Request; use Elastica\Request;
use Elastica\Transport\Http;
use Elastica\Transport\Https;
/** /**
* @author Gordon Franke <info@nevalon.de> * @author Gordon Franke <info@nevalon.de>
@ -21,14 +19,14 @@ class Client extends ElasticaClient
$time = microtime(true) - $start; $time = microtime(true) - $start;
$connection = $this->getLastRequest()->getConnection(); $connection = $this->getLastRequest()->getConnection();
$transport = $connection->getTransportObject();
$full_host = null;
if ($transport instanceof Http || $transport instanceof Https) { $connection_array = array(
$full_host = $connection->getTransport().'://'.$connection->getHost().':'.$connection->getPort(); 'host' => $connection->getHost(),
} 'port' => $connection->getPort(),
'transport' => $connection->getTransport(),
);
$this->_logger->logQuery($path, $method, $data, $time, $full_host); $this->_logger->logQuery($path, $method, $data, $time, $connection_array);
} }
return $response; return $response;

View file

@ -38,9 +38,9 @@ class ElasticaLogger implements LoggerInterface
* @param string $method Rest method to use (GET, POST, DELETE, PUT) * @param string $method Rest method to use (GET, POST, DELETE, PUT)
* @param array $data arguments * @param array $data arguments
* @param float $time execution time * @param float $time execution time
* @param string $full_host host and port of the query * @param array $connection host, port and transport of the query
*/ */
public function logQuery($path, $method, $data, $time, $full_host = null) public function logQuery($path, $method, $data, $time, $connection = array())
{ {
if ($this->debug) { if ($this->debug) {
$this->queries[] = array( $this->queries[] = array(
@ -48,7 +48,7 @@ class ElasticaLogger implements LoggerInterface
'method' => $method, 'method' => $method,
'data' => $data, 'data' => $data,
'executionMS' => $time, 'executionMS' => $time,
'full_host' => $full_host 'connection' => $connection
); );
} }

View file

@ -44,7 +44,7 @@
{% for key, query in collector.queries %} {% for key, query in collector.queries %}
<li class="{{ cycle(['odd', 'even'], loop.index) }}"> <li class="{{ cycle(['odd', 'even'], loop.index) }}">
<strong>Path</strong>: {{ query.path }}<br /> <strong>Path</strong>: {{ query.path }}<br />
<strong>Method</strong>: {{ query.method }} <strong>Method</strong>: {{ query.method }} <small>({{ query.connection.transport }} on {{ query.connection.host }}:{{ query.connection.port }})</small>
<div> <div>
<code>{{ query.data|json_encode }}</code> <code>{{ query.data|json_encode }}</code>
</div> </div>
@ -52,7 +52,7 @@
<strong>Time</strong>: {{ '%0.2f'|format(query.executionMS * 1000) }} ms <strong>Time</strong>: {{ '%0.2f'|format(query.executionMS * 1000) }} ms
</small> </small>
{% if query.full_host %} {% if query.connection.transport in ['Http', 'Https'] %}{# cURL support only HTTP #}
<a href="#elastica_curl_query_{{ key }}" onclick="return curl_version(this);" style="text-decoration: none;" title="Get the cURL repeatable query"> <a href="#elastica_curl_query_{{ key }}" onclick="return curl_version(this);" style="text-decoration: none;" title="Get the cURL repeatable query">
<img alt="+" src="{{ asset('bundles/framework/images/blue_picto_more.gif') }}" style="display: inline; width: 12px; height: 12px; vertical-align: bottom;" /> <img alt="+" src="{{ asset('bundles/framework/images/blue_picto_more.gif') }}" style="display: inline; width: 12px; height: 12px; vertical-align: bottom;" />
<img alt="-" src="{{ asset('bundles/framework/images/blue_picto_less.gif') }}" style="display: none; width: 12px; height: 12px; vertical-align: bottom;" /> <img alt="-" src="{{ asset('bundles/framework/images/blue_picto_less.gif') }}" style="display: none; width: 12px; height: 12px; vertical-align: bottom;" />
@ -60,7 +60,7 @@
</a> </a>
<div style="display: none;" id="elastica_curl_query_{{ key }}"> <div style="display: none;" id="elastica_curl_query_{{ key }}">
<code>curl -X{{ query.method }} '{{ query.full_host }}/{{ query.path }}' -d '{{ query.data|json_encode }}'</code> <code>curl -X{{ query.method }} '{{ query.connection.transport|lower }}://{{ query.connection.host }}:{{ query.connection.port }}/{{ query.path }}' -d '{{ query.data|json_encode }}'</code>
</div> </div>
{% endif %} {% endif %}
</li> </li>

View file

@ -35,17 +35,17 @@ class ElasticaLoggerTest extends \PHPUnit_Framework_TestCase
$method = 'testMethod'; $method = 'testMethod';
$data = array('data'); $data = array('data');
$time = 12; $time = 12;
$full_host = 'http://example.com:9200'; $connection = array('host' => 'localhost', 'port' => '8999', 'transport' => 'https');
$expected = array( $expected = array(
'path' => $path, 'path' => $path,
'method' => $method, 'method' => $method,
'data' => $data, 'data' => $data,
'executionMS' => $time, 'executionMS' => $time,
'full_host' => $full_host, 'connection' => $connection,
); );
$elasticaLogger->logQuery($path, $method, $data, $time, $full_host); $elasticaLogger->logQuery($path, $method, $data, $time, $connection);
$returnedQueries = $elasticaLogger->getQueries(); $returnedQueries = $elasticaLogger->getQueries();
$this->assertEquals($expected, $returnedQueries[0]); $this->assertEquals($expected, $returnedQueries[0]);
} }