Skip to content

Commit f06a857

Browse files
authored
fix(json-serializer): fixes serialization for strings including backslashes. (#189)
* fix(json-serializer): fixes serialization for strings including backslashes. * chore(json-serializer): improves serialization. * chore(lint): makes lint happy.
1 parent 8776142 commit f06a857

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

src/Zipkin/Reporters/JsonV2Serializer.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function serialize(array $spans): string
3333

3434
private static function serializeEndpoint(Endpoint $endpoint): string
3535
{
36-
$endpointStr = '{"serviceName":"' . \strtolower($endpoint->getServiceName()) . '"';
36+
$endpointStr = '{"serviceName":"' . \strtolower(self::escapeString($endpoint->getServiceName())) . '"';
3737

3838
if ($endpoint->getIpv4() !== null) {
3939
$endpointStr .= ',"ipv4":"' . $endpoint->getIpv4() . '"';
@@ -50,10 +50,10 @@ private static function serializeEndpoint(Endpoint $endpoint): string
5050
return $endpointStr . '}';
5151
}
5252

53-
private static function escapeQuotes(string $s): string
53+
private static function escapeString(string $s): string
5454
{
55-
$encodedString = json_encode($s);
56-
return $encodedString ? trim($encodedString, '"') : $s;
55+
$encodedString = \json_encode($s);
56+
return $encodedString === false ? $s : \mb_substr($encodedString, 1, -1);
5757
}
5858

5959
private function serializeSpan(ReadbackSpan $span): string
@@ -64,7 +64,7 @@ private function serializeSpan(ReadbackSpan $span): string
6464
. ',"timestamp":' . $span->getTimestamp();
6565

6666
if ($span->getName() !== null) {
67-
$spanStr .= ',"name":"' . \strtolower($span->getName()) . '"';
67+
$spanStr .= ',"name":"' . \strtolower(self::escapeString($span->getName())) . '"';
6868
}
6969

7070
if ($span->getDuration() !== null) {
@@ -104,7 +104,7 @@ private function serializeSpan(ReadbackSpan $span): string
104104
} else {
105105
$spanStr .= ',';
106106
}
107-
$spanStr .= '{"value":"' . self::escapeQuotes($annotation['value'])
107+
$spanStr .= '{"value":"' . self::escapeString($annotation['value'])
108108
. '","timestamp":' . $annotation['timestamp'] . '}';
109109
}
110110
$spanStr .= ']';
@@ -125,7 +125,7 @@ private function serializeSpan(ReadbackSpan $span): string
125125
} else {
126126
$spanStr .= ',';
127127
}
128-
$spanStr .= '"' . $key . '":"' . self::escapeQuotes($value) . '"';
128+
$spanStr .= '"' . $key . '":"' . self::escapeString($value) . '"';
129129
}
130130
$spanStr .= '}';
131131
}

tests/Integration/Reporters/Http/CurlFactoryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function (RequestInterface $request, ResponseInterface &$response) use ($t) {
4141
'endpoint_url' => $server->getUrl(),
4242
]);
4343

44-
$curlClient(json_encode([]));
44+
$curlClient(\json_encode([]));
4545
$this->assertTrue(true);
4646
} finally {
4747
$server->stop();
@@ -69,7 +69,7 @@ function (RequestInterface $request, ResponseInterface &$response) use ($t) {
6969
'headers' => ['From' => 'user@example.com', 'Content-Type' => 'test'],
7070
]);
7171

72-
$curlClient(json_encode([]));
72+
$curlClient(\json_encode([]));
7373
$this->assertTrue(true);
7474
} finally {
7575
$server->stop();
@@ -137,7 +137,7 @@ function (RequestInterface $request, ResponseInterface &$response) use ($t) {
137137
]);
138138

139139
ob_start();
140-
$curlClient(json_encode([]));
140+
$curlClient(\json_encode([]));
141141
} finally {
142142
$server->stop();
143143
$output = ob_get_clean();

tests/Unit/Reporters/JsonV2SerializerTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function testErrorTagIsNotClobberedBySpanError()
6262
$this->assertEquals($expectedSerialization, $serializedSpans);
6363
}
6464

65-
public function testJSONTagsAreSerializedCorrectly()
65+
public function testStringValuesAreEscapedAndSerializedCorrectly()
6666
{
6767
$jsonValue = '{"name":"Kurt"}';
6868
$mutilineValue = <<<EOD
@@ -75,16 +75,16 @@ public function testJSONTagsAreSerializedCorrectly()
7575
$span = Span::createFromContext($context, $localEndpoint);
7676
$startTime = 1594044779509687;
7777
$span->start($startTime);
78-
$span->setName('Test');
78+
$span->setName('My\Command');
7979
$span->tag('test_key_1', $jsonValue);
8080
$span->tag('test_key_2', $mutilineValue);
8181
$span->finish($startTime + 1000);
8282
$serializer = new JsonV2Serializer();
8383
$serializedSpans = $serializer->serialize([$span]);
8484

8585
$expectedSerialization = '[{'
86-
. '"id":"186f11b67460db4e","traceId":"186f11b67460db4e","timestamp":1594044779509687,"name":"test",'
87-
. '"duration":1000,"localEndpoint":{"serviceName":"service1"},'
86+
. '"id":"186f11b67460db4e","traceId":"186f11b67460db4e","timestamp":1594044779509687,'
87+
. '"name":"my\\\\command","duration":1000,"localEndpoint":{"serviceName":"service1"},'
8888
. '"tags":{"test_key_1":"{\"name\":\"Kurt\"}","test_key_2":"foo\nbar"}'
8989
. '}]';
9090

0 commit comments

Comments
 (0)