Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
feat(metrics): update prom-client for more dimensions
Browse files Browse the repository at this point in the history
also removes the need for gc-stats, using the Node.JS API instead
  • Loading branch information
PixnBits committed May 14, 2020
1 parent 9364b8b commit d133259
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 60 deletions.
11 changes: 8 additions & 3 deletions __tests__/integration/__snapshots__/one-app.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,16 @@ Array [
"nodejs_active_handles_total",
"nodejs_active_requests",
"nodejs_active_requests_total",
"nodejs_eventloop_lag_max_seconds",
"nodejs_eventloop_lag_mean_seconds",
"nodejs_eventloop_lag_min_seconds",
"nodejs_eventloop_lag_p50_seconds",
"nodejs_eventloop_lag_p90_seconds",
"nodejs_eventloop_lag_p99_seconds",
"nodejs_eventloop_lag_seconds",
"nodejs_eventloop_lag_stddev_seconds",
"nodejs_external_memory_bytes",
"nodejs_gc_pause_seconds_total",
"nodejs_gc_reclaimed_bytes_total",
"nodejs_gc_runs_total",
"nodejs_gc_duration_seconds",
"nodejs_heap_size_total_bytes",
"nodejs_heap_size_used_bytes",
"nodejs_heap_space_size_available_bytes",
Expand Down
7 changes: 0 additions & 7 deletions __tests__/server/__snapshots__/metricsServer.spec.js.snap

This file was deleted.

31 changes: 1 addition & 30 deletions __tests__/server/metricsServer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,17 @@ describe('metricsServer', () => {
jest.spyOn(console, 'warn').mockImplementation(() => {});

let client;
let promGcStats;
let healthCheck;
let logging;

function load({ gcStatsError = false } = {}) {
function load() {
jest.resetModules();

jest.mock('prom-client');
if (gcStatsError) {
jest.mock('gc-stats', () => {
throw new Error('unable to resolve gc-stats');
}, { virtual: true });
} else {
jest.mock('gc-stats', () => jest.fn(), { virtual: true });
}
jest.mock('prometheus-gc-stats', () => jest.fn(() => () => {}));
jest.mock('../../src/server/utils/logging/serverMiddleware', () => jest.fn((req, res, next) => next()));
jest.mock('../../src/server/middleware/healthCheck');

client = require('prom-client');
promGcStats = require('prometheus-gc-stats');
logging = require('../../src/server/utils/logging/serverMiddleware');
healthCheck = require('../../src/server/middleware/healthCheck').default;

Expand All @@ -53,25 +43,6 @@ describe('metricsServer', () => {
load();
expect(client.collectDefaultMetrics).toHaveBeenCalledTimes(1);
});

it('collects default metrics every ten seconds', () => {
load();
expect(client.collectDefaultMetrics).toHaveBeenCalledTimes(1);
expect(client.collectDefaultMetrics.mock.calls[0][0]).toHaveProperty('timeout', 10 * 1e3);
});

it('collects garbage collection metrics', () => {
load();
expect(promGcStats).toHaveBeenCalledTimes(1);
expect(promGcStats).toHaveBeenCalledWith(client.register);
});

it('warns if unable to collect garbage collection metrics', () => {
console.warn.mockClear();
load({ gcStatsError: true });
expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn.mock.calls[0]).toMatchSnapshot();
});
});

describe('unknown routes', () => {
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@
"opossum": "^5.0.0",
"opossum-prometheus": "^0.1.0",
"pidusage": "^2.0.17",
"prom-client": "^11.5.3",
"prometheus-gc-stats": "^0.6.2",
"prom-client": "^12.0.0",
"prop-types": "^15.7.2",
"react": "^16.13.1",
"react-dom": "^16.13.1",
Expand Down Expand Up @@ -181,7 +180,6 @@
}
},
"optionalDependencies": {
"gc-stats": "^1.4.0",
"heapdump": "^0.3.15"
},
"standard-version": {
Expand Down
15 changes: 1 addition & 14 deletions src/server/metricsServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,11 @@
import express from 'express';
import helmet from 'helmet';
import { register as metricsRegister, collectDefaultMetrics } from 'prom-client';
import gcStats from 'prometheus-gc-stats';

import logging from './utils/logging/serverMiddleware';
import healthCheck from './middleware/healthCheck';

// Probe every 10th second.
collectDefaultMetrics({ timeout: 10e3 });
gcStats(metricsRegister)();

// prometheus-gc-stats uses gc-stats but swallows the error if not importable
// try importing ourselves so we can log a warning
try {
/* eslint import/no-extraneous-dependencies: ["error", {"optionalDependencies": true}] */
// eslint-disable-next-line global-require, import/no-unresolved
require('gc-stats');
} catch (err) {
console.warn('Unable to set up garbage collection monitor. This is not an issue for local development.');
}
collectDefaultMetrics();

export function createMetricsServer() {
const app = express();
Expand Down

0 comments on commit d133259

Please sign in to comment.