{
  "__inputs": [
    {
      "name": "DS_PROMETHEUS",
      "label": "Prometheus",
      "description": "Prometheus scraping the crucible daemon's /metrics",
      "type": "datasource",
      "pluginId": "prometheus",
      "pluginName": "Prometheus"
    }
  ],
  "annotations": { "list": [] },
  "editable": true,
  "graphTooltip": 1,
  "time": { "from": "now-1h", "to": "now" },
  "refresh": "30s",
  "schemaVersion": 39,
  "version": 1,
  "title": "crucible — apps",
  "uid": "crucible-apps",
  "tags": ["crucible"],
  "templating": {
    "list": [
      {
        "name": "datasource",
        "label": "Prometheus",
        "type": "datasource",
        "query": "prometheus",
        "current": {}
      },
      {
        "name": "app",
        "label": "App",
        "type": "query",
        "datasource": { "type": "prometheus", "uid": "${datasource}" },
        "query": "label_values(app_up, app)",
        "includeAll": true,
        "multi": true,
        "current": { "text": "All", "value": "$__all" },
        "refresh": 2
      }
    ]
  },
  "panels": [
    {
      "title": "Requests/sec by app",
      "type": "timeseries",
      "datasource": { "type": "prometheus", "uid": "${datasource}" },
      "gridPos": { "h": 8, "w": 12, "x": 0, "y": 0 },
      "fieldConfig": { "defaults": { "unit": "reqps" }, "overrides": [] },
      "targets": [
        {
          "expr": "sum by (app) (rate(app_requests_total{app=~\"$app\"}[5m]))",
          "legendFormat": "{{app}}",
          "refId": "A"
        }
      ]
    },
    {
      "title": "5xx error ratio by app",
      "type": "timeseries",
      "datasource": { "type": "prometheus", "uid": "${datasource}" },
      "gridPos": { "h": 8, "w": 12, "x": 12, "y": 0 },
      "fieldConfig": { "defaults": { "unit": "percentunit", "min": 0, "max": 1 }, "overrides": [] },
      "targets": [
        {
          "expr": "sum by (app) (rate(app_requests_total{app=~\"$app\",code=\"5xx\"}[5m])) / clamp_min(sum by (app) (rate(app_requests_total{app=~\"$app\"}[5m])), 1e-9)",
          "legendFormat": "{{app}}",
          "refId": "A"
        }
      ]
    },
    {
      "title": "Request latency (p50 / p90 / p99, all apps)",
      "type": "timeseries",
      "datasource": { "type": "prometheus", "uid": "${datasource}" },
      "gridPos": { "h": 8, "w": 12, "x": 0, "y": 8 },
      "fieldConfig": { "defaults": { "unit": "s" }, "overrides": [] },
      "targets": [
        { "expr": "histogram_quantile(0.50, sum by (le) (rate(app_request_duration_seconds_bucket{app=~\"$app\"}[5m])))", "legendFormat": "p50", "refId": "A" },
        { "expr": "histogram_quantile(0.90, sum by (le) (rate(app_request_duration_seconds_bucket{app=~\"$app\"}[5m])))", "legendFormat": "p90", "refId": "B" },
        { "expr": "histogram_quantile(0.99, sum by (le) (rate(app_request_duration_seconds_bucket{app=~\"$app\"}[5m])))", "legendFormat": "p99", "refId": "C" }
      ]
    },
    {
      "title": "Replicas: desired vs ready",
      "type": "timeseries",
      "datasource": { "type": "prometheus", "uid": "${datasource}" },
      "gridPos": { "h": 8, "w": 12, "x": 12, "y": 8 },
      "fieldConfig": { "defaults": { "unit": "short", "min": 0 }, "overrides": [] },
      "targets": [
        { "expr": "app_replicas{app=~\"$app\"}", "legendFormat": "{{app}} desired", "refId": "A" },
        { "expr": "app_ready_replicas{app=~\"$app\"}", "legendFormat": "{{app}} ready", "refId": "B" }
      ]
    },
    {
      "title": "Fleet asleep (scale-to-zero)",
      "type": "stat",
      "datasource": { "type": "prometheus", "uid": "${datasource}" },
      "gridPos": { "h": 8, "w": 6, "x": 0, "y": 16 },
      "fieldConfig": { "defaults": { "unit": "percentunit", "min": 0, "max": 1 }, "overrides": [] },
      "options": { "reduceOptions": { "calcs": ["lastNotNull"] }, "colorMode": "value" },
      "targets": [
        { "expr": "sum(app_asleep) / clamp_min(count(app_up), 1)", "refId": "A" }
      ]
    },
    {
      "title": "Wake latency (p95, all apps)",
      "type": "stat",
      "datasource": { "type": "prometheus", "uid": "${datasource}" },
      "gridPos": { "h": 8, "w": 6, "x": 6, "y": 16 },
      "fieldConfig": { "defaults": { "unit": "s" }, "overrides": [] },
      "options": { "reduceOptions": { "calcs": ["lastNotNull"] }, "colorMode": "value" },
      "targets": [
        { "expr": "histogram_quantile(0.95, sum by (le) (rate(app_wake_latency_seconds_bucket[5m])))", "refId": "A" }
      ]
    },
    {
      "title": "Sleep cycles by app (total)",
      "type": "timeseries",
      "datasource": { "type": "prometheus", "uid": "${datasource}" },
      "gridPos": { "h": 8, "w": 12, "x": 12, "y": 16 },
      "fieldConfig": { "defaults": { "unit": "short", "min": 0 }, "overrides": [] },
      "targets": [
        { "expr": "app_sleep_total{app=~\"$app\"}", "legendFormat": "{{app}}", "refId": "A" }
      ]
    }
  ]
}
