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

Commit f4e3c41

Browse files
authored
Fix ExporterInterface (#150)
* Remove accessors from Span. Add Span#spanData which returns its SpanData * ExporterInterface is now export(array ) * Fix extension test * Fix doc for EchoExporter * Mock StackdriverExporter client for tests * Missed spot to mock Stackdriver client
1 parent f1d71ba commit f4e3c41

22 files changed

+295
-432
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"cache/adapter-common": "^1.0"
1919
},
2020
"require-dev": {
21-
"phpunit/phpunit": ">= 4.8",
21+
"phpunit/phpunit": ">= 6.0",
2222
"squizlabs/php_codesniffer": "2.*",
2323
"google/cloud-trace": "^0.4",
2424
"twig/twig": "~2.0",

src/Trace/Exporter/EchoExporter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
namespace OpenCensus\Trace\Exporter;
1919

20-
use OpenCensus\Trace\Tracer\TracerInterface;
20+
use OpenCensus\Trace\SpanData;
2121

2222
/**
2323
* This implementation of the ExporterInterface uses `print_r` to output
@@ -37,12 +37,12 @@ class EchoExporter implements ExporterInterface
3737
/**
3838
* Report the provided Trace to a backend.
3939
*
40-
* @param TracerInterface $tracer
40+
* @param SpanData[] $spans
4141
* @return bool
4242
*/
43-
public function report(TracerInterface $tracer)
43+
public function export(array $spans)
4444
{
45-
print_r($tracer->spans());
45+
print_r($spans);
4646
return true;
4747
}
4848
}

src/Trace/Exporter/ExporterInterface.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@
1717

1818
namespace OpenCensus\Trace\Exporter;
1919

20-
use OpenCensus\Trace\Tracer\TracerInterface;
20+
use OpenCensus\Trace\SpanData;
2121

2222
/**
2323
* The ExporterInterface allows you to swap out the Trace reporting mechanism
2424
*/
2525
interface ExporterInterface
2626
{
2727
/**
28-
* Report the provided Trace to a backend.
28+
* Export the provided SpanData to a backend.
2929
*
30-
* @param TracerInterface $tracer
30+
* @param SpanData[] $spans
3131
* @return bool
3232
*/
33-
public function report(TracerInterface $tracer);
33+
public function export(array $spans);
3434
}

src/Trace/Exporter/FileExporter.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
namespace OpenCensus\Trace\Exporter;
1919

20-
use OpenCensus\Trace\Span;
20+
use OpenCensus\Trace\SpanData;
2121
use OpenCensus\Trace\Tracer\TracerInterface;
2222

2323
/**
@@ -51,30 +51,28 @@ public function __construct($filename)
5151
}
5252

5353
/**
54-
* Report the provided Trace to a backend.
54+
* Report the provided SpanData to a file in json format.
5555
*
56-
* @param TracerInterface $tracer
56+
* @param SpanData[] $spans
5757
* @return bool
5858
*/
59-
public function report(TracerInterface $tracer)
59+
public function export(array $spans)
6060
{
61-
$spans = $this->convertSpans($tracer);
61+
$spans = $this->convertSpans($spans);
6262
return file_put_contents($this->filename, json_encode($spans) . PHP_EOL, FILE_APPEND) !== false;
6363
}
6464

6565
/**
6666
* Convert spans into array ready for serialization.
6767
*
68-
* @param TracerInterface $tracer
68+
* @param SpanData[] $spans
6969
* @return array Representation of the collected trace spans ready for serialization
7070
*/
71-
private function convertSpans(TracerInterface $tracer)
71+
private function convertSpans(array $spans)
7272
{
73-
$traceId = $tracer->spanContext()->traceId();
74-
75-
return array_map(function (Span $span) use ($traceId) {
73+
return array_map(function (SpanData $span) use ($traceId) {
7674
return [
77-
'traceId' => $traceId,
75+
'traceId' => $span->traceId(),
7876
'name' => $span->name(),
7977
'spanId' => $span->spanId(),
8078
'parentSpanId' => $span->parentSpanId(),
@@ -87,6 +85,6 @@ private function convertSpans(TracerInterface $tracer)
8785
'links' => $span->links(),
8886
'sameProcessAsParentSpan' => $span->sameProcessAsParentSpan(),
8987
];
90-
}, $tracer->spans());
88+
}, $spans);
9189
}
9290
}

src/Trace/Exporter/LoggerExporter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
namespace OpenCensus\Trace\Exporter;
1919

20-
use OpenCensus\Trace\Tracer\TracerInterface;
20+
use OpenCensus\Trace\SpanData;
2121
use Psr\Log\LoggerInterface;
2222

2323
/**
@@ -65,13 +65,13 @@ public function __construct(LoggerInterface $logger, $level = self::DEFAULT_LOG_
6565
/**
6666
* Report the provided Trace to a backend.
6767
*
68-
* @param TracerInterface $tracer
68+
* @param SpanData[] $spans
6969
* @return bool
7070
*/
71-
public function report(TracerInterface $tracer)
71+
public function export(array $spans)
7272
{
7373
try {
74-
$this->logger->log($this->level, json_encode($tracer->spans()));
74+
$this->logger->log($this->level, json_encode($spans));
7575
} catch (\Exception $e) {
7676
error_log('Reporting the Trace data failed: ' . $e->getMessage());
7777
return false;

src/Trace/Exporter/NullExporter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ class NullExporter implements ExporterInterface
3636
/**
3737
* Does nothing.
3838
*
39-
* @param TracerInterface $tracer
39+
* @param SpanData[] $spans
4040
* @return bool
4141
*/
42-
public function report(TracerInterface $tracer)
42+
public function export(array $spans)
4343
{
4444
return true;
4545
}

src/Trace/Exporter/StackdriverExporter.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
use Google\Cloud\Trace\TraceClient;
2323
use Google\Cloud\Trace\Span;
2424
use Google\Cloud\Trace\Trace;
25-
use OpenCensus\Trace\Tracer\TracerInterface;
2625
use OpenCensus\Trace\Span as OCSpan;
26+
use OpenCensus\Trace\SpanData;
2727

2828
/**
2929
* This implementation of the ExporterInterface use the BatchRunner to provide
@@ -135,19 +135,20 @@ public function __construct(array $options = [])
135135
/**
136136
* Report the provided Trace to a backend.
137137
*
138-
* @param TracerInterface $tracer
138+
* @param SpanData[] $spans
139139
* @return bool
140140
*/
141-
public function report(TracerInterface $tracer)
141+
public function export(array $spans)
142142
{
143-
$spans = $this->convertSpans($tracer);
143+
$spans = $this->convertSpans($spans);
144144

145145
if (empty($spans)) {
146146
return false;
147147
}
148148

149+
// Pull the traceId from the first span
149150
$trace = self::$client->trace(
150-
$tracer->spanContext()->traceId()
151+
$spans[0]->traceId()
151152
);
152153

153154
// build a Trace object and assign Spans
@@ -170,16 +171,16 @@ public function report(TracerInterface $tracer)
170171
*
171172
* @access private
172173
*
173-
* @param TracerInterface $tracer
174+
* @param SpanData[] $spans
174175
* @return Span[] Representation of the collected trace spans ready for serialization
175176
*/
176-
public function convertSpans(TracerInterface $tracer)
177+
public function convertSpans(array $spans)
177178
{
178179
// transform OpenCensus Spans to Google\Cloud\Trace\Spans
179-
return array_map([$this, 'mapSpan'], $tracer->spans());
180+
return array_map([$this, 'mapSpan'], $spans);
180181
}
181182

182-
private function mapSpan($span)
183+
private function mapSpan(SpanData $span)
183184
{
184185
return new Span($span->traceId(), [
185186
'name' => $span->name(),

src/Trace/Exporter/ZipkinExporter.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
namespace OpenCensus\Trace\Exporter;
1919

2020
use OpenCensus\Trace\MessageEvent;
21-
use OpenCensus\Trace\Tracer\TracerInterface;
22-
use OpenCensus\Trace\Span;
21+
use OpenCensus\Trace\SpanData;
2322

2423
/**
2524
* This implementation of the ExporterInterface appends a json
@@ -96,12 +95,12 @@ public function setLocalIpv6($ipv6)
9695
/**
9796
* Report the provided Trace to a backend.
9897
*
99-
* @param TracerInterface $tracer
98+
* @param SpanData[] $spans
10099
* @return bool
101100
*/
102-
public function report(TracerInterface $tracer)
101+
public function export(array $spans)
103102
{
104-
$spans = $this->convertSpans($tracer);
103+
$spans = $this->convertSpans($spans);
105104

106105
if (empty($spans)) {
107106
return false;
@@ -129,15 +128,13 @@ public function report(TracerInterface $tracer)
129128
* Convert spans into Zipkin's expected JSON output format. See
130129
* <a href="http://zipkin.io/zipkin-api/#/default/post_spans">output format definition</a>.
131130
*
132-
* @param TracerInterface $tracer
131+
* @param SpanData[] $spans
133132
* @param array $headers [optional] HTTP headers to parse. **Defaults to** $_SERVER
134133
* @return array Representation of the collected trace spans ready for serialization
135134
*/
136-
public function convertSpans(TracerInterface $tracer, $headers = null)
135+
public function convertSpans(array $spans, $headers = null)
137136
{
138137
$headers = $headers ?: $_SERVER;
139-
$spans = $tracer->spans();
140-
$traceId = $tracer->spanContext()->traceId();
141138

142139
// True is a request to store this span even if it overrides sampling policy.
143140
// This is true when the X-B3-Flags header has a value of 1.
@@ -154,6 +151,7 @@ public function convertSpans(TracerInterface $tracer, $headers = null)
154151
$parentSpanId = $span->parentSpanId()
155152
? str_pad($span->parentSpanId(), 16, '0', STR_PAD_LEFT)
156153
: null;
154+
$traceId = str_pad($span->traceId(), 32, '0', STR_PAD_LEFT);
157155

158156
$attributes = $span->attributes();
159157
if (empty($attributes)) {
@@ -184,7 +182,7 @@ public function convertSpans(TracerInterface $tracer, $headers = null)
184182
return $zipkinSpans;
185183
}
186184

187-
private function spanKind(Span $span)
185+
private function spanKind(SpanData $span)
188186
{
189187
if (strpos($span->name(), 'Sent.') === 0) {
190188
return self::KIND_CLIENT;

src/Trace/RequestHandler.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class RequestHandler
4747
/**
4848
* @var ExporterInterface The reported to use at the end of the request
4949
*/
50-
private $reporter;
50+
private $exporter;
5151

5252
/**
5353
* @var TracerInterface The tracer to use for this request
@@ -72,7 +72,7 @@ class RequestHandler
7272
/**
7373
* Create a new RequestHandler.
7474
*
75-
* @param ExporterInterface $reporter How to report the trace at the end of the request
75+
* @param ExporterInterface $exporter How to report the trace at the end of the request
7676
* @param SamplerInterface $sampler Which sampler to use for sampling requests
7777
* @param PropagatorInterface $propagator SpanContext propagator
7878
* @param array $options [optional] {
@@ -85,12 +85,12 @@ class RequestHandler
8585
* }
8686
*/
8787
public function __construct(
88-
ExporterInterface $reporter,
88+
ExporterInterface $exporter,
8989
SamplerInterface $sampler,
9090
PropagatorInterface $propagator,
9191
array $options = []
9292
) {
93-
$this->reporter = $reporter;
93+
$this->exporter = $exporter;
9494
$this->headers = array_key_exists('headers', $options)
9595
? $options['headers']
9696
: $_SERVER;
@@ -135,7 +135,12 @@ public function onExit()
135135
$this->addCommonRequestAttributes($this->headers);
136136

137137
$this->scope->close();
138-
$this->reporter->report($this->tracer);
138+
139+
// Fetch the SpanData of all the collected Spans
140+
$spans = array_map(function (Span $span) {
141+
return $span->spanData();
142+
}, $this->tracer->spans());
143+
$this->exporter->export($spans);
139144
}
140145

141146
/**

0 commit comments

Comments
 (0)