Skip to content

feat: Grafana alerts, Loki logs, and bearer auth for Alertmanager - #101

Merged
OkiriGabriel merged 2 commits into
mainfrom
feat/grafana-alerts-and-logs
Oct 19, 2026
Merged

OkiriGabriel merged 2 commits into
mainfrom
feat/grafana-alerts-and-logs

Conversation

@OkiriGabriel

Copy link
Copy Markdown
Collaborator

Description

Fix: Alertmanager was always rejected when WEBHOOK_SECRET is set. /webhook/alertmanager only accepted an HMAC signature of the request body, which Alertmanager can't produce. It can only send a fixed bearer token or basic auth. So with a secret configured, every Alertmanager delivery got a 401, and the README example only worked with auth turned off. The endpoint now also accepts Authorization: Bearer <WEBHOOK_SECRET>, compared in constant time like the GitLab token.

New: Grafana alerts in, Loki logs to the agent.

  • POST /webhook/grafana receives Grafana Alerting webhook contact points (bearer token or HMAC). Each firing alert becomes an incident that includes dashboard_url, panel_url, runbook_url, alert_values and starts_at. It shares one alert-to-incident helper with the Alertmanager endpoint.
  • Logs come from Loki through Grafana's datasource proxy (GRAFANA_URL + GRAFANA_TOKEN + new GRAFANA_LOKI_DATASOURCE_UID) or directly (LOKI_URL, LOKI_TOKEN).
    • Automatic: before diagnosing an incident from Grafana or Alertmanager, the agent attaches alert_logs. These are up to 100 error-like lines, starting 15 minutes before the alert fired, with the window capped at 24 hours. The query is built from the alert's namespace/pod, job, app or service labels. A logql annotation on the alert rule overrides it.
    • On demand: a new read-only query_logs tool (LogQL), so the agent can widen the time window or check neighbouring services. It counts as grounding evidence.
    • When a log provider is configured, the system prompt tells the agent to read alert_logs first and quote the log lines that support its root cause.
  • Remediation is unchanged. The agent fixes with its existing tools under the same AUTO_APPLY, approval and emergency-stop rules. Fetching logs never blocks an incident: on error, the incident proceeds without logs.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Documentation update

Platform/Component

  • Core Agent
  • API Server
  • Tools/Executor
  • Documentation
  • Tests

Testing

  • Unit tests added (tests/test_grafana_alerts_logs.py, 34 tests)
    • bearer parsing; Alertmanager bearer accepted / wrong token 401 / Basic 401
    • Grafana endpoint: field mapping, resolved alerts skipped, missing auth 401, HMAC still works, logql annotation, X-Org-ID
    • Loki via Grafana proxy and direct: URL, auth header, nanosecond window, newest-first merge across streams, limit clamp, line truncation, HTTP errors, not configured
    • label-based selector with escaping; 15-minute lead-in; 24-hour cap; annotation override
    • agent: alert_logs attached; a log failure doesn't block the incident; query_logs dispatch; prompt hint only when configured
  • Full suite: 380 passed, 2 skipped (Python 3.11)
  • flake8 --select=E9,F63,F7,F82: 0; bandit -ll on changed files: no issues

Setup

# alertmanager.yml
webhook_configs:
  - url: https://agent/webhook/alertmanager
    http_config:
      authorization: { type: Bearer, credentials: '<WEBHOOK_SECRET>' }

Grafana: Contact point → Webhook → https://agent/webhook/grafana, Authorization Header Bearer / WEBHOOK_SECRET. Then set GRAFANA_LOKI_DATASOURCE_UID.

Breaking Changes

None. HMAC signatures still work on both endpoints.

🤖 Generated with Claude Code

OkiriGabriel and others added 2 commits October 19, 2026 13:26
Alertmanager cannot HMAC-sign webhook bodies, so with WEBHOOK_SECRET set it
was always rejected with 401. /webhook/alertmanager now also accepts
`Authorization: Bearer <WEBHOOK_SECRET>`, which Alertmanager sends via
http_config.authorization.

Add /webhook/grafana for Grafana Alerting webhook contact points (bearer or
HMAC auth). Firing alerts become incidents with dashboard/panel/runbook URLs.

Add Loki log retrieval through Grafana's datasource proxy
(GRAFANA_LOKI_DATASOURCE_UID) or directly (LOKI_URL):
- Alerts from Grafana/Alertmanager get recent error lines attached as
  alert_logs before diagnosis (selector from alert labels, or a `logql`
  annotation; window starts 15 min before the alert, capped at 24h).
- New read-only query_logs tool (LogQL) counts as grounding evidence.
- System prompt tells the agent to use the logs when a provider is set.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Step-by-step setup for Alertmanager and Grafana alert intake, Loki log
fetching, testing with curl, how log selectors are chosen, the logql
annotation, and troubleshooting. Linked from the docs nav, docs home and
README.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant