Integrations API¶
driftwatch.integrations.fastapi.DriftMiddleware
¶
DriftMiddleware(app: ASGIApp, monitor: Monitor, feature_extractor: Callable[[dict[str, Any]], dict[str, Any]] | None = None, prediction_extractor: Callable[[dict[str, Any]], dict[str, Any]] | None = None, check_interval: int = 100, min_samples: int = 50, buffer_size: int = 10000, enabled: bool = True)
Bases: BaseHTTPMiddleware
FastAPI middleware for automatic drift monitoring.
Collects input features from requests and runs drift detection on a configurable schedule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
ASGIApp
|
The ASGI application |
required |
monitor
|
Monitor
|
DriftWatch Monitor instance with reference data |
required |
feature_extractor
|
Callable[[dict[str, Any]], dict[str, Any]] | None
|
Function to extract features from request body. Defaults to returning the entire request body as features. |
None
|
check_interval
|
int
|
Number of requests between drift checks. Set to 0 to disable automatic checks. |
100
|
min_samples
|
int
|
Minimum samples required before running drift check. |
50
|
enabled
|
bool
|
Whether drift collection is enabled. |
True
|
Example
Source code in src/driftwatch/integrations/fastapi.py
dispatch
async
¶
Process request and collect features for drift monitoring.
Source code in src/driftwatch/integrations/fastapi.py
driftwatch.integrations.alerting.SlackAlerter
¶
SlackAlerter(webhook_url: str, throttle_minutes: int = 60, mention_user: str | None = None, channel_override: str | None = None)
Send drift alerts to Slack via webhook.
Formats drift reports as Slack Block Kit messages with feature-level details and supports alert throttling to avoid spam.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
webhook_url
|
str
|
Slack webhook URL (https://hooks.slack.com/...) |
required |
throttle_minutes
|
int
|
Minimum minutes between alerts (default: 60) |
60
|
mention_user
|
str | None
|
Optional Slack user ID to mention (@U123ABC) |
None
|
channel_override
|
str | None
|
Optional channel to post to (overrides webhook default) |
None
|
Example
Source code in src/driftwatch/integrations/alerting.py
send
¶
send(report: DriftReport, force: bool = False, custom_message: str | None = None) -> bool
Send drift report to Slack.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
report
|
DriftReport
|
DriftReport to send |
required |
force
|
bool
|
Skip throttling check |
False
|
custom_message
|
str | None
|
Optional custom message prefix |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if alert was sent, False if throttled |
Raises:
| Type | Description |
|---|---|
HTTPError
|
If webhook request fails |
Source code in src/driftwatch/integrations/alerting.py
get_next_alert_time
¶
Get the earliest time the next alert can be sent.