|
| 1 | +# Grafana Queries for go-redis OTel Metrics |
| 2 | + |
| 3 | +This document provides ready-to-use Grafana queries for visualizing go-redis OpenTelemetry metrics. |
| 4 | + |
| 5 | +## Connection Creation Time Metrics |
| 6 | + |
| 7 | +### Metric Name |
| 8 | +`db.client.connection.create_time` |
| 9 | + |
| 10 | +### Available Attributes |
| 11 | +- `db.system` - Always "redis" |
| 12 | +- `server.address` - Redis server address |
| 13 | +- `server.port` - Redis server port (if not default 6379) |
| 14 | +- `redis.client.library` - Client library name and version (e.g., "go-redis:9.16.0-beta.1") |
| 15 | +- `db.client.connection.pool.name` - Unique pool identifier |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +## Query Examples |
| 20 | + |
| 21 | +### 1. Average Connection Creation Time |
| 22 | + |
| 23 | +**Use Case**: Monitor average time to create connections over time |
| 24 | + |
| 25 | +**PromQL**: |
| 26 | +```promql |
| 27 | +rate(db_client_connection_create_time_sum{db_system="redis"}[5m]) |
| 28 | +/ |
| 29 | +rate(db_client_connection_create_time_count{db_system="redis"}[5m]) |
| 30 | +``` |
| 31 | + |
| 32 | +**Panel Settings**: |
| 33 | +- Type: Time series |
| 34 | +- Unit: seconds (s) |
| 35 | +- Legend: `{{server_address}} - {{db_client_connection_pool_name}}` |
| 36 | + |
| 37 | +--- |
| 38 | + |
| 39 | +### 2. Connection Creation Time Percentiles |
| 40 | + |
| 41 | +**Use Case**: Understand latency distribution (P50, P95, P99) |
| 42 | + |
| 43 | +**PromQL (P50)**: |
| 44 | +```promql |
| 45 | +histogram_quantile(0.50, |
| 46 | + rate(db_client_connection_create_time_bucket{db_system="redis"}[5m]) |
| 47 | +) |
| 48 | +``` |
| 49 | + |
| 50 | +**PromQL (P95)**: |
| 51 | +```promql |
| 52 | +histogram_quantile(0.95, |
| 53 | + rate(db_client_connection_create_time_bucket{db_system="redis"}[5m]) |
| 54 | +) |
| 55 | +``` |
| 56 | + |
| 57 | +**PromQL (P99)**: |
| 58 | +```promql |
| 59 | +histogram_quantile(0.99, |
| 60 | + rate(db_client_connection_create_time_bucket{db_system="redis"}[5m]) |
| 61 | +) |
| 62 | +``` |
| 63 | + |
| 64 | +**Panel Settings**: |
| 65 | +- Type: Time series |
| 66 | +- Unit: seconds (s) |
| 67 | +- Multiple queries in one panel |
| 68 | +- Color coding: P50 (green), P95 (yellow), P99 (red) |
| 69 | + |
| 70 | +--- |
| 71 | + |
| 72 | +### 3. Connection Creation Rate |
| 73 | + |
| 74 | +**Use Case**: Monitor how frequently new connections are being created |
| 75 | + |
| 76 | +**PromQL**: |
| 77 | +```promql |
| 78 | +rate(db_client_connection_create_time_count{db_system="redis"}[5m]) |
| 79 | +``` |
| 80 | + |
| 81 | +**Panel Settings**: |
| 82 | +- Type: Time series |
| 83 | +- Unit: connections per second (cps) |
| 84 | +- Legend: `{{server_address}}` |
| 85 | + |
| 86 | +--- |
| 87 | + |
| 88 | +### 4. Total Connections Created |
| 89 | + |
| 90 | +**Use Case**: See total number of connections created since start |
| 91 | + |
| 92 | +**PromQL**: |
| 93 | +```promql |
| 94 | +sum(db_client_connection_create_time_count{db_system="redis"}) |
| 95 | +``` |
| 96 | + |
| 97 | +**Panel Settings**: |
| 98 | +- Type: Stat |
| 99 | +- Unit: short |
| 100 | +- Graph mode: Area |
| 101 | +- Thresholds: 0 (green), 1000 (yellow), 10000 (red) |
| 102 | + |
| 103 | +--- |
| 104 | + |
| 105 | +### 5. Connection Creation Time Heatmap |
| 106 | + |
| 107 | +**Use Case**: Visualize distribution of connection creation times |
| 108 | + |
| 109 | +**PromQL**: |
| 110 | +```promql |
| 111 | +sum(rate(db_client_connection_create_time_bucket{db_system="redis"}[5m])) by (le) |
| 112 | +``` |
| 113 | + |
| 114 | +**Panel Settings**: |
| 115 | +- Type: Heatmap |
| 116 | +- Format: Heatmap |
| 117 | +- Color scheme: Spectral |
| 118 | + |
| 119 | +--- |
| 120 | + |
| 121 | +### 6. Slow Connection Creations Alert |
| 122 | + |
| 123 | +**Use Case**: Identify pools with slow connection creation (P99 > 100ms) |
| 124 | + |
| 125 | +**PromQL**: |
| 126 | +```promql |
| 127 | +histogram_quantile(0.99, |
| 128 | + rate(db_client_connection_create_time_bucket{db_system="redis"}[5m]) |
| 129 | +) > 0.1 |
| 130 | +``` |
| 131 | + |
| 132 | +**Panel Settings**: |
| 133 | +- Type: Table |
| 134 | +- Format: Table |
| 135 | +- Instant query: true |
| 136 | +- Columns: server_address, db_client_connection_pool_name, redis_client_library, Value |
| 137 | + |
| 138 | +--- |
| 139 | + |
| 140 | +### 7. Connection Creation Time by Pool |
| 141 | + |
| 142 | +**Use Case**: Compare connection creation time across different pools |
| 143 | + |
| 144 | +**PromQL**: |
| 145 | +```promql |
| 146 | +avg by (db_client_connection_pool_name) ( |
| 147 | + rate(db_client_connection_create_time_sum{db_system="redis"}[5m]) |
| 148 | + / |
| 149 | + rate(db_client_connection_create_time_count{db_system="redis"}[5m]) |
| 150 | +) |
| 151 | +``` |
| 152 | + |
| 153 | +**Panel Settings**: |
| 154 | +- Type: Bar chart or Time series |
| 155 | +- Unit: seconds (s) |
| 156 | +- Legend: `{{db_client_connection_pool_name}}` |
| 157 | + |
| 158 | +--- |
| 159 | + |
| 160 | +## Filtering Examples |
| 161 | + |
| 162 | +### Filter by Server Address |
| 163 | +```promql |
| 164 | +rate(db_client_connection_create_time_sum{ |
| 165 | + db_system="redis", |
| 166 | + server_address="localhost" |
| 167 | +}[5m]) |
| 168 | +``` |
| 169 | + |
| 170 | +### Filter by Pool Name |
| 171 | +```promql |
| 172 | +rate(db_client_connection_create_time_sum{ |
| 173 | + db_system="redis", |
| 174 | + db_client_connection_pool_name="localhost:6379" |
| 175 | +}[5m]) |
| 176 | +``` |
| 177 | + |
| 178 | +### Filter by Client Library Version |
| 179 | +```promql |
| 180 | +rate(db_client_connection_create_time_sum{ |
| 181 | + db_system="redis", |
| 182 | + redis_client_library=~"go-redis:9.*" |
| 183 | +}[5m]) |
| 184 | +``` |
| 185 | + |
| 186 | +--- |
| 187 | + |
| 188 | +## Alert Rules |
| 189 | + |
| 190 | +### Alert: High Connection Creation Latency |
| 191 | + |
| 192 | +**Condition**: P99 connection creation time > 500ms for 5 minutes |
| 193 | + |
| 194 | +**PromQL**: |
| 195 | +```promql |
| 196 | +histogram_quantile(0.99, |
| 197 | + rate(db_client_connection_create_time_bucket{db_system="redis"}[5m]) |
| 198 | +) > 0.5 |
| 199 | +``` |
| 200 | + |
| 201 | +**Alert Rule**: |
| 202 | +```yaml |
| 203 | +- alert: HighRedisConnectionCreationLatency |
| 204 | + expr: | |
| 205 | + histogram_quantile(0.99, |
| 206 | + rate(db_client_connection_create_time_bucket{db_system="redis"}[5m]) |
| 207 | + ) > 0.5 |
| 208 | + for: 5m |
| 209 | + labels: |
| 210 | + severity: warning |
| 211 | + annotations: |
| 212 | + summary: "High Redis connection creation latency" |
| 213 | + description: "P99 connection creation time is {{ $value }}s for pool {{ $labels.db_client_connection_pool_name }}" |
| 214 | +``` |
| 215 | +
|
| 216 | +--- |
| 217 | +
|
| 218 | +### Alert: High Connection Creation Rate |
| 219 | +
|
| 220 | +**Condition**: Creating more than 10 connections/second for 5 minutes |
| 221 | +
|
| 222 | +**PromQL**: |
| 223 | +```promql |
| 224 | +rate(db_client_connection_create_time_count{db_system="redis"}[5m]) > 10 |
| 225 | +``` |
| 226 | + |
| 227 | +**Alert Rule**: |
| 228 | +```yaml |
| 229 | +- alert: HighRedisConnectionCreationRate |
| 230 | + expr: | |
| 231 | + rate(db_client_connection_create_time_count{db_system="redis"}[5m]) > 10 |
| 232 | + for: 5m |
| 233 | + labels: |
| 234 | + severity: warning |
| 235 | + annotations: |
| 236 | + summary: "High Redis connection creation rate" |
| 237 | + description: "Creating {{ $value }} connections/sec for pool {{ $labels.db_client_connection_pool_name }}" |
| 238 | +``` |
| 239 | +
|
| 240 | +--- |
| 241 | +
|
| 242 | +## Dashboard Import |
| 243 | +
|
| 244 | +To import the complete dashboard: |
| 245 | +
|
| 246 | +1. Copy the contents of `grafana-dashboard-connection-create-time.json` |
| 247 | +2. In Grafana, go to **Dashboards** → **Import** |
| 248 | +3. Paste the JSON |
| 249 | +4. Select your Prometheus data source |
| 250 | +5. Click **Import** |
| 251 | + |
| 252 | +--- |
| 253 | + |
| 254 | +## Troubleshooting |
| 255 | + |
| 256 | +### No data showing up? |
| 257 | + |
| 258 | +1. **Check if metrics are enabled**: |
| 259 | + ```go |
| 260 | + redisotel.Init(rdb) |
| 261 | + ``` |
| 262 | + |
| 263 | +2. **Verify Prometheus is scraping**: |
| 264 | + ```promql |
| 265 | + db_client_connection_create_time_count |
| 266 | + ``` |
| 267 | + |
| 268 | +3. **Check time range**: Metrics only appear after connections are created |
| 269 | + |
| 270 | +4. **Verify labels**: Use Prometheus query browser to see available labels: |
| 271 | + ```promql |
| 272 | + db_client_connection_create_time_count{db_system="redis"} |
| 273 | + ``` |
| 274 | + |
| 275 | +### Histogram buckets not showing? |
| 276 | + |
| 277 | +Make sure your Prometheus is configured to scrape histogram buckets: |
| 278 | +```yaml |
| 279 | +# prometheus.yml |
| 280 | +scrape_configs: |
| 281 | + - job_name: 'my-app' |
| 282 | + static_configs: |
| 283 | + - targets: ['localhost:8080'] |
| 284 | +``` |
| 285 | + |
| 286 | +--- |
| 287 | + |
| 288 | +## Next Steps |
| 289 | + |
| 290 | +- See `connection_create_time_test.go` for testing examples |
| 291 | +- Check `README.md` for full instrumentation guide |
| 292 | +- Explore other connection metrics (coming soon): |
| 293 | + - `db.client.connection.count` |
| 294 | + - `db.client.connection.wait_time` |
| 295 | + - `db.client.connection.use_time` |
| 296 | + |
0 commit comments