Skip to content

Instantly share code, notes, and snippets.

@svrnm
Created January 8, 2025 08:57
Show Gist options
  • Save svrnm/d9d6bef850839aa629c2907ff10be9a2 to your computer and use it in GitHub Desktop.
Save svrnm/d9d6bef850839aa629c2907ff10be9a2 to your computer and use it in GitHub Desktop.
Docker compose override for OpenTelemetry zero-code instrumentation

Docker compose override for OpenTelemetry zero-code instrumentation

If you want to add OpenTelemetry to the services in an existing docker-compose.yml you can use a docker-compose.override.yml to do so without touching your existing file.

Imagine you have three services listed called frontend, processing and backend in your compose file. Two of them are written in Java, one of them in Node.JS. Create the following docker-compose.override.yml file alongside your docker-compose.yml to inject the Java agent and the @opentelemetry/auto-instrumentations-node package into them:

services:
  frontend:
    environment:
      - JAVA_TOOL_OPTIONS=-javaagent:/mnt/opentelemetry-javaagent.jar
      - OTEL_RESOURCE_ATTRIBUTES=service.name=frontend
      - OTEL_EXPORTER=otlp
      - OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger:4318
    volumes:
      - ./opentelemetry-javaagent.jar:/mnt/opentelemetry-javaagent.jar
  processing:
    environment:
      - JAVA_TOOL_OPTIONS=-javaagent:/mnt/opentelemetry-javaagent.jar
      - OTEL_RESOURCE_ATTRIBUTES=service.name=processing
      - OTEL_EXPORTER=otlp
      - OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger:4318
    volumes:
      - ./opentelemetry-javaagent.jar:/mnt/opentelemetry-javaagent.jar
  backend:
    environment:
      - NODE_PATH=/mnt/node_modules
      - NODE_OPTIONS=-r "@opentelemetry/auto-instrumentations-node/register"
      - OTEL_RESOURCE_ATTRIBUTES=service.name=backend
      - OTEL_EXPORTER=otlp
      - OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger:4318
    volumes:
      - ./node_modules:/mnt/node_modules
# If you want to add jaeger for visualization as well, uncomment the following
#  jaeger:
#    image: jaegertracing/all-in-one
#    ports:
#      - "16686:16686"
#      - "4317:4317"
#      - "4318:4318"

Next, download the Java agent and the Node.JS module using the following commands:

curl -L -O https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/latest/download/opentelemetry-javaagent.jar
echo {} > package.json
npm install @opentelemetry/auto-instrumentations-node

In your folder you should now have the following:

  • file docker-compose.yml
  • file docker-compose.override.yml
  • file opentelemetry-javaagent.jar
  • directory node_modules

With this setup ready, run your application:

docker compose up

Your services are now emitting logs, metrics and traces!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment