1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-28 00:17:12 +01:00
This commit is contained in:
sveisvei 2016-12-04 14:09:37 +01:00 committed by Ivar Conradi Østhus
parent d4d63af2b0
commit f425597640
23 changed files with 106 additions and 94 deletions

View File

@ -146,14 +146,13 @@ test('should build up list of seend toggles when new metrics arrives', t => {
});
test('should handle a lot of toggles', t => {
const store = new EventEmitter();
const metrics = new UnleashClientMetrics(store);
const toggleCounts = {};
for (let i = 0; i < 100; i++) {
toggleCounts[`toggle${i}`] = {yes: i, no: i}
toggleCounts[`toggle${i}`] = { yes: i, no: i };
}
store.emit('metrics', {

View File

@ -5,7 +5,6 @@ const TTLList = require('./ttl-list.js');
module.exports = class UnleashClientMetrics {
constructor (clientMetricsStore) {
this.globalCount = 0;
this.apps = {};
@ -40,7 +39,7 @@ module.exports = class UnleashClientMetrics {
Object.keys(this.apps).forEach(appName => {
const seenToggles = Object.keys(this.apps[appName].seenToggles);
const metricsCount = this.apps[appName].count;
apps.push({appName, seenToggles, metricsCount})
apps.push({ appName, seenToggles, metricsCount });
});
return apps;
}
@ -57,8 +56,8 @@ module.exports = class UnleashClientMetrics {
addPayload (data) {
const { appName, bucket } = data;
const app = this.getApp(appName)
this.addBucket(app, data.bucket);
const app = this.getApp(appName);
this.addBucket(app, bucket);
}
getApp (appName) {
@ -89,7 +88,9 @@ module.exports = class UnleashClientMetrics {
}
addSeenToggles (app, toggleNames) {
toggleNames.forEach(t => app.seenToggles[t] = true);
toggleNames.forEach(t => {
app.seenToggles[t] = true;
});
}
destroy () {

View File

@ -36,11 +36,9 @@ function toNewFormat (feature) {
description: feature.description,
enabled: feature.enabled,
strategies: feature.strategies,
createdAt: feature.createdAt
createdAt: feature.createdAt,
};
}
}
}
module.exports = { addOldFields, toNewFormat };

View File

@ -27,7 +27,7 @@ test('adds old fields to feature', t => {
test('adds old fields to feature handles missing strategies field', t => {
const feature = {
name: 'test',
enabled: 0
enabled: 0,
};
const mappedFeature = mapper.addOldFields(feature);

View File

@ -13,10 +13,10 @@ const mapRow = (row) => ({
createdAt: row.created_at,
});
const mapAppsRow = (row) => ({
appName: row.app_name,
createdAt: row.created_at,
});
// const mapAppsRow = (row) => ({
// appName: row.app_name,
// createdAt: row.created_at,
// });
class ClientInstanceStore {

View File

@ -12,7 +12,6 @@ class ClientMetricsStore extends EventEmitter {
super();
this.metricsDb = metricsDb;
this.highestIdSeen = 0;
this.timer;
// Build internal state
metricsDb.getMetricsLastHour()
@ -29,7 +28,7 @@ class ClientMetricsStore extends EventEmitter {
_fetchNewAndEmit () {
this.metricsDb.getNewMetrics(this.highestIdSeen)
.then((metrics) => this._emitMetrics(metrics))
.then((metrics) => this._emitMetrics(metrics));
}
_emitMetrics (metrics) {
@ -41,7 +40,7 @@ class ClientMetricsStore extends EventEmitter {
// Insert new client metrics
insert (metrics) {
return this.metricsDb.insert(metrics)
return this.metricsDb.insert(metrics);
}
destroy () {

View File

@ -5,15 +5,19 @@ const ClientMetricStore = require('./client-metrics-store');
const sinon = require('sinon');
function getMockDb () {
const list = [{ id: 4, metrics: {appName: 'test'} }, { id: 3, metrics: {appName: 'test'} }, { id: 2, metrics: {appName: 'test'} }];
const list = [
{ id: 4, metrics: { appName: 'test' } },
{ id: 3, metrics: { appName: 'test' } },
{ id: 2, metrics: { appName: 'test' } },
];
return {
getMetricsLastHour () {
return Promise.resolve([{ id: 1, metrics: { appName: 'test' } }]);
},
getNewMetrics (v) {
getNewMetrics () {
return Promise.resolve([list.pop() || { id: 0 }]);
}
},
};
}

View File

@ -1,3 +1,5 @@
'use strict';
module.exports = {
REQUEST_TIME: 'request_time',
}
};

View File

@ -4,7 +4,7 @@ const log4js = require('log4js');
log4js.configure({
appenders: [
{ type: 'console' },
]
],
});
const logger = log4js.getLogger('unleash');

View File

@ -1,3 +1,5 @@
'use strict';
const events = require('./events');
exports.startMonitoring = (enable, eventBus) => {

View File

@ -1,3 +1,4 @@
'use strict';
const test = require('ava');
const { EventEmitter } = require('events');
@ -11,6 +12,5 @@ test('should collect metrics for requests', t => {
eventBus.emit(REQUEST_TIME, { path: 'somePath', method: 'GET', statusCode: 200, time: 1337 });
const metrics = prometheusRegister.metrics();
t.regex(metrics, /http_request_duration_milliseconds{quantile="0.99",status="200",method="GET",path="somePath"} 1337/)
t.regex(metrics, /http_request_duration_milliseconds{quantile="0.99",status="200",method="GET",path="somePath"} 1337/);
});

View File

@ -1,4 +1,5 @@
'use strict';
const { publicFolder } = require('unleash-frontend');
const isDev = () => process.env.NODE_ENV === 'development';
@ -9,7 +10,7 @@ const DEFAULT_OPTIONS = {
baseUriPath: process.env.BASE_URI_PATH || '',
serverMetrics: true,
publicFolder,
enableRequestLogger: isDev() ? true : false
enableRequestLogger: isDev(),
};
module.exports = {
@ -25,5 +26,5 @@ module.exports = {
throw new Error('You must either pass databaseUrl option or set environemnt variable DATABASE_URL');
}
return options;
}
}
},
};

View File

@ -3,11 +3,12 @@
const test = require('ava');
delete process.env.DATABASE_URL;
const { createOptions } = require('./options');
test('should require DATABASE_URI', t => {
const { createOptions } = require('./options');
t.throws(() => {
const options = createOptions({});
createOptions({});
});
});

View File

@ -70,7 +70,7 @@ module.exports = function (app, config) {
validateRequest(req)
.then(validateFormat)
.then(validateUniqueName)
.then((req) => legacyFeatureMapper.toNewFormat(req.body))
.then((_req) => legacyFeatureMapper.toNewFormat(_req.body))
.then(validateStrategy)
.then((featureToggle) => eventStore.store({
type: eventType.featureCreated,

View File

@ -23,4 +23,4 @@ const clientRegisterSchema = joi.object().keys({
interval: joi.number().required(),
});
module.exports = { clientMetricsSchema, clientRegisterSchema }
module.exports = { clientMetricsSchema, clientRegisterSchema };

View File

@ -87,12 +87,12 @@ module.exports = function (app, config) {
clientInstanceStore.getApplications()
.then(apps => {
const applications = apps.map(({ appName }) => ({
appName: appName,
appName,
links: {
appDetails: `/api/client/applications/${appName}`
}
}))
res.json({applications})
appDetails: `/api/client/applications/${appName}`,
},
}));
res.json({ applications });
})
.catch(err => catchLogAndSendErrorResponse(err, res));
});
@ -102,7 +102,7 @@ module.exports = function (app, config) {
const seenToggles = metrics.getSeenTogglesByAppName(appName);
Promise.all([
clientInstanceStore.getByAppName(appName),
clientStrategyStore.getByAppName(appName)
clientStrategyStore.getByAppName(appName),
])
.then(([instances, strategies]) => res.json({ appName, instances, strategies, seenToggles }))
.catch(err => catchLogAndSendErrorResponse(err, res));

View File

@ -1,8 +1,10 @@
'use strict';
const logger = require('../logger');
const catchLogAndSendErrorResponse = (err, res) => {
logger.error(err);
res.status(500).end();
}
};
module.exports = { catchLogAndSendErrorResponse };

View File

@ -5,7 +5,6 @@ const { EventEmitter } = require('events');
const logger = require('./logger');
const migrator = require('../migrator');
const getApp = require('./app');
const events = require('./events');
const { startMonitoring } = require('./metrics');
const { createStores } = require('./db');

View File

@ -39,6 +39,8 @@
"start:dev:pg-chain": "export DATABASE_URL=postgres://$PGUSER:$PGPASSWORD@localhost:$PGPORT/postgres ; db-migrate up && npm run start:dev",
"db-migrate": "db-migrate up",
"db-migrate:down": "db-migrate down",
"lint": "eslint lib",
"pretest": "npm run lint",
"test": "PORT=4243 ava test lib/*.test.js lib/**/*.test.js",
"test:docker": "./scripts/docker-postgres.sh",
"test:watch": "npm run test -- --watch",
@ -82,6 +84,8 @@
"@types/node": "^6.0.46",
"ava": "^0.17.0",
"coveralls": "^2.11.15",
"eslint": "^3.11.1",
"eslint-config-finn": "^1.0.0-beta.1",
"nyc": "^9.0.1",
"sinon": "^1.17.5",
"superagent": "^2.3.0",

View File

@ -23,7 +23,7 @@ function getSetup () {
return {
request: supertest(app),
stores
stores,
};
}
@ -99,7 +99,7 @@ test('should return seen toggles even when there is nothing', t => {
test('should return list of seen-toggles per app', t => {
const { request, stores } = getSetup();
const appName = 'asd!23'
const appName = 'asd!23';
stores.clientMetricsStore.emit('metrics', {
appName,
instanceId: 'instanceId',
@ -128,12 +128,12 @@ test('should return feature-toggles metrics even when there is nothing', t => {
const { request } = getSetup();
return request
.get('/api/client/metrics/feature-toggles')
.expect(200)
.expect(200);
});
test('should return metrics for all toggles', t => {
const { request, stores } = getSetup();
const appName = 'asd!23'
const appName = 'asd!23';
stores.clientMetricsStore.emit('metrics', {
appName,
instanceId: 'instanceId',
@ -142,7 +142,7 @@ test('should return metrics for all toggles', t => {
stop: new Date(),
toggles: {
toggleX: { yes: 123, no: 0 },
toggleY: {yes: 123,no: 0}
toggleY: { yes: 123, no: 0 },
},
},
});
@ -160,7 +160,7 @@ test('should return metrics for all toggles', t => {
test('should return list of client strategies', t => {
const { request, stores } = getSetup();
const { request } = getSetup();
return request
.get('/api/client/strategies')
.expect(200)
@ -170,7 +170,7 @@ test('should return list of client strategies', t => {
});
test('should return list of client applications', t => {
const { request, stores } = getSetup();
const { request } = getSetup();
return request
.get('/api/client/applications')
.expect(200)