Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

end of exercise checkpoint #19

Open
wants to merge 1 commit into
base: prometheus
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 36 additions & 16 deletions bulletin-board-app/server.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,58 @@
var express = require('express'),
bodyParser = require('body-parser'),
methodOverride = require('method-override'),
errorHandler = require('errorhandler'),
morgan = require('morgan'),
routes = require('./backend'),
api = require('./backend/api');

var express = require('express'),
bodyParser = require('body-parser'),
methodOverride = require('method-override'),
errorHandler = require('errorhandler'),
morgan = require('morgan'),
prometheus = require('prom-client'),
routes = require('./backend'),
api = require('./backend/api');
var app = module.exports = express();

app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');

// middleware monitoring function
var monitor = function (req, res, next){
res.on("finish", function() {
routeRequests.labels(req.method, req.path, res.statusCode).inc()
});
next();
}

// middleware stack
app.use(monitor)
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(methodOverride());
app.use(express.static(__dirname + '/'));
app.use('/build', express.static('public'));

var env = process.env.NODE_ENV;
if ('development' == env) {
app.use(errorHandler({
dumpExceptions: true,
showStack: true
}));
app.use(errorHandler({
dumpExceptions: true,
showStack: true
}));
}

if ('production' == app.get('env')) {
app.use(errorHandler());
app.use(errorHandler());
}
// counter for Prometheus:
const routeRequests = new prometheus.Counter({
name: 'bb_route_requests',
help: 'request by route',
labelNames: ['method', 'route', 'code']
})

app.get('/', routes.index);
app.get('/api/events', api.events);
app.post('/api/events', api.event);
app.delete('/api/events/:eventId', api.event);
app.get('/metrics', (req, res) => {
res.set('Content-Type', prometheus.register.contentType)
res.end(prometheus.register.metrics())
})
prometheus.collectDefaultMetrics();

app.listen(8080);
console.log('Magic happens on port 8080...');
console.log('Magic happens on port 8080...');
18 changes: 17 additions & 1 deletion myapp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ spec:
spec:
containers:
- name: web
image: <docker hub user name>/bb:web-0.3
image: chetrobinson/bb:web-0.3
---
apiVersion: v1
kind: Service
Expand All @@ -68,3 +68,19 @@ spec:
ports:
- port: 5432
targetPort: 5432
---
apiVersion: v1
kind: Service
metadata:
name: bb-entrypoint
namespace: default
labels:
app: bb
spec:
type: NodePort
selector:
bb: frontend
ports:
- port: 8080
targetPort: 8080
nodePort: 30001
17 changes: 17 additions & 0 deletions values-prometh.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
prometheus:
service:
type: NodePort
additionalServiceMonitors:
- name: bb-monitor
selector:
matchLabels:
app: bb
namespaceSelector:
matchNames:
- default
endpoints:
- interval: 15s
path: /metrics
alertmanager:
service:
type: NodePort