How cihanisildar/go-grafana works
1. Overall shape
- This is a single Go service that acts as the control plane for a job queue system.
- It exposes a browser-facing UI and HTTP API, persists job state in PostgreSQL, dispatches work through Redis, and exports Prometheus metrics for Grafana.
2. Request path / UI boundary
- The browser talks directly to the Go HTTP server.
- The UI is server-rendered with HTMX and Go templates, so the frontend is mostly HTML partials rather than a separate SPA.
- templates/index.html, templates/job_list.html, and templates/stats.html are the main presentation assets; static/css/style.css provides minimal styling.
3. HTTP handlers subsystem
- internal/handlers/handlers.go is the web layer boundary.
- It serves the dashboard page, job list partials, stats partials, job submission, and job cancellation.
- The handlers are responsible for translating HTTP/HTMX requests into store operations, queue operations, and metric updates.
4. Persistent state boundary
- internal/store/store.go is the PostgreSQL access layer.
- PostgreSQL is the source of truth for job lifecycle state: pending, running, completed, failed, cancelled.
- The store also supports paginated reads for the job list UI, which is why the browser can refresh job tables without reloading the whole page.
5. Queue boundary
- internal/queue/queue.go wraps Redis as the dispatch mechanism.
- Jobs are enqueued from the HTTP side and later consumed by workers using blocking pop semantics.
- This separates “job accepted by system” from “job actively being processed,” which is important for the architecture diagram: PostgreSQL stores state, Redis carries execution intent.
6. Worker pool / execution boundary
- internal/worker/worker.go contains the background processor pool.
- The system runs a fixed pool of 3 workers, each consuming jobs from Redis and simulating work for different job types.
- Workers update job status in PostgreSQL and emit metrics around activity and duration.
7. Metrics subsystem
- internal/metrics/metrics.go defines Prometheus instruments.
- The key signals are job counts by type/status, job duration histograms, queue depth, and active workers.
- The Go server exposes /metrics, which Prometheus scrapes on a 5-second interval.
8. Observability pipeline
- Prometheus is the metrics storage and scrape layer; Grafana is the visualization layer.
- docker/prometheus.yml configures scraping of the Go service.
- docker/grafana/provisioning and docker/grafana/dashboards/job-monitor.json auto-provision the datasource and dashboard, so observability is part of the repo’s runtime stack, not an afterthought.
9. Container/orchestration boundary
- Dockerfile builds the Go service.
- docker-compose.yml ties together the web server, PostgreSQL, Redis, Prometheus, and Grafana for local development.
- The architecture is therefore multi-container, with explicit service boundaries rather than a monolith that embeds storage or monitoring.
10. Job lifecycle flow
- A user submits a job through the HTMX UI.
- The handler creates or updates the job record in PostgreSQL, then enqueues it in Redis.
- A worker blocks on Redis, claims the job, marks it running, simulates execution, and finally writes completed/failed state back to PostgreSQL.
- The UI can then re-read the latest job list and stats from the server-rendered endpoints.
11. Cancellation and consistency
- The DELETE /jobs/{id} endpoint only makes sense for jobs that are still pending.
- That implies the DB is the authoritative place to decide whether cancellation is allowed, while Redis is just the transport for pending execution.
- In an architecture diagram, that boundary should be shown as “cancel affects persisted state; workers consume from queue independently.”
12. What matters most for a diagram
- The key nodes are: Browser/HTMX, Go HTTP server, PostgreSQL, Redis, worker pool, Prometheus, Grafana.
- The key arrows are: browser → server; server → PostgreSQL; server → Redis; Redis → workers; workers → PostgreSQL; server → Prometheus scrape endpoint; Prometheus → Grafana.
- The repo’s main design idea is a clean split between UI, persistence, queueing, execution, and observability.
Main entry points: handlers.go HTTP Handlers · docker-compose.yml Compose Stack · worker.go Worker Pool · store.go Job Store
Components 15
App Runtime
Go service and its execution boundaries
- Browser · User-facing entry point for the HTMX UI
- HTTP Handlers · Serves dashboard pages, partials, submission, and cancellation
internal/handlers/handlers.go - Templates · Rendered UI fragments and pages for the dashboard
templates/index.html - Styles · Minimal presentation styling for the UI
static/css/style.css - Worker Pool · Fixed pool of background workers that consume and execute jobs
internal/worker/worker.go - Job Logic · Simulated handling for different job types within workers
internal/worker/worker.go - Compose Stack · Local multi-container wiring for service, database, queue, and observability
docker-compose.yml
State & Queue
Persistent job state and dispatch transport
- Job Store · PostgreSQL persistence for job lifecycle state and paginated reads
internal/store/store.go - PostgreSQL · Source of truth for job status and lifecycle
- Redis Queue · Redis-backed job transport using blocking pop semantics
internal/queue/queue.go - Redis · Queue backend for pending execution intent
Observability
Metrics scraping and dashboarding stack
- Metrics · Counters, histograms, queue depth, and active worker gauges
internal/metrics/metrics.go - Metrics Endpoint · Prometheus scrape target exposed by the Go service
- Prometheus · Collects and stores service metrics on a short scrape interval
docker/prometheus.yml - Grafana · Visualizes the job monitoring dashboard
docker/grafana/dashboards/job-monitor.json
Connections 20
- Browser → HTTP Handlers: HTTP/HTMX · Browser sends page loads, partial refreshes, submissions, and cancellations
- HTTP Handlers → Templates: render · Handlers render full pages and HTMX partials
- HTTP Handlers → Styles: serve static · Dashboard assets include minimal CSS
- HTTP Handlers → Job Store: read/write jobs · Web layer persists and queries job state
- HTTP Handlers → Redis Queue: enqueue job · Accepted jobs are dispatched to the Redis-backed queue
- Redis Queue → Redis: transport · Redis holds pending execution intent
- Job Store → PostgreSQL: SQL · Persistent job lifecycle state lives in PostgreSQL
- Redis → Worker Pool: blocking pop · Workers consume queued jobs from Redis
- Worker Pool → Job Logic: execute · Workers simulate work for different job types
- Worker Pool → Job Store: update status · Workers mark jobs running, completed, failed, or cancelled state
- Worker Pool → Metrics: emit · Processing activity updates counters, histograms, and gauges
- HTTP Handlers → Metrics: record · HTTP-side actions also update job and queue metrics
- Metrics → Metrics Endpoint: expose · Prometheus-formatted metrics are served on /metrics
- Prometheus → Metrics Endpoint: scrape · Prometheus collects metrics from the Go service
- Grafana → Prometheus: query · Grafana reads Prometheus as its data source
- Compose Stack → HTTP Handlers: run · Compose starts the Go web service
- Compose Stack → PostgreSQL: provision · Compose wires in the PostgreSQL runtime
- Compose Stack → Redis: provision · Compose wires in the Redis runtime
- Compose Stack → Prometheus: provision · Compose includes Prometheus for scraping
- Compose Stack → Grafana: provision · Compose includes Grafana for dashboards