José Agretti
11/03/2025, 3:07 PMMarvin
11/03/2025, 3:07 PMMarvin
11/03/2025, 3:15 PMimport os
from datetime import datetime, timedelta, timezone
from prefect.client.orchestration import SyncPrefectClient
from prefect.server.schemas.filters import (
FlowRunFilter,
FlowRunFilterState,
FlowRunFilterStateType,
FlowRunFilterStartTime,
)
api_url = os.environ.get("PREFECT_API_URL", "<http://localhost:4200/api>")
now = datetime.now(timezone.utc)
since = now - timedelta(days=1)
client = SyncPrefectClient(api=api_url)
# 1) Currently running
running_count = client.count_flow_runs(
flow_runs=FlowRunFilter(
state=FlowRunFilterState(
type=FlowRunFilterStateType(any_=["RUNNING"])
)
)
)
# 2) Failed in last 24h
failed_24h = client.count_flow_runs(
flow_runs=FlowRunFilter(
state=FlowRunFilterState(
type=FlowRunFilterStateType(any_=["FAILED"])
),
start_time=FlowRunFilterStartTime(after_=since)
)
)
# 3) Average runtime (seconds) for completed runs in last 24h
# Use history to get summed run_time and counts
bucket_seconds = (now - since).total_seconds() # single bucket for 24h
history = client.flow_run_history(
history_start=since,
history_end=now,
history_interval=bucket_seconds,
flow_runs=FlowRunFilter(
state=FlowRunFilterState(
type=FlowRunFilterStateType(any_=["COMPLETED"])
)
),
)
total_runtime = sum(h.sum_estimated_run_time for h in history)
completed_count = sum(h.count_runs for h in history)
avg_runtime_sec = (total_runtime / completed_count) if completed_count else 0
print("Running now:", running_count)
print("Failed last 24h:", failed_24h)
print("Avg runtime (completed, last 24h):", avg_runtime_sec)
Notes:
- The filter classes mirror the REST payload. See: count_flow_runs and flow_run_history
- History responses include count_runs and sum_estimated_run_time so you can compute averages.
3) Alerts and SLAs (Automations)
- If you want proactive alerts (e.g., “failure rate > X%” or “runs taking > Y seconds”), set up Automations with Metric Triggers.
- Docs: Automations and Metric Triggers
4) External dashboards (Grafana/Datadog/etc.)
- Prefect does not expose a built-in Prometheus metrics endpoint today. Common pattern: run a lightweight exporter that queries the Prefect API (like the script above) on an interval and pushes those values to your telemetry stack (Prometheus Pushgateway, Datadog API, etc.). I’m happy to share a sample exporter if you tell me your target system.
5) Health checks
- Server health/readiness endpoints: /api/health and /api/readinessMarvin
11/03/2025, 3:15 PM