Skip to content

Commit

Permalink
DB - Removed some template code and also tests to metrics helper
Browse files Browse the repository at this point in the history
  • Loading branch information
David Boyne authored and David Boyne committed May 15, 2018
1 parent 61c096d commit 30bf2e1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 28 deletions.
34 changes: 9 additions & 25 deletions 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);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions utils/metrics/index.js
Expand Up @@ -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',
Expand All @@ -17,6 +15,8 @@ const raiseMetric = (name, product, value) => {

metrics[name].set({ product }, parseFloat(value));

return metrics;

};

module.exports = {
Expand Down
27 changes: 27 additions & 0 deletions 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
})
}
}));

});

});

});

0 comments on commit 30bf2e1

Please sign in to comment.