# cihanisildar/go-grafana architecture

> Architecture diagram of the GitHub repository cihanisildar/go-grafana, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/cihanisildar/go-grafana
- Repository: https://github.com/cihanisildar/go-grafana

## Overview

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: [`internal/handlers/handlers.go`](https://github.com/cihanisildar/go-grafana/blob/main/internal/handlers/handlers.go) (HTTP Handlers), [`docker-compose.yml`](https://github.com/cihanisildar/go-grafana/blob/main/docker-compose.yml) (Compose Stack), [`internal/worker/worker.go`](https://github.com/cihanisildar/go-grafana/blob/main/internal/worker/worker.go) (Worker Pool), [`internal/store/store.go`](https://github.com/cihanisildar/go-grafana/blob/main/internal/store/store.go) (Job Store)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["App Runtime"]
  node_browser(("Browser<br/>client"))
  node_handlers["HTTP Handlers<br/>web layer<br/>[handlers.go]"]
  node_templates["Templates<br/>server HTML<br/>[index.html]"]
  node_styles["Styles<br/>static css<br/>[style.css]"]
  node_workers{{"Worker Pool<br/>processor<br/>[worker.go]"}}
  node_job_types["Job Logic<br/>execution rules<br/>[worker.go]"]
  node_compose["Compose Stack<br/>orchestration<br/>[docker-compose.yml]"]
end

subgraph group_data["State & Queue"]
  node_store[("Job Store<br/>postgres access<br/>[store.go]")]
  node_postgres[("PostgreSQL<br/>database")]
  node_queue["Redis Queue<br/>dispatch layer<br/>[queue.go]"]
  node_redis[("Redis<br/>broker")]
end

subgraph group_obs["Observability"]
  node_metrics{{"Metrics<br/>prometheus instruments<br/>[metrics.go]"}}
  node_scrape["Metrics Endpoint<br/>http /metrics"]
  node_prometheus[("Prometheus<br/>scraper<br/>[prometheus.yml]")]
  node_grafana["Grafana<br/>dashboard<br/>[job-monitor.json]"]
end

node_browser -->|"HTTP/HTMX"| node_handlers
node_handlers -->|"render"| node_templates
node_handlers -->|"serve static"| node_styles
node_handlers -->|"read/write jobs"| node_store
node_handlers -->|"enqueue job"| node_queue
node_queue -->|"transport"| node_redis
node_store -->|"SQL"| node_postgres
node_redis -->|"blocking pop"| node_workers
node_workers -->|"execute"| node_job_types
node_workers -->|"update status"| node_store
node_workers -->|"emit"| node_metrics
node_handlers -->|"record"| node_metrics
node_metrics -->|"expose"| node_scrape
node_prometheus -->|"scrape"| node_scrape
node_grafana -->|"query"| node_prometheus
node_compose -->|"run"| node_handlers
node_compose -->|"provision"| node_postgres
node_compose -->|"provision"| node_redis
node_compose -->|"provision"| node_prometheus
node_compose -->|"provision"| node_grafana

click node_handlers "https://github.com/cihanisildar/go-grafana/blob/main/internal/handlers/handlers.go"
click node_templates "https://github.com/cihanisildar/go-grafana/blob/main/templates/index.html"
click node_styles "https://github.com/cihanisildar/go-grafana/blob/main/static/css/style.css"
click node_store "https://github.com/cihanisildar/go-grafana/blob/main/internal/store/store.go"
click node_queue "https://github.com/cihanisildar/go-grafana/blob/main/internal/queue/queue.go"
click node_workers "https://github.com/cihanisildar/go-grafana/blob/main/internal/worker/worker.go"
click node_job_types "https://github.com/cihanisildar/go-grafana/blob/main/internal/worker/worker.go"
click node_metrics "https://github.com/cihanisildar/go-grafana/blob/main/internal/metrics/metrics.go"
click node_prometheus "https://github.com/cihanisildar/go-grafana/blob/main/docker/prometheus.yml"
click node_grafana "https://github.com/cihanisildar/go-grafana/blob/main/docker/grafana/dashboards/job-monitor.json"
click node_compose "https://github.com/cihanisildar/go-grafana/blob/main/docker-compose.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_browser,node_handlers,node_templates,node_styles,node_workers,node_job_types,node_compose toneBlue
class node_store,node_postgres,node_queue,node_redis toneAmber
class node_metrics,node_scrape,node_prometheus,node_grafana toneMint
```

## Components

### App Runtime

Go service and its execution boundaries

- **Browser**: client · User-facing entry point for the HTMX UI
- [HTTP Handlers](https://github.com/cihanisildar/go-grafana/blob/main/internal/handlers/handlers.go): web layer · Serves dashboard pages, partials, submission, and cancellation · `internal/handlers/handlers.go`
- [Templates](https://github.com/cihanisildar/go-grafana/blob/main/templates/index.html): server HTML · Rendered UI fragments and pages for the dashboard · `templates/index.html`
- [Styles](https://github.com/cihanisildar/go-grafana/blob/main/static/css/style.css): static css · Minimal presentation styling for the UI · `static/css/style.css`
- [Worker Pool](https://github.com/cihanisildar/go-grafana/blob/main/internal/worker/worker.go): processor · Fixed pool of background workers that consume and execute jobs · `internal/worker/worker.go`
- [Job Logic](https://github.com/cihanisildar/go-grafana/blob/main/internal/worker/worker.go): execution rules · Simulated handling for different job types within workers · `internal/worker/worker.go`
- [Compose Stack](https://github.com/cihanisildar/go-grafana/blob/main/docker-compose.yml): orchestration · Local multi-container wiring for service, database, queue, and observability · `docker-compose.yml`

### State & Queue

Persistent job state and dispatch transport

- [Job Store](https://github.com/cihanisildar/go-grafana/blob/main/internal/store/store.go): postgres access · PostgreSQL persistence for job lifecycle state and paginated reads · `internal/store/store.go`
- **PostgreSQL**: database · Source of truth for job status and lifecycle
- [Redis Queue](https://github.com/cihanisildar/go-grafana/blob/main/internal/queue/queue.go): dispatch layer · Redis-backed job transport using blocking pop semantics · `internal/queue/queue.go`
- **Redis**: broker · Queue backend for pending execution intent

### Observability

Metrics scraping and dashboarding stack

- [Metrics](https://github.com/cihanisildar/go-grafana/blob/main/internal/metrics/metrics.go): prometheus instruments · Counters, histograms, queue depth, and active worker gauges · `internal/metrics/metrics.go`
- **Metrics Endpoint**: http /metrics · Prometheus scrape target exposed by the Go service
- [Prometheus](https://github.com/cihanisildar/go-grafana/blob/main/docker/prometheus.yml): scraper · Collects and stores service metrics on a short scrape interval · `docker/prometheus.yml`
- [Grafana](https://github.com/cihanisildar/go-grafana/blob/main/docker/grafana/dashboards/job-monitor.json): dashboard · Visualizes the job monitoring dashboard · `docker/grafana/dashboards/job-monitor.json`

## Connections

- 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
