APIToolkit FastAPI SDK is a middleware that can be used to monitor incoming HTTP requests, errors and outgoing requests. It is provides additional functionalities on top of the open telemetry instrumentation which creates a custom span for each request capturing details about the request including request, response bodies, headers, status code, duration, etc.
Kindly run the command below to install the apitoolkit pyramid sdks and necessary opentelemetry packages:
pip install apitoolkit-fastapi opentelemetry-distro opentelemetry-exporter-otlp
opentelemetry-bootstrap -a installSetting up open telemetry allows you to send traces, metrics and logs to the APIToolkit platform. To setup open telemetry, you need to configure the following environment variables:
OTEL_EXPORTER_OTLP_ENDPOINT="http://otelcol.apitoolkit.io:4317"
OTEL_SERVICE_NAME="my-service" # Specifies the name of the service.
OTEL_RESOURCE_ATTRIBUTES="at-project-key={ENTER_YOUR_API_KEY_HERE}" # Adds your API KEY to the resource.
OTEL_EXPORTER_OTLP_PROTOCOL="grpc" #Specifies the protocol to use for the OpenTelemetry exporter.Then run the command below to start your server with opentelemetry instrumented:
opentelemetry-instrument python3 myapp.pyNext, initialize APItoolkit in your application's entry point (e.g., main.py), like so:
from fastapi import FastAPI
from apitoolkit_fastapi import APIToolkit
app = FastAPI()
# Initialize APItoolkit
apitoolkit = APIToolkit(service_name="my-service")
app.middleware('http')(apitoolkit.middleware)
@app.get("/")
def read_root():
return {"Hello": "World"}Note
The {ENTER_YOUR_API_KEY_HERE} demo string should be replaced with the API key generated from the APItoolkit dashboard.
Important
To learn more configuration options (redacting fields, error reporting, outgoing requests, etc.), please read this SDK documentation.
To contribute to the development of this SDK or request help from the community and our team, kindly do any of the following:
- Read our Contributors Guide.
- Join our community Discord Server.
- Create a new issue in this repository.
This repository is published under the MIT license.