2021-08-12 15:04:37 +02:00
|
|
|
import { register } from 'prom-client';
|
|
|
|
import EventEmitter from 'events';
|
|
|
|
import { createTestConfig } from '../test/config/test-config';
|
|
|
|
import { REQUEST_TIME, DB_TIME } from './metric-events';
|
|
|
|
import { FEATURE_UPDATED } from './types/events';
|
|
|
|
import { createMetricsMonitor } from './metrics';
|
|
|
|
import createStores from '../test/fixtures/store';
|
2020-12-16 14:49:11 +01:00
|
|
|
|
|
|
|
const monitor = createMetricsMonitor();
|
2021-08-12 15:04:37 +02:00
|
|
|
const eventBus = new EventEmitter();
|
|
|
|
const prometheusRegister = register;
|
|
|
|
let stores;
|
2021-05-28 11:10:24 +02:00
|
|
|
beforeAll(() => {
|
2021-04-22 10:07:10 +02:00
|
|
|
const config = createTestConfig({
|
|
|
|
server: {
|
|
|
|
serverMetrics: true,
|
|
|
|
},
|
|
|
|
});
|
2021-08-12 15:04:37 +02:00
|
|
|
stores = createStores();
|
|
|
|
const db = {
|
|
|
|
client: {
|
|
|
|
pool: {
|
|
|
|
min: 0,
|
|
|
|
max: 4,
|
|
|
|
numUsed: () => 2,
|
|
|
|
numFree: () => 2,
|
|
|
|
numPendingAcquires: () => 0,
|
|
|
|
numPendingCreates: () => 1,
|
2021-02-04 14:14:46 +01:00
|
|
|
},
|
2020-08-03 13:34:10 +02:00
|
|
|
},
|
|
|
|
};
|
2021-08-12 15:04:37 +02:00
|
|
|
// @ts-ignore - We don't want a full knex implementation for our tests, it's enough that it actually yields the numbers we want.
|
|
|
|
monitor.startMonitoring(config, stores, '4.0.0', eventBus, db);
|
2020-12-16 14:49:11 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
afterAll(() => {
|
2020-12-16 14:49:11 +01:00
|
|
|
monitor.stopMonitoring();
|
2018-05-23 11:24:24 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should collect metrics for requests', async () => {
|
2017-06-28 10:17:14 +02:00
|
|
|
eventBus.emit(REQUEST_TIME, {
|
|
|
|
path: 'somePath',
|
|
|
|
method: 'GET',
|
|
|
|
statusCode: 200,
|
|
|
|
time: 1337,
|
|
|
|
});
|
2016-12-04 14:09:37 +01:00
|
|
|
|
2021-02-04 13:20:10 +01:00
|
|
|
const metrics = await prometheusRegister.metrics();
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(metrics).toMatch(
|
2021-08-12 15:04:37 +02:00
|
|
|
/http_request_duration_milliseconds{quantile="0\.99",path="somePath",method="GET",status="200"}.*1337/,
|
2017-06-28 10:17:14 +02:00
|
|
|
);
|
2016-12-04 14:09:37 +01:00
|
|
|
});
|
2018-05-23 11:24:24 +02:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should collect metrics for updated toggles', async () => {
|
2021-08-12 15:04:37 +02:00
|
|
|
stores.eventStore.emit(FEATURE_UPDATED, {
|
2018-05-23 11:24:24 +02:00
|
|
|
data: { name: 'TestToggle' },
|
|
|
|
});
|
|
|
|
|
2021-02-04 13:20:10 +01:00
|
|
|
const metrics = await prometheusRegister.metrics();
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(metrics).toMatch(
|
|
|
|
/feature_toggle_update_total{toggle="TestToggle"} 1/,
|
|
|
|
);
|
2018-05-23 11:24:24 +02:00
|
|
|
});
|
2018-11-28 15:50:49 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should collect metrics for client metric reports', async () => {
|
2021-08-12 15:04:37 +02:00
|
|
|
stores.clientMetricsStore.emit('metrics', {
|
2018-11-28 15:50:49 +01:00
|
|
|
bucket: {
|
|
|
|
toggles: {
|
|
|
|
TestToggle: {
|
|
|
|
yes: 10,
|
|
|
|
no: 5,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-02-04 13:20:10 +01:00
|
|
|
const metrics = await prometheusRegister.metrics();
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(metrics).toMatch(
|
2020-04-14 22:29:11 +02:00
|
|
|
/feature_toggle_usage_total{toggle="TestToggle",active="true",appName="undefined"} 10\nfeature_toggle_usage_total{toggle="TestToggle",active="false",appName="undefined"} 5/,
|
2018-11-28 15:50:49 +01:00
|
|
|
);
|
|
|
|
});
|
2019-08-04 11:10:51 +02:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should collect metrics for db query timings', async () => {
|
2019-08-04 11:10:51 +02:00
|
|
|
eventBus.emit(DB_TIME, {
|
|
|
|
store: 'foo',
|
|
|
|
action: 'bar',
|
|
|
|
time: 0.1337,
|
|
|
|
});
|
|
|
|
|
2021-02-04 13:20:10 +01:00
|
|
|
const metrics = await prometheusRegister.metrics();
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(metrics).toMatch(
|
2020-04-14 22:29:11 +02:00
|
|
|
/db_query_duration_seconds{quantile="0\.99",store="foo",action="bar"} 0.1337/,
|
2019-08-04 11:10:51 +02:00
|
|
|
);
|
|
|
|
});
|
2020-02-28 14:50:32 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should collect metrics for feature toggle size', async () => {
|
2021-02-04 13:20:10 +01:00
|
|
|
const metrics = await prometheusRegister.metrics();
|
2021-08-12 15:04:37 +02:00
|
|
|
expect(metrics).toMatch(/feature_toggles_total{version="(.*)"} 0/);
|
2020-02-28 14:50:32 +01:00
|
|
|
});
|
2021-02-04 14:14:46 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('Should collect metrics for database', async () => {
|
2021-02-04 14:14:46 +01:00
|
|
|
const metrics = await prometheusRegister.metrics();
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(metrics).toMatch(/db_pool_max/);
|
|
|
|
expect(metrics).toMatch(/db_pool_min/);
|
|
|
|
expect(metrics).toMatch(/db_pool_used/);
|
|
|
|
expect(metrics).toMatch(/db_pool_free/);
|
|
|
|
expect(metrics).toMatch(/db_pool_pending_creates/);
|
|
|
|
expect(metrics).toMatch(/db_pool_pending_acquires/);
|
2021-02-04 14:14:46 +01:00
|
|
|
});
|