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

Commit 82d1dea

Browse files
hectorjchingor13
authored andcommitted
Fix zipkin span serialization with empty attributes (#102)
* ZipkinExporter: change test to verify that tags always serialize as a JSON object * ZipkinExporter: ensure empty attributes get serialized as a JSON object
1 parent 71278c2 commit 82d1dea

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

src/Trace/Exporter/ZipkinExporter.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ public function convertSpans(TracerInterface $tracer, $headers = null)
140140
? str_pad($span->parentSpanId(), 16, '0', STR_PAD_LEFT)
141141
: null;
142142

143+
$attributes = $span->attributes();
144+
if (empty($attributes)) {
145+
// force json_encode to render an empty object ("{}") instead of an empty array ("[]")
146+
$attributes = new \stdClass();
147+
}
148+
143149
$zipkinSpan = [
144150
'traceId' => $traceId,
145151
'name' => $span->name(),
@@ -150,7 +156,7 @@ public function convertSpans(TracerInterface $tracer, $headers = null)
150156
'debug' => $isDebug,
151157
'shared' => $isShared,
152158
'localEndpoint' => $localEndpoint,
153-
'tags' => $span->attributes()
159+
'tags' => $attributes
154160
];
155161

156162
$zipkinSpans[] = $zipkinSpan;

tests/unit/Trace/Exporter/ZipkinExporterTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ public function testFormatsTrace()
6666
$this->assertInternalType('int', $span['timestamp']);
6767
$this->assertInternalType('int', $span['duration']);
6868

69-
$this->assertInternalType('array', $span['tags']);
69+
// make sure we have a JSON object, even when there is no tags
70+
$this->assertStringStartsWith('{', \json_encode($span['tags']));
71+
$this->assertStringEndsWith('}', \json_encode($span['tags']));
72+
7073
foreach ($span['tags'] as $key => $value) {
7174
$this->assertInternalType('string', $key);
7275
$this->assertInternalType('string', $value);

0 commit comments

Comments
 (0)