Skip to content

Commit e83efd2

Browse files
committed
test(tracer): add http streams tests
Signed-off-by: Alexandre Rulleau <alexandre.rulleau@datadoghq.com>
1 parent 8588b3b commit e83efd2

5 files changed

Lines changed: 243 additions & 1 deletion

File tree

ext/ddtrace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1431,7 +1431,7 @@ static php_stream *ddtrace_stream_opener(
14311431

14321432
// Open internal span
14331433
ddtrace_span_data *span = NULL;
1434-
if (ddtrace_integrations[DDTRACE_INTEGRATION_HTTPSTREAM].is_enabled()) {
1434+
if (ddtrace_integrations[DDTRACE_INTEGRATION_HTTPSTREAM].is_enabled() && get_DD_TRACE_ENABLED()) {
14351435
span = ddtrace_alloc_execute_data_span(-2, EG(current_execute_data));
14361436
if (span) {
14371437
ddtrace_set_global_span_properties(span);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
function dt_decode_headers_from_httpbin($response)
4+
{
5+
$data = json_decode($response, true);
6+
if (!$data || !isset($data['headers'])) {
7+
echo 'Invalid response:' . PHP_EOL;
8+
var_dump($response);
9+
return [];
10+
}
11+
$headers = [];
12+
foreach ($data['headers'] as $key => $value) {
13+
$headers[strtolower($key)] = mb_convert_encoding($value, 'ISO-8859-1', 'UTF-8');
14+
}
15+
return $headers;
16+
}
17+
18+
function dt_dump_headers_from_httpbin(array $headers, array $whitelist)
19+
{
20+
foreach ($headers as $key => $value) {
21+
if (!in_array($key, $whitelist, true)) {
22+
continue;
23+
}
24+
echo $key . ': ' . $value . PHP_EOL;
25+
}
26+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
--TEST--
2+
Distributed tracing span is generated for file_get_contents()
3+
--SKIPIF--
4+
<?php
5+
if (!getenv('HTTPBIN_HOSTNAME')) {
6+
die('skip: HTTPBIN_HOSTNAME env var required');
7+
}
8+
?>
9+
--ENV--
10+
DD_TRACE_AUTO_FLUSH_ENABLED=0
11+
DD_TRACE_LOG_LEVEL=info,startup=off
12+
DD_TRACE_GENERATE_ROOT_SPAN=0
13+
DD_TRACE_HTTPSTREAM_ENABLED=1
14+
--FILE--
15+
<?php
16+
17+
$port = getenv('HTTPBIN_PORT') ?: '80';
18+
$host = getenv('HTTPBIN_HOSTNAME');
19+
$url = 'http://' . $host . ':' . $port . '/headers';
20+
21+
DDTrace\trace_function('file_get_contents', function (\DDTrace\SpanData $span) {
22+
$span->name = 'httpstream';
23+
});
24+
25+
$response = file_get_contents($url);
26+
27+
include 'distributed_tracing.inc';
28+
$headers = dt_decode_headers_from_httpbin($response);
29+
dt_dump_headers_from_httpbin($headers, [
30+
'x-datadog-trace-id',
31+
'x-datadog-parent-id',
32+
'x-datadog-sampling-priority',
33+
'x-datadog-tags',
34+
'traceparent',
35+
'tracestate',
36+
]);
37+
38+
echo "\n=== Span ===\n";
39+
$spans = dd_trace_serialize_closed_spans();
40+
$span = $spans[1];
41+
42+
echo "name: " . $span['name'] . "\n";
43+
echo "resource: " . $span['resource'] . "\n";
44+
echo "type: " . $span['type'] . "\n";
45+
echo "service: " . $span['service'] . "\n";
46+
echo "meta:\n";
47+
ksort($span['meta']);
48+
foreach ($span['meta'] as $k => $v) {
49+
echo " $k: $v\n";
50+
}
51+
52+
echo 'Done.' . PHP_EOL;
53+
?>
54+
--EXPECTF--
55+
[ddtrace] [warning] Error loading deferred integration DDTrace\Integrations\Filesystem\FilesystemIntegration: Class not loaded and not autoloadable
56+
traceparent: %s
57+
tracestate: %s
58+
x-datadog-parent-id: %d
59+
x-datadog-sampling-priority: 1
60+
x-datadog-tags: %s
61+
x-datadog-trace-id: %d
62+
63+
=== Span ===
64+
name: file_get_contents
65+
resource: %s
66+
type: cli
67+
service: %s
68+
meta:
69+
component: php.stream
70+
http.url: http://%s:%d/headers
71+
network.destination.name: %s
72+
span.kind: client
73+
Done.
74+
[ddtrace] [info] No finished traces to be sent to the agent
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
--TEST--
2+
Distributed tracing headers propagate via file_get_contents() with stream contexts
3+
--SKIPIF--
4+
<?php
5+
if (!getenv('HTTPBIN_HOSTNAME')) {
6+
die('skip: HTTPBIN_HOSTNAME env var required');
7+
}
8+
?>
9+
--ENV--
10+
DD_TRACE_AUTO_FLUSH_ENABLED=0
11+
DD_TRACE_LOG_LEVEL=info,startup=off
12+
DD_TRACE_HTTPSTREAM_ENABLED=1
13+
--FILE--
14+
<?php
15+
16+
$port = getenv('HTTPBIN_PORT') ?: '80';
17+
$host = getenv('HTTPBIN_HOSTNAME');
18+
$url = 'http://' . $host . ':' . $port . '/headers';
19+
20+
DDTrace\trace_function('file_get_contents', function (\DDTrace\SpanData $span) {
21+
$span->name = 'httpstream';
22+
});
23+
24+
function fetch_with_headers(array $headers)
25+
{
26+
$ctx = stream_context_create([
27+
'http' => [
28+
'method' => 'GET',
29+
'header' => $headers,
30+
],
31+
]);
32+
return file_get_contents($GLOBALS['url'], false, $ctx);
33+
}
34+
35+
$responses = [];
36+
37+
$responses[] = fetch_with_headers([
38+
'x-foo: one',
39+
'x-bar: alpha',
40+
]);
41+
42+
$responses[] = fetch_with_headers([
43+
'x-foo: two',
44+
'x-bar: beta',
45+
]);
46+
47+
include 'distributed_tracing.inc';
48+
foreach ($responses as $key => $response) {
49+
echo 'Response #' . $key . PHP_EOL;
50+
$headers = dt_decode_headers_from_httpbin($response);
51+
dt_dump_headers_from_httpbin($headers, [
52+
'x-datadog-trace-id',
53+
'x-datadog-parent-id',
54+
'x-datadog-sampling-priority',
55+
'x-datadog-tags',
56+
'traceparent',
57+
'tracestate',
58+
'x-foo',
59+
'x-bar',
60+
]);
61+
echo PHP_EOL;
62+
}
63+
64+
echo 'Done.' . PHP_EOL;
65+
?>
66+
--EXPECTF--
67+
[ddtrace] [warning] Error loading deferred integration DDTrace\Integrations\Filesystem\FilesystemIntegration: Class not loaded and not autoloadable
68+
Response #0
69+
traceparent: %s
70+
tracestate: %s
71+
x-bar: alpha
72+
x-datadog-parent-id: %d
73+
x-datadog-sampling-priority: 1
74+
x-datadog-tags: %s
75+
x-datadog-trace-id: %d
76+
x-foo: one
77+
78+
Response #1
79+
traceparent: %s
80+
tracestate: %s
81+
x-bar: beta
82+
x-datadog-parent-id: %d
83+
x-datadog-sampling-priority: 1
84+
x-datadog-tags: %s
85+
x-datadog-trace-id: %d
86+
x-foo: two
87+
88+
Done.
89+
[ddtrace] [info] Flushing trace of size 5 to send-queue for %s
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
--TEST--
2+
Distributed tracing span is generated for file_get_contents()
3+
--SKIPIF--
4+
<?php
5+
if (!getenv('HTTPBIN_HOSTNAME')) {
6+
die('skip: HTTPBIN_HOSTNAME env var required');
7+
}
8+
?>
9+
--ENV--
10+
DD_TRACE_AUTO_FLUSH_ENABLED=0
11+
DD_TRACE_LOG_LEVEL=info,startup=off
12+
DD_TRACE_GENERATE_ROOT_SPAN=0
13+
DD_TRACE_HTTPSTREAM_ENABLED=0
14+
--FILE--
15+
<?php
16+
17+
$port = getenv('HTTPBIN_PORT') ?: '80';
18+
$host = getenv('HTTPBIN_HOSTNAME');
19+
$url = 'http://' . $host . ':' . $port . '/headers';
20+
21+
DDTrace\trace_function('file_get_contents', function (\DDTrace\SpanData $span) {
22+
$span->name = 'httpstream';
23+
});
24+
25+
$response = file_get_contents($url);
26+
27+
include 'distributed_tracing.inc';
28+
$headers = dt_decode_headers_from_httpbin($response);
29+
dt_dump_headers_from_httpbin($headers, [
30+
'x-datadog-trace-id',
31+
'x-datadog-parent-id',
32+
'x-datadog-sampling-priority',
33+
'x-datadog-tags',
34+
'traceparent',
35+
'tracestate',
36+
]);
37+
38+
$spans = dd_trace_serialize_closed_spans();
39+
var_dump(count($spans) === 1);
40+
41+
echo 'Done.' . PHP_EOL;
42+
?>
43+
--EXPECTF--
44+
[ddtrace] [warning] Error loading deferred integration DDTrace\Integrations\Filesystem\FilesystemIntegration: Class not loaded and not autoloadable
45+
traceparent: %s
46+
tracestate: %s
47+
x-datadog-parent-id: %d
48+
x-datadog-sampling-priority: 1
49+
x-datadog-tags: %s
50+
x-datadog-trace-id: %d
51+
bool(true)
52+
Done.
53+
[ddtrace] [info] No finished traces to be sent to the agent

0 commit comments

Comments
 (0)