Aiohttp Server support
Serverless ObservabilityStackAPM Agent Python
Getting Elastic APM set up for your Aiohttp Server project is easy, and there are various ways you can tweak it to fit to your needs.
Install the Elastic APM agent using pip:
$ pip install elastic-apmor addelastic-apm to your project’srequirements.txt file.
To set up the agent, you need to initialize it with appropriate settings.
The settings are configured either via environment variables, the application’s settings, or as initialization arguments.
You can find a list of all available settings in theConfiguration page.
To initialize the agent for your application using environment variables:
from aiohttp import webfrom elasticapm.contrib.aiohttp import ElasticAPMapp = web.Application()apm = ElasticAPM(app)To configure the agent usingELASTIC_APM in your application’s settings:
from aiohttp import webfrom elasticapm.contrib.aiohttp import ElasticAPMapp = web.Application()app['ELASTIC_APM'] = { 'SERVICE_NAME': '<SERVICE-NAME>', 'SECRET_TOKEN': '<SECRET-TOKEN>',}apm = ElasticAPM(app)Once you have configured the agent, it will automatically track transactions and capture uncaught exceptions within aiohttp.
Capture an arbitrary exception by callingcapture_exception:
try: 1 / 0except ZeroDivisionError: apm.client.capture_exception()Log a generic message withcapture_message:
apm.client.capture_message('hello, world!')If you’ve followed the instructions above, the agent has already installed our middleware. This will measure response times, as well as detailed performance data for all supported technologies.
due to the fact thatasyncio drivers are usually separate from their synchronous counterparts, specific instrumentation is needed for all drivers. The support for asynchronous drivers is currently quite limited.
You can use theTRANSACTIONS_IGNORE_PATTERNS configuration option to ignore specific routes. The list given should be a list of regular expressions which are matched against the transaction name:
app['ELASTIC_APM'] = { # ... 'TRANSACTIONS_IGNORE_PATTERNS': ['^OPTIONS ', '/api/'] # ...}This would ignore any requests using theOPTIONS method and any requests containing/api/.
A list of supportedaiohttp andPython versions can be found on ourSupported Technologies page.
Elastic APM only supportsasyncio when using Python 3.7+