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
40 changes: 40 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,95 @@ jobs:
"php-5.6":
docker:
- image: circleci/php:5.6
- image: datadog/docker-dd-agent
environment:
- DD_APM_ENABLED=true
- DD_BIND_HOST=0.0.0.0
- DD_API_KEY=invalid_key_but_this_is_fine
working_directory: ~/datadog
steps:
- checkout
- run:
name: Install php-bcmath
command: sudo docker-php-ext-install bcmath
- run: composer install -n
- run:
name: Run lint
command: composer lint
- run:
command: dockerize -wait tcp://127.0.0.1:8126 -timeout 1m
- run:
name: Run tests
command: composer test

"php-7.0":
docker:
- image: circleci/php:7.0
- image: datadog/docker-dd-agent
environment:
- DD_APM_ENABLED=true
- DD_BIND_HOST=0.0.0.0
- DD_API_KEY=invalid_key_but_this_is_fine
working_directory: ~/datadog
steps:
- checkout
- run:
name: Install php-bcmath
command: sudo docker-php-ext-install bcmath
Copy link
Contributor

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.

Copy link
Contributor Author

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?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok.

- run: composer install
- run:
name: Run lint
command: composer lint
- run:
command: dockerize -wait tcp://127.0.0.1:8126 -timeout 1m
- run:
name: Run tests
command: composer test

"php-7.1":
docker:
- image: circleci/php:7.1
- image: datadog/docker-dd-agent
environment:
- DD_APM_ENABLED=true
- DD_BIND_HOST=0.0.0.0
- DD_API_KEY=invalid_key_but_this_is_fine
working_directory: ~/datadog
steps:
- checkout
- run:
name: Install php-bcmath
command: sudo docker-php-ext-install bcmath
- run: composer install
- run:
name: Run lint
command: composer lint
- run:
command: dockerize -wait tcp://127.0.0.1:8126 -timeout 1m
- run:
name: Run tests
command: composer test

"php-7.2":
docker:
- image: circleci/php:7.2
- image: datadog/docker-dd-agent
environment:
- DD_APM_ENABLED=true
- DD_BIND_HOST=0.0.0.0
- DD_API_KEY=invalid_key_but_this_is_fine
working_directory: ~/datadog
steps:
- checkout
- run:
name: Install php-bcmath
command: sudo docker-php-ext-install bcmath
- run: composer install
- run:
name: Run lint
command: composer lint
- run:
command: dockerize -wait tcp://127.0.0.1:8126 -timeout 1m
- run:
name: Run tests
command: composer test
Expand Down
19 changes: 17 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
],
"minimum-stability": "dev",
"require": {
"php": "^5.6||^7.0",
"ext-bcmath": "*",
"opentracing/opentracing": "1.0.0-beta2",
"symfony/polyfill": "~1.7.0",
"guzzlehttp/psr7": "^1.4@dev"
"guzzlehttp/psr7": "^1.4@dev",
"psr/log": "^1.0@dev"
},
"require-dev": {
"phpunit/phpunit": "~5.7.19",
Expand All @@ -41,7 +44,19 @@
}
},
"scripts": {
"test": "./vendor/bin/phpunit ./tests",
"test": [
"@test-unit",
"@test-integration"
],
"test-unit": "./vendor/bin/phpunit ./tests/Unit",
"test-integration":[
"echo \"Integration tests require the agent up and running. Use composer run-agent.\"",
"./vendor/bin/phpunit ./tests/Integration"
],
"run-agent": [
"docker run -p 8126:8126 -e \"DD_APM_ENABLED=true\" -e \"DD_BIND_HOST=0.0.0.0\" -e \"DD_API_KEY=invalid_key_but_this_is_fine\" --rm datadog/docker-dd-agent",
"while ! echo exit | nc localhost 8126; do sleep 1; done"
],
"lint": "./vendor/bin/phpcs --standard=ZEND --standard=PSR2 --ignore=*/vendor/* ./",
"fix-lint": "./vendor/bin/phpcbf --standard=ZEND --standard=PSR2 --ignore=*/vendor/* ./"
}
Expand Down
58 changes: 42 additions & 16 deletions src/DDTrace/Encoders/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];

Expand All @@ -48,11 +85,11 @@ private function spanToArray(Span $span)
}

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

Choose a reason for hiding this comment

The 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())) {
Expand All @@ -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)));
}
}
5 changes: 5 additions & 0 deletions src/DDTrace/Span.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ public function getAllTags()
return $this->tags;
}

public function setResource($resource)
{
$this->resource = (string) $resource;
}

/**
* 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.
Expand Down
95 changes: 95 additions & 0 deletions src/DDTrace/Transport/Http.php
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;
}
}
}
Loading