Skip to content

Commit

Permalink
Added facades for hosts functions (#49)
Browse files Browse the repository at this point in the history
* Setting up PHPUnit

* Added facades for hosts functions
  • Loading branch information
sergeyklay authored and jonahgeorge committed Jul 19, 2018
1 parent 0942cdd commit bf572e4
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
vendor
composer.lock
jaeger-client-php.iml
phpunit.xml
2 changes: 2 additions & 0 deletions build/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"autoload": {
"psr-4": {
"Jaeger\\": "src\\Jaeger"
"Jaeger\\": "src/Jaeger/"
},
"files": [
"./src/Jaeger/Constants.php"
Expand Down
23 changes: 0 additions & 23 deletions phpunit.xml

This file was deleted.

47 changes: 47 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<filter>
<whitelist>
<directory suffix=".php">src/Jaeger</directory>
<exclude>
<directory suffix="Interface.php">src/</directory>
<file>src/Jaeger/Constants.php</file>
<file>src/Jaeger/Thrift/Agent/AgentIf.php</file>
<file>src/Jaeger/Thrift/Agent/AggregationValidatorIf.php</file>
<file>src/Jaeger/Thrift/Agent/BaggageRestrictionManagerIf.php</file>
<file>src/Jaeger/Thrift/Agent/DependencyIf.php</file>
<file>src/Jaeger/Thrift/Agent/SamplingManagerIf.php</file>
<file>src/Jaeger/Thrift/CollectorIf.php</file>
<file>src/Jaeger/Thrift/Crossdock/TracedServiceIf.php</file>
</exclude>
</whitelist>
</filter>

<testsuites>
<testsuite name="Jaeger Test Suite">
<directory suffix=".php">tests/Jaeger</directory>
</testsuite>
</testsuites>

<php>
<ini name="date.timezone" value="UTC"/>
<ini name="display_errors" value="on"/>
<ini name="display_startup_errors" value="on"/>
</php>

<logging>
<log
type="coverage-text"
target="php://stdout"
lowUpperBound="60"
highLowerBound="90"/>

<log
type="coverage-clover"
target="build/coverage.xml"/>
</logging>
</phpunit>
45 changes: 35 additions & 10 deletions src/Jaeger/Tracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ class Tracer implements OTTracer
*/
private $sampler;

/**
* @var string
*/
private $ipAddress;

private $metricsFactory;

private $metrics;

/**
* @var string
*/
Expand Down Expand Up @@ -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 = [
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions tests/Jaeger/TracerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
}
}

0 comments on commit bf572e4

Please sign in to comment.