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

Commit da11ddb

Browse files
jcchavezschingor13
authored andcommitted
[#125] Simplifies the Zipkin exporter construction. (#139)
1 parent bec83f9 commit da11ddb

File tree

2 files changed

+18
-31
lines changed

2 files changed

+18
-31
lines changed

src/Trace/Exporter/ZipkinExporter.php

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,20 @@
3030
* use OpenCensus\Trace\Exporter\ZipkinExporter;
3131
* use OpenCensus\Trace\Tracer;
3232
*
33-
* $exporter = new ZipkinExporter('my_app', 'localhost', 9411);
33+
* $exporter = new ZipkinExporter('my_app');
3434
* Tracer::begin($exporter);
3535
* ```
3636
*/
3737
class ZipkinExporter implements ExporterInterface
3838
{
3939
const KIND_SERVER = 'SERVER';
4040
const KIND_CLIENT = 'CLIENT';
41+
const DEFAULT_ENDPOINT = 'http://localhost:9411/api/v2/spans';
4142

4243
/**
4344
* @var string
4445
*/
45-
private $host;
46-
47-
/**
48-
* @var int
49-
*/
50-
private $port;
51-
52-
/**
53-
* @var string
54-
*/
55-
private $url;
46+
private $endpointUrl;
5647

5748
/**
5849
* @var array
@@ -63,19 +54,15 @@ class ZipkinExporter implements ExporterInterface
6354
* Create a new ZipkinExporter
6455
*
6556
* @param string $name The name of this application
66-
* @param string $host The hostname of the Zipkin server
67-
* @param int $port The port of the Zipkin server
68-
* @param string $endpoint (optional) The path for the span reporting
69-
* endpoint. **Defaults to** `/api/v2/spans`
70-
* @param array $server (optoinal) The server array to search for the
57+
* @param string $endpointUrl (optional) The url for the span reporting
58+
* endpoint. **Defaults to** `http://localhost:9411/api/v2/spans`
59+
* @param array $server (optional) The server array to search for the
7160
* SERVER_PORT. **Defaults to** $_SERVER
7261
*/
73-
public function __construct($name, $host, $port, $endpoint = '/api/v2/spans', $server = null)
62+
public function __construct($name, $endpointUrl = null, array $server = null)
7463
{
7564
$server = $server ?: $_SERVER;
76-
$this->host = $host;
77-
$this->port = $port;
78-
$this->url = "http://${host}:${port}${endpoint}";
65+
$this->endpointUrl = ($endpointUrl === null) ? self::DEFAULT_ENDPOINT : $endpointUrl;
7966
$this->localEndpoint = [
8067
'serviceName' => $name
8168
];
@@ -131,7 +118,7 @@ public function report(TracerInterface $tracer)
131118
];
132119

133120
$context = stream_context_create($contextOptions);
134-
file_get_contents($this->url, false, $context);
121+
file_get_contents($this->endpointUrl, false, $context);
135122
} catch (\Exception $e) {
136123
return false;
137124
}

tests/unit/Trace/Exporter/ZipkinExporterTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function testFormatsTrace()
5555
$this->tracer->spanContext()->willReturn(new SpanContext());
5656
$this->tracer->spans()->willReturn($spans);
5757

58-
$reporter = new ZipkinExporter('myapp', 'localhost', 9411);
58+
$reporter = new ZipkinExporter('myapp');
5959

6060
$data = $reporter->convertSpans($this->tracer->reveal());
6161

@@ -90,7 +90,7 @@ public function testSpanKind($spanOpts, $kind)
9090
$tracer->inSpan($spanOpts, 'usleep', [1]);
9191
});
9292

93-
$reporter = new ZipkinExporter('myapp', 'localhost', 9411);
93+
$reporter = new ZipkinExporter('myapp');
9494
$spans = $reporter->convertSpans($tracer);
9595

9696
$this->assertCount(2, $spans);
@@ -114,7 +114,7 @@ public function testSpanDebug()
114114
$tracer = new ContextTracer($spanContext);
115115
$tracer->inSpan(['name' => 'main'], function () {});
116116

117-
$reporter = new ZipkinExporter('myapp', 'localhost', 9411);
117+
$reporter = new ZipkinExporter('myapp');
118118
$spans = $reporter->convertSpans($tracer, [
119119
'HTTP_X_B3_FLAGS' => '1'
120120
]);
@@ -129,7 +129,7 @@ public function testSpanShared()
129129
$tracer = new ContextTracer($spanContext);
130130
$tracer->inSpan(['name' => 'main'], function () {});
131131

132-
$reporter = new ZipkinExporter('myapp', 'localhost', 9411);
132+
$reporter = new ZipkinExporter('myapp');
133133
$spans = $reporter->convertSpans($tracer);
134134

135135
$this->assertCount(1, $spans);
@@ -140,7 +140,7 @@ public function testEmptyTrace()
140140
{
141141
$spanContext = new SpanContext('testtraceid', 12345);
142142
$tracer = new ContextTracer($spanContext);
143-
$reporter = new ZipkinExporter('myapp', 'localhost', 9411);
143+
$reporter = new ZipkinExporter('myapp');
144144
$spans = $reporter->convertSpans($tracer);
145145
$this->assertEmpty($spans);
146146
}
@@ -151,7 +151,7 @@ public function testSkipsIpv4()
151151
$tracer = new ContextTracer($spanContext);
152152
$tracer->inSpan(['name' => 'main'], function () {});
153153

154-
$reporter = new ZipkinExporter('myapp', 'localhost', 9411);
154+
$reporter = new ZipkinExporter('myapp');
155155
$spans = $reporter->convertSpans($tracer);
156156
$endpoint = $spans[0]['localEndpoint'];
157157
$this->assertArrayNotHasKey('ipv4', $endpoint);
@@ -164,7 +164,7 @@ public function testSetsIpv4()
164164
$tracer = new ContextTracer($spanContext);
165165
$tracer->inSpan(['name' => 'main'], function () {});
166166

167-
$reporter = new ZipkinExporter('myapp', 'localhost', 9411);
167+
$reporter = new ZipkinExporter('myapp');
168168
$reporter->setLocalIpv4('1.2.3.4');
169169
$spans = $reporter->convertSpans($tracer);
170170
$endpoint = $spans[0]['localEndpoint'];
@@ -178,7 +178,7 @@ public function testSetsIpv6()
178178
$tracer = new ContextTracer($spanContext);
179179
$tracer->inSpan(['name' => 'main'], function () {});
180180

181-
$reporter = new ZipkinExporter('myapp', 'localhost', 9411);
181+
$reporter = new ZipkinExporter('myapp');
182182
$reporter->setLocalIpv6('2001:db8:85a3::8a2e:370:7334');
183183
$spans = $reporter->convertSpans($tracer);
184184
$endpoint = $spans[0]['localEndpoint'];
@@ -192,7 +192,7 @@ public function testSetsLocalEndpointPort()
192192
$tracer = new ContextTracer($spanContext);
193193
$tracer->inSpan(['name' => 'main'], function () {});
194194

195-
$reporter = new ZipkinExporter('myapp', 'localhost', 9411, '/api/v2/spans', ['SERVER_PORT' => "80"]);
195+
$reporter = new ZipkinExporter('myapp', null, ['SERVER_PORT' => "80"]);
196196
$spans = $reporter->convertSpans($tracer);
197197
$endpoint = $spans[0]['localEndpoint'];
198198
$this->assertArrayHasKey('port', $endpoint);

0 commit comments

Comments
 (0)