Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

feat: provision Sentry dashboards from sentry/dashboards.json

justin2d912320365c
diff --git a/scripts/provision-sentry-dashboards.sh b/scripts/provision-sentry-dashboards.sh
new file mode 100755
index 000000000..c69b7fbb5
--- /dev/null
+++ b/scripts/provision-sentry-dashboards.sh
@@ -0,0 +1,200 @@
+#!/usr/bin/env bash
+# Provisions Sentry dashboards from sentry/dashboards.json via the Sentry REST API.
+# Idempotent: existing dashboards with the same title are updated in place.
+# Preserves any filter settings (projects, environment, period, etc.) the
+# maintainer has set in the Sentry UI by reading the current dashboard first
+# and only swapping the widgets array.
+#
+# Usage:
+# SENTRY_API_TOKEN=sntrys_... ./scripts/provision-sentry-dashboards.sh
+# SENTRY_API_TOKEN=... ./scripts/provision-sentry-dashboards.sh --dry-run
+#
+# The token needs scope: org:write.
+# Generate at: https://sentry.io/settings/account/api/auth-tokens/
+set -euo pipefail
+
+ORG="kaneo"
+REGION="de"
+API_BASE="${SENTRY_API_BASE:-https://${REGION}.sentry.io/api/0}"
+
+SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+CONFIG_DIR="$(dirname "$SCRIPT_DIR")"
+DASHBOARDS_FILE="${SENTRY_DASHBOARDS_FILE:-${CONFIG_DIR}/sentry/dashboards.json}"
+
+DRY_RUN=0
+if [ "${1:-}" = "--dry-run" ] || [ "${SENTRY_DRY_RUN:-0}" = "1" ]; then
+ DRY_RUN=1
+fi
+
+# ---- output helpers ----
+if [ -t 1 ]; then
+ C_RESET=$'\033[0m'
+ C_INFO=$'\033[1;34m'
+ C_OK=$'\033[1;32m'
+ C_WARN=$'\033[1;33m'
+ C_ERR=$'\033[1;31m'
+else
+ C_RESET=""; C_INFO=""; C_OK=""; C_WARN=""; C_ERR=""
+fi
+info() { printf '%s[info]%s %s\n' "$C_INFO" "$C_RESET" "$*"; }
+ok() { printf '%s[ok]%s %s\n' "$C_OK" "$C_RESET" "$*"; }
+warn() { printf '%s[warn]%s %s\n' "$C_WARN" "$C_RESET" "$*" >&2; }
+err() { printf '%s[err]%s %s\n' "$C_ERR" "$C_RESET" "$*" >&2; }
+
+# ---- prerequisites ----
+if [ -z "${SENTRY_API_TOKEN:-}" ]; then
+ err "SENTRY_API_TOKEN is not set. Generate at https://sentry.io/settings/account/api/auth-tokens/ (scope: org:write)."
+ exit 1
+fi
+if [ ! -f "$DASHBOARDS_FILE" ]; then
+ err "Dashboards config not found at $DASHBOARDS_FILE"
+ exit 1
+fi
+command -v jq >/dev/null || { err "jq is required"; exit 1; }
+
+# ---- API helpers ----
+api_call() {
+ local method="$1"
+ local url="$2"
+ local body="${3:-}"
+
+ local body_args=()
+ if [ -n "$body" ]; then
+ body_args+=(-d "$body")
+ fi
+
+ local tmp
+ tmp=$(mktemp)
+ local status
+ status=$(curl -sS --fail-with-body \
+ -o "$tmp" \
+ -w "%{http_code}" \
+ -X "$method" \
+ -H "Authorization: Bearer ${SENTRY_API_TOKEN}" \
+ -H "Content-Type: application/json" \
+ "${body_args[@]}" \
+ "$url" 2>/dev/null) || {
+ err "HTTP ${status:-?} from $method $url"
+ err "Request body: $body"
+ err "Response body:"
+ [ -s "$tmp" ] && sed 's/^/ /' "$tmp" >&2 || echo " (empty)" >&2
+ rm -f "$tmp"
+ return 1
+ }
+ cat "$tmp"
+ rm -f "$tmp"
+}
+
+list_dashboards() {
+ api_call GET "${API_BASE}/organizations/${ORG}/dashboards/"
+}
+
+# Find existing dashboard by title. Returns the full dashboard object.
+find_dashboard_by_title() {
+ local title="$1"
+ list_dashboards | jq -c --arg title "$title" \
+ '.[] | select(.title == $title)' | head -n1
+}
+
+# POST when id is empty, PUT otherwise. Echoes the new/updated ID.
+create_or_update_dashboard() {
+ local body="$1"
+ local id="${2:-}"
+ local method=POST
+ local url="${API_BASE}/organizations/${ORG}/dashboards/"
+ if [ -n "$id" ]; then
+ method=PUT
+ url="${url}${id}/"
+ fi
+ if [ "$DRY_RUN" = "1" ]; then
+ echo "[dry-run] would $method $url" >&2
+ echo "$body" | jq . >&2
+ echo "stub-id"
+ return 0
+ fi
+ local resp
+ resp=$(api_call "$method" "$url" "$body") || return 1
+ echo "$resp" | jq -r '.id // empty'
+}
+
+# ---- input validation ----
+COUNT=$(jq '.dashboards | length' "$DASHBOARDS_FILE")
+info "Reading $COUNT dashboard(s) from $DASHBOARDS_FILE"
+[ "$DRY_RUN" = "1" ] && info "DRY RUN: no changes will be made"
+info "Organization: $ORG, Region: $REGION"
+
+jq -r '.dashboards[] | select(.title == null or .title == "")' "$DASHBOARDS_FILE" \
+ | grep -q . && {
+ err "One or more dashboards in $DASHBOARDS_FILE have a missing title. Fix and retry."
+ exit 1
+ }
+
+# ---- main loop ----
+APPLIED=0
+ERRORS=0
+
+for i in $(seq 0 $((COUNT - 1))); do
+ DASHBOARD=$(jq -c ".dashboards[$i]" "$DASHBOARDS_FILE")
+ TITLE=$(echo "$DASHBOARD" | jq -r '.title')
+
+ echo
+ info "==== $TITLE ===="
+
+ EXISTING=$(find_dashboard_by_title "$TITLE")
+ EXISTING_ID=""
+ if [ -n "$EXISTING" ]; then
+ EXISTING_ID=$(echo "$EXISTING" | jq -r '.id // empty')
+ info " existing dashboard id $EXISTING_ID \u2014 will update widgets"
+ fi
+
+ # Build the request body. On update, merge widgets into the existing
+ # dashboard so user-set filters (projects, environment, period, etc.)
+ # are preserved. On create, set sensible defaults.
+ if [ -n "$EXISTING_ID" ]; then
+ BODY=$(echo "$DASHBOARD" | jq --argjson existing "$EXISTING" '
+ {
+ title: $existing.title,
+ widgets: .widgets,
+ projects: ($existing.projects // []),
+ environment: ($existing.environment // []),
+ period: ($existing.period // "24h"),
+ start: ($existing.start // null),
+ end: ($existing.end // null),
+ filters: ($existing.filters // {}),
+ utc: ($existing.utc // false),
+ permissions: ($existing.permissions // null),
+ is_favorited: ($existing.is_favorited // false)
+ }
+ ')
+ else
+ BODY=$(echo "$DASHBOARD" | jq '
+ {
+ title: .title,
+ widgets: .widgets,
+ projects: [],
+ environment: [],
+ period: "24h",
+ filters: {},
+ utc: false
+ }
+ ')
+ fi
+
+ if NEW_ID=$(create_or_update_dashboard "$BODY" "$EXISTING_ID") && [ -n "$NEW_ID" ]; then
+ if [ -n "$EXISTING_ID" ]; then
+ ok "Updated dashboard $NEW_ID"
+ else
+ ok "Created dashboard $NEW_ID"
+ fi
+ APPLIED=$((APPLIED + 1))
+ else
+ ERRORS=$((ERRORS + 1))
+ fi
+done
+
+echo
+echo "-----------------------------------------------------------"
+info "Done: $APPLIED applied (created or updated), $ERRORS failed"
+[ "$DRY_RUN" = "1" ] && info "Re-run without --dry-run to apply"
+[ "$ERRORS" -gt 0 ] && exit 1
+exit 0
diff --git a/sentry/README.md b/sentry/README.md
index d44001288..5d7c1c2b5 100644
--- a/sentry/README.md
+++ b/sentry/README.md
@@ -2,20 +2,20 @@
This directory holds Sentry configuration as code, so the alert rules and dashboards that Kaneo depends on are version-controlled and reproducible.
-## What lives here
+## Contents
- `alerts.json` — alert rules. Six rules cover first-seen issues, error spikes, missed cron check-ins, slow p95 latency, and MCP error rate. Both `kaneo-api` and `kaneo-web` are covered.
+- `dashboards.json` — four Kaneo-specific dashboards (Backend, Frontend, MCP, Cron Monitors) with widgets for the metrics above. The prebuilt Sentry templates are left untouched.
## What's NOT here yet
-- Dashboards. The 10 prebuilt dashboards that Sentry ships are still empty (zero widgets each). Filling them in is the next monitoring item.
-- Cron monitor creation. These are auto-created the first time each cron job runs (`Sentry.captureCheckIn` from `apps/api/src/scheduler/index.ts`).
+- Cron monitor alert. The cron jobs in `apps/api/src/scheduler/index.ts` call `Sentry.captureCheckIn` with stable slugs (`due-date-reminders`, `project-webhook-reminders`, `seat-reconciliation`, `trial-reminders`). The Sentry monitors are auto-created the first time each job runs after the next deploy. Once they exist, the next run of `provision-sentry-alerts.sh` will resolve the slugs and create the cron-missed workflow.
## Applying the alerts
### Option A: provisioning script (recommended)
-The script lives at `scripts/provision-sentry-alerts.sh`. It reads `sentry/alerts.json`, finds existing workflows by name, and **post-or-puts** so edited specs propagate. Idempotent — re-runs converge to the spec's state.
+The script lives at `scripts/provision-sentry-alerts.sh`. It reads `sentry/alerts.json`, finds existing workflows by name, and **post-or-puts** so edited specs propagate.
```bash
# 1. Generate a Sentry auth token (admin scope)
@@ -56,8 +56,44 @@ Two rule families:
- **Issue rules** (`kind: "issue"`) — trigger on event condition types (first_seen_event, regression_event, etc.). One JSON entry per rule.
- **Metric rules** (`kind: "metric"`) — trigger on aggregate conditions (count, p95, failure_rate). Each entry creates a Detector (the metric source) and a Workflow (the alert) and links them via `detectorIds`.
+## Applying the dashboards
+
+`scripts/provision-sentry-dashboards.sh` reads `sentry/dashboards.json` and creates-or-updates each dashboard by title. The script preserves any filter settings (projects, environment, period) the maintainer has set in the Sentry UI by reading the current dashboard first and only swapping the widgets array.
+
+```bash
+SENTRY_API_TOKEN=sntrys_... ./scripts/provision-sentry-dashboards.sh
+SENTRY_API_TOKEN=sntrys_... ./scripts/provision-sentry-dashboards.sh --dry-run
+```
+
+The prebuilt Sentry dashboards (Backend Overview, Frontend Overview, etc.) are **not** touched. Kaneo-specific dashboards are created with the `Kaneo:` prefix.
+
+## Schema notes
+
+### Alerts
+
+Two rule families:
+
+- **Issue rules** (`kind: "issue"`) — trigger on event condition types (first_seen_event, regression_event, etc.). One JSON entry per rule.
+- **Metric rules** (`kind: "metric"`) — trigger on aggregate conditions (count, p95, failure_rate). Each entry creates a Detector (the metric source) and a Workflow (the alert) and links them via `detectorIds`.
+
The cron-missed-check-in rule is special: it doesn't create a Detector because the cron monitors already exist. The script resolves the slug strings in `detector_slugs` to numeric IDs via `GET /api/0/organizations/{org}/monitors/`. If the cron jobs haven't run yet, the monitors don't exist and the script will skip with a warning.
+### Dashboards
+
+Each dashboard entry has:
+- `title` — dashboard name
+- `widgets` — array of `{ title, displayType, interval, queries }` objects
+- `queries` — array of `{ fields, aggregates, columns, query, orderby, limit }`
+ - `fields` — columns to display
+ - `aggregates` — aggregate functions to compute (e.g., `count()`, `p95(transaction.duration)`)
+ - `columns` — group-by fields
+ - `query` — Sentry search filter (e.g., `event.type:error project:kaneo-api`)
+ - `orderby` — sort field (e.g., `-count()`)
+ - `limit` — max rows for table widgets
+
+Performance metrics use `event.type:transaction is_transaction:true` (the dataset migration removed `dataset: transactions`). Error metrics use `event.type:error`. Cron check-in events use `event.type:transaction monitor.check_in_id:*`.
+
The Sentry API surface used is documented at:
- <https://docs.sentry.io/api/monitors/create-an-alert-for-an-organization/>
- <https://docs.sentry.io/api/monitors/create-a-monitor-for-a-project/>
+- <https://docs.sentry.io/api/dashboards/create-a-new-dashboard-for-an-organization/>
diff --git a/sentry/dashboards.json b/sentry/dashboards.json
new file mode 100644
index 000000000..b6e1b4c4d
--- /dev/null
+++ b/sentry/dashboards.json
@@ -0,0 +1,248 @@
+{
+ "version": 1,
+ "organization": "kaneo",
+ "region": "de",
+ "api_base": "https://de.sentry.io/api/0",
+ "_comment": "Each dashboard here becomes a top-level dashboard in Sentry. The script creates-or-updates by title. Widgets are versioned here so dashboard changes are auditable.",
+ "_comment_field_types": "Widget queries use Sentry's discover query syntax. Performance widgets use dataset: events_analytics_platform (with is_transaction:true) per the current Sentry API. Error widgets use dataset: events.",
+ "dashboards": [
+ {
+ "title": "Kaneo: Backend Overview",
+ "description": "API errors, latency, and request volume for kaneo-api.",
+ "widgets": [
+ {
+ "title": "Error events (count over time)",
+ "displayType": "line",
+ "interval": "1h",
+ "queries": [
+ {
+ "fields": ["count()", "time"],
+ "aggregates": ["count()"],
+ "columns": ["time"],
+ "query": "event.type:error project:kaneo-api",
+ "orderby": "time"
+ }
+ ]
+ },
+ {
+ "title": "API p95 latency (ms)",
+ "displayType": "line",
+ "interval": "1h",
+ "queries": [
+ {
+ "fields": ["p95(transaction.duration)", "time"],
+ "aggregates": ["p95(transaction.duration)"],
+ "columns": ["time"],
+ "query": "transaction:*/api/* is_transaction:true project:kaneo-api",
+ "orderby": "time"
+ }
+ ]
+ },
+ {
+ "title": "API request volume",
+ "displayType": "line",
+ "interval": "1h",
+ "queries": [
+ {
+ "fields": ["count()", "time"],
+ "aggregates": ["count()"],
+ "columns": ["time"],
+ "query": "transaction:*/api/* is_transaction:true project:kaneo-api",
+ "orderby": "time"
+ }
+ ]
+ },
+ {
+ "title": "Top errors by exception type",
+ "displayType": "table",
+ "interval": "24h",
+ "queries": [
+ {
+ "fields": ["exception.type", "count()"],
+ "aggregates": ["count()"],
+ "columns": ["exception.type"],
+ "query": "event.type:error project:kaneo-api",
+ "orderby": "-count()",
+ "limit": 10
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "title": "Kaneo: Frontend Overview",
+ "description": "Web errors, page loads, and web vitals for kaneo-web.",
+ "widgets": [
+ {
+ "title": "Web error events (count over time)",
+ "displayType": "line",
+ "interval": "1h",
+ "queries": [
+ {
+ "fields": ["count()", "time"],
+ "aggregates": ["count()"],
+ "columns": ["time"],
+ "query": "event.type:error project:kaneo-web",
+ "orderby": "time"
+ }
+ ]
+ },
+ {
+ "title": "Page load p95 (ms)",
+ "displayType": "line",
+ "interval": "1h",
+ "queries": [
+ {
+ "fields": ["p95(transaction.duration)", "time"],
+ "aggregates": ["p95(transaction.duration)"],
+ "columns": ["time"],
+ "query": "project:kaneo-web is_transaction:true",
+ "orderby": "time"
+ }
+ ]
+ },
+ {
+ "title": "LCP (p75, ms)",
+ "displayType": "line",
+ "interval": "1h",
+ "queries": [
+ {
+ "fields": ["p75(measurements.lcp)", "time"],
+ "aggregates": ["p75(measurements.lcp)"],
+ "columns": ["time"],
+ "query": "project:kaneo-web is_transaction:true",
+ "orderby": "time"
+ }
+ ]
+ },
+ {
+ "title": "Top error pages",
+ "displayType": "table",
+ "interval": "24h",
+ "queries": [
+ {
+ "fields": ["transaction", "count()"],
+ "aggregates": ["count()"],
+ "columns": ["transaction"],
+ "query": "event.type:error project:kaneo-web",
+ "orderby": "-count()",
+ "limit": 10
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "title": "Kaneo: MCP",
+ "description": "MCP tool traffic, error rate, and latency.",
+ "widgets": [
+ {
+ "title": "MCP request volume",
+ "displayType": "line",
+ "interval": "1h",
+ "queries": [
+ {
+ "fields": ["count()", "time"],
+ "aggregates": ["count()"],
+ "columns": ["time"],
+ "query": "transaction:*/api/mcp/* is_transaction:true",
+ "orderby": "time"
+ }
+ ]
+ },
+ {
+ "title": "MCP error rate",
+ "displayType": "line",
+ "interval": "1h",
+ "queries": [
+ {
+ "fields": ["failure_rate()", "time"],
+ "aggregates": ["failure_rate()"],
+ "columns": ["time"],
+ "query": "transaction:*/api/mcp/* is_transaction:true",
+ "orderby": "time"
+ }
+ ]
+ },
+ {
+ "title": "MCP tool usage (top 20)",
+ "displayType": "table",
+ "interval": "24h",
+ "queries": [
+ {
+ "fields": ["transaction", "count()"],
+ "aggregates": ["count()"],
+ "columns": ["transaction"],
+ "query": "transaction:*/api/mcp/* is_transaction:true",
+ "orderby": "-count()",
+ "limit": 20
+ }
+ ]
+ },
+ {
+ "title": "MCP p95 latency (ms)",
+ "displayType": "line",
+ "interval": "1h",
+ "queries": [
+ {
+ "fields": ["p95(transaction.duration)", "time"],
+ "aggregates": ["p95(transaction.duration)"],
+ "columns": ["time"],
+ "query": "transaction:*/api/mcp/* is_transaction:true",
+ "orderby": "time"
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "title": "Kaneo: Cron Monitors",
+ "description": "Scheduled-job check-ins from apps/api/src/scheduler/index.ts. Monitors appear in Sentry once each cron has run at least once.",
+ "widgets": [
+ {
+ "title": "Check-in status by monitor",
+ "displayType": "table",
+ "interval": "24h",
+ "queries": [
+ {
+ "fields": ["monitor.slug", "count()"],
+ "aggregates": ["count()"],
+ "columns": ["monitor.slug"],
+ "query": "event.type:transaction monitor.check_in_id:*",
+ "orderby": "-count()",
+ "limit": 20
+ }
+ ]
+ },
+ {
+ "title": "Check-in events over time",
+ "displayType": "line",
+ "interval": "1h",
+ "queries": [
+ {
+ "fields": ["count()", "time"],
+ "aggregates": ["count()"],
+ "columns": ["time"],
+ "query": "event.type:transaction monitor.check_in_id:*",
+ "orderby": "time"
+ }
+ ]
+ },
+ {
+ "title": "Check-in errors over time",
+ "displayType": "line",
+ "interval": "1h",
+ "queries": [
+ {
+ "fields": ["count()", "time"],
+ "aggregates": ["count()"],
+ "columns": ["time"],
+ "query": "event.type:transaction monitor.check_in_id:* monitor.status:missed_or_failed",
+ "orderby": "time"
+ }
+ ]
+ }
+ ]
+ }
+ ]
+}