Initial commit

This commit is contained in:
Zhongwei Li
2025-11-29 18:16:40 +08:00
commit f125e90b9f
370 changed files with 67769 additions and 0 deletions

View File

@@ -0,0 +1,160 @@
# Grafana Dashboard Templates
This directory contains pre-configured Grafana dashboards for Claude Code telemetry.
## Available Dashboards
### 1. claude-code-overview.json
**Comprehensive dashboard with all key metrics**
**Panels:**
- Total Lines of Code (all-time counter)
- Total Cost (24h rolling window)
- Total Tokens (24h rolling window)
- Active Time (24h rolling window)
- Cost Over Time (per hour rate)
- Token Usage by Type (stacked timeseries)
- Lines of Code Modified (bar chart)
- Commits Created (24h counter)
**Metrics Used:**
- `claude_code_claude_code_lines_of_code_count_total`
- `claude_code_claude_code_cost_usage_USD_total`
- `claude_code_claude_code_token_usage_tokens_total`
- `claude_code_claude_code_active_time_seconds_total`
- `claude_code_claude_code_commit_count_total`
**Note:** This dashboard uses the correct double-prefix metric names.
### 2. claude-code-simple.json
**Simplified dashboard for quick overview**
**Panels:**
- Active Sessions
- Total Cost (24h)
- Total Tokens (24h)
- Active Time (24h)
- Cost Over Time
- Token Usage by Type
**Use Case:** Lightweight dashboard for basic monitoring without detailed breakdowns.
## Importing Dashboards
### Method 1: Grafana UI (Recommended)
1. Access Grafana: http://localhost:3000
2. Login with admin/admin
3. Go to: Dashboards → New → Import
4. Click "Upload JSON file"
5. Select the dashboard JSON file
6. Click "Import"
### Method 2: Grafana API
```bash
# Get the datasource UID first
DATASOURCE_UID=$(curl -s -u admin:admin http://localhost:3000/api/datasources | jq -r '.[] | select(.type=="prometheus") | .uid')
# Update dashboard with correct UID
cat claude-code-overview.json | jq --arg uid "$DATASOURCE_UID" '
walk(if type == "object" and .datasource.type == "prometheus" then .datasource.uid = $uid else . end)
' > dashboard-updated.json
# Import dashboard
curl -X POST http://localhost:3000/api/dashboards/db \
-u admin:admin \
-H "Content-Type: application/json" \
-d @dashboard-updated.json
```
## Datasource UID Configuration
**Important:** The dashboards have a hardcoded Prometheus datasource UID: `PBFA97CFB590B2093`
If your Grafana instance has a different UID, you need to replace it:
```bash
# Find your datasource UID
curl -s -u admin:admin http://localhost:3000/api/datasources | jq '.[] | select(.type=="prometheus") | {name, uid}'
# Replace UID in dashboard
YOUR_UID="YOUR_ACTUAL_UID_HERE"
cat claude-code-overview.json | sed "s/PBFA97CFB590B2093/$YOUR_UID/g" > claude-code-overview-fixed.json
# Import the fixed version
```
The skill handles this automatically during Mode 1 setup!
## Customizing Dashboards
### Adding Custom Panels
Use these PromQL queries as templates:
**Total Tokens by Model:**
```promql
sum by (model) (increase(claude_code_claude_code_token_usage_tokens_total[24h]))
```
**Cost per Session:**
```promql
increase(claude_code_claude_code_cost_usage_USD_total[24h])
/
increase(claude_code_claude_code_session_count_total[24h])
```
**Lines of Code per Hour:**
```promql
rate(claude_code_claude_code_lines_of_code_count_total[5m]) * 3600
```
**Average Session Duration:**
```promql
increase(claude_code_claude_code_active_time_seconds_total[24h])
/
increase(claude_code_claude_code_session_count_total[24h])
```
### Time Range Recommendations
- **Real-time monitoring:** Last 15 minutes, 30s refresh
- **Daily review:** Last 24 hours, 1m refresh
- **Weekly analysis:** Last 7 days, 5m refresh
- **Monthly reports:** Last 30 days, 15m refresh
## Troubleshooting
### Dashboard Shows "No Data"
1. **Check data source connection:**
```bash
curl -s http://localhost:3000/api/health | jq .
```
2. **Verify Prometheus has data:**
```bash
curl -s 'http://localhost:9090/api/v1/label/__name__/values' | jq . | grep claude_code
```
3. **Check metric naming:**
- Ensure queries use double prefix: `claude_code_claude_code_*`
- Not single prefix: `claude_code_*`
### Dashboard Shows "Datasource Not Found"
- Your datasource UID doesn't match the dashboard
- Follow the "Datasource UID Configuration" section above
### Panels Show Different Time Ranges
- Set dashboard time range at top-right
- Individual panels inherit from dashboard unless overridden
- Check panel settings: Edit → Query Options → Time Range
## Additional Resources
- **Metric Reference:** See `../data/metrics-reference.md`
- **PromQL Queries:** See `../data/prometheus-queries.md`
- **Grafana Docs:** https://grafana.com/docs/grafana/latest/

