Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent build up of duplicate Distributed Tracing headers #120

Merged
merged 2 commits into from
Mar 11, 2021
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 agent/php_file_get_contents.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ static void nr_php_file_get_contents_add_headers_internal(zval* context,
return;
}

if (nr_stridx(Z_STRVAL_P(http_header), W3C_TRACESTATE":") != -1 &&
nr_stridx(headers, W3C_TRACESTATE":") != -1) {
/* Distributed Tracing headers already present and we are trying to
add them again, don't add duplicates. */
return;
}

/* There is a non-empty header string which must be preserved. */
{
char* all_headers = NULL;
Expand Down
8 changes: 7 additions & 1 deletion tests/include/tracing_endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@

foreach (array(DT_TRACEPARENT, DT_TRACESTATE, DT_NEWRELIC) as $header) {
if (array_key_exists(strtolower($header), $request_headers)) {
echo $header . "=found ";
/* A comma indicates there is more than one value for the header.
Flag this as having duplicate values. */
if (strpos($request_headers[$header], ",") !== false) {
echo $header . "=dup ";
} else {
echo $header . "=found ";
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
traceparent=found tracestate=found newrelic=found X-NewRelic-ID=missing X-NewRelic-Transaction=missing tracing endpoint reached
traceparent=found tracestate=found newrelic=found X-NewRelic-ID=missing X-NewRelic-Transaction=missing tracing endpoint reached
traceparent=found tracestate=found newrelic=found X-NewRelic-ID=missing X-NewRelic-Transaction=missing tracing endpoint reached
traceparent=found tracestate=found newrelic=found X-NewRelic-ID=missing X-NewRelic-Transaction=missing tracing endpoint reached
traceparent=found tracestate=found newrelic=found X-NewRelic-ID=missing X-NewRelic-Transaction=missing Customer-Header=found tracing endpoint reached
traceparent=found tracestate=found newrelic=found X-NewRelic-ID=missing X-NewRelic-Transaction=missing Customer-Header=found tracing endpoint reached
traceparent=found tracestate=found newrelic=found X-NewRelic-ID=missing X-NewRelic-Transaction=missing Customer-Header=found tracing endpoint reached
Expand All @@ -39,11 +40,11 @@
"?? start time",
"?? stop time",
[
[{"name":"External/all"}, [12, "??", "??", "??", "??", "??"]],
[{"name":"External/allOther"}, [12, "??", "??", "??", "??", "??"]],
[{"name":"External/127.0.0.1/all"}, [12, "??", "??", "??", "??", "??"]],
[{"name":"External/all"}, [13, "??", "??", "??", "??", "??"]],
[{"name":"External/allOther"}, [13, "??", "??", "??", "??", "??"]],
[{"name":"External/127.0.0.1/all"}, [13, "??", "??", "??", "??", "??"]],
[{"name":"External/127.0.0.1/all",
"scope":"OtherTransaction/php__FILE__"}, [12, "??", "??", "??", "??", "??"]],
"scope":"OtherTransaction/php__FILE__"}, [13, "??", "??", "??", "??", "??"]],
[{"name":"OtherTransaction/all"}, [ 1, "??", "??", "??", "??", "??"]],
[{"name":"OtherTransaction/php__FILE__"}, [ 1, "??", "??", "??", "??", "??"]],
[{"name":"OtherTransactionTotalTime"}, [ 1, "??", "??", "??", "??", "??"]],
Expand All @@ -53,9 +54,9 @@
[{"name":"DurationByCaller/Unknown/Unknown/Unknown/Unknown/allOther"},
[1, "??", "??", "??", "??", "??"]],
[{"name":"Supportability/TraceContext/Create/Success"},
[12, "??", "??", "??", "??", "??"]],
[13, "??", "??", "??", "??", "??"]],
[{"name":"Supportability/DistributedTrace/CreatePayload/Success"},
[12, "??", "??", "??", "??", "??"]]
[13, "??", "??", "??", "??", "??"]]
]
]
*/
Expand All @@ -74,6 +75,9 @@
$context = stream_context_create($opts);
echo file_get_contents($url, false, $context);

/* Reuse Existing Context (headers should not be duplicated). */
echo file_get_contents($url, false, $context);

/* Context Without ['http'] */
$opts = array('kitesurfing' => array('windsurfing' => 'zen'));
$context = stream_context_create($opts);
Expand Down