-
Notifications
You must be signed in to change notification settings - Fork 1
feat(grouper): add Prometheus metrics #520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
c80ce79
b7db107
38db313
9e4b6af
ba3f4b9
0d9f719
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,97 @@ | ||||||
| import * as client from 'prom-client'; | ||||||
| import os from 'os'; | ||||||
| import { nanoid } from 'nanoid'; | ||||||
| import createLogger from './logger'; | ||||||
|
|
||||||
| const register = new client.Registry(); | ||||||
| const logger = createLogger(); | ||||||
|
|
||||||
| const DEFAULT_PUSH_INTERVAL_MS = 10_000; | ||||||
| const ID_SIZE = 5; | ||||||
| const METRICS_JOB_NAME = 'workers'; | ||||||
|
|
||||||
| let pushInterval: NodeJS.Timeout | null = null; | ||||||
| let currentWorkerName = ''; | ||||||
|
|
||||||
| client.collectDefaultMetrics({ register }); | ||||||
|
|
||||||
| export { register, client }; | ||||||
Kuchizu marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| /** | ||||||
| * Parse push interval from environment. | ||||||
| */ | ||||||
| function getPushIntervalMs(): number { | ||||||
| const rawInterval = process.env.PROMETHEUS_PUSHGATEWAY_INTERVAL; | ||||||
| const parsedInterval = rawInterval === undefined | ||||||
| ? DEFAULT_PUSH_INTERVAL_MS | ||||||
| : Number(rawInterval); | ||||||
|
|
||||||
| const interval = Number.isFinite(parsedInterval) && parsedInterval > 0 | ||||||
| ? parsedInterval | ||||||
| : DEFAULT_PUSH_INTERVAL_MS; | ||||||
|
|
||||||
| if (rawInterval !== undefined && interval !== parsedInterval) { | ||||||
| logger.warn(`[metrics] invalid PROMETHEUS_PUSHGATEWAY_INTERVAL="${rawInterval}", fallback to ${DEFAULT_PUSH_INTERVAL_MS}ms`); | ||||||
| } | ||||||
|
|
||||||
| return interval; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Stop periodic push to pushgateway. | ||||||
| */ | ||||||
| export function stopMetricsPushing(): void { | ||||||
| if (!pushInterval) { | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| clearInterval(pushInterval); | ||||||
| pushInterval = null; | ||||||
| logger.info(`[metrics] stopped pushing metrics for worker=${currentWorkerName}`); | ||||||
| currentWorkerName = ''; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Start periodic push to pushgateway. | ||||||
| * | ||||||
| * @param workerName - name of the worker for grouping. | ||||||
| */ | ||||||
| export function startMetricsPushing(workerName: string): () => void { | ||||||
| const url = process.env.PROMETHEUS_PUSHGATEWAY_URL; | ||||||
|
|
||||||
| if (!url) { | ||||||
| return stopMetricsPushing; | ||||||
| } | ||||||
|
|
||||||
| if (pushInterval) { | ||||||
| logger.warn(`[metrics] pushing is already started for worker=${currentWorkerName}, skip duplicate start for worker=${workerName}`); | ||||||
|
|
||||||
| return stopMetricsPushing; | ||||||
| } | ||||||
|
|
||||||
| const interval = getPushIntervalMs(); | ||||||
| const hostname = os.hostname(); | ||||||
| const id = nanoid(ID_SIZE); | ||||||
| const gateway = new client.Pushgateway(url, [], register); | ||||||
|
||||||
| const gateway = new client.Pushgateway(url, [], register); | |
| const gateway = new client.Pushgateway(url, undefined, register); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please specify units of measurement in comments