This is a simple FastAPI application that is instrumented with OpenTelemetry using the OpenTelemetry Auto Instrumentation for Python. It listens on the port 8000, and generates telemetry data for each request.
A full reference of the Opentelemetry environment variables can be found here: opentelemetry/environment-variables
OTEL_SERVICE_NAME: The name of the service generating the telemetry data.
OTEL_LOGS_EXPORTER: The logs exporter to use, In this doc we're using the console exporter. If you have an opentelemetry collector set up, you can change this variable's value to console,otlp or just otlp. You will also have to set the OTEL_EXPORTER_OTLP_ENDPOINT variable to the endpoint of your opentelemetry collector.
OTEL_TRACES_EXPORTER: The traces exporter to use, In this doc we're using the console exporter. If you have an opentelemetry collector set up, you can change this variable's value to console,otlp or just otlp. You will also have to set the OTEL_EXPORTER_OTLP_ENDPOINT variable to the endpoint of your opentelemetry collector.
OTEL_METRICS_EXPORTER: The metrics exporter to use, In this doc we're using the console exporter. If you have an opentelemetry collector set up, you can change this variable's value to console,otlp or just otlp. You will also have to set the OTEL_EXPORTER_OTLP_ENDPOINT variable to the endpoint of your opentelemetry collector.
OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED: This variable enables auto-instrumentation for the logging module.
The image for this application is published on DockerHub, it can be found here: rutush10/otel-autoinstrumentation-fastapi-simple-app
To use the image with simple docker run command:
docker run
-e OTEL_SERVICE_NAME=single-app-single-collector-fastapi-app \
-e OTEL_LOGS_EXPORTER=console \
-e OTEL_TRACES_EXPORTER=console \
-e OTEL_METRICS_EXPORTER=console \
-e OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true \
-p 8000:8000 \
rutush10/otel-autoinstrumentation-fastapi-simple-app:latestTo use it within a docker-compose.yaml:
services:
app:
image: rutush10/otel-autoinstrumentation-fastapi-simple-app:latest
ports:
- "8000:8000"
environment:
- OTEL_SERVICE_NAME=otel-autoinstrumentation-fastapi-simple-app
- OTEL_LOGS_EXPORTER=console
- OTEL_TRACES_EXPORTER=console
- OTEL_METRICS_EXPORTER=console
- OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=trueTo create a k8s deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: simple-fastapi-app
labels:
app: simple-fastapi-app
spec:
replicas: 1
selector:
matchLabels:
app: simple-fastapi-app
template:
metadata:
labels:
app: simple-fastapi-app
spec:
containers:
- name: simple-fastapi-app
imagePullPolicy: Always
image: rutush10/otel-autoinstrumentation-fastapi-simple-app:latest
ports:
- containerPort: 8000
environment:
-e OTEL_SERVICE_NAME=simple-fastapi-app
-e OTEL_LOGS_EXPORTER=console
-e OTEL_TRACES_EXPORTER=console
-e OTEL_METRICS_EXPORTER=console
-e OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=trueIf you're working with this applicaiton within a k8s environment, don't forget to add a NodePort or a ClusterIP service:
apiVersion: v1
kind: Service
metadata:
name: simple-fastapi-app
labels:
app: simple-fastapi-app
spec:
type: NodePort
selector:
app: simple-fastapi-app
ports:
- protocol: TCP
port: 8000
targetPort: 8000
nodePort: 30000For a ClusterIP service, change the ports to
ports:
- protocol: TCP
port: 8000
targetPort: 8000make build-app TAG=0.0.1make run-app TAG=0.0.1make stop-appThe postman directory contains the postman collection and the environment for testing the application locally. You have to simply import the collection and environment in your postman and run the requests.
Examples are provided within the requests for reference.
The opentelemetry-instrument command line tool can be used to automatically instrument a FastAPI application. In this case we define the application without any instrumentation code:
import uvicorn
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def health_check():
return {"message": "Hello World"}
def main():
uvicorn.run(app, host="0.0.0.0", port=8000)
if __name__ == "__main__":
main()Then we run application via the opentelemetry-instrument command line tool.
opentelemetry-instrument python main.pyYou can see the run command in build/Dockerfile for this application.
If you want to use this code as a reference for your own FastAPI application, you can copy the following to your own application:
-
The
builddirectory, which contains theDockerfileand therequirements.txtfile. You can edit therequirements.txtfile to add any additional dependencies that you might need. -
The
srcdirectory, which contains the source code. You can add more files and folders as needed. -
The
Makefile, which contains the commands to build, run, publish, and stop the application. To publish the application to your own repository, edit theREPOSITORY,APP_IMAGE_NAME, and theDOCKER_REPO_NAMEvariables in theMakefile.