From 30bf2e17a64665422b784ff7152541236b5c14f6 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Tue, 15 May 2018 08:27:04 +0100 Subject: [PATCH] DB - Removed some template code and also tests to metrics helper --- app.js | 34 +++++++++------------------------- package.json | 3 ++- utils/metrics/index.js | 4 ++-- utils/metrics/spec.js | 27 +++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 28 deletions(-) create mode 100644 utils/metrics/spec.js diff --git a/app.js b/app.js index b95f62e..59e3356 100644 --- a/app.js +++ b/app.js @@ -1,36 +1,20 @@ -var createError = require('http-errors'); -var express = require('express'); -var logger = require('morgan'); +const createError = require('http-errors'); +const express = require('express'); +const logger = require('morgan'); + +const app = express(); +const client = require('prom-client'); const lighthouse = require('./plugins/lighthouse'); const pageSpeedInsights = require('./plugins/page-speed-insights'); -var app = express(); - -app.use(logger('dev')); -app.use(express.json()); -app.use(express.urlencoded({ extended: false })); - -const client = require('prom-client'); - const collectDefaultMetrics = client.collectDefaultMetrics; const Registry = client.Registry; const register = new Registry(); -// collectDefaultMetrics({ register }); - -const gauge = new client.Gauge({ name: 'metric_name', help: 'metric_help' }); -const counter = new client.Counter({ - name: 'metric_name2', - help: 'metric_help32' -}); - -counter.inc(Math.random() * 100); - -app.get('/count', (req, res) => { - counter.inc(Math.random() * 100); - res.send(200); -}); +app.use(logger('dev')); +app.use(express.json()); +app.use(express.urlencoded({ extended: false })); app.get('/metrics', (req, res) => { res.set('Content-Type', register.contentType); diff --git a/package.json b/package.json index 7f9eb92..929d506 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "private": true, "scripts": { "start": "node ./bin/www", - "test": "PAGE_SPEED_INSIGHTS_KEY=PAGE_SPEED_INSIGHTS_KEY LIGHTHOUSE_API_KEY=LIGHTHOUSE_API_KEY jest" + "test": "PAGE_SPEED_INSIGHTS_KEY=PAGE_SPEED_INSIGHTS_KEY LIGHTHOUSE_API_KEY=LIGHTHOUSE_API_KEY jest", + "test:watch": "PAGE_SPEED_INSIGHTS_KEY=PAGE_SPEED_INSIGHTS_KEY LIGHTHOUSE_API_KEY=LIGHTHOUSE_API_KEY jest --watch" }, "dependencies": { "debug": "~2.6.9", diff --git a/utils/metrics/index.js b/utils/metrics/index.js index 69609c1..4ca3bb0 100644 --- a/utils/metrics/index.js +++ b/utils/metrics/index.js @@ -6,8 +6,6 @@ const raiseMetric = (name, product, value) => { if(!metrics[name]){ - console.log(client) - metrics[name] = new client.Gauge({ name: name, help: 'metric_help', @@ -17,6 +15,8 @@ const raiseMetric = (name, product, value) => { metrics[name].set({ product }, parseFloat(value)); + return metrics; + }; module.exports = { diff --git a/utils/metrics/spec.js b/utils/metrics/spec.js new file mode 100644 index 0000000..658028f --- /dev/null +++ b/utils/metrics/spec.js @@ -0,0 +1,27 @@ +const { raiseMetric } = require('./'); + +describe('metrics helper', () => { + + describe('raiseMetric', () => { + + it('sets a new promethus Gauge with the given name, product and value', () => { + + const metrics = raiseMetric('test', 'test-product', 10); + + expect(metrics['test']).toEqual(expect.objectContaining({ + name: 'test', + labelNames: ['product'], + help: 'metric_help', + aggregator: 'sum', + hashMap: { + 'product:test-product': expect.objectContaining({ + value: 10 + }) + } + })); + + }); + + }); + +}); \ No newline at end of file