forked from XStreamRollz/XStreamRoll
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics.ts
More file actions
241 lines (212 loc) · 8.1 KB
/
Copy pathmetrics.ts
File metadata and controls
241 lines (212 loc) · 8.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import { createServer, IncomingMessage, ServerResponse } from "http"
import { DeadLetterStore } from "./dead-letter-store"
export interface Metrics {
messagesProcessed: number
errors: number
queueDepth: number
uptimeSeconds: number
// Lock-related metrics (issue #338)
lockAcquisitionsTotal: number
lockAcquisitionsDenied: number
lockRenewalsTotal: number
lockRenewalsFailed: number
lockReleasesTotal: number
lockReleasesFailed: number
activeLocks: number
concurrentRouteDedupes: number
}
const startTime = Date.now()
const counters = {
messagesProcessed: 0,
errors: 0,
queueDepth: 0,
// Lock-related metrics (issue #338)
lockAcquisitionsTotal: 0,
lockAcquisitionsDenied: 0,
lockRenewalsTotal: 0,
lockRenewalsFailed: 0,
lockReleasesTotal: 0,
lockReleasesFailed: 0,
activeLocks: 0,
concurrentRouteDedupes: 0,
}
export function incrementProcessed(): void {
counters.messagesProcessed++
}
export function incrementErrors(): void {
counters.errors++
}
export function setQueueDepth(depth: number): void {
counters.queueDepth = depth
}
// Lock-related metrics (issue #338)
export function incrementLockAcquisitions(): void {
counters.lockAcquisitionsTotal++
counters.activeLocks++
}
export function incrementLockAcquisitionsDenied(): void {
counters.lockAcquisitionsDenied++
}
export function incrementLockRenewals(): void {
counters.lockRenewalsTotal++
}
export function incrementLockRenewalsFailed(): void {
counters.lockRenewalsFailed++
counters.activeLocks--
}
export function incrementLockReleases(): void {
counters.lockReleasesTotal++
counters.activeLocks--
}
export function incrementLockReleasesFailed(): void {
counters.lockReleasesFailed++
}
export function incrementConcurrentRouteDedupes(): void {
counters.concurrentRouteDedupes++
}
export function setActiveLocks(count: number): void {
counters.activeLocks = count
}
export function getMetrics(): Metrics {
return {
...counters,
uptimeSeconds: Math.floor((Date.now() - startTime) / 1000),
}
}
/**
* State that the metrics server exposes to its /healthz endpoint. The
* worker toggles this from its top-level `shuttingDown` flag so that
* Kubernetes can drain traffic before SIGTERM completes.
*/
let live = true
/** Mark the server as no-longer-ready. Called by the worker on shutdown. */
export function markShuttingDown(): void {
live = false
}
/** Mark the server as ready again. Exposed for tests. */
export function markReady(): void {
live = true
}
export function startMetricsServer(
port = 3002,
deadLetterStore?: DeadLetterStore,
): ReturnType<typeof createServer> {
const server = createServer((req: IncomingMessage, res: ServerResponse) => {
const url = req.url || ""
const accept = (req.headers.accept || "").toLowerCase()
if (req.method === "GET") {
// Pure liveness probe — always 200 if the process is alive.
// We deliberately do not consult the `live` flag here so that
// Kubernetes does not restart the pod during a graceful drain.
if (url === "/livez") {
const body = JSON.stringify({
status: "ok",
timestamp: new Date().toISOString(),
})
res.writeHead(200, { "Content-Type": "application/json" })
res.end(body)
return
}
// Readiness probe — returns 503 once shutdown begins so that the
// pod is removed from any service endpoints before the loop dies.
if (url === "/healthz" || url === "/health") {
if (!live) {
res.writeHead(503, { "Content-Type": "application/json" })
res.end(JSON.stringify({ status: "shutting-down" }))
return
}
const body = JSON.stringify({
status: "ok",
timestamp: new Date().toISOString(),
})
res.writeHead(200, { "Content-Type": "application/json" })
res.end(body)
return
}
if (url === "/metrics/json") {
const body = JSON.stringify(getMetrics())
res.writeHead(200, { "Content-Type": "application/json" })
res.end(body)
return
}
if (url === "/dead-letters" || url === "/dead-letters/") {
if (!deadLetterStore) {
res.writeHead(503, { "Content-Type": "application/json" })
res.end(JSON.stringify({ error: "dead-letter store unavailable" }))
return
}
void deadLetterStore.list().then(
(records) => {
res.writeHead(200, { "Content-Type": "application/json" })
res.end(JSON.stringify(records))
},
() => {
res.writeHead(500, { "Content-Type": "application/json" })
res.end(JSON.stringify({ error: "dead-letter store unavailable" }))
},
)
return
}
if (url === "/metrics" || url === "/metrics/" || url === "/metrics/prometheus") {
const wantsJson =
accept.includes("application/json") &&
!accept.includes("text/plain") &&
!accept.includes("*/*")
if (wantsJson) {
const body = JSON.stringify(getMetrics())
res.writeHead(200, { "Content-Type": "application/json" })
res.end(body)
} else {
const m = getMetrics()
const body = [
`# HELP xstreamroll_messages_processed_total Total messages processed by the worker`,
`# TYPE xstreamroll_messages_processed_total counter`,
`xstreamroll_messages_processed_total ${m.messagesProcessed}`,
`# HELP xstreamroll_errors_total Total errors encountered by the worker`,
`# TYPE xstreamroll_errors_total counter`,
`xstreamroll_errors_total ${m.errors}`,
`# HELP xstreamroll_queue_depth Current queue depth of the worker`,
`# TYPE xstreamroll_queue_depth gauge`,
`xstreamroll_queue_depth ${m.queueDepth}`,
`# HELP xstreamroll_uptime_seconds Uptime of the worker in seconds`,
`# TYPE xstreamroll_uptime_seconds gauge`,
`xstreamroll_uptime_seconds ${m.uptimeSeconds}`,
`# HELP xstreamroll_lock_acquisitions_total Total lock acquisitions attempted`,
`# TYPE xstreamroll_lock_acquisitions_total counter`,
`xstreamroll_lock_acquisitions_total ${m.lockAcquisitionsTotal}`,
`# HELP xstreamroll_lock_acquisitions_denied Total lock acquisitions denied (another worker owns stream)`,
`# TYPE xstreamroll_lock_acquisitions_denied counter`,
`xstreamroll_lock_acquisitions_denied ${m.lockAcquisitionsDenied}`,
`# HELP xstreamroll_lock_renewals_total Total lock renewals attempted`,
`# TYPE xstreamroll_lock_renewals_total counter`,
`xstreamroll_lock_renewals_total ${m.lockRenewalsTotal}`,
`# HELP xstreamroll_lock_renewals_failed Total lock renewals that failed (lock lost)`,
`# TYPE xstreamroll_lock_renewals_failed counter`,
`xstreamroll_lock_renewals_failed ${m.lockRenewalsFailed}`,
`# HELP xstreamroll_lock_releases_total Total lock releases attempted`,
`# TYPE xstreamroll_lock_releases_total counter`,
`xstreamroll_lock_releases_total ${m.lockReleasesTotal}`,
`# HELP xstreamroll_lock_releases_failed Total lock releases that failed`,
`# TYPE xstreamroll_lock_releases_failed counter`,
`xstreamroll_lock_releases_failed ${m.lockReleasesFailed}`,
`# HELP xstreamroll_active_locks Current number of active locks held`,
`# TYPE xstreamroll_active_locks gauge`,
`xstreamroll_active_locks ${m.activeLocks}`,
`# HELP xstreamroll_concurrent_route_dedupes Total concurrent route() calls deduplicated`,
`# TYPE xstreamroll_concurrent_route_dedupes counter`,
`xstreamroll_concurrent_route_dedupes ${m.concurrentRouteDedupes}`,
].join("\n") + "\n"
res.writeHead(200, { "Content-Type": "text/plain; version=0.0.4" })
res.end(body)
}
return
}
}
res.writeHead(404)
res.end()
})
server.listen(port, () => {
console.log(`[metrics] server listening on port ${port}`)
})
return server
}