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
7 changes: 7 additions & 0 deletions src/DDTrace/Data/SpanContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ abstract class SpanContext implements SpanContextInterface
*/
public $propagatedPrioritySampling;

/**
* The origin of the distributed trace.
*
* @var string|null
*/
public $origin;

/**
* @var SpanContextInterface
*/
Expand Down
7 changes: 7 additions & 0 deletions src/DDTrace/Encoders/SpanEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DDTrace\GlobalTracer;
use DDTrace\Log\LoggingTrait;
use DDTrace\Sampling\PrioritySampling;
use DDTrace\Tag;

final class SpanEncoder
{
Expand Down Expand Up @@ -43,6 +44,12 @@ public static function encode(DataSpan $span)
}

$tags = $span->tags;
if (
!empty($span->context->origin)
&& $span->context->isHostRoot()
) {
$tags[Tag::ORIGIN] = $span->context->origin;
}
if (!empty($tags)) {
$arraySpan['meta'] = $tags;
}
Expand Down
1 change: 1 addition & 0 deletions src/DDTrace/Propagator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface Propagator
const DEFAULT_TRACE_ID_HEADER = 'x-datadog-trace-id';
const DEFAULT_PARENT_ID_HEADER = 'x-datadog-parent-id';
const DEFAULT_SAMPLING_PRIORITY_HEADER = 'x-datadog-sampling-priority';
const DEFAULT_ORIGIN_HEADER = 'x-datadog-origin';

/**
* Inject takes the SpanContext and injects it into the carrier using
Expand Down
25 changes: 8 additions & 17 deletions src/DDTrace/Propagators/CurlHeadersMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,11 @@ public function inject(SpanContext $spanContext, &$carrier)
{
foreach ($carrier as $index => $value) {
if (
substr($value, 0, strlen(Propagator::DEFAULT_TRACE_ID_HEADER))
=== Propagator::DEFAULT_TRACE_ID_HEADER
) {
unset($carrier[$index]);
} elseif (
substr($value, 0, strlen(Propagator::DEFAULT_PARENT_ID_HEADER))
=== Propagator::DEFAULT_PARENT_ID_HEADER
) {
unset($carrier[$index]);
} elseif (
substr($value, 0, strlen(Propagator::DEFAULT_BAGGAGE_HEADER_PREFIX))
=== Propagator::DEFAULT_BAGGAGE_HEADER_PREFIX
) {
unset($carrier[$index]);
} elseif (
substr($value, 0, strlen(Propagator::DEFAULT_SAMPLING_PRIORITY_HEADER))
=== Propagator::DEFAULT_SAMPLING_PRIORITY_HEADER
strpos($value, Propagator::DEFAULT_TRACE_ID_HEADER) === 0
|| strpos($value, Propagator::DEFAULT_PARENT_ID_HEADER) === 0
|| strpos($value, Propagator::DEFAULT_BAGGAGE_HEADER_PREFIX) === 0
|| strpos($value, Propagator::DEFAULT_SAMPLING_PRIORITY_HEADER) === 0
|| strpos($value, Propagator::DEFAULT_ORIGIN_HEADER) === 0
) {
unset($carrier[$index]);
}
Expand All @@ -66,6 +54,9 @@ public function inject(SpanContext $spanContext, &$carrier)
if (PrioritySampling::UNKNOWN !== $prioritySampling) {
$carrier[] = Propagator::DEFAULT_SAMPLING_PRIORITY_HEADER . ': ' . $prioritySampling;
}
if (!empty($spanContext->origin)) {
$carrier[] = Propagator::DEFAULT_ORIGIN_HEADER . ': ' . $spanContext->origin;
}
}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/DDTrace/Propagators/TextMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public function inject(SpanContextInterface $spanContext, &$carrier)
if (PrioritySampling::UNKNOWN !== $prioritySampling) {
$carrier[Propagator::DEFAULT_SAMPLING_PRIORITY_HEADER] = $prioritySampling;
}
if (!empty($spanContext->origin)) {
$carrier[Propagator::DEFAULT_ORIGIN_HEADER] = $spanContext->origin;
}
}

/**
Expand Down Expand Up @@ -67,6 +70,7 @@ public function extract($carrier)

$spanContext = new SpanContext($traceId, $spanId, null, $baggageItems, true);
$this->extractPrioritySampling($spanContext, $carrier);
$this->extractOrigin($spanContext, $carrier);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

extracting the origin is only valid if we were able to successfully extract a trace id.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for clarifying! This line only gets run in this case as on line 67-69 there is a null-check on the trace ID. :)

return $spanContext;
}

Expand Down Expand Up @@ -102,4 +106,20 @@ private function extractPrioritySampling(SpanContextInterface $spanContext, $car
$spanContext->setPropagatedPrioritySampling(PrioritySampling::parse($rawValue));
}
}

/**
* Extract the origin from the carrier.
*
* @param SpanContextInterface $spanContext
* @param array $carrier
*/
private function extractOrigin(SpanContextInterface $spanContext, $carrier)
{
if (
property_exists($spanContext, 'origin')
&& isset($carrier[Propagator::DEFAULT_ORIGIN_HEADER])
) {
$spanContext->origin = $this->extractStringOrFirstArrayElement($carrier[Propagator::DEFAULT_ORIGIN_HEADER]);
}
}
}
6 changes: 6 additions & 0 deletions src/DDTrace/SpanContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public static function createAsChild(SpanContextInterface $parentContext)
);
$instance->parentContext = $parentContext;
$instance->setPropagatedPrioritySampling($parentContext->getPropagatedPrioritySampling());
if (
property_exists($instance, 'origin')
&& !empty($parentContext->origin)
) {
$instance->origin = $parentContext->origin;
}
return $instance;
}

Expand Down
1 change: 1 addition & 0 deletions src/DDTrace/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Tag
const BYTES_OUT = 'net.out.bytes';
const ANALYTICS_KEY = '_dd1.sr.eausr';
const HOSTNAME = '_dd.hostname';
const ORIGIN = '_dd.origin';

// Elasticsearch
const ELASTICSEARCH_BODY = 'elasticsearch.body';
Expand Down
20 changes: 20 additions & 0 deletions tests/Integrations/Curl/CurlIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,26 @@ public function testDistributedTracingIsPropagated()
$this->assertSame('preserved_value', $found['headers']['Honored']);
}

public function testOriginIsPropagatedAndSetsRootSpanTag()
{
$found = [];
$traces = $this->isolateTracer(function () use (&$found) {
/** @var Tracer $tracer */
$tracer = GlobalTracer::get();
$span = $tracer->startActiveSpan('custom')->getSpan();
$span->getContext()->origin = 'foo_origin';

$ch = curl_init(self::URL . '/headers');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$found = json_decode(curl_exec($ch), 1);

$span->finish();
});

$this->assertSame('foo_origin', $found['headers']['X-Datadog-Origin']);
$this->assertSame('foo_origin', $traces[0][0]['meta']['_dd.origin']);
}

public function testDistributedTracingIsPropagatedOnCopiedHandle()
{
$found = [];
Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/Propagators/CurlHeadersMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,16 @@ public function testExistingDistributedTracingHeadersAreReplaced()
'ot-baggage-' . self::BAGGAGE_ITEM_KEY . ': ' . self::BAGGAGE_ITEM_VALUE,
], array_values($carrier));
}

public function testOriginIsPropagated()
{
$rootContext = SpanContext::createAsRoot();
$rootContext->origin = 'foo_origin';
$context = SpanContext::createAsChild($rootContext);

$carrier = [];
(new CurlHeadersMap($this->tracer))->inject($context, $carrier);

$this->assertContains('x-datadog-origin: foo_origin', $carrier);
}
}
26 changes: 26 additions & 0 deletions tests/Unit/Propagators/TextMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,30 @@ public function testExtractPrioritySamplingWhenNotProvided()
$context = $textMapPropagator->extract($carrier);
$this->assertSame(null, $context->getPropagatedPrioritySampling());
}

public function testOriginIsPropagated()
{
$rootContext = SpanContext::createAsRoot();
$rootContext->origin = 'foo_origin';
$context = SpanContext::createAsChild($rootContext);

$carrier = [];
$textMapPropagator = new TextMap($this->tracer);
$textMapPropagator->inject($context, $carrier);

$this->assertSame('foo_origin', $carrier['x-datadog-origin']);
}

public function testOriginIsExtracted()
{
$carrier = [
'x-datadog-trace-id' => self::TRACE_ID,
'x-datadog-parent-id' => self::SPAN_ID,
'x-datadog-origin' => 'foo_origin',
];
$textMapPropagator = new TextMap($this->tracer);
$context = $textMapPropagator->extract($carrier);

$this->assertSame('foo_origin', $context->origin);
}
}