How to instrument a FastAPI python application with Elastic APM
1. Create an Elasticsearch deployment
Either in ECH or in other platforms (ECK, ECE, etc).
It should have an APM server.
2. Go to the APM page
Capture the following information from the Elastic APM page from Kibana:
SERVICE_NAME=<YOUR_SERVICE_NAME>
SERVER_URL=<APM_SERVER_ENDPOINT>
SECRET_TOKEN=<APM_TOKEN>
3. Install apm python library
In your virtualenv run:
pip install elastic-apm
4. Go to your python app
Inside your app.py file add:
from elasticapm.contrib.starlette import ElasticAPM
app = FastAPI() # this should already exist
app.add_middleware(ElasticAPM) # APM
5. Set environment variables
In a CLI write:
export ELASTIC_APM_SERVICE_NAME=<YOUR_SERVICE_NAME>
export ELASTIC_APM_SERVER_URL=<APM_SERVER_ENDPOINT>
export ELASTIC_APM_SECRET_TOKEN=<APM_TOKEN>
6. Run app
python3 app.py
And send some responses.
7. Check APM
Go to Observability > Aoplications > Service Inventory and see your service.
Originally published as a gist.