# Python APM Agent for Flask doesn't report metrics

**URL:** <https://discuss.elastic.co/t/python-apm-agent-for-flask-doesnt-report-metrics/223083>\
**Category:** APM\
**Tags:** python\
**Created:** [March 11, 2020, 9:26am UTC](https://discuss.elastic.co/t/python-apm-agent-for-flask-doesnt-report-metrics/223083 "2020-03-11T09:26:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![francois.tarlier](https://avatars.discourse-cdn.com/v4/letter/f/b5ac83/32.png) [@francois.tarlier](https://discuss.elastic.co/u/francois.tarlier)\
**Post date:** [March 11, 2020, 9:26am UTC](https://discuss.elastic.co/t/python-apm-agent-for-flask-doesnt-report-metrics/223083/1 "2020-03-11T09:26:42Z")

</div>

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:**

```auto
    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)

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

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

    @app.route('/api/<entity>')
    def api(entity):
        """API."""
        limit = int(request.args.get("limit", 0))
        entities = get_entity(entity, limit=limit)
        nb = len(entities)
        app.logger.info("Found {nb} {entity} with limit to {limit}".format(
            nb=nb,
            entity=entity,
            limit=limit))
        return render_template('api.html', 
            entity=entity,
            entities=entities,
            nb=nb)

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

    if __name__ == ' __main__':
        handler = LoggingHandler(client=apm.client)
        handler.setLevel(logging.INFO)
        app.logger.addHandler(handler)
        app.config['ELASTIC_APM'] = {
            'DEBUG': DEBUG
        }
        app.run(host="127.0.0.1", port="8080", debug=DEBUG)

```

**Kibana version** :  
7.6.0

**Elasticsearch version** :  
7.6.0

**APM Server version** :  
7.6.0

**APM Agent language and version** :  
elastic-apm==5.4.3  
Flask==1.1.1

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

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

---

<div class="post-metadata">

**Author:** ![basepi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/basepi/32/53056_2.png) [@basepi](https://discuss.elastic.co/u/basepi)\
**Post date:** [March 11, 2020, 7:41pm UTC](https://discuss.elastic.co/t/python-apm-agent-for-flask-doesnt-report-metrics/223083/2 "2020-03-11T19:41:17Z")

</div>

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:

```python
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

```auto
pip install flask 'elastic-apm[flask]' psutil
python app.py
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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![francois.tarlier](https://avatars.discourse-cdn.com/v4/letter/f/b5ac83/32.png) [@francois.tarlier](https://discuss.elastic.co/u/francois.tarlier)\
**Post date:** [March 13, 2020, 9:06am UTC](https://discuss.elastic.co/t/python-apm-agent-for-flask-doesnt-report-metrics/223083/3 "2020-03-13T09:06:34Z")

</div>

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`](https://pypi.org/project/psutil/) for this metric set.`

It is also needed for OSX.

Thanks a lot for your quick feedback.

F.

---

<div class="post-metadata">

**Author:** ![basepi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/basepi/32/53056_2.png) [@basepi](https://discuss.elastic.co/u/basepi)\
**Post date:** [March 13, 2020, 2:45pm UTC](https://discuss.elastic.co/t/python-apm-agent-for-flask-doesnt-report-metrics/223083/4 "2020-03-13T14:45:54Z")

</div>

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`: [https://github.com/elastic/apm-agent-python/pull/756](https://github.com/elastic/apm-agent-python/pull/756)

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!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 3, 2020, 10:45am UTC](https://discuss.elastic.co/t/python-apm-agent-for-flask-doesnt-report-metrics/223083/5 "2020-04-03T10:45:57Z")

</div>

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