Skip to content

Commit

Permalink
Improve exception handling when resolving ip address from host
Browse files Browse the repository at this point in the history
  • Loading branch information
adiachenko committed Aug 27, 2019
1 parent b8cd528 commit aed0c01
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/Drivers/Zipkin/ZipkinTracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Vinelab\Tracing\Drivers\Zipkin\Injectors\ZipkinInjector;
use Vinelab\Tracing\Exceptions\MissingTraceContextException;
use Vinelab\Tracing\Exceptions\UnregisteredFormatException;
use Vinelab\Tracing\Exceptions\UnresolvedCollectorIpException;
use Vinelab\Tracing\Propagation\Formats;
use Zipkin\Endpoint;
use Zipkin\Reporter;
Expand Down Expand Up @@ -325,7 +326,9 @@ protected function createReporter(): Reporter
protected function createEndpoint(): Endpoint
{
if (strpos($this->host, ":") === false) {
return Endpoint::create($this->serviceName, gethostbyname($this->host), null, $this->port);
$ipv4 = filter_var($this->host, FILTER_VALIDATE_IP) ? $this->host : $this->resolveCollectorIp($this->host);

return Endpoint::create($this->serviceName, $ipv4, null, $this->port);
}

return Endpoint::create($this->serviceName, null, $this->host, $this->port);
Expand Down Expand Up @@ -385,4 +388,19 @@ protected function resolveExtractor(string $format): ZipkinExtractor

return $extractor;
}

/**
* @param string $host
* @return string
*/
protected function resolveCollectorIp(string $host): string
{
$ipv4 = gethostbyname($host);

if ($ipv4 == $host) {
throw new UnresolvedCollectorIpException("Unable to resolve collector's IP address from hostname $host");
}

return $ipv4;
}
}
10 changes: 10 additions & 0 deletions src/Exceptions/UnresolvedCollectorIpException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Vinelab\Tracing\Exceptions;

use RuntimeException;

class UnresolvedCollectorIpException extends RuntimeException
{

}

0 comments on commit aed0c01

Please sign in to comment.