Skip to content

Commit

Permalink
[priority-sampling] Encode and send to the proper endpoint prioirty s…
Browse files Browse the repository at this point in the history
…ampling info
  • Loading branch information
labbati committed Dec 5, 2018
1 parent a907ce5 commit c5bbde1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/DDTrace/Encoders/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace DDTrace\Encoders;

use DDTrace\Encoder;
use DDTrace\Sampling\PrioritySampling;
use DDTrace\Span;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
Expand Down Expand Up @@ -107,6 +108,10 @@ private function spanToArray(Span $span)
$arraySpan['meta'] = $span->getAllTags();
}

if ($prioritySampling = $span->getPrioritySampling() !== PrioritySampling::UNKNOWN) {
$arraySpan['metrics']['_sampling_priority_v1'] = $prioritySampling;
}

return $arraySpan;
}
}
5 changes: 0 additions & 5 deletions src/DDTrace/Integrations/Guzzle/V5/GuzzleIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,5 @@ public static function injectDistributedTracingHeaders($request, $span)
$tracer = GlobalTracer::get();
$tracer->inject($context, Formats\HTTP_HEADERS, $headers);
$request->setHeaders($headers);

$context = $span->getContext();
$tracer = GlobalTracer::get();
$tracer->inject($context, Formats\HTTP_HEADERS, $headers);
$request->addHeaders(Formats\$headers);
}
}
2 changes: 2 additions & 0 deletions src/DDTrace/Tracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ private function record(Span $span)
}

/**
* Handles the priority sampling for the current span.
*
* @param Span $span
*/
private function handlePrioritySampling(Span $span)
Expand Down
43 changes: 39 additions & 4 deletions src/DDTrace/Transport/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace DDTrace\Transport;

use DDTrace\Configuration;
use DDTrace\Encoder;
use DDTrace\Sampling\PrioritySampling;
use DDTrace\Span;
use DDTrace\Transport;
use DDTrace\Version;
use Psr\Log\LoggerInterface;
Expand All @@ -19,6 +22,7 @@ final class Http implements Transport
const DEFAULT_AGENT_HOST = 'localhost';
const DEFAULT_TRACE_AGENT_PORT = '8126';
const DEFAULT_TRACE_AGENT_PATH = '/v0.3/traces';
const PRIORITY_SAMPLING_TRACE_AGENT_PATH = '/v0.4/traces';

/**
* @var Encoder
Expand Down Expand Up @@ -62,19 +66,24 @@ private function configure($config)
{
$host = getenv(self::AGENT_HOST_ENV) ?: self::DEFAULT_AGENT_HOST;
$port = getenv(self::TRACE_AGENT_PORT_ENV) ?: self::DEFAULT_TRACE_AGENT_PORT;
$path = self::DEFAULT_TRACE_AGENT_PATH;
$endpoint = "http://${host}:${port}${path}";
$defaultEndpoint = "http://${host}:${port}" . self::DEFAULT_TRACE_AGENT_PATH;
$prioritySamlpingEndpoint = "http://${host}:${port}" . self::PRIORITY_SAMPLING_TRACE_AGENT_PATH;

$this->config = array_merge([
'endpoint' => $endpoint,
'endpoint' => $defaultEndpoint,
'endpoint_priority_sampling' => $prioritySamlpingEndpoint,
], $config);
}

public function send(array $traces)
{
$tracesPayload = $this->encoder->encodeTraces($traces);

$this->sendRequest($this->config['endpoint'], $this->headers, $tracesPayload);
$endpoint = $this->isPrioritySamplingEndpoint($traces)
? $this->config['endpoint_priority_sampling']
: $this->config['endpoint'];

$this->sendRequest($endpoint, $this->headers, $tracesPayload);
}

public function setHeader($key, $value)
Expand Down Expand Up @@ -128,4 +137,30 @@ private function sendRequest($url, array $headers, $body)
return;
}
}

/**
* Returns whether or not we should send these traces to the priority sampling aware trace agent endpoint.
* This approach could be optimized in the future if we refactor how traces are organized in parent/child relations
* but this would be out of scope at the moment.
*
* @param array $traces
* @return bool
*/
private function isPrioritySamplingEndpoint(array $traces)
{
if (!Configuration::instance()->isPrioritySamplingEnabled()) {
return false;
}

foreach ($traces as $traceSpans) {
/** @var Span $span */
foreach ($traceSpans as $span) {
if ($span->getPrioritySampling() !== PrioritySampling::UNKNOWN) {
return true;
}
}
}

return false;
}
}

0 comments on commit c5bbde1

Please sign in to comment.