Skip to content

Instantly share code, notes, and snippets.

@burnsie7
Created November 14, 2018 22:19
Show Gist options
  • Save burnsie7/40899c0430eece4a0eaa91fef56f50bc to your computer and use it in GitHub Desktop.
Save burnsie7/40899c0430eece4a0eaa91fef56f50bc to your computer and use it in GitHub Desktop.
Building custom docker agent image for jmx (or any integration)

Example of building your own agent image using jmx.yaml

Step 0 - Create jmx.yaml

Example:

touch jmx.yaml

Update jmx.yaml:

ad_identifiers:
  - <your-app-name>

init_config:

instances:
  - host: '%%host%%'
    port: '%%port%%'

Step 1 - Create your own agent image

vim Dockerfile

FROM datadog/agent:latest
COPY jmx.yaml /conf.d/jmx.yaml

docker image build -t <agent-image> .

Step 2 - Launching datadog-agent container

Launch the dd-agent container using the docker run command below. Make sure to update with your api key.

docker run -d --name <agent-image> \
  -v /var/run/docker.sock:/var/run/docker.sock:ro \
  -v /proc/:/host/proc/:ro \
  -v /cgroup/:/host/sys/fs/cgroup:ro \
  -e DD_API_KEY="<your-api-key>" \
  -e DD_APM_ENABLED=true \
  -e DD_LOGS_ENABLED=true \
  -e DD_APM_NON_LOCAL_TRAFFIC=true \
  -e DD_HOSTNAME="$HOSTNAME.jmx-service-discovery" \
  -e DD_DOGSTATSD_ORIGIN_DETECTION=true \
  -e DD_DOGSTATSD_NON_LOCAL_TRAFFIC=true \
  -e SD_JMX_ENABLE=true \
  -e SD_BACKEND=docker \
  -e TAGS="tester:jmx-sd" \
  -p 8125:8125/tcp \
  -p 8126:8126/tcp \
  <agent-image>:latest

Step 3 - Download dd-java-agent.jar to the same directory as your application's Dockerfile

wget -O dd-java-agent.jar 'https://search.maven.org/classic/remote_content?g=com.datadoghq&a=dd-java-agent&v=LATEST'

Step 4 - Update your container's execution script to include agent host and port

See run.sh for example.

When launched using --link dd-agent:dd-agent the environment variables $DD_AGENT_PORT_8126_TCP_ADDR and $DD_AGENT_PORT_8126_TCP_PORT will be available.

For example, use the following in JAVA_OPTS or as options when executing your jar file:

-javaagent:/usr/local/tomcat/bin/dd-java-agent.jar /
-Ddd.agent.host=$DD_AGENT_PORT_8126_TCP_ADDR /
-Ddd.agent.port=$DD_AGENT_PORT_8126_TCP_PORT

Step 5 - Build the container image for your application including dd-java-agent.jar

See Dockerfile for example.

sudo docker image build -t .

Step 6 - Run your image using

Run your container using the --link dd-agent:dd-agent option.

sudo docker container run -d --name <your-app-name> -p 8080:8080 -p 8000:8000 -p 9999:9999 --link <agent-image>:<agent-image> <your-app-name>

Step 7 - Confirm jmx integration

docker exec -it my-dd-agent agent configcheck

Example output:

=== jmx check === Source: File Instance ID: jmx:32fba248b55a8a95 host: 172.17.0.3 port: "9999" tags:

  • docker_image:my-app:latest
  • image_name:my-app
  • short_image:my-app
  • image_tag:latest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment