Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/DDTrace/Encoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace DDTrace;

use Psr\Http\Message\StreamInterface;

interface Encoder
{
/**
* @param Span[][]|array $traces
* @return string|StreamInterface
*/
public function encodeTraces(array $traces);

/**
* @return string
*/
public function getContentType();
}
75 changes: 75 additions & 0 deletions src/DDTrace/Encoders/Json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace DDTrace\Encoders;

use DDTrace\Encoder;
use DDTrace\Span;

final class Json implements Encoder
{
/**
* {@inheritdoc}
*/
public function encodeTraces(array $traces)
{
return '[' . implode(',', array_map(function ($trace) {
return '[' . implode(',', array_map(function ($span) {
return $this->encodeSpan($span);
}, $trace)) . ']';
}, $traces)) . ']';
}

/**
* {@inheritdoc}
*/
public function getContentType()
{
return 'application/json';
}

/**
* @param Span $span
* @return array
*/
private function spanToArray(Span $span)
{
$arraySpan = [
'trace_id' => $span->getTraceId(),
'span_id' => $span->getSpanId(),
'name' => $span->getOperationName(),
'resource' => $span->getResource(),
'service' => $span->getService(),
'start_micro' => 0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems a little hacky.

'error' => $span->hasError() ? 1 : 0,
];

if ($span->getType() !== null) {
$arraySpan['type'] = $span->getType();
}

if ($span->isFinished()) {
$arraySpan['duration_micro'] = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems a little hacky. Why are you doing the replace inside encodeSpan? If it is a matter of int precision even with those rare 32-bit runtime you have more then 2000second to store the duration.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was a matter of consistency. Since both start and duration are being stored as micro and then converted into nano when encoding, doing so only for start and not for duration seemed weird to me. If this bugs you too much we can have a chat and get into an agreement. WDYT?

}

if ($span->getParentId() !== null) {
$arraySpan['parent_id'] = $span->getParentId();
}

if (!empty($span->getAllTags())) {
$arraySpan['meta'] = $span->getAllTags();
}

return $arraySpan;
}

private function encodeSpan(Span $span)
{
return str_replace([
'"start_micro":0',
'"duration_micro":0',
], [
'"start":' . $span->getStartTime() . '000',
'"duration":' . $span->getDuration() . '000',
], json_encode($this->spanToArray($span)));
}
}
24 changes: 24 additions & 0 deletions src/DDTrace/Encoders/Noop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace DDTrace\Encoders;

use DDTrace\Encoder;

final class Noop implements Encoder
{
/**
* {@inheritdoc}
*/
public function encodeTraces(array $traces)
{
return '';
}

/**
* {@inheritdoc}
*/
public function getContentType()
{
return 'noop';
}
}
4 changes: 2 additions & 2 deletions src/DDTrace/Span.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ public function getAllTags()
}

/**
* Stores a Throwable object within the span meta. The error status is
* updated and the error.Error() string is included with a default meta key.
* Stores a Throwable object within the span tags. The error status is
* updated and the error.Error() string is included with a default tag key.
* If the Span has been finished, it will not be modified by this method.
*
* @param Throwable|Exception $error
Expand Down
33 changes: 33 additions & 0 deletions tests/Unit/Encoders/JsonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace DDTrace\Tests\Unit\Encoders;

use DDTrace\Encoders\Json;
use DDTrace\Span;
use DDTrace\SpanContext;
use PHPUnit_Framework_TestCase;

final class JsonTest extends PHPUnit_Framework_TestCase
{
public function testEncodeTracesSuccess()
{
$expectedPayload = <<<JSON
[[{"trace_id":"160e7072ff7bd5f1","span_id":"160e7072ff7bd5f2","name":"test_name","resource":"test_resource",
JSON
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this concatenation necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was because maximum line length per file (linters). The way to avoid this is to ident the json fields but that screw up the comparison in line 31. Remember, we can't convert the json into array/object in order to compare it because of the lack of support for int64 in PHP.

. <<<JSON
"service":"test_service","start":1518038421211969000,"error":0}]]
JSON;

$context = new SpanContext('160e7072ff7bd5f1', '160e7072ff7bd5f2');
$span = new Span(
'test_name',
$context,
'test_service',
'test_resource',
1518038421211969
);
$jsonEncoder = new Json();
$encodedTrace = $jsonEncoder->encodeTraces([[$span]]);
$this->assertEquals($expectedPayload, $encodedTrace);
}
}