How to send custom metrics and events to datadog with laravel in docker-compose environment

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.

docker-compose settings

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

Laravel container settings

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

datadog-agent container settings

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 package to laravel

Add the following two packages.

"datadog/php-datadogstatsd" "chaseconey/laravel-datadog-helper"

composer require datadog/php-datadogstatsd
composer require chaseconey/laravel-datadog-helper

Operation check

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

Try sending metrics from cli

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

Send Metrics Event from Tinker

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
>>> 

If you can send it successfully, it will be displayed in datadog as below.

スクリーンショット 2020-11-01 13.29.25.png スクリーンショット 2020-11-01 13.28.53.png

You can send various other logs, but if you make it so far, I think that you can manage by reading the document.

Reference URL (extremely unfriendly official document)

https://docs.datadoghq.com/ja/developers/dogstatsd/?tab=agent

https://docs.datadoghq.com/ja/developers/events/dogstatsd/?tab=php

Recommended Posts

How to send custom metrics and events to datadog with laravel in docker-compose environment
How to monitor nginx with docker-compose with datadog
How to use args :, environment :, env_file: and .env files with docker-compose command
What happened in "Java 8 to Java 11" and how to build an environment
Create Laravel environment with Docker (docker-compose)
How to install the language used in Ubuntu and how to build the environment
How to encrypt and decrypt with RSA public key in Java
How to use custom helpers in rails
How to deploy to AWS using NUXTJS official S3 and CloudFront? With docker-compose
How to reflect seeds.rb in production environment
Build and manage RStudio environment with Docker-compose
How to execute with commands of normal development language in Docker development environment
[Node.js express Docker] How to define Docker environment variables and load them with node.js
How to use environment variables in RubyOnRails
How to write and explain Dockerfile, docker-compose
How to build Rails 6 environment with Docker
Analyzing and visualizing csv logs with Excel Elastic Stack (docker-compose) --How to deal with data duplication errors in Elasticsearch
How to install Titan2D (v4.2.0) in virtual environment
How to build API with GraphQL and Rails
[Rails] How to build an environment with Docker
[Rails] How to use PostgreSQL in Vagrant environment
How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
How to deal with Selenium :: WebDriver :: Error :: UnknownError that occurs in Dokcer environment etc.
[Rough explanation] How to separate the operation of the production environment and the development environment with Rails
How to embed JavaScript variables in HTML with Thymeleaf
How to implement UICollectionView in Swift with code only
How to call functions in bulk with Java reflection
How to create a Java environment in just 3 seconds
How to send value in HTML without screen transition
How to switch Tomcat context.xml with WTP in Eclipse
How to use Z3 library in Scala with Eclipse
I started MySQL 5.7 with docker-compose and tried to connect
[Docker environment] How to deal with ActiveSupport :: MessageEncryptor :: InvalidMessage
[How to insert a video in haml with Rails]
How to delete untagged images in bulk with Docker
How to serialize and deserialize LocalDateTime type with GSON
How to use JDD library in Scala with Eclipse
How to convert A to a and a to A using AND and OR in Java
How to build Java development environment with VS Code
How to make Laravel faster with Docker for Mac
How to handle TSV files and CSV files in Ruby
How to query Array in jsonb with Rails + postgres
How to use RealSense with ubuntu 20.04 and ROS Noetic
How to boot by environment with Spring Boot of Maven
How to install Gradle and Kotlin with SDKMAN (Mac)
Common problems with WSL and how to deal with them
How to launch Swagger UI and Swagger Editor in Docker
How to develop in a container with --privileged and / sbin / init passed in VSCode Remote Containers
How to encode and calculate bitmaps of active user IDs with different dates in MaxCompute
How to manage the difference in each environment with yml without increasing the number of RAILS_ENV
How to change Java version and execute in an environment where Java cannot be installed freely
How to write docker-compose
How to create docker-compose
How to set environment variables when using Payjp with Rails
How to specify character code and line feed code in JAXB
How to separate words in names in classes, methods, and variables
exited with code 1 error resolution with docker-compose up in rails environment
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Express ~
How to transform ARKit and SceneKit shapes with Metal shader
How to get values in real time with TextWatcher (Android)
[Rails] How to define macros in Rspec and standardize processing