View File

@@ -0,0 +1,391 @@
{
"title": "Claude Code - Overview (Working)",
"description": "High-level overview of Claude Code usage, costs, and performance",
"tags": ["claude-code", "overview"],
"timezone": "browser",
"schemaVersion": 42,
"version": 1,
"refresh": "30s",
"panels": [
{
"fieldConfig": {
"defaults": {
"color": {"mode": "thresholds"},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{"color": "green", "value": null},
{"color": "yellow", "value": 10},
{"color": "red", "value": 50}
]
},
"unit": "short"
},
"overrides": []
},
"gridPos": {"h": 4, "w": 6, "x": 0, "y": 0},
"id": 1,
"options": {
"colorMode": "value",
"graphMode": "area",
"justifyMode": "auto",
"orientation": "auto",
"percentChangeColorMode": "standard",
"reduceOptions": {
"calcs": ["lastNotNull"],
"fields": "",
"values": false
},
"showPercentChange": false,
"textMode": "auto",
"wideLayout": true
},
"pluginVersion": "12.2.1",
"targets": [
{
"datasource": {"type": "prometheus", "uid": "PBFA97CFB590B2093"},
"expr": "claude_code_claude_code_lines_of_code_count_total",
"refId": "A"
}
],
"title": "Total Lines of Code",
"type": "stat"
},
{
"fieldConfig": {
"defaults": {
"color": {"mode": "thresholds"},
"decimals": 2,
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{"color": "green", "value": null},
{"color": "yellow", "value": 5},
{"color": "red", "value": 10}
]
},
"unit": "currencyUSD"
},
"overrides": []
},
"gridPos": {"h": 4, "w": 6, "x": 6, "y": 0},
"id": 2,
"options": {
"colorMode": "value",
"graphMode": "area",
"justifyMode": "auto",
"orientation": "auto",
"percentChangeColorMode": "standard",
"reduceOptions": {
"calcs": ["lastNotNull"],
"fields": "",
"values": false
},
"showPercentChange": false,
"textMode": "auto",
"wideLayout": true
},
"pluginVersion": "12.2.1",
"targets": [
{
"datasource": {"type": "prometheus", "uid": "PBFA97CFB590B2093"},
"expr": "increase(claude_code_claude_code_cost_usage_USD_total[24h])",
"refId": "A"
}
],
"title": "Total Cost (24h)",
"type": "stat"
},
{
"fieldConfig": {
"defaults": {
"color": {"mode": "thresholds"},
"decimals": 0,
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [{"color": "green", "value": null}]
},
"unit": "short"
},
"overrides": []
},
"gridPos": {"h": 4, "w": 6, "x": 12, "y": 0},
"id": 3,
"options": {
"colorMode": "value",
"graphMode": "area",
"justifyMode": "auto",
"orientation": "auto",
"percentChangeColorMode": "standard",
"reduceOptions": {
"calcs": ["lastNotNull"],
"fields": "",
"values": false
},
"showPercentChange": false,
"textMode": "auto",
"wideLayout": true
},
"pluginVersion": "12.2.1",
"targets": [
{
"datasource": {"type": "prometheus", "uid": "PBFA97CFB590B2093"},
"expr": "increase(claude_code_claude_code_token_usage_tokens_total[24h])",
"refId": "A"
}
],
"title": "Total Tokens (24h)",
"type": "stat"
},
{
"fieldConfig": {
"defaults": {
"color": {"mode": "palette-classic"},
"decimals": 1,
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [{"color": "green", "value": null}]
},
"unit": "h"
},
"overrides": []
},
"gridPos": {"h": 4, "w": 6, "x": 18, "y": 0},
"id": 4,
"options": {
"colorMode": "value",
"graphMode": "area",
"justifyMode": "auto",
"orientation": "auto",
"percentChangeColorMode": "standard",
"reduceOptions": {
"calcs": ["lastNotNull"],
"fields": "",
"values": false
},
"showPercentChange": false,
"textMode": "auto",
"wideLayout": true
},
"pluginVersion": "12.2.1",
"targets": [
{
"datasource": {"type": "prometheus", "uid": "PBFA97CFB590B2093"},
"expr": "increase(claude_code_claude_code_active_time_seconds_total[24h]) / 3600",
"refId": "A"
}
],
"title": "Active Time (24h)",
"type": "stat"
},
{
"fieldConfig": {
"defaults": {
"color": {"mode": "palette-classic"},
"custom": {
"axisBorderShow": false,
"axisCenteredZero": false,
"axisColorMode": "text",
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"barWidthFactor": 0.6,
"drawStyle": "line",
"fillOpacity": 10,
"gradientMode": "none",
"hideFrom": {"legend": false, "tooltip": false, "viz": false},
"insertNulls": false,
"lineInterpolation": "linear",
"lineWidth": 2,
"pointSize": 5,
"scaleDistribution": {"type": "linear"},
"showPoints": "auto",
"spanNulls": false,
"stacking": {"group": "A", "mode": "none"},
"thresholdsStyle": {"mode": "off"}
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [{"color": "green", "value": null}]
},
"unit": "currencyUSD"
},
"overrides": []
},
"gridPos": {"h": 8, "w": 12, "x": 0, "y": 4},
"id": 5,
"options": {
"legend": {"calcs": [], "displayMode": "list", "placement": "bottom", "showLegend": true},
"tooltip": {"mode": "single", "sort": "none"}
},
"pluginVersion": "12.2.1",
"targets": [
{
"datasource": {"type": "prometheus", "uid": "PBFA97CFB590B2093"},
"expr": "rate(claude_code_claude_code_cost_usage_USD_total[5m]) * 3600",
"legendFormat": "Cost per hour",
"refId": "A"
}
],
"title": "Cost Over Time (per hour)",
"type": "timeseries"
},
{
"fieldConfig": {
"defaults": {
"color": {"mode": "palette-classic"},
"custom": {
"axisBorderShow": false,
"axisCenteredZero": false,
"axisColorMode": "text",
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"barWidthFactor": 0.6,
"drawStyle": "line",
"fillOpacity": 20,
"gradientMode": "none",
"hideFrom": {"legend": false, "tooltip": false, "viz": false},
"insertNulls": false,
"lineInterpolation": "linear",
"lineWidth": 2,
"pointSize": 5,
"scaleDistribution": {"type": "linear"},
"showPoints": "auto",
"spanNulls": false,
"stacking": {"group": "A", "mode": "normal"},
"thresholdsStyle": {"mode": "off"}
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [{"color": "green", "value": null}]
},
"unit": "short"
},
"overrides": []
},
"gridPos": {"h": 8, "w": 12, "x": 12, "y": 4},
"id": 6,
"options": {
"legend": {"calcs": [], "displayMode": "list", "placement": "bottom", "showLegend": true},
"tooltip": {"mode": "single", "sort": "none"}
},
"pluginVersion": "12.2.1",
"targets": [
{
"datasource": {"type": "prometheus", "uid": "PBFA97CFB590B2093"},
"expr": "sum by (type) (rate(claude_code_claude_code_token_usage_tokens_total[5m]) * 60)",
"legendFormat": "{{type}}",
"refId": "A"
}
],
"title": "Token Usage by Type",
"type": "timeseries"
},
{
"fieldConfig": {
"defaults": {
"color": {"mode": "palette-classic"},
"custom": {
"axisBorderShow": false,
"axisCenteredZero": false,
"axisColorMode": "text",
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"barWidthFactor": 0.6,
"drawStyle": "bars",
"fillOpacity": 80,
"gradientMode": "none",
"hideFrom": {"legend": false, "tooltip": false, "viz": false},
"insertNulls": false,
"lineInterpolation": "linear",
"lineWidth": 1,
"pointSize": 5,
"scaleDistribution": {"type": "linear"},
"showPoints": "never",
"spanNulls": false,
"stacking": {"group": "A", "mode": "none"},
"thresholdsStyle": {"mode": "off"}
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [{"color": "green", "value": null}]
},
"unit": "short"
},
"overrides": []
},
"gridPos": {"h": 8, "w": 12, "x": 0, "y": 12},
"id": 7,
"options": {
"legend": {"calcs": [], "displayMode": "list", "placement": "bottom", "showLegend": true},
"tooltip": {"mode": "single", "sort": "none"}
},
"pluginVersion": "12.2.1",
"targets": [
{
"datasource": {"type": "prometheus", "uid": "PBFA97CFB590B2093"},
"expr": "sum by (type) (rate(claude_code_claude_code_lines_of_code_count_total[5m]) * 60)",
"legendFormat": "{{type}}",
"refId": "A"
}
],
"title": "Lines of Code Modified",
"type": "timeseries"
},
{
"fieldConfig": {
"defaults": {
"color": {"mode": "palette-classic"},
"decimals": 0,
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [{"color": "green", "value": null}]
},
"unit": "short"
},
"overrides": []
},
"gridPos": {"h": 6, "w": 12, "x": 12, "y": 12},
"id": 10,
"options": {
"colorMode": "value",
"graphMode": "area",
"justifyMode": "auto",
"orientation": "auto",
"percentChangeColorMode": "standard",
"reduceOptions": {
"calcs": ["lastNotNull"],
"fields": "",
"values": false
},
"showPercentChange": false,
"textMode": "auto",
"wideLayout": true
},
"pluginVersion": "12.2.1",
"targets": [
{
"datasource": {"type": "prometheus", "uid": "PBFA97CFB590B2093"},
"expr": "increase(claude_code_claude_code_commit_count_total[24h])",
"refId": "A"
}
],
"title": "Commits Created (24h)",
"type": "stat"
}
],
"time": {"from": "now-6h", "to": "now"},
"timepicker": {},
"timezone": "browser",
"version": 1
}

