Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 5150388

Browse files
authored
Fix Zipkin ipv4 localEndpoint (#112)
* Add test for skipping ipv4/ipv6 fields * phpdoc for server var
1 parent dcf4b30 commit 5150388

File tree

2 files changed

+81
-15
lines changed

2 files changed

+81
-15
lines changed

src/Trace/Exporter/ZipkinExporter.php

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@
3535
*/
3636
class ZipkinExporter implements ExporterInterface
3737
{
38-
/**
39-
* @var string
40-
*/
41-
private $name;
42-
4338
/**
4439
* @var string
4540
*/
@@ -55,20 +50,56 @@ class ZipkinExporter implements ExporterInterface
5550
*/
5651
private $url;
5752

53+
/**
54+
* @var array
55+
*/
56+
private $localEndpoint;
57+
5858
/**
5959
* Create a new ZipkinExporter
6060
*
6161
* @param string $name The name of this application
6262
* @param string $host The hostname of the Zipkin server
6363
* @param int $port The port of the Zipkin server
64-
* @param string $endpoint (optional) The path for the span reporting endpoint. **Defaults to** `/api/v2/spans`
64+
* @param string $endpoint (optional) The path for the span reporting
65+
* endpoint. **Defaults to** `/api/v2/spans`
66+
* @param array $server (optoinal) The server array to search for the
67+
* SERVER_PORT. **Defaults to** $_SERVER
6568
*/
66-
public function __construct($name, $host, $port, $endpoint = '/api/v2/spans')
69+
public function __construct($name, $host, $port, $endpoint = '/api/v2/spans', $server = null)
6770
{
68-
$this->name = $name;
71+
$server = $server ?: $_SERVER;
6972
$this->host = $host;
7073
$this->port = $port;
7174
$this->url = "http://${host}:${port}${endpoint}";
75+
$this->localEndpoint = [
76+
'serviceName' => $name
77+
];
78+
if (array_key_exists('SERVER_PORT', $server)) {
79+
$this->localEndpoint['port'] = $server['SERVER_PORT'];
80+
}
81+
}
82+
83+
/**
84+
* Set the localEndpoint ipv4 value for all reported spans. Note that this
85+
* is optional because the reverse DNS lookup can be slow.
86+
*
87+
* @param string $ipv4 IPv4 address
88+
*/
89+
public function setLocalIpv4($ipv4)
90+
{
91+
$this->localEndpoint['ipv4'] = $ipv4;
92+
}
93+
94+
/**
95+
* Set the localEndpoint ipv6 value for all reported spans. Note that this
96+
* is optional because the reverse DNS lookup can be slow.
97+
*
98+
* @param string $ipv6 IPv6 address
99+
*/
100+
public function setLocalIpv6($ipv6)
101+
{
102+
$this->localEndpoint['ipv6'] = $ipv6;
72103
}
73104

74105
/**
@@ -124,12 +155,6 @@ public function convertSpans(TracerInterface $tracer, $headers = null)
124155
// True if we are contributing to a span started by another tracer (ex on a different host).
125156
$isShared = !empty($spans) && $spans[0]->parentSpanId() != null;
126157

127-
$localEndpoint = [
128-
'serviceName' => $this->name,
129-
'ipv4' => $this->host,
130-
'port' => $this->port
131-
];
132-
133158
$zipkinSpans = [];
134159
foreach ($spans as $span) {
135160
$startTime = (int)((float) $span->startTime()->format('U.u') * 1000 * 1000);
@@ -154,7 +179,7 @@ public function convertSpans(TracerInterface $tracer, $headers = null)
154179
'duration' => $endTime - $startTime,
155180
'debug' => $isDebug,
156181
'shared' => $isShared,
157-
'localEndpoint' => $localEndpoint,
182+
'localEndpoint' => $this->localEndpoint,
158183
'tags' => $attributes
159184
];
160185

tests/unit/Trace/Exporter/ZipkinExporterTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,45 @@ public function testEmptyTrace()
136136
$spans = $reporter->convertSpans($tracer);
137137
$this->assertEmpty($spans);
138138
}
139+
140+
public function testSkipsIpv4()
141+
{
142+
$spanContext = new SpanContext('testtraceid', 12345);
143+
$tracer = new ContextTracer($spanContext);
144+
$tracer->inSpan(['name' => 'main'], function () {});
145+
146+
$reporter = new ZipkinExporter('myapp', 'localhost', 9411);
147+
$spans = $reporter->convertSpans($tracer);
148+
$endpoint = $spans[0]['localEndpoint'];
149+
$this->assertArrayNotHasKey('ipv4', $endpoint);
150+
$this->assertArrayNotHasKey('ipv6', $endpoint);
151+
}
152+
153+
public function testSetsIpv4()
154+
{
155+
$spanContext = new SpanContext('testtraceid', 12345);
156+
$tracer = new ContextTracer($spanContext);
157+
$tracer->inSpan(['name' => 'main'], function () {});
158+
159+
$reporter = new ZipkinExporter('myapp', 'localhost', 9411);
160+
$reporter->setLocalIpv4('1.2.3.4');
161+
$spans = $reporter->convertSpans($tracer);
162+
$endpoint = $spans[0]['localEndpoint'];
163+
$this->assertArrayHasKey('ipv4', $endpoint);
164+
$this->assertEquals('1.2.3.4', $endpoint['ipv4']);
165+
}
166+
167+
public function testSetsIpv6()
168+
{
169+
$spanContext = new SpanContext('testtraceid', 12345);
170+
$tracer = new ContextTracer($spanContext);
171+
$tracer->inSpan(['name' => 'main'], function () {});
172+
173+
$reporter = new ZipkinExporter('myapp', 'localhost', 9411);
174+
$reporter->setLocalIpv6('2001:db8:85a3::8a2e:370:7334');
175+
$spans = $reporter->convertSpans($tracer);
176+
$endpoint = $spans[0]['localEndpoint'];
177+
$this->assertArrayHasKey('ipv6', $endpoint);
178+
$this->assertEquals('2001:db8:85a3::8a2e:370:7334', $endpoint['ipv6']);
179+
}
139180
}

0 commit comments

Comments
 (0)