APIToolkit pyramid 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-pyramid 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.pyAfter setting up open telemetry, you can now configure and start the apitoolkit pyramid middleware.
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.view import view_config
@view_config(
route_name='home'
)
def home(request):
return Response('Welcome!')
if __name__ == '__main__':
setting = {
"APITOOLKIT_SERVICE_NAME": "my-service",
"APITOOLKIT_CAPTURE_REQUEST_BODY": True,
"APITOOLKIT_CAPTURE_RESPONSE_BODY": True
}
with Configurator(settings=setting) as config:
# Initialize APItoolkit
config.add_tween("apitoolkit_pyramid.APIToolkit")
config.add_route('home', '/')
config.scan()
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 6543, app)
server.serve_forever()