Why python agent is not capturing handled exception error events

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?

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 and Django. 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.:

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.

1 Like

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?

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

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