-
Notifications
You must be signed in to change notification settings - Fork 166
Adds http transport #9
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f26964b
Adds HTTP transport.
jcchavezs 65ba6df
Adds support for bcmath.
jcchavezs c4da786
Split test tasks into unit and integration.
jcchavezs 1902dd4
Adds logger for HTTP transport.
jcchavezs 5d42770
Fixes tests for Http transport.
jcchavezs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,19 +27,56 @@ public function getContentType() | |
| return 'application/json'; | ||
| } | ||
|
|
||
| /** | ||
| * @param Span $span | ||
| * @return string | ||
| */ | ||
| private function encodeSpan(Span $span) | ||
| { | ||
| return str_replace([ | ||
| '"start_micro":"-"', | ||
| '"duration_micro":"-"', | ||
| '"trace_id_hex":"-"', | ||
| '"span_id_hex":"-"', | ||
| '"parent_id_hex":"-"', | ||
| ], [ | ||
| '"start":' . $span->getStartTime() . '000', | ||
| '"duration":' . $span->getDuration() . '000', | ||
| '"trace_id":' . $this->hex2dec($span->getTraceId()), | ||
| '"span_id":' . $this->hex2dec($span->getSpanId()), | ||
| '"parent_id":' . $this->hex2dec($span->getParentId()), | ||
| ], json_encode($this->spanToArray($span))); | ||
| } | ||
|
|
||
| /** | ||
| * @param string $hex | ||
| * @return string | ||
| */ | ||
| private function hex2dec($hex) | ||
| { | ||
| $decimal = 0; | ||
| $len = strlen($hex); | ||
|
|
||
| for ($i = 1; $i <= $len; $i++) { | ||
| $decimal = bcadd($decimal, bcmul((string) hexdec($hex[$i - 1]), bcpow('16', (string) ($len - $i)))); | ||
| } | ||
|
|
||
| return $decimal; | ||
| } | ||
|
|
||
| /** | ||
| * @param Span $span | ||
| * @return array | ||
| */ | ||
| private function spanToArray(Span $span) | ||
| { | ||
| $arraySpan = [ | ||
| 'trace_id' => $span->getTraceId(), | ||
| 'span_id' => $span->getSpanId(), | ||
| 'trace_id_hex' => '-', | ||
| 'span_id_hex' => '-', | ||
| 'name' => $span->getOperationName(), | ||
| 'resource' => $span->getResource(), | ||
| 'service' => $span->getService(), | ||
| 'start_micro' => 0, | ||
| 'start_micro' => '-', | ||
| 'error' => $span->hasError() ? 1 : 0, | ||
| ]; | ||
|
|
||
|
|
@@ -48,11 +85,11 @@ private function spanToArray(Span $span) | |
| } | ||
|
|
||
| if ($span->isFinished()) { | ||
| $arraySpan['duration_micro'] = 0; | ||
| $arraySpan['duration_micro'] = '-'; | ||
|
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. I prefer this way! It is clearer that a trick exists during the payload construction. |
||
| } | ||
|
|
||
| if ($span->getParentId() !== null) { | ||
| $arraySpan['parent_id'] = $span->getParentId(); | ||
| $arraySpan['parent_id_hex'] = '-'; | ||
| } | ||
|
|
||
| if (!empty($span->getAllTags())) { | ||
|
|
@@ -61,15 +98,4 @@ private function spanToArray(Span $span) | |
|
|
||
| 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))); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| <?php | ||
|
|
||
| namespace DDTrace\Transport; | ||
|
|
||
| use DDTrace\Encoder; | ||
| use DDTrace\Transport; | ||
| use Psr\Log\LoggerInterface; | ||
|
|
||
| final class Http implements Transport | ||
| { | ||
| const DEFAULT_ENDPOINT = 'http://localhost:8126/v0.3/traces'; | ||
|
|
||
| /** | ||
| * @var Encoder | ||
| */ | ||
| private $encoder; | ||
|
|
||
| /** | ||
| * @var array | ||
| */ | ||
| private $headers = []; | ||
|
|
||
| /** | ||
| * @var array | ||
| */ | ||
| private $config; | ||
|
|
||
| /** | ||
| * @var LoggerInterface | ||
| */ | ||
| private $logger; | ||
|
|
||
| public function __construct(Encoder $encoder, LoggerInterface $logger, array $config = []) | ||
| { | ||
| $this->encoder = $encoder; | ||
| $this->logger = $logger; | ||
| $this->config = array_merge([ | ||
| 'endpoint' => self::DEFAULT_ENDPOINT, | ||
| ], $config); | ||
| } | ||
|
|
||
| public function send(array $traces) | ||
| { | ||
| $tracesPayload = $this->encoder->encodeTraces($traces); | ||
|
|
||
| $this->sendRequest($this->config['endpoint'], $this->headers, $tracesPayload); | ||
| } | ||
|
|
||
| public function setHeader($key, $value) | ||
| { | ||
| $this->headers[(string) $key] = (string) $value; | ||
| } | ||
|
|
||
| public function getConfig() | ||
| { | ||
| return $this->config; | ||
| } | ||
|
|
||
| private function sendRequest($url, array $headers, $body) | ||
| { | ||
| $handle = curl_init($url); | ||
| curl_setopt($handle, CURLOPT_POST, 1); | ||
| curl_setopt($handle, CURLOPT_POSTFIELDS, $body); | ||
| curl_setopt($handle, CURLOPT_HTTPHEADER, array_merge($headers, [ | ||
| 'Content-Type: ' . $this->encoder->getContentType(), | ||
| 'Content-Length: ' . strlen($body), | ||
| ])); | ||
|
|
||
| if (curl_exec($handle) !== true) { | ||
| $this->logger->debug(sprintf( | ||
| 'Reporting of spans failed: %s, error code %s', | ||
| curl_error($handle), | ||
| curl_errno($handle) | ||
| )); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| $statusCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); | ||
| curl_close($handle); | ||
|
|
||
| if ($statusCode === 415) { | ||
| $this->logger->debug('Reporting of spans failed, upgrade your client library.'); | ||
| return; | ||
| } | ||
|
|
||
| if ($statusCode !== 200) { | ||
| $this->logger->debug( | ||
| sprintf('Reporting of spans failed, status code %d', $statusCode) | ||
| ); | ||
|
|
||
| return; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This should be part of the README in a "requirements" section.
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.
Totally what about postponing this for then it is time to write the README?
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.
Ok.