/datadog_java_statsd_example

Sample code to send metrics via statsd from Java in Docker to Datadog

Primary LanguageJavaApache License 2.0Apache-2.0

Datadog Java StatsD Example

Sample code to send metrics via statsd from Java in Docker to Datadog

The base setup is based on DataDog/java-dogstatsd-client

Assumptions and Note:

  1. The Datadog agent is running in a container on the host.

  2. There is a valid Datadog account available to send metrics

  3. This was tested on Ubuntu Ubuntu 18.04.4 LTS

Files:

  1. Dockerfile - to create the container where the Java code will execute.

  2. pom.xml - the pom file that includes the java-dogstatsd-client dependency from DataDog/java-dogstatsd-client

  3. /src/main/java/dogstatsdclient/DogStatsdClient.java - the sample Java code to send StatsD metrics to Datadog

Configuration:

  1. Execute docker ps and grab the container ID of the Datadog agent

  2. Execute docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_id> to get the IP of the container running the Datadog agent.

  3. In the Dockerfile change <IP_OF_DD_AGENT_CONTAINER> to the ip in step 2.

  4. Get your Datadog API key from the Datadog UI

Execution:

  1. Execute mvn compile && mvn package && docker build -t my-java-app . to compile and package the Java application and build the Docker image.

  2. After replacing <DATADOG_API_KEY> with that from step 4 above execute docker run -it --rm -v /var/run/docker.sock:/var/run/docker.sock:ro -v /proc/:/host/proc/:ro -v /sys/fs/cgroup/:/host/sys/fs/cgroup:ro -e DD_API_KEY="<DATADOG_API_KEY>" -e DD_DOGSTATSD_NON_LOCAL_TRAFFIC=true --name my-running-app my-java-app to run the application.

  3. Go here and type example_metric to view the metrics in Datadog or here to see them visualized.