Skip to content

Commit 6449e08

Browse files
committed
Added port in url for trace and logger messages
1 parent 9c91316 commit 6449e08

File tree

2 files changed

+129
-14
lines changed

2 files changed

+129
-14
lines changed

src/Elasticsearch/Connections/Connection.php

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -394,13 +394,16 @@ public function logWarning(array $request, array $response): void
394394
*/
395395
public function logRequestSuccess(array $request, array $response): void
396396
{
397+
$port = $request['client']['curl'][CURLOPT_PORT] ?? $response['transfer_stats']['primary_port'] ?? '';
398+
$uri = $this->addPortInUrl($response['effective_url'], (int) $port);
399+
397400
$this->log->debug('Request Body', array($request['body']));
398401
$this->log->info(
399402
'Request Success:',
400403
array(
401404
'method' => $request['http_method'],
402-
'uri' => $response['effective_url'],
403-
'port' => $response['transfer_stats']['primary_port'] ?? '',
405+
'uri' => $uri,
406+
'port' => $port,
404407
'headers' => $request['headers'],
405408
'HTTP code' => $response['status'],
406409
'duration' => $response['transfer_stats']['total_time'],
@@ -409,14 +412,15 @@ public function logRequestSuccess(array $request, array $response): void
409412
$this->log->debug('Response', array($response['body']));
410413

411414
// Build the curl command for Trace.
412-
$curlCommand = $this->buildCurlCommand($request['http_method'], $response['effective_url'], $request['body']);
415+
$curlCommand = $this->buildCurlCommand($request['http_method'], $uri, $request['body']);
413416
$this->trace->info($curlCommand);
414417
$this->trace->debug(
415418
'Response:',
416419
array(
417420
'response' => $response['body'],
418421
'method' => $request['http_method'],
419-
'uri' => $response['effective_url'],
422+
'uri' => $uri,
423+
'port' => $port,
420424
'HTTP code' => $response['status'],
421425
'duration' => $response['transfer_stats']['total_time'],
422426
)
@@ -434,14 +438,16 @@ public function logRequestSuccess(array $request, array $response): void
434438
*/
435439
public function logRequestFail(array $request, array $response, \Exception $exception): void
436440
{
437-
$this->log->debug('Request Body', array($request['body']));
441+
$port = $request['client']['curl'][CURLOPT_PORT] ?? $response['transfer_stats']['primary_port'] ?? '';
442+
$uri = $this->addPortInUrl($response['effective_url'], (int) $port);
438443

444+
$this->log->debug('Request Body', array($request['body']));
439445
$this->log->warning(
440446
'Request Failure:',
441447
array(
442448
'method' => $request['http_method'],
443-
'uri' => $response['effective_url'],
444-
'port' => $response['transfer_stats']['primary_port'] ?? '',
449+
'uri' => $uri,
450+
'port' => $port,
445451
'headers' => $request['headers'],
446452
'HTTP code' => $response['status'],
447453
'duration' => $response['transfer_stats']['total_time'],
@@ -451,14 +457,15 @@ public function logRequestFail(array $request, array $response, \Exception $exce
451457
$this->log->warning('Response', array($response['body']));
452458

453459
// Build the curl command for Trace.
454-
$curlCommand = $this->buildCurlCommand($request['http_method'], $response['effective_url'], $request['body']);
460+
$curlCommand = $this->buildCurlCommand($request['http_method'], $uri, $request['body']);
455461
$this->trace->info($curlCommand);
456462
$this->trace->debug(
457463
'Response:',
458464
array(
459465
'response' => $response,
460466
'method' => $request['http_method'],
461-
'uri' => $response['effective_url'],
467+
'uri' => $uri,
468+
'port' => $port,
462469
'HTTP code' => $response['status'],
463470
'duration' => $response['transfer_stats']['total_time'],
464471
)
@@ -624,19 +631,30 @@ private function getOSVersion(): string
624631
return $this->OSVersion;
625632
}
626633

634+
/**
635+
* Add the port value in the URL if not present
636+
*/
637+
private function addPortInUrl(string $uri, int $port): string
638+
{
639+
if (strpos($uri, ':', 7) !== false) {
640+
return $uri;
641+
}
642+
return preg_replace('#([^/])/([^/])#', sprintf("$1:%s/$2", $port), $uri, 1);
643+
}
644+
627645
/**
628646
* Construct a string cURL command
629647
*/
630-
private function buildCurlCommand(string $method, string $uri, ?string $body): string
648+
private function buildCurlCommand(string $method, string $url, ?string $body): string
631649
{
632-
if (strpos($uri, '?') === false) {
633-
$uri .= '?pretty=true';
650+
if (strpos($url, '?') === false) {
651+
$url .= '?pretty=true';
634652
} else {
635-
str_replace('?', '?pretty=true', $uri);
653+
str_replace('?', '?pretty=true', $url);
636654
}
637655

638656
$curlCommand = 'curl -X' . strtoupper($method);
639-
$curlCommand .= " '" . $uri . "'";
657+
$curlCommand .= " '" . $url . "'";
640658

641659
if (isset($body) === true && $body !== '') {
642660
$curlCommand .= " -d '" . $body . "'";

tests/Elasticsearch/Tests/Connections/ConnectionTest.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
use Elasticsearch\Connections\Connection;
2525
use Elasticsearch\Serializers\SerializerInterface;
2626
use Elasticsearch\Serializers\SmartSerializer;
27+
use Elasticsearch\Tests\ClientBuilder\ArrayLogger;
28+
use Exception;
2729
use Psr\Log\LoggerInterface;
2830
use ReflectionClass;
2931

@@ -452,4 +454,99 @@ public function testParametersAreSent()
452454

453455
$this->assertEquals('/?foo=true&baz=false&bar=baz', $request['uri']);
454456
}
457+
458+
public function testPortInUrlWhenLogRequestSuccess()
459+
{
460+
$logger = new ArrayLogger();
461+
$trace = new ArrayLogger();
462+
463+
$connection = new Connection(
464+
ClientBuilder::defaultHandler(),
465+
[
466+
'host' => 'localhost',
467+
'port' => 9200,
468+
'scheme' => 'http',
469+
'path' => '/info'
470+
],
471+
[],
472+
$this->serializer,
473+
$logger,
474+
$trace
475+
);
476+
$request = [
477+
'body' => '{}',
478+
'http_method' => 'GET',
479+
'headers' => [
480+
'User-Agent: Testing'
481+
]
482+
];
483+
$response = [
484+
'effective_url' => 'http://localhost/info',
485+
'status' => 200,
486+
'transfer_stats' => [
487+
'primary_port' => 9200,
488+
'total_time' => 1
489+
],
490+
'body' => '{}'
491+
];
492+
$connection->logRequestSuccess($request, $response);
493+
// Check for localhost:9200 in trace
494+
foreach($trace->output as $row) {
495+
$this->assertStringContainsString('localhost:9200', $row);
496+
}
497+
// Check for localhost:9200 in logger
498+
foreach($logger->output as $row) {
499+
if (false !== strpos('info: Request Success', $row)) {
500+
$this->assertStringContainsString('localhost:9200', $row);
501+
}
502+
}
503+
}
504+
505+
public function testPortInLogUrlWhenLogRequestFail()
506+
{
507+
$logger = new ArrayLogger();
508+
$trace = new ArrayLogger();
509+
510+
$connection = new Connection(
511+
ClientBuilder::defaultHandler(),
512+
[
513+
'host' => 'localhost',
514+
'port' => 9200,
515+
'scheme' => 'http',
516+
'path' => '/info'
517+
],
518+
[],
519+
$this->serializer,
520+
$logger,
521+
$trace
522+
);
523+
$request = [
524+
'body' => '{}',
525+
'http_method' => 'GET',
526+
'headers' => [
527+
'User-Agent: Testing'
528+
]
529+
];
530+
$response = [
531+
'effective_url' => 'http://localhost/info',
532+
'status' => 400,
533+
'transfer_stats' => [
534+
'primary_port' => 9200,
535+
'total_time' => 1
536+
],
537+
'body' => '{}'
538+
];
539+
$connection->logRequestFail($request, $response, new Exception());
540+
541+
// Check for localhost:9200 in trace
542+
foreach($trace->output as $row) {
543+
$this->assertStringContainsString('localhost:9200', $row);
544+
}
545+
// Check for localhost:9200 in logger
546+
foreach($logger->output as $row) {
547+
if (false !== strpos('warning: Request Failure:', $row)) {
548+
$this->assertStringContainsString('localhost:9200', $row);
549+
}
550+
}
551+
}
455552
}

0 commit comments

Comments
 (0)