Skip to content

Commit

Permalink
single line json encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleferguson committed Oct 21, 2021
1 parent 2a8e3d5 commit e90ca56
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,12 @@ private function registerDefaultLogSendHandler()
$this->registerLogSendHandler(function (array $logs) {
$url = getenv('TAIL_LOGS_ENDPOINT') ?: self::LOGS_ENDPOINT;

$encodedLogs = array_map(function ($log) {
return json_encode($log);
}, $logs);

$this->guzzle->post($url, [
'json' => $logs,
'body' => implode("\n", $encodedLogs),
'headers' => [
'Authorization' => 'Bearer ' . $this->token,
],
Expand All @@ -119,7 +123,7 @@ private function registerDefaultApmSendHandler()
$url = getenv('TAIL_APM_ENDPOINT') ?: self::APM_ENDPOINT;

$this->guzzle->post($url, [
'json' => $transaction,
'body' => json_encode($transaction),
'headers' => [
'Authorization' => 'Bearer ' . $this->token,
],
Expand Down
7 changes: 5 additions & 2 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ public function test_send_logs()
['message' => "message \n2"],
];

$expectEncoded = "{\"message\":\"message 1\"}\n{\"message\":\"message \\n2\"}";

$this->guzzle->shouldReceive('post')->with(
Client::LOGS_ENDPOINT,
[
'json' => $logs,
'body' => $expectEncoded,
'headers' => [
'Authorization' => 'Bearer secret_token',
],
Expand Down Expand Up @@ -71,11 +73,12 @@ public function test_stop_log_sending_chain_with_handler_that_returns_false()
public function test_send_apm()
{
$transaction = ['some' => 'data'];
$expectEncoded = "{\"some\":\"data\"}";

$this->guzzle->shouldReceive('post')->with(
Client::APM_ENDPOINT,
[
'json' => $transaction,
'body' => $expectEncoded,
'headers' => [
'Authorization' => 'Bearer secret_token',
],
Expand Down

0 comments on commit e90ca56

Please sign in to comment.