Datadog – PHP – Adding client IP to your trace
When i use Datadog to collect tracer/span in our PHP , i realize that Datadog library does not collect the client IP address. I don’t know what the reason behind this, but not having the client IP is very hard to troubleshoot the issue. After a while i figured out the way to hack the code. The hack is to modify this file /opt/datadog-php/dd-trace-sources/bridge/_generated.php
This is my modification – check the line #================My Customize code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
private static function initRootSpan(Tracer $tracer) { $options = array('start_time' => Time::now()); if ('cli' === PHP_SAPI) { $operationName = isset($_SERVER['argv'][0]) ? basename($_SERVER['argv'][0]) : 'cli.command'; $span = $tracer->startRootSpan($operationName, StartSpanOptions::create($options))->getSpan(); $span->setTag(Tag::SPAN_TYPE, Type::CLI); } else { $operationName = 'web.request'; $span = $tracer->startRootSpan($operationName, StartSpanOptionsFactory::createForWebRequest($tracer, $options, Request::getHeaders()))->getSpan(); $span->setTag(Tag::SPAN_TYPE, Type::WEB_SERVLET); if (isset($_SERVER['REQUEST_METHOD'])) { $span->setTag(Tag::HTTP_METHOD, $_SERVER['REQUEST_METHOD']); } if (isset($_SERVER['REQUEST_URI'])) { $span->setTag(Tag::HTTP_URL, $_SERVER['REQUEST_URI']); } #================My Customize code if (isset($_SERVER['REMOTE_ADDR'])) { $span->setTag("http.client_ip", $_SERVER['REMOTE_ADDR']); } if (isset($_SERVER['SCRIPT_NAME'])) { $span->setTag("http.script_name", $_SERVER['SCRIPT_NAME']); } if (isset($_SERVER['HTTP_USER_AGENT'])) { $span->setTag("http.user_agent", $_SERVER['HTTP_USER_AGENT']); } if (isset($_SERVER['X-Forwarded-For'])) { $span->setTag("http.real_client_ip", $_SERVER['X-Forwarded-For']); } #================End of My Customize code $span->setTag(Tag::HTTP_STATUS_CODE, 200); |
Leave a Reply