Datadog & PHP

Install Datadog agent


DD_AGENT_MAJOR_VERSION=7 DD_API_KEY=replace_with_your_key bash -c "$(curl -L https://raw.githubusercontent.com/DataDog/datadog-agent/master/cmd/agent/install_script.sh)"

Install PHP agent

wget https://github.com/DataDog/dd-trace-php/releases/download/0.46.0/datadog-php-tracer_0.46.0_amd64.deb
dpkg -i datadog-php-tracer_0.46.0_amd64.deb
Optimize _generated.php in /opt/datadog-php/dd-trace-sources/bridge 
(see the other post)

 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']);
            }
            $span->setTag(Tag::HTTP_STATUS_CODE, 200);
           #================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']);
            }
           if (isset($_SERVER['HTTP_HOST'])) {
                $span->setTag("http.http_host", $_SERVER['HTTP_HOST']);
            }
           $span->setTag("http.domain_name", @$_SERVER['HTTP_HOST']);

Restart your webserver (apachectl restart)

Leave a Reply

Your email address will not be published. Required fields are marked *