Python APM Agent for Flask doesn't report metrics

I'm trying to do a simple example in Flask by following the documentation and online tutorials to see how the flask integration works.
I managed to have the span and request working, but I have no metrics reported in Kibana.
also the app.logger doesn't seems to report the logs into Kibana

When I start the Flask server I get the following message :
No handlers could be found for logger "elasticapm.metrics"

Here is my code:

    import logging

    from flask import Flask
    from flask import request
    from flask import render_template

    import elasticapm
    from elasticapm.contrib.flask import ElasticAPM
    from elasticapm.handlers.logging import LoggingHandler

    DEBUG = True

    app = Flask(__name__)
    apm = ElasticAPM(app, service_name="flask-app", logging=logging.INFO)

    def bar():
            1 / 0
        except ZeroDivisionError:
            app.logger.error('I cannot math', exc_info=True)

    @app.route('/login', methods=['GET', 'POST'])
    def login():
        if request.method == 'POST':
            return "post method"
            return "get method"

    def api(entity):
        limit = int(request.args.get("limit", 0))
        entities = get_entity(entity, limit=limit)
        nb = len(entities)"Found {nb} {entity} with limit to {limit}".format(
        return render_template('api.html', 

    def get_entity(entity, limit=0):
        """Call to in house DB."""
        entities = db.find(entity,
        return entities

    if __name__ == '__main__':
        handler = LoggingHandler(client=apm.client)
        app.config['ELASTIC_APM'] = {
            'DEBUG': DEBUG
        }"", port="8080", debug=DEBUG)

Kibana version:

Elasticsearch version:

APM Server version:

APM Agent language and version:

Original install method (e.g. download page, yum, deb, from source, etc.) and version:

Fresh install or upgraded from other version?
local install using docker-compose

Welcome to the forum, @francois.tarlier!

I found one issue in your code. You need to do the app.config piece before you set up the apm object, or APM is set up without those config values:

app = Flask(__name__)
app.config["ELASTIC_APM"] = {"DEBUG": DEBUG}
apm = ElasticAPM(app, service_name="flask-app", logging=logging.INFO)

Additionally, did you install psutil? You'll need that to get any of the system metrics.

I took your code, with the one change I showed above, did

pip install flask 'elastic-apm[flask]' psutil
curl localhost:8080/login

And saw both transactions and metrics in my local kibana instance.

My guess is that elasticapm.metrics is trying to send the following warning to the logs:

Could not register elasticapm.metrics.sets.cpu.CPUMetricSet metricset: psutil not found. Install it to get system and process metrics

But it's failing because something is odd in the logging. (Probably related to running it in docker -- I didn't tweak the logging at all but I am running it in a local virtualenv.)

Keep me posted if that code change and installing psutil fix the issue for you!

Thank you, I was missing the psutil like you mentioned. I would recommand to make it more abvious in the documentation or maybe as requirement in elasticapm module.

It is specified in the middle of the documentation in metrics as :
if you do **not** use Linux, you need to install [ psutil ]( for this metric set.

It is also needed for OSX.

Thanks a lot for your quick feedback.


Thanks for the feedback! In fact, as a result of my investigation of your issue, I added a troubleshooting doc, which includes the note about psutil:

Interestingly, my coworker pointed out that psutil is only required on non-linux systems. (In your case, docker is probably the culprit.)

Have a great day!

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.