|
23 | 23 | from prometheus_client import Counter, Gauge, Histogram, Info |
24 | 24 | from .estimate import get_server_price |
25 | 25 | from .constants import standby_server_name_prefix |
| 26 | +from .constants import recycle_server_name_prefix |
26 | 27 | from .server import get_runner_server_name |
27 | 28 |
|
28 | 29 |
|
|
257 | 258 | ], |
258 | 259 | ) |
259 | 260 |
|
| 261 | +# Recycled servers metrics |
| 262 | +RECYCLED_SERVERS_TOTAL = Gauge( |
| 263 | + "github_hetzner_runners_recycled_servers_total", |
| 264 | + "Total number of recycled servers", |
| 265 | + ["server_type", "location"], |
| 266 | +) |
| 267 | + |
| 268 | +RECYCLED_SERVERS_TOTAL_COUNT = Gauge( |
| 269 | + "github_hetzner_runners_recycled_servers_total_count", |
| 270 | + "Total number of recycled servers across all types and locations", |
| 271 | +) |
| 272 | + |
| 273 | +RECYCLED_SERVER_INFO = Gauge( |
| 274 | + "github_hetzner_runners_recycled_server", |
| 275 | + "Recycled server information including age in seconds", |
| 276 | + ["server_id", "server_name", "server_type", "location", "status", "created"], |
| 277 | +) |
| 278 | + |
260 | 279 | # Runner pool metrics |
261 | 280 | RUNNER_POOL_STATUS = Gauge( |
262 | 281 | "github_hetzner_runners_pool_status", |
@@ -1201,6 +1220,55 @@ def update_unused_runners(unused_runners_dict): |
1201 | 1220 | ) |
1202 | 1221 |
|
1203 | 1222 |
|
| 1223 | +def update_recycled_servers(servers): |
| 1224 | + """Update recycled server metrics. |
| 1225 | +
|
| 1226 | + Args: |
| 1227 | + servers: List of server objects to check for recycled servers |
| 1228 | + """ |
| 1229 | + # Clear existing recycled server metrics |
| 1230 | + RECYCLED_SERVERS_TOTAL._metrics.clear() |
| 1231 | + RECYCLED_SERVER_INFO._metrics.clear() |
| 1232 | + RECYCLED_SERVERS_TOTAL_COUNT.set(0) |
| 1233 | + |
| 1234 | + total_recycled_servers = 0 |
| 1235 | + recycled_counts = {} |
| 1236 | + current_time = time.time() |
| 1237 | + |
| 1238 | + for server in servers: |
| 1239 | + # Check if this is a recycled server by name prefix |
| 1240 | + if server.name.startswith(recycle_server_name_prefix): |
| 1241 | + server_type = server.server_type.name |
| 1242 | + location = server.datacenter.location.name |
| 1243 | + key = (server_type, location) |
| 1244 | + |
| 1245 | + # Count by type and location |
| 1246 | + recycled_counts[key] = recycled_counts.get(key, 0) + 1 |
| 1247 | + total_recycled_servers += 1 |
| 1248 | + |
| 1249 | + # Track recycled server age |
| 1250 | + server_age = ( |
| 1251 | + current_time - server.created.timestamp() if server.created else 0 |
| 1252 | + ) |
| 1253 | + RECYCLED_SERVER_INFO.labels( |
| 1254 | + server_id=str(server.id), |
| 1255 | + server_name=server.name, |
| 1256 | + server_type=server_type, |
| 1257 | + location=location, |
| 1258 | + status=server.status, |
| 1259 | + created=server.created.isoformat() if server.created else "", |
| 1260 | + ).set(server_age) |
| 1261 | + |
| 1262 | + # Set total count |
| 1263 | + RECYCLED_SERVERS_TOTAL_COUNT.set(total_recycled_servers) |
| 1264 | + |
| 1265 | + # Set counts by type and location |
| 1266 | + for (server_type, location), count in recycled_counts.items(): |
| 1267 | + RECYCLED_SERVERS_TOTAL.labels(server_type=server_type, location=location).set( |
| 1268 | + count |
| 1269 | + ) |
| 1270 | + |
| 1271 | + |
1204 | 1272 | def record_server_creation(server_type: str, location: str, creation_time: float): |
1205 | 1273 | """Record metrics for a server creation. |
1206 | 1274 |
|
|
0 commit comments