It is a method to send custom metrics and events from laravel to datadog via dogstatsd with datadog-agent, nginx and laravel installed in the multi-container environment built with docker-compose.
The official Datadog documentation is ** as usual unusable **, so I managed to build the settings myself.
First, docker-compose settings.
(Reference) Click here for how to install and configure datadog-agent using docker-compose https://qiita.com/reopa_sharkun/items/cd4dbfdaa60bcfbefd74
The point is the setting of environment and links. Allow links to communicate between Laravel container and datadog-agent container The connection destination is set by the environment variables DD_AGENT_HOST and DD_DOGSTATSD_PORT. (Datadogstatsd described later reads and uses this environment variable) Set DD_ENTITY_ID as you like.
app:
build:
context: ./docker/php
dockerfile: Dockerfile
volumes:
- php-fpm-socket:/var/run/php-fpm
- ../backend:/work/backend:delegated
- ./docker/php/bash/.bash_history:/root/.bash_history
- ./docker/php/bash/psysh:/root/.config/psysh
environment:
- DD_AGENT_HOST=dd-agent
- DD_DOGSTATSD_PORT=8125
- DD_ENTITY_ID=Laravel
links:
- dd-agent:dd-agent
The important thing here is DD_DOGSTATSD_NON_LOCAL_TRAFFIC = true. In addition, it should be noted Note that it will not work if DD_DOGSTATSD_NON_LOCAL_TRAFFIC = "true" is set!
Because dogstatsd listens on UDP port 8215 Add "8125: 8125 / udp".
Other settings are settings to monitor nginx.
dd-agent:
container_name: dd-agent
image: datadog/agent:7
environment:
- DD_API_KEY=Write your API key here
- DD_APM_ENABLED=true
- DD_LOGS_ENABLED=true
- DD_LOGS_CONFIG_CONTAINER_COLLECT_ALL=true
- DD_AC_EXCLUDE="name:dd-agent"
- DD_DOGSTATSD_NON_LOCAL_TRAFFIC=true
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /proc/:/host/proc/:ro
- /sys/fs/cgroup:/host/sys/fs/cgroup:ro
ports:
- "8125:8125/udp"
- "8126:8126/tcp"
It is OK if you set docker-compose as above. Let's restart the container.
Add the following two packages.
"datadog/php-datadogstatsd" "chaseconey/laravel-datadog-helper"
composer require datadog/php-datadogstatsd
composer require chaseconey/laravel-datadog-helper
Confirm that the container is started correctly with docker ps. dd-agent is listening on 0.0.0.0:8125-> 8125 / udp.
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0fa807546066 docker-laravel_app "docker-php-entrypoi…" 35 minutes ago Up 35 minutes 9000/tcp docker-laravel_app_1
8b970a3b89c3 datadog/agent:7 "/init" 35 minutes ago Up 35 minutes (healthy) 0.0.0.0:8125->8125/udp, 0.0.0.0:8126->8126/tcp dd-agent
I will try to send from the host running Docker. Nothing is displayed whether it succeeds or fails: disappointed_relieved:
echo "deploys.test.myservice:1|c" | nc -w 1 -u 127.0.0.1 8125
Again, I only see null whether it succeeds or fails: disappointed_relieved: Only the first time, a lot of notices will appear, but there is no problem.
Psy Shell v0.10.4 (PHP 7.4.11 — cli) by Justin Hileman
>>> Datadog::event('An test error occurred.',array( 'text' => 'Test Info message','alert_type' => 'error'));
=> null
>>> Datadog::increment('my.sweet.metrics');
=> null
>>>
You can send various other logs, but if you make it so far, I think that you can manage by reading the document.
https://docs.datadoghq.com/ja/developers/dogstatsd/?tab=agent
https://docs.datadoghq.com/ja/developers/events/dogstatsd/?tab=php