-
Notifications
You must be signed in to change notification settings - Fork 166
Adds encoders. #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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(); | ||
| } |
| 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, | ||
| 'error' => $span->hasError() ? 1 : 0, | ||
| ]; | ||
|
|
||
| if ($span->getType() !== null) { | ||
| $arraySpan['type'] = $span->getType(); | ||
| } | ||
|
|
||
| if ($span->isFinished()) { | ||
| $arraySpan['duration_micro'] = 0; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems a little hacky. Why are you doing the replace inside
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was a matter of consistency. Since both |
||
| } | ||
|
|
||
| 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))); | ||
| } | ||
| } | ||
| 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'; | ||
| } | ||
| } |
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this concatenation necessary?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| . <<<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); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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.