Skip to content

Commit f999c1d

Browse files
authored
Merge pull request #728 from aws-samples/riv25-eks-http-metrics-dashboard
Added HTTP Metrics dashboard
2 parents 4612050 + 565ec80 commit f999c1d

File tree

1 file changed

+204
-0
lines changed

1 file changed

+204
-0
lines changed
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
log() {
6+
echo "[$(date +'%H:%M:%S')] $*"
7+
}
8+
9+
NAMESPACE="monitoring"
10+
GRAFANA_USER="admin"
11+
SECRET_NAME="unicornstore-ide-password-lambda"
12+
FOLDER_NAME="JVM Metrics"
13+
DASHBOARD_TITLE="HTTP Metrics"
14+
15+
AWS_REGION=${AWS_REGION:-$(aws configure get region)}
16+
if [[ -z "$AWS_REGION" ]]; then
17+
AWS_REGION=$(curl -s http://169.254.169.254/latest/meta-data/placement/region 2>/dev/null || echo "us-east-1")
18+
fi
19+
20+
# Setup Grafana monitoring
21+
SECRET_VALUE=$(aws secretsmanager get-secret-value --secret-id "$SECRET_NAME" --query 'SecretString' --output text)
22+
GRAFANA_PASSWORD=$(echo "$SECRET_VALUE" | jq -r '.password')
23+
24+
GRAFANA_LB=$(kubectl get svc grafana -n "$NAMESPACE" -o jsonpath="{.status.loadBalancer.ingress[0].hostname}" 2>/dev/null || echo "")
25+
if [[ -z "$GRAFANA_LB" ]]; then
26+
log "❌ Grafana LoadBalancer not found. Run monitoring.sh first."
27+
exit 1
28+
fi
29+
30+
GRAFANA_URL="http://$GRAFANA_LB"
31+
32+
log "⏳ Waiting for Grafana..."
33+
for i in {1..20}; do
34+
STATUS=$(curl -s -u "$GRAFANA_USER:$GRAFANA_PASSWORD" "$GRAFANA_URL/api/health" | jq -r .database 2>/dev/null || true)
35+
if [[ "$STATUS" == "ok" ]]; then
36+
break
37+
fi
38+
sleep 5
39+
done
40+
41+
log "📁 Creating folder '$FOLDER_NAME'..."
42+
FOLDER_RESPONSE=$(curl -s -X POST -H "Content-Type: application/json" \
43+
-u "$GRAFANA_USER:$GRAFANA_PASSWORD" \
44+
-d "{\"title\": \"$FOLDER_NAME\"}" \
45+
"$GRAFANA_URL/api/folders")
46+
47+
FOLDER_UID=$(echo "$FOLDER_RESPONSE" | jq -r '.uid // empty')
48+
FOLDER_ID=$(echo "$FOLDER_RESPONSE" | jq -r '.id // empty')
49+
if [[ -z "$FOLDER_UID" ]]; then
50+
# Try to get existing folder
51+
EXISTING_FOLDER=$(curl -s -u "$GRAFANA_USER:$GRAFANA_PASSWORD" "$GRAFANA_URL/api/folders" | jq -r ".[] | select(.title == \"$FOLDER_NAME\")")
52+
if [[ -n "$EXISTING_FOLDER" ]]; then
53+
FOLDER_UID=$(echo "$EXISTING_FOLDER" | jq -r '.uid')
54+
FOLDER_ID=$(echo "$EXISTING_FOLDER" | jq -r '.id')
55+
log "📁 Using existing folder: $FOLDER_UID"
56+
else
57+
FOLDER_UID=""
58+
FOLDER_ID=0
59+
log "⚠️ Using General folder"
60+
fi
61+
else
62+
log "✅ Folder created: $FOLDER_UID"
63+
fi
64+
65+
log "📊 Creating HTTP metrics dashboard..."
66+
cat > /tmp/dashboard.json <<EOF
67+
{
68+
"title": "$DASHBOARD_TITLE",
69+
"uid": "http-metrics-dashboard",
70+
"version": 1,
71+
"weekStart": "",
72+
"editable": true,
73+
"fiscalYearStartMonth": 0,
74+
"graphTooltip": 0,
75+
"id": null,
76+
"timepicker": {},
77+
"timezone": "browser",
78+
"links": [],
79+
"panels": [
80+
{
81+
"datasource": {
82+
"type": "prometheus",
83+
"uid": "Prometheus"
84+
},
85+
"fieldConfig": {
86+
"defaults": {
87+
"color": {
88+
"mode": "palette-classic"
89+
},
90+
"custom": {
91+
"axisBorderShow": false,
92+
"axisCenteredZero": false,
93+
"axisColorMode": "text",
94+
"axisLabel": "",
95+
"axisPlacement": "auto",
96+
"barAlignment": 0,
97+
"drawStyle": "line",
98+
"fillOpacity": 0,
99+
"gradientMode": "none",
100+
"hideFrom": {
101+
"legend": false,
102+
"tooltip": false,
103+
"viz": false
104+
},
105+
"insertNulls": false,
106+
"lineInterpolation": "linear",
107+
"lineWidth": 1,
108+
"pointSize": 5,
109+
"scaleDistribution": {
110+
"type": "linear"
111+
},
112+
"showPoints": "auto",
113+
"spanNulls": false,
114+
"stacking": {
115+
"group": "A",
116+
"mode": "none"
117+
},
118+
"thresholdsStyle": {
119+
"mode": "off"
120+
}
121+
},
122+
"mappings": [],
123+
"thresholds": {
124+
"mode": "absolute",
125+
"steps": [
126+
{
127+
"color": "green",
128+
"value": null
129+
},
130+
{
131+
"color": "red",
132+
"value": 80
133+
}
134+
]
135+
},
136+
"unit": "reqps"
137+
},
138+
"overrides": []
139+
},
140+
"gridPos": {
141+
"h": 8,
142+
"w": 24,
143+
"x": 0,
144+
"y": 0
145+
},
146+
"id": 1,
147+
"options": {
148+
"legend": {
149+
"calcs": [],
150+
"displayMode": "list",
151+
"placement": "bottom",
152+
"showLegend": true
153+
},
154+
"tooltip": {
155+
"mode": "single",
156+
"sort": "none"
157+
}
158+
},
159+
"targets": [
160+
{
161+
"datasource": {
162+
"type": "prometheus",
163+
"uid": "Prometheus"
164+
},
165+
"disableTextWrap": false,
166+
"editorMode": "code",
167+
"expr": "rate(http_server_requests_seconds_count{method=\"POST\"}[30s])",
168+
"fullMetaSearch": false,
169+
"includeNullMetadata": true,
170+
"instant": false,
171+
"legendFormat": "POST {{uri}} - {{status}}",
172+
"range": true,
173+
"refId": "A",
174+
"useBackend": false
175+
}
176+
],
177+
"title": "HTTP POST Request Rate",
178+
"type": "timeseries"
179+
}
180+
],
181+
"refresh": "5s",
182+
"schemaVersion": 39,
183+
"tags": ["http", "metrics"],
184+
"templating": {
185+
"list": []
186+
},
187+
"time": {
188+
"from": "now-5m",
189+
"to": "now"
190+
}
191+
}
192+
EOF
193+
194+
DASHBOARD_RESPONSE=$(curl -s -X POST -H "Content-Type: application/json" \
195+
-u "$GRAFANA_USER:$GRAFANA_PASSWORD" \
196+
-d "{\"dashboard\": $(cat /tmp/dashboard.json), \"overwrite\": true, \"folderId\": $FOLDER_ID}" \
197+
"$GRAFANA_URL/api/dashboards/db")
198+
199+
DASHBOARD_UID=$(echo "$DASHBOARD_RESPONSE" | jq -r '.uid')
200+
rm /tmp/dashboard.json
201+
202+
log "✅ HTTP metrics dashboard created: $DASHBOARD_UID"
203+
204+
log "📊 Dashboard shows HTTP requests metrics"

0 commit comments

Comments
 (0)