Skip to content
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

chore: control metric/healthcheck interval with env METRICS_COLLECTOR… #247

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Optional configuration:

- `BLOCKFROST_BACKEND_URL` URL pointing to your own backend (blockfrost-backend-ryo) if you prefer not to use the public Blockfrost API
- `BLOCKFROST_BLOCK_LISTEN_INTERVAL` how often should be the server pulling the backend for new data (in milliseconds, default `5000`)
- `METRICS_COLLECTOR_INTERVAL_MS` frequency for refreshing metrics and performing health check (default `10000`)
- `PORT` which port the server should listen to (default `3005`)

Once your server has started, you can connect to it.
Expand Down
4 changes: 3 additions & 1 deletion src/constants/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export const FIAT_RATES_ENABLE_ON_TESTNET = false;
export const BLOCKFROST_REQUEST_CONCURRENCY = 500;

// How often should metrics be updated
export const METRICS_COLLECTOR_INTERVAL_MS = 10_000;
export const METRICS_COLLECTOR_INTERVAL_MS = Number(
process.env.METRICS_COLLECTOR_INTERVAL_MS ?? 10_000,
);

// If healthcheck repeatedly fails for duration longer than this constant the process exits
export const HEALTHCHECK_FAIL_THRESHOLD_MS = 6 * METRICS_COLLECTOR_INTERVAL_MS; // 6 health checks
Expand Down
10 changes: 6 additions & 4 deletions src/utils/prometheus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class MetricsCollector {
if (this.healthCheckFailingSince) {
const failDurationMs = Date.now() - this.healthCheckFailingSince;

if (failDurationMs > HEALTHCHECK_FAIL_THRESHOLD_MS) {
if (HEALTHCHECK_FAIL_THRESHOLD_MS > 0 && failDurationMs > HEALTHCHECK_FAIL_THRESHOLD_MS) {
logger.error(
`Healthcheck failing for longer than ${HEALTHCHECK_FAIL_THRESHOLD_MS} ms. Exiting process.`,
);
Expand Down Expand Up @@ -79,10 +79,12 @@ export class MetricsCollector {
};

startCollector = async (interval: number) => {
this.metrics = await this._collect();
this.intervalId = setInterval(async () => {
if (interval > 0) {
this.metrics = await this._collect();
}, interval);
this.intervalId = setInterval(async () => {
this.metrics = await this._collect();
}, interval);
}
};

stopCollector = () => {
Expand Down
Loading