Flask SDK
APIToolkit Flask SDK is a middleware that can be used to monitor HTTP requests, report errors and monitor 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, and more.
Table of Contents
Installation
Kindly run the command below to install the apitoolkit django sdks and necessary opentelemetry packages:
pip install monoscope-flask opentelemetry-distro opentelemetry-exporter-otlp
opentelemetry-bootstrap -a install
Setup Open Telemetry
Setting 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:
export OTEL_EXPORTER_OTLP_ENDPOINT="http://otelcol.apitoolkit.io:4317"
export OTEL_SERVICE_NAME="my-service" # Specifies the name of the service.
export OTEL_RESOURCE_ATTRIBUTES="at-project-key={ENTER_YOUR_API_KEY_HERE}" # Adds your API KEY to the resource.
export 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 flask run --app app
Monoscope Django Configuration
After setting up open telemetry, you can now configure the apitoolkit django middleware.
Next, initialize Monoscope in your application's entry point (e.g., main.py), like so:
from flask import Flask
from monoscope_flask import Monoscope
app = Flask(__name__)
# Initialize Monoscope
monoscope = Monoscope(
service_name = "my-service",
capture_request_body = True,
capture_response_body = True,
)
@app.before_request
def before_request():
monoscope.beforeRequest()
@app.after_request
def after_request(response):
monoscope .afterRequest(response)
return response
# END Initialize Monoscope
@app.route('/hello', methods=['GET', 'POST'])
def sample_route():
return {"Hello": "World"}
app.run(debug=True)
[!NOTE]
The
{ENTER_YOUR_API_KEY_HERE}demo string should be replaced with the API key generated from the Monoscope dashboard.
[!IMPORTANT]
To learn more configuration options (redacting fields, error reporting, outgoing requests, etc.), please read this SDK documentation.
Contributing and Help
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.
License
This repository is published under the MIT license.
Metadata
Release files for monoscope-flask 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| monoscope_flask-1.0.0.tar.gz | 4.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| monoscope_flask-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.7 kB
Release files / monoscope_flask-1.0.0.tar.gz
| Download URL | monoscope_flask-1.0.0.tar.gz |
|---|---|
| Size | 4.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
978ab2b7f9b3632c052b9659c247eb7af52ccac0607a1b781f26acf2a17cea04
|
|
BLAKE2b-256 checksum How to use checksums |
0158799ae273052fb7aff06d8f702f2da82a21288b7c1e84ab5fe30e9a1acacc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.3
|
Release files / monoscope_flask-1.0.0-py3-none-any.whl
| Download URL | monoscope_flask-1.0.0-py3-none-any.whl |
|---|---|
| Size | 4.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
eedc66c89664deea9aafa11987a00f6ec12299ee9fd3690068e046e5cb72a78f
|
|
BLAKE2b-256 checksum How to use checksums |
0a1f543ba0aa8439efca7eebb141370f330beb3b5ea39b73c57a0e5a08d05ee2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.3
|