View File

@@ -0,0 +1,179 @@
{
"title": "Claude Code - Overview",
"description": "High-level overview of Claude Code usage, costs, and performance",
"tags": ["claude-code", "overview"],
"timezone": "browser",
"schemaVersion": 38,
"version": 1,
"refresh": "30s",
"panels": [
{
"id": 1,
"gridPos": { "h": 4, "w": 6, "x": 0, "y": 0 },
"type": "stat",
"title": "Active Sessions",
"targets": [
{
"expr": "sum(claude_code_session_count_total)",
"refId": "A"
}
],
"fieldConfig": {
"defaults": {
"unit": "short",
"color": { "mode": "thresholds" },
"thresholds": {
"mode": "absolute",
"steps": [
{ "value": null, "color": "green" }
]
}
}
},
"options": {
"reduceOptions": {
"values": false,
"calcs": ["lastNotNull"]
}
}
},
{
"id": 2,
"gridPos": { "h": 4, "w": 6, "x": 6, "y": 0 },
"type": "stat",
"title": "Total Cost (24h)",
"targets": [
{
"expr": "sum(increase(claude_code_cost_usage_total[24h]))",
"refId": "A"
}
],
"fieldConfig": {
"defaults": {
"unit": "currencyUSD",
"decimals": 2,
"color": { "mode": "thresholds" },
"thresholds": {
"mode": "absolute",
"steps": [
{ "value": null, "color": "green" },
{ "value": 5, "color": "yellow" },
{ "value": 10, "color": "red" }
]
}
}
},
"options": {
"reduceOptions": {
"values": false,
"calcs": ["lastNotNull"]
}
}
},
{
"id": 3,
"gridPos": { "h": 4, "w": 6, "x": 12, "y": 0 },
"type": "stat",
"title": "Total Tokens (24h)",
"targets": [
{
"expr": "sum(increase(claude_code_token_usage_total[24h]))",
"refId": "A"
}
],
"fieldConfig": {
"defaults": {
"unit": "short",
"decimals": 0,
"color": { "mode": "thresholds" },
"thresholds": {
"mode": "absolute",
"steps": [
{ "value": null, "color": "green" }
]
}
}
},
"options": {
"reduceOptions": {
"values": false,
"calcs": ["lastNotNull"]
}
}
},
{
"id": 4,
"gridPos": { "h": 4, "w": 6, "x": 18, "y": 0 },
"type": "stat",
"title": "Active Time (24h)",
"targets": [
{
"expr": "sum(increase(claude_code_active_time_total_seconds[24h])) / 3600",
"refId": "A"
}
],
"fieldConfig": {
"defaults": {
"unit": "h",
"decimals": 1,
"color": { "mode": "palette-classic" }
}
},
"options": {
"reduceOptions": {
"values": false,
"calcs": ["lastNotNull"]
}
}
},
{
"id": 5,
"gridPos": { "h": 8, "w": 12, "x": 0, "y": 4 },
"type": "timeseries",
"title": "Cost Over Time (per hour)",
"targets": [
{
"expr": "sum(rate(claude_code_cost_usage_total[5m])) * 3600",
"legendFormat": "Cost per hour",
"refId": "A"
}
],
"fieldConfig": {
"defaults": {
"unit": "currencyUSD",
"custom": {
"drawStyle": "line",
"lineWidth": 2,
"fillOpacity": 10,
"showPoints": "auto"
}
}
}
},
{
"id": 6,
"gridPos": { "h": 8, "w": 12, "x": 12, "y": 4 },
"type": "timeseries",
"title": "Token Usage by Type",
"targets": [
{
"expr": "sum by (type) (rate(claude_code_token_usage_total[5m]) * 60)",
"legendFormat": "{{type}}",
"refId": "A"
}
],
"fieldConfig": {
"defaults": {
"unit": "short",
"custom": {
"drawStyle": "line",
"lineWidth": 2,
"fillOpacity": 20,
"showPoints": "auto",
"stacking": { "mode": "normal" }
}
}
}
}
]
}