-
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
Open
Kuchizu
wants to merge
7
commits into
master
Choose a base branch
from
feat/grouper-metrics
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c80ce79
feat(grouper): add Prometheus metrics
Kuchizu b7db107
feat(grouper): OOM-debug logging
Kuchizu 38db313
fix(grouper): handle undefined delta
Kuchizu 9e4b6af
merge: resolve conflicts with origin/master for grouper metrics
Kuchizu ba3f4b9
feat(grouper): add memory leak diagnostics logs
Kuchizu 0d9f719
fix(metrics): validate push interval, add push cleanup, and prevent r…
Kuchizu 650c4da
fix(grouper-metrics): (docs, log context, pushgateway options, duplic…
Kuchizu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, undefined, register); | ||
|
|
||
| currentWorkerName = workerName; | ||
|
|
||
| logger.info(`Start pushing metrics to ${url} every ${interval}ms (host: ${hostname}, id: ${id}, worker: ${workerName})`); | ||
|
|
||
| pushInterval = setInterval(() => { | ||
| gateway.pushAdd({ | ||
| jobName: METRICS_JOB_NAME, | ||
| groupings: { | ||
| worker: workerName, | ||
| host: hostname, | ||
| id, | ||
| }, | ||
| }, (err) => { | ||
| if (err) { | ||
| logger.error(`Metrics push error: ${err.message || err}`); | ||
| } | ||
| }); | ||
| }, interval); | ||
|
|
||
| return stopMetricsPushing; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.