Skip to content

Commit bf572e4

Browse files
sergeyklayjonahgeorge
authored andcommitted
Added facades for hosts functions (#49)
* Setting up PHPUnit * Added facades for hosts functions
1 parent 0942cdd commit bf572e4

File tree

7 files changed

+105
-34
lines changed

7 files changed

+105
-34
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
vendor
66
composer.lock
77
jaeger-client-php.iml
8+
phpunit.xml

build/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"autoload": {
2121
"psr-4": {
22-
"Jaeger\\": "src\\Jaeger"
22+
"Jaeger\\": "src/Jaeger/"
2323
},
2424
"files": [
2525
"./src/Jaeger/Constants.php"

phpunit.xml

Lines changed: 0 additions & 23 deletions
This file was deleted.

phpunit.xml.dist

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
>
7+
<filter>
8+
<whitelist>
9+
<directory suffix=".php">src/Jaeger</directory>
10+
<exclude>
11+
<directory suffix="Interface.php">src/</directory>
12+
<file>src/Jaeger/Constants.php</file>
13+
<file>src/Jaeger/Thrift/Agent/AgentIf.php</file>
14+
<file>src/Jaeger/Thrift/Agent/AggregationValidatorIf.php</file>
15+
<file>src/Jaeger/Thrift/Agent/BaggageRestrictionManagerIf.php</file>
16+
<file>src/Jaeger/Thrift/Agent/DependencyIf.php</file>
17+
<file>src/Jaeger/Thrift/Agent/SamplingManagerIf.php</file>
18+
<file>src/Jaeger/Thrift/CollectorIf.php</file>
19+
<file>src/Jaeger/Thrift/Crossdock/TracedServiceIf.php</file>
20+
</exclude>
21+
</whitelist>
22+
</filter>
23+
24+
<testsuites>
25+
<testsuite name="Jaeger Test Suite">
26+
<directory suffix=".php">tests/Jaeger</directory>
27+
</testsuite>
28+
</testsuites>
29+
30+
<php>
31+
<ini name="date.timezone" value="UTC"/>
32+
<ini name="display_errors" value="on"/>
33+
<ini name="display_startup_errors" value="on"/>
34+
</php>
35+
36+
<logging>
37+
<log
38+
type="coverage-text"
39+
target="php://stdout"
40+
lowUpperBound="60"
41+
highLowerBound="90"/>
42+
43+
<log
44+
type="coverage-clover"
45+
target="build/coverage.xml"/>
46+
</logging>
47+
</phpunit>

src/Jaeger/Tracer.php

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,11 @@ class Tracer implements OTTracer
4141
*/
4242
private $sampler;
4343

44+
/**
45+
* @var string
46+
*/
4447
private $ipAddress;
4548

46-
private $metricsFactory;
47-
48-
private $metrics;
49-
5049
/**
5150
* @var string
5251
*/
@@ -108,8 +107,6 @@ public function __construct(
108107
$this->logger = $logger ?? new NullLogger();
109108
$this->scopeManager = $scopeManager ?? new ScopeManager();
110109

111-
$this->ipAddress = gethostbyname(gethostname());
112-
113110
$this->debugIdHeader = $debugIdHeader;
114111

115112
$this->codecs = [
@@ -136,10 +133,10 @@ public function __construct(
136133
$this->tags = array_merge($this->tags, $tags);
137134
}
138135

139-
$hostname = gethostname();
140-
if ($hostname === FALSE) {
141-
$this->logger->error('Unable to determine host name');
142-
} else {
136+
$hostname = $this->getHostname();
137+
$this->ipAddress = $this->getHostByName($hostname);
138+
139+
if (empty($hostname) != false) {
143140
$this->tags[JAEGER_HOSTNAME_TAG_KEY] = $hostname;
144141
}
145142
}
@@ -342,6 +339,34 @@ private function randomId(): string
342339
return (string) random_int(0, PHP_INT_MAX);
343340
}
344341

342+
/**
343+
* The facade to get the host name.
344+
*
345+
* @return string
346+
*/
347+
protected function getHostName()
348+
{
349+
return gethostname();
350+
}
351+
352+
/**
353+
* The facade to get IPv4 address corresponding to a given Internet host name.
354+
*
355+
* NOTE: DNS Resolution may take too long, and during this time your script is NOT being executed.
356+
*
357+
* @param string|null $hostname
358+
* @return string
359+
*/
360+
protected function getHostByName($hostname)
361+
{
362+
if (empty($hostname)) {
363+
$this->logger->error('Unable to determine host name');
364+
return '127.0.0.1';
365+
}
366+
367+
return gethostbyname($hostname);
368+
}
369+
345370
/**
346371
* @param SamplerInterface $sampler
347372
* @return $this

tests/Jaeger/TracerTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,23 @@ function testFlush()
126126

127127
$this->tracer->flush();
128128
}
129+
130+
/** @test */
131+
public function shouldHandleEmptyHostName()
132+
{
133+
$tracer = new \ReflectionClass(Tracer::class);
134+
135+
$getHostByName = $tracer->getMethod('getHostByName');
136+
$getHostByName->setAccessible(true);
137+
138+
$stub = $this->getMockBuilder(Tracer::class)
139+
->disableOriginalConstructor()
140+
->getMockForAbstractClass();
141+
142+
$logger = $tracer->getProperty('logger');
143+
$logger->setAccessible(true);
144+
$logger->setValue($stub, $this->logger);
145+
146+
$this->assertEquals('127.0.0.1', $getHostByName->invokeArgs($stub, [null]));
147+
}
129148
}

0 commit comments

Comments
 (0)