Datadog PHP : Capturing data sent from datadog library

As we all know, to monitor our PHP application performance with Datadog , we need to install 2 components – datadog-agent and its PHP package.

The data-agent is a server listening on port 8126 to receive tracer/span from our application.

The PHP package is used to inject some library to our run time PHP, it does some magic thing to collect our PHP application such as response time, database connection.

Have you ever wondered what data that datadog tracing library send to its agent? Today we do a little trick , instead of using datadog-agent to receive data, we will use some tools to receive the data.

We start with the easiest one: netcat , open your terminal and run this command:

nc -l 8126

Go to your browser, after a while you will see something in your terminal

[root@apm01 signalfx]# nc -l 8126
PUT /v0.4/traces HTTP/1.1
Host: localhost:8126
Accept: */*
Datadog-Meta-Lang: php
Datadog-Meta-Lang-Version: 5.6.40
Datadog-Meta-Lang-Interpreter: apache2handler
Datadog-Meta-Tracer-Version: 0.46.0
Transfer-Encoding: chunked
Content-Type: application/msgpack
X-Datadog-Trace-Count: 2

????trace_id?C?dЧspan_id?C?dФname?web.request?resource?GET /?service?web.request?start??+#mD??error?type?web?duration?Q?meta??녑??trace_id?+΂/???span_id?+΂/???name?web.request?resource?GET /?service?web.request?start??+X?nȥerror?type?web?duration?Kth?meta??$???S?

Bingo, you see the data, but it’s hard to read. Now, let build a sample server listening on this port and collect this data. I create a simple script (datadog_server.php)


$putfp = fopen('php://input', 'r');

$putdata = '';
while($data = fread($putfp, 1024))
    $putdata .= $data;

Header("Content-Type: application/json");
echo <<<BLOCK

Data sent from Datadog library is using msgpack compression method , we will decode it and save it a file name data.php. Let run the server

php -S datadog_server.php

Go back to your browser , do some refresh, now check your data.txt , it will have something like this:

    [0] => Array
            [0] => Array
                    [trace_id] => 8331185014639043155
                    [span_id] => 8331185014639043155
                    [name] => web.request
                    [resource] => GET /index1.php
                    [service] => web.request
                    [start] => 1591636673513941000
                    [error] => 0
                    [type] => web
                    [duration] => 7551000
                    [meta] => Array
                            [] => 9300
                            [http.method] => GET
                            [http.url] => /index1.php
                            [client_ip] =>
                            [http.status_code] => 200

                    [metrics] => Array
                            [_sampling_priority_v1] => 1
                            [php.compilation.total_time_ms] => 23.352




Leave a Reply

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