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

Commit ac4be6d

Browse files
Arquiveichingor13
authored andcommitted
Fix file exporter serialization (#124)
* Init arrays as arrays If we try to convert OpenCensus\Trace\Span to Google\Cloud\Trace\Span and timeEvents is null, we'll get an error while calling json_encode on spans with null timeEvents. As Google\Cloud\Trace\Span addTimeEvents method expects array if we give null it will raise an error. * Fix FileExporter report serialization (#119) The FileExporter report method uses json_encode but pass an OpenCensus\Trace\Span array as arg. As OpenCensus\Trace\Span does not implements JsonSerializable it isn't serealizable. Convert spans into arrays solves the issue.
1 parent 8aee341 commit ac4be6d

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

src/Trace/Exporter/FileExporter.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace OpenCensus\Trace\Exporter;
1919

20+
use OpenCensus\Trace\Span;
2021
use OpenCensus\Trace\Tracer\TracerInterface;
2122

2223
/**
@@ -57,6 +58,35 @@ public function __construct($filename)
5758
*/
5859
public function report(TracerInterface $tracer)
5960
{
60-
return file_put_contents($this->filename, json_encode($tracer->spans()) . PHP_EOL, FILE_APPEND) !== false;
61+
$spans = $this->convertSpans($tracer);
62+
return file_put_contents($this->filename, json_encode($spans) . PHP_EOL, FILE_APPEND) !== false;
63+
}
64+
65+
/**
66+
* Convert spans into array ready for serialization.
67+
*
68+
* @param TracerInterface $tracer
69+
* @return array Representation of the collected trace spans ready for serialization
70+
*/
71+
private function convertSpans(TracerInterface $tracer)
72+
{
73+
$traceId = $tracer->spanContext()->traceId();
74+
75+
return array_map(function (Span $span) use ($traceId) {
76+
return [
77+
'traceId' => $traceId,
78+
'name' => $span->name(),
79+
'spanId' => $span->spanId(),
80+
'parentSpanId' => $span->parentSpanId(),
81+
'stackTrace' => $span->stackTrace(),
82+
'startTime' => $span->startTime(),
83+
'endTime' => $span->endTime(),
84+
'status' => $span->status(),
85+
'attributes' => $span->attributes(),
86+
'timeEvents' => $span->timeEvents(),
87+
'links' => $span->links(),
88+
'sameProcessAsParentSpan' => $span->sameProcessAsParentSpan(),
89+
];
90+
}, $tracer->spans());
6191
}
6292
}

src/Trace/Span.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class Span
8888
*
8989
* @var array
9090
*/
91-
private $stackTrace;
91+
private $stackTrace = [];
9292

9393
/**
9494
* A collection of `TimeEvent`s. A `TimeEvent` is a time-stamped annotation
@@ -97,15 +97,15 @@ class Span
9797
*
9898
* @var TimeEvent[]
9999
*/
100-
private $timeEvents;
100+
private $timeEvents = [];
101101

102102
/**
103103
* A collection of links, which are references from this span to a span
104104
* in the same or different trace.
105105
*
106-
* @var Link
106+
* @var Link[]
107107
*/
108-
private $links;
108+
private $links = [];
109109

110110
/**
111111
* An optional final status for this span.

tests/unit/Trace/Exporter/FileExporterTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public function tearDown()
4343

4444
public function testLogsTrace()
4545
{
46+
$this->tracer->spanContext()->willReturn(new SpanContext());
47+
4648
$spans = [
4749
new Span([
4850
'name' => 'span',

0 commit comments

Comments
 (0)