Skip to content

Commit

Permalink
src: add prometheus metrics to insult service
Browse files Browse the repository at this point in the history
  • Loading branch information
lance committed Oct 30, 2018
1 parent 2a9f71e commit d6e4c0d
Show file tree
Hide file tree
Showing 10 changed files with 5,624 additions and 19,707 deletions.
5,190 changes: 923 additions & 4,267 deletions adjective/package-lock.json

Large diffs are not rendered by default.

16 changes: 2 additions & 14 deletions adjective/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@
"author": "Lance Ball, Red Hat, Inc.",
"license": "MIT",
"scripts": {
"pretest": "npm run lint",
"test": "tape test/*.js | tap-spec",
"test:coverage": "nyc tape test/*.js | tap-spec",
"lint": "semistandard test/*.js index.js",
"prepare": "nsp check",
"release": "standard-version",
"deploy": "nodeshift --strictSSL=false --metadata.out=deployment-metadata.json --nodeVersion=10.x --expose",
"postinstall": "license-reporter report",
"deploy": "nodeshift --strictSSL=false --nodeVersion=10.x --expose",
"start": "node ."
},
"repository": {
Expand All @@ -33,14 +28,8 @@
"homepage": "https://github.com/lance/elizabethan-insult",
"devDependencies": {
"nodeshift": "~1.7.1",
"nsp": "~3.2.1",
"nyc": "~11.7.3",
"rhoaster": "~0.2.0",
"semistandard": "~12.0.1",
"standard-version": "^3.0.0",
"supertest": "~3.0.0",
"tap-spec": "~4.1.1",
"tape": "~4.6.2"
"standard-version": "^3.0.0"
},
"description": "Simple web service that returns a random Elizabethan adjective",
"keywords": [
Expand All @@ -55,7 +44,6 @@
"body-parser": "~1.18.2",
"express": "~4.16.3",
"kube-probe": "~0.3.1",
"license-reporter": "~1.2.1",
"swagger-ui-express": "~3.0.9"
}
}
2 changes: 2 additions & 0 deletions insult/assets/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const circuitBreakerOptions = {

const insult = circuitBreaker(getOrPostInsult, circuitBreakerOptions);
const localStats = insult.hystrixStats.getHystrixStream();
// insult.status.on('snapshot', console.log);
localStats.on('data', message => updateStats(JSON.parse(message.substr(6))));

new EventSource('/stats.stream').onmessage =
Expand Down Expand Up @@ -61,6 +62,7 @@ function clearInsultList (e) {
}

function updateStats (stats) {
console.log(stats);
const [ serviceName, _ ] = stats.name.split(' ');
$(`#${serviceName}_successes`).html(stats.rollingCountSuccess || stats.successes);
$(`#${serviceName}_failures`).html(stats.errorCount || stats.errors);
Expand Down
10 changes: 6 additions & 4 deletions insult/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ const path = require('path');

const express = require('express');
const swaggerUi = require('swagger-ui-express');

const stats = require('./lib/stats');
const api = require('./lib/insult');
const probe = require('kube-probe');
const port = process.env.PORT || 8080;
const client = require('prom-client');

const app = express();

app.use('/stats.stream', stats);

app.use('/metrics', (req, res) => {
res.set('Content-Type', client.register.contentType);
res.end(client.register.metrics());
});

// add swagger API docs
app.use('/api-docs', swaggerUi.serve,
swaggerUi.setup(require('./lib/insult.json')));
Expand Down Expand Up @@ -49,9 +54,6 @@ app.use('/js/jquery.js', express.static(
app.use('/js/app.js', express.static(
path.join(__dirname, 'public', 'app.js')));

app.use('/licenses', express.static(
path.join(__dirname, 'licenses')));

app.listen(port);

console.log(`insult service listening on ${port}`);
13 changes: 12 additions & 1 deletion insult/lib/insult.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
const bodyParser = require('body-parser');
const nounService = require('./noun-service');
const adjectiveService = require('./adjective-service');
const client = require('prom-client');

const collectDefaultMetrics = client.collectDefaultMetrics;
collectDefaultMetrics({prefix: 'insult_'});
const durationMetric = new client.Histogram({
name: 'duration',
help: 'HTTP request response duration',
buckets: [0.1, 5, 15, 50, 100, 500]
});

function get (req, res) {
res.type('application/json');
Expand All @@ -28,6 +37,7 @@ function post (req, res) {
}

function buildInsult () {
const end = durationMetric.startTimer();
// call adjective and noun services
return Promise.all([
adjectiveService.get(),
Expand All @@ -39,7 +49,8 @@ function buildInsult () {
adj2: words[1],
noun: words[2]
}))
.catch(e => console.error(`An unexpected error occurred: ${e}`));
.catch(e => console.error(`An unexpected error occurred: ${e}`))
.finally(end);
}

module.exports = exports = function insultApi (router) {
Expand Down
Loading

0 comments on commit d6e4c0d

Please sign in to comment.