diff --git a/.gitignore b/.gitignore index d958ca0..bd78348 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ vendor composer.lock jaeger-client-php.iml +phpunit.xml diff --git a/build/.gitignore b/build/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/build/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/composer.json b/composer.json index b813817..46749c4 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ }, "autoload": { "psr-4": { - "Jaeger\\": "src\\Jaeger" + "Jaeger\\": "src/Jaeger/" }, "files": [ "./src/Jaeger/Constants.php" diff --git a/phpunit.xml b/phpunit.xml deleted file mode 100644 index a38ce00..0000000 --- a/phpunit.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - src/Jaeger - - - - - - tests/Jaeger - - - diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..d6795d9 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,47 @@ + + + + + src/Jaeger + + src/ + src/Jaeger/Constants.php + src/Jaeger/Thrift/Agent/AgentIf.php + src/Jaeger/Thrift/Agent/AggregationValidatorIf.php + src/Jaeger/Thrift/Agent/BaggageRestrictionManagerIf.php + src/Jaeger/Thrift/Agent/DependencyIf.php + src/Jaeger/Thrift/Agent/SamplingManagerIf.php + src/Jaeger/Thrift/CollectorIf.php + src/Jaeger/Thrift/Crossdock/TracedServiceIf.php + + + + + + + tests/Jaeger + + + + + + + + + + + + + + + diff --git a/src/Jaeger/Tracer.php b/src/Jaeger/Tracer.php index 6a7c49e..30415ec 100644 --- a/src/Jaeger/Tracer.php +++ b/src/Jaeger/Tracer.php @@ -41,12 +41,11 @@ class Tracer implements OTTracer */ private $sampler; + /** + * @var string + */ private $ipAddress; - private $metricsFactory; - - private $metrics; - /** * @var string */ @@ -108,8 +107,6 @@ public function __construct( $this->logger = $logger ?? new NullLogger(); $this->scopeManager = $scopeManager ?? new ScopeManager(); - $this->ipAddress = gethostbyname(gethostname()); - $this->debugIdHeader = $debugIdHeader; $this->codecs = [ @@ -136,10 +133,10 @@ public function __construct( $this->tags = array_merge($this->tags, $tags); } - $hostname = gethostname(); - if ($hostname === FALSE) { - $this->logger->error('Unable to determine host name'); - } else { + $hostname = $this->getHostname(); + $this->ipAddress = $this->getHostByName($hostname); + + if (empty($hostname) != false) { $this->tags[JAEGER_HOSTNAME_TAG_KEY] = $hostname; } } @@ -342,6 +339,34 @@ private function randomId(): string return (string) random_int(0, PHP_INT_MAX); } + /** + * The facade to get the host name. + * + * @return string + */ + protected function getHostName() + { + return gethostname(); + } + + /** + * The facade to get IPv4 address corresponding to a given Internet host name. + * + * NOTE: DNS Resolution may take too long, and during this time your script is NOT being executed. + * + * @param string|null $hostname + * @return string + */ + protected function getHostByName($hostname) + { + if (empty($hostname)) { + $this->logger->error('Unable to determine host name'); + return '127.0.0.1'; + } + + return gethostbyname($hostname); + } + /** * @param SamplerInterface $sampler * @return $this diff --git a/tests/Jaeger/TracerTest.php b/tests/Jaeger/TracerTest.php index b81a3fe..d39251c 100644 --- a/tests/Jaeger/TracerTest.php +++ b/tests/Jaeger/TracerTest.php @@ -126,4 +126,23 @@ function testFlush() $this->tracer->flush(); } + + /** @test */ + public function shouldHandleEmptyHostName() + { + $tracer = new \ReflectionClass(Tracer::class); + + $getHostByName = $tracer->getMethod('getHostByName'); + $getHostByName->setAccessible(true); + + $stub = $this->getMockBuilder(Tracer::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + $logger = $tracer->getProperty('logger'); + $logger->setAccessible(true); + $logger->setValue($stub, $this->logger); + + $this->assertEquals('127.0.0.1', $getHostByName->invokeArgs($stub, [null])); + } }