# Why python agent is not capturing handled exception error events

**URL:** <https://discuss.elastic.co/t/why-python-agent-is-not-capturing-handled-exception-error-events/277727>\
**Category:** APM\
**Tags:** python\
**Created:** [July 4, 2021, 9:42am UTC](https://discuss.elastic.co/t/why-python-agent-is-not-capturing-handled-exception-error-events/277727 "2021-07-04T09:42:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bimanarajesh](https://avatars.discourse-cdn.com/v4/letter/b/eada6e/32.png) [@bimanarajesh](https://discuss.elastic.co/u/bimanarajesh)\
**Post date:** [July 4, 2021, 9:42am UTC](https://discuss.elastic.co/t/why-python-agent-is-not-capturing-handled-exception-error-events/277727/1 "2021-07-04T09:42:17Z")

</div>

**Elasticsearch version** :  
6.8.4  
**APM Server version** :  
7.6.0  
**APM Agent language and version** :  
Java - 1.16.0

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

python agent is not capturing the handled exception error events. below is script to connect elasticsearch and performing search query on non existing index.

try:  
es = Elasticsearch(['ip:9200'], http\_auth=('user','pass'), timeout=30)  
result = es.search(index="index123\_read")  
log.info(str(result))  
except:  
log.error("got error")

for this we are getting only elasticsearch span without any exception details.  
We would like to capture the python handled exceptions error events

NotFoundError(404, u'index\_not\_found\_exception', u'no such index', index123\_read, index\_or\_alias)

is there anything python agent configuration to capture handled exception errors? or does it not support?

---

<div class="post-metadata">

**Author:** ![beniwohli](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/beniwohli/32/21256_2.png) [@beniwohli](https://discuss.elastic.co/u/beniwohli)\
**Post date:** [July 5, 2021, 9:03am UTC](https://discuss.elastic.co/t/why-python-agent-is-not-capturing-handled-exception-error-events/277727/2 "2021-07-05T09:03:38Z")

</div>

Hi @bimanarajesh!

If you want log messages to be captured bei the Elastic APM agent, you need to set up our logging handler. How to do this depends a bit on wether you use a web framework. See the docs for [Flask](https://www.elastic.co/guide/en/apm/agent/python/current/flask-support.html#flask-logging) and [Django](https://www.elastic.co/guide/en/apm/agent/python/current/django-support.html#django-logging). If you don't use one of these frameworks, it's probably easiest to adapt the last code snippet in the Flask section to your needs.

Another option is to explicitly capture the exception without going through logging, e.g.:

```auto
import elasticapm
apm = elasticapm.get_client()

try:
    es = Elasticsearch(['ip:9200'], http_auth=('user','pass'), timeout=30)
    result = es.search(index="index123_read")
    log.info(str(result))
except:
    apm.capture_exception(handled=True)

```

`handled=True` indicates that you caught the exception.

---

<div class="post-metadata">

**Author:** ![bimanarajesh](https://avatars.discourse-cdn.com/v4/letter/b/eada6e/32.png) [@bimanarajesh](https://discuss.elastic.co/u/bimanarajesh)\
**Post date:** [July 5, 2021, 9:16am UTC](https://discuss.elastic.co/t/why-python-agent-is-not-capturing-handled-exception-error-events/277727/3 "2021-07-05T09:16:00Z")

</div>

Thanks @beniwohli

I am using Django framework, without enabling logging or custom capture. don't agent capture the error doc when we handle the exception?

---

<div class="post-metadata">

**Author:** ![beniwohli](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/beniwohli/32/21256_2.png) [@beniwohli](https://discuss.elastic.co/u/beniwohli)\
**Post date:** [July 5, 2021, 2:37pm UTC](https://discuss.elastic.co/t/why-python-agent-is-not-capturing-handled-exception-error-events/277727/4 "2021-07-05T14:37:37Z")

</div>

@bimanarajesh the agent only automatically captures unhandled exceptions (exceptions that result in a HTTP 500 error status).

---

<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:** [July 26, 2021, 10:37am UTC](https://discuss.elastic.co/t/why-python-agent-is-not-capturing-handled-exception-error-events/277727/5 "2021-07-26T10:37:50Z")

</div>

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