2016-12-04 14:09:37 +01:00
|
|
|
'use strict';
|
2016-12-01 18:10:25 +01:00
|
|
|
|
2018-12-17 09:24:49 +01:00
|
|
|
const test = require('ava');
|
2016-12-01 18:10:25 +01:00
|
|
|
const { EventEmitter } = require('events');
|
2020-04-14 22:29:11 +02:00
|
|
|
|
2016-12-01 18:10:25 +01:00
|
|
|
const eventBus = new EventEmitter();
|
2018-05-23 11:24:24 +02:00
|
|
|
const eventStore = new EventEmitter();
|
2018-11-28 15:50:49 +01:00
|
|
|
const clientMetricsStore = new EventEmitter();
|
2020-04-14 22:29:11 +02:00
|
|
|
const { register: prometheusRegister } = require('prom-client');
|
2019-08-04 11:10:51 +02:00
|
|
|
const { REQUEST_TIME, DB_TIME } = require('./events');
|
2018-05-23 11:24:24 +02:00
|
|
|
const { FEATURE_UPDATED } = require('./event-type');
|
2020-12-16 14:49:11 +01:00
|
|
|
const { createMetricsMonitor } = require('./metrics');
|
|
|
|
|
|
|
|
const monitor = createMetricsMonitor();
|
2016-12-01 18:10:25 +01:00
|
|
|
|
2018-05-23 11:24:24 +02:00
|
|
|
test.before(() => {
|
2020-02-28 14:50:32 +01:00
|
|
|
const featureToggleStore = {
|
|
|
|
count: () => 123,
|
|
|
|
};
|
2020-08-03 13:34:10 +02:00
|
|
|
const config = {
|
|
|
|
serverMetrics: true,
|
2020-02-28 14:50:32 +01:00
|
|
|
eventBus,
|
2020-08-03 13:34:10 +02:00
|
|
|
stores: {
|
|
|
|
eventStore,
|
|
|
|
clientMetricsStore,
|
|
|
|
featureToggleStore,
|
2021-02-04 14:14:46 +01:00
|
|
|
db: {
|
|
|
|
client: {
|
|
|
|
pool: {
|
|
|
|
min: 0,
|
|
|
|
max: 4,
|
|
|
|
numUsed: () => 2,
|
|
|
|
numFree: () => 2,
|
|
|
|
numPendingAcquires: () => 0,
|
|
|
|
numPendingCreates: () => 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-08-03 13:34:10 +02:00
|
|
|
},
|
|
|
|
version: '3.4.1',
|
|
|
|
};
|
2020-12-16 14:49:11 +01:00
|
|
|
monitor.startMonitoring(config);
|
|
|
|
});
|
|
|
|
|
|
|
|
test.after(() => {
|
|
|
|
monitor.stopMonitoring();
|
2018-05-23 11:24:24 +02:00
|
|
|
});
|
|
|
|
|
2021-02-04 13:20:10 +01:00
|
|
|
test('should collect metrics for requests', async t => {
|
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();
|
2017-06-28 10:17:14 +02:00
|
|
|
t.regex(
|
|
|
|
metrics,
|
2020-04-14 22:29:11 +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-02-04 13:20:10 +01:00
|
|
|
test('should collect metrics for updated toggles', async t => {
|
2018-05-23 11:24:24 +02:00
|
|
|
eventStore.emit(FEATURE_UPDATED, {
|
|
|
|
data: { name: 'TestToggle' },
|
|
|
|
});
|
|
|
|
|
2021-02-04 13:20:10 +01:00
|
|
|
const metrics = await prometheusRegister.metrics();
|
2018-05-23 11:24:24 +02:00
|
|
|
t.regex(metrics, /feature_toggle_update_total{toggle="TestToggle"} 1/);
|
|
|
|
});
|
2018-11-28 15:50:49 +01:00
|
|
|
|
2021-02-04 13:20:10 +01:00
|
|
|
test('should collect metrics for client metric reports', async t => {
|
2018-11-28 15:50:49 +01:00
|
|
|
clientMetricsStore.emit('metrics', {
|
|
|
|
bucket: {
|
|
|
|
toggles: {
|
|
|
|
TestToggle: {
|
|
|
|
yes: 10,
|
|
|
|
no: 5,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-02-04 13:20:10 +01:00
|
|
|
const metrics = await prometheusRegister.metrics();
|
2018-11-28 15:50:49 +01:00
|
|
|
t.regex(
|
|
|
|
metrics,
|
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-02-04 13:20:10 +01:00
|
|
|
test('should collect metrics for db query timings', async t => {
|
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();
|
2019-08-04 11:10:51 +02:00
|
|
|
t.regex(
|
|
|
|
metrics,
|
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-02-04 13:20:10 +01:00
|
|
|
test('should collect metrics for feature toggle size', async t => {
|
|
|
|
const metrics = await prometheusRegister.metrics();
|
2020-02-28 14:50:32 +01:00
|
|
|
t.regex(metrics, /feature_toggles_total{version="(.*)"} 123/);
|
|
|
|
});
|
2021-02-04 14:14:46 +01:00
|
|
|
|
|
|
|
test('Should collect metrics for database', async t => {
|
|
|
|
const metrics = await prometheusRegister.metrics();
|
|
|
|
t.regex(metrics, /db_pool_max/);
|
|
|
|
t.regex(metrics, /db_pool_min/);
|
|
|
|
t.regex(metrics, /db_pool_used/);
|
|
|
|
t.regex(metrics, /db_pool_free/);
|
|
|
|
t.regex(metrics, /db_pool_pending_creates/);
|
|
|
|
t.regex(metrics, /db_pool_pending_acquires/);
|
|
|
|
});
|