Skip to content

Feat/observability - #6

Merged
notsubash merged 2 commits into
mainfrom
feat/observability
Jul 23, 2026
Merged

Feat/observability#6
notsubash merged 2 commits into
mainfrom
feat/observability

Conversation

@notsubash

Copy link
Copy Markdown
Owner

Summary

Adds end-to-end observability for the hobby API stack and tightens the hobby VPS bootstrap so metrics, logs, and kubectl access work without SSH tunnels.

  • API (RED metrics + request correlation): Middleware attaches X-Request-ID, records http_requests_total and http_request_duration_seconds for every request (except /metrics), and logs structured request lines with request_id for Loki filtering. Includes a teaching-only /debug/boom endpoint for error-rate drills.
  • Kubernetes scraping: Helm ServiceMonitor scrapes /metrics on port http every 30s when metrics.serviceMonitor.enabled is true (enabled by default in values.yaml).
  • Monitoring stack configs: Helm values for kube-prometheus-stack (hobby-sized resources, 3d retention, cross-namespace ServiceMonitor discovery) and Grafana Loki (single-binary, filesystem storage, 72h retention). PrometheusRule alert ApiHighErrorRate fires when 5xx ratio exceeds 5% for 2m.
  • Hobby infra hardening: Replaces ad-hoc ssh_source_cidrs edits with admin_cidrs in terraform.tfvars. Firewall allows SSH (22) and Kubernetes API (6443) only from admin IPs; HTTP/HTTPS remain open. k3s cloud-init sets --tls-san to the VPS public IP so direct kubectl works.
  • DX: New scripts/fetch-hobby-kubeconfig.sh waits for k3s, fetches kubeconfig, rewrites 127.0.0.1 → public IP, and clears stale SSH host keys. README and gitops/README.md updated accordingly.

Details

API (apps/api/app/main.py)

  • ObservabilityMiddleware — propagates or generates X-Request-ID, emits RED Prometheus metrics, logs method, path, status, duration_ms.
  • RequestIdFilter — prevents KeyError on log records missing request_id.
  • GET /debug/boom — forced 500 for observability practice (marked teaching-only).

Helm (helm/api/)

  • New templates/servicemonitor.yaml — Prometheus Operator CRD targeting the API Service on /metrics.
  • values.yamlmetrics.serviceMonitor.enabled: true.

Monitoring (monitoring/)

  • kube-prometheus-stack-values.yaml — single-replica, resource-capped Prometheus/Grafana/Alertmanager for a small VPS.
  • loki-values.yaml — single-binary Loki with filesystem storage.
  • alerts/api-rules.yamlApiHighErrorRate PrometheusRule.
  • README.md and dashboards/api-red.json — empty placeholders (no content yet).

Terraform & scripts

  • admin_cidrs variable replaces inline firewall CIDR edits; no wide-open SSH default.
  • Firewall module opens 22, 6443 (admin CIDRs) and 80, 443 (public).
  • cloud-init installs k3s with --tls-san from Hetzner metadata public IP.
  • outputs.tf adds kubeconfig_hint pointing to the fetch script.
  • scripts/fetch-hobby-kubeconfig.sh — automated kubeconfig setup after terraform apply.

Docs

  • README.mdadmin_cidrs workflow, fetch script usage, firewall/IP-change notes.
  • gitops/README.md — direct kubectl on :6443, troubleshooting for host-key and firewall issues.

- Revised the README to clarify the process for accessing the VPS and managing the firewall settings, emphasizing the use of `admin_cidrs` for SSH and kubectl access.
- Introduced a new script, `fetch-hobby-kubeconfig.sh`, to automate the retrieval and configuration of the kubeconfig file after deploying the k3s cluster.
- Updated Terraform configurations to replace `ssh_source_cidrs` with `admin_cidrs`, enhancing security by restricting access to specified IPs.
- Enhanced cloud-init template to automatically set the TLS SAN to the VPS public IP, ensuring secure communication with the Kubernetes API.
- Added output hints in Terraform to guide users on fetching the kubeconfig after deployment.

These changes improve the security and usability of the hobby environment setup.
- Introduced middleware to attach X-Request-ID for log correlation and record RED metrics (request counts and duration) for all HTTP requests.
- Updated logging format to include request_id for better traceability.
- Added a debug endpoint to simulate errors for observability drills.
- Configured Prometheus ServiceMonitor for scraping metrics from the API.
- Updated Helm values to enable metrics scraping and added necessary configurations for Prometheus and Loki.

These changes improve the observability and monitoring capabilities of the API, facilitating better performance tracking and error diagnosis.
@notsubash
notsubash merged commit b6b8bd1 into main Jul 23, 2026
3 checks passed
@notsubash
notsubash deleted the feat/observability branch July 23, 2026 05:04
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