2016-12-04 14:09:37 +01:00
|
|
|
'use strict';
|
2016-12-01 18:10:25 +01:00
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
const { test } = require('ava');
|
2016-12-01 18:10:25 +01:00
|
|
|
const { EventEmitter } = require('events');
|
|
|
|
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();
|
2016-12-01 18:10:25 +01:00
|
|
|
const { REQUEST_TIME } = require('./events');
|
2018-05-23 11:24:24 +02:00
|
|
|
const { FEATURE_UPDATED } = require('./event-type');
|
2016-12-01 18:10:25 +01:00
|
|
|
const { startMonitoring } = require('./metrics');
|
2017-06-28 14:21:05 +02:00
|
|
|
const { register: prometheusRegister } = require('prom-client');
|
2016-12-01 18:10:25 +01:00
|
|
|
|
2018-05-23 11:24:24 +02:00
|
|
|
test.before(() => {
|
2018-11-28 15:50:49 +01:00
|
|
|
startMonitoring(true, eventBus, eventStore, clientMetricsStore);
|
2018-05-23 11:24:24 +02:00
|
|
|
});
|
|
|
|
|
2016-12-01 18:10:25 +01:00
|
|
|
test('should collect metrics for requests', 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
|
|
|
|
2016-12-01 18:10:25 +01:00
|
|
|
const metrics = prometheusRegister.metrics();
|
2017-06-28 10:17:14 +02:00
|
|
|
t.regex(
|
|
|
|
metrics,
|
2017-08-23 11:27:58 +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
|
|
|
|
|
|
|
test('should collect metrics for updated toggles', t => {
|
|
|
|
eventStore.emit(FEATURE_UPDATED, {
|
|
|
|
data: { name: 'TestToggle' },
|
|
|
|
});
|
|
|
|
|
|
|
|
const metrics = prometheusRegister.metrics();
|
|
|
|
t.regex(metrics, /feature_toggle_update_total{toggle="TestToggle"} 1/);
|
|
|
|
});
|
2018-11-28 15:50:49 +01:00
|
|
|
|
|
|
|
test('should collect metrics for client metric reports', t => {
|
|
|
|
clientMetricsStore.emit('metrics', {
|
|
|
|
bucket: {
|
|
|
|
toggles: {
|
|
|
|
TestToggle: {
|
|
|
|
yes: 10,
|
|
|
|
no: 5,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const metrics = prometheusRegister.metrics();
|
|
|
|
t.regex(
|
|
|
|
metrics,
|
|
|
|
/feature_toggle_usage_total{toggle="TestToggle",active="true"} 10\nfeature_toggle_usage_total{toggle="TestToggle",active="false"} 5/
|
|
|
|
);
|
|
|
|
});
|