{
  "schemaVersion": 39,
  "title": "API — Golden Signals",
  "uid": "url-shortener-golden-signals",
  "version": 1,
  "refresh": "30s",
  "time": { "from": "now-1h", "to": "now" },
  "templating": {
    "list": [
      {
        "name": "namespace",
        "type": "constant",
        "current": { "value": "url-shortener" },
        "hide": 0,
        "label": "Namespace"
      },
      {
        "name": "app",
        "type": "constant",
        "current": { "value": "api" },
        "hide": 0,
        "label": "App"
      }
    ]
  },
  "panels": [
    {
      "id": 1,
      "type": "timeseries",
      "title": "Request Rate by Route (rps)",
      "gridPos": { "h": 8, "w": 12, "x": 0, "y": 0 },
      "targets": [
        {
          "datasource": { "type": "prometheus" },
          "expr": "sum by (route) (rate(http_requests_total[1m]))",
          "legendFormat": "{{route}}"
        }
      ],
      "fieldConfig": {
        "defaults": { "unit": "reqps", "custom": { "lineWidth": 2 } }
      }
    },
    {
      "id": 2,
      "type": "timeseries",
      "title": "5xx Error Rate",
      "gridPos": { "h": 8, "w": 12, "x": 12, "y": 0 },
      "targets": [
        {
          "datasource": { "type": "prometheus" },
          "expr": "sum(rate(http_requests_total{code=~\"5..\"}[5m])) / sum(rate(http_requests_total[5m]))",
          "legendFormat": "error rate"
        }
      ],
      "fieldConfig": {
        "defaults": {
          "unit": "percentunit",
          "thresholds": {
            "mode": "absolute",
            "steps": [
              { "color": "green", "value": null },
              { "color": "yellow", "value": 0.01 },
              { "color": "red", "value": 0.02 }
            ]
          }
        }
      }
    },
    {
      "id": 3,
      "type": "timeseries",
      "title": "Latency Percentiles (p50 / p95 / p99)",
      "gridPos": { "h": 8, "w": 12, "x": 0, "y": 8 },
      "targets": [
        {
          "datasource": { "type": "prometheus" },
          "expr": "histogram_quantile(0.50, sum by (le) (rate(http_request_duration_seconds_bucket[5m])))",
          "legendFormat": "p50"
        },
        {
          "datasource": { "type": "prometheus" },
          "expr": "histogram_quantile(0.95, sum by (le) (rate(http_request_duration_seconds_bucket[5m])))",
          "legendFormat": "p95"
        },
        {
          "datasource": { "type": "prometheus" },
          "expr": "histogram_quantile(0.99, sum by (le) (rate(http_request_duration_seconds_bucket[5m])))",
          "legendFormat": "p99"
        }
      ],
      "fieldConfig": {
        "defaults": { "unit": "s", "custom": { "lineWidth": 2 } }
      }
    },
    {
      "id": 4,
      "type": "timeseries",
      "title": "CPU & Memory Saturation per Pod",
      "gridPos": { "h": 8, "w": 12, "x": 12, "y": 8 },
      "targets": [
        {
          "datasource": { "type": "prometheus" },
          "expr": "sum by (pod) (rate(container_cpu_usage_seconds_total{namespace=\"url-shortener\", container!=\"\"}[5m]))",
          "legendFormat": "cpu {{pod}}"
        },
        {
          "datasource": { "type": "prometheus" },
          "expr": "sum by (pod) (container_memory_working_set_bytes{namespace=\"url-shortener\", container!=\"\"}) / sum by (pod) (kube_pod_container_resource_limits{namespace=\"url-shortener\", resource=\"memory\"})",
          "legendFormat": "mem% {{pod}}"
        }
      ],
      "fieldConfig": {
        "defaults": { "unit": "short" }
      }
    },
    {
      "id": 5,
      "type": "timeseries",
      "title": "Worker Throughput & Job Duration",
      "gridPos": { "h": 8, "w": 12, "x": 0, "y": 16 },
      "targets": [
        {
          "datasource": { "type": "prometheus" },
          "expr": "rate(worker_jobs_processed_total{outcome=\"ok\"}[1m])",
          "legendFormat": "jobs/s (ok)"
        },
        {
          "datasource": { "type": "prometheus" },
          "expr": "histogram_quantile(0.95, sum by (le) (rate(worker_job_duration_seconds_bucket[5m])))",
          "legendFormat": "p95 job duration"
        }
      ],
      "fieldConfig": {
        "defaults": { "unit": "short" }
      }
    },
    {
      "id": 6,
      "type": "logs",
      "title": "Recent Error Logs",
      "gridPos": { "h": 8, "w": 12, "x": 12, "y": 16 },
      "targets": [
        {
          "datasource": { "type": "loki" },
          "expr": "{namespace=\"url-shortener\"} | json | level=\"error\"",
          "queryType": "range"
        }
      ],
      "options": {
        "showLabels": false,
        "showTime": true,
        "sortOrder": "Descending",
        "wrapLogMessage": true
      }
    }
  ]
}
