In out last article here , we explained how to send Docker and application logs to ELK.

Now is the time to add some observability to our app using Elastic APM.

Configure Elastic APM

First, we add Elastic APM to our Docker Compose file from last time, using the same version as Elastic Search.

  apm-server:
    image: docker.elastic.co/apm/apm-server:7.17.24
    container_name: apm-server
    user: apm-server
    ports:
      - "8200:8200"
    volumes:
      - ./apm-server.docker.yml:/usr/share/apm-server/apm-server.yml:ro
    command: >
      --strict.perms=false -e
      -E output.elasticsearch.hosts=["http://elasticsearch:9200"]      

Next, run the containers using the docker compose up command and wait a few minutes.

If everything is ok, you can open Kibana at http://localhost:5601.

Configure Flask for Elastic APM

Import elastic-apm[flask] pip packages or add it to requirements.txt file:

elastic-apm[flask]

Then, update your Flask app to send traces and events to Elastic APM by adding the following code:

from elasticapm.contrib.flask import ElasticAPM

app = Flask(__name__)

app.config['ELASTIC_APM'] = {
        'SERVICE_NAME': '<update with app name>',
        'DEBUG': True		# needed for debug only !!!
}
apm = ElasticAPM(app)

Once the app is running, you should see the new service listed in Kibana:

services

And see some traces:

traces