Skip to content

Commit

Permalink
Merge pull request #555 from DataDog/levi/issue-502
Browse files Browse the repository at this point in the history
Fix #502: Curl headers are overwritten when using `curl_copy_handle` function
  • Loading branch information
morrisonlevi committed Sep 4, 2019
2 parents 3bdd47e + 6e651a2 commit 156c4e5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/DDTrace/Integrations/Curl/CurlIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ public static function load()
return dd_trace_forward_call();
});

dd_trace('curl_copy_handle', function ($ch1) use ($globalConfig) {
$ch2 = dd_trace_forward_call();
/* The store needs to copy the CURLOPT_HTTPHEADER value to the new handle;
* see https://github.com/DataDog/dd-trace-php/issues/502 */
if (\is_resource($ch2) && $globalConfig->isDistributedTracingEnabled()) {
$httpHeaders = ArrayKVStore::getForResource($ch1, Format::CURL_HTTP_HEADERS, []);
if (\is_array($httpHeaders)) {
ArrayKVStore::putForResource($ch2, Format::CURL_HTTP_HEADERS, $httpHeaders);
}
}
return $ch2;
});

dd_trace('curl_close', function ($ch) use ($globalConfig) {
ArrayKVStore::deleteResource($ch);
return dd_trace_forward_call();
Expand Down
30 changes: 30 additions & 0 deletions tests/Integrations/Curl/CurlIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,36 @@ public function testDistributedTracingIsPropagated()
$this->assertSame('preserved_value', $found['headers']['Honored']);
}

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

$ch1 = \curl_init(self::URL . '/headers');
\curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
\curl_setopt($ch1, CURLOPT_HTTPHEADER, [
'honored: preserved_value',
]);
$ch2 = \curl_copy_handle($ch1);
\curl_close($ch1);
$found = \json_decode(\curl_exec($ch2), 1);

$span->finish();
});

// trace is: custom
$this->assertSame($traces[0][0]['span_id'], (int) $found['headers']['X-Datadog-Trace-Id']);
// parent is: curl_exec
$this->assertSame($traces[0][1]['span_id'], (int) $found['headers']['X-Datadog-Parent-Id']);
$this->assertSame('1', $found['headers']['X-Datadog-Sampling-Priority']);
// existing headers are honored
$this->assertSame('preserved_value', $found['headers']['Honored']);
}

public function testDistributedTracingIsNotPropagatedIfDisabled()
{
$found = [];
Expand Down

0 comments on commit 156c4e5

Please sign in to comment.