Skip to content
Snippets Groups Projects
Commit d0888340 authored by Christopher Luzzi's avatar Christopher Luzzi
Browse files

code commented

parent 6ce80864
No related branches found
No related tags found
No related merge requests found
# Node alpine is used as base for the image.
# The node alpine is the lightweight version of node, that way the image stays as small in size as possible
FROM node:lts-alpine FROM node:lts-alpine
ENV NODE_ENV=production ENV NODE_ENV=production
# Here the Working directory is created and navigated to.
# Into this Working directory we copy the code from the local maschine to the container.
WORKDIR /usr/src/CLOUD-COMPUTING_2022 WORKDIR /usr/src/CLOUD-COMPUTING_2022
# Here we copy the package*.json and then install all the dependencies.
# The Maven folder will not be copied to the Container as its on the dockerignore list.
# That way we ensure all dependencies are correctly set inside the container
COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"] COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]
RUN npm install --production --silent && mv node_modules ../ RUN npm install --production --silent && mv node_modules ../
# After installing the dependencies we copy the actual code into the container and expose port 3000.
COPY . . COPY . .
EXPOSE 3000 EXPOSE 3000
RUN chown -R node /usr/src/CLOUD-COMPUTING_2022 RUN chown -R node /usr/src/CLOUD-COMPUTING_2022
USER node USER node
# When running the Image in a container the application will start
CMD ["node", "server.js"] CMD ["node", "server.js"]
const client = require('prom-client') const client = require('prom-client')
//creating a registry where the metrics are collected
const register = new client.Registry() const register = new client.Registry()
register.setDefaultLabels({ register.setDefaultLabels({
app: 'cloud-computing-chat' app: 'cloud-computing-chat'
}) })
//this enables collecting the defaul metrics the prom-client for node offers
client.collectDefaultMetrics({register}) client.collectDefaultMetrics({register})
//here the metrics are defined
const msgSent = new client.Counter({ const msgSent = new client.Counter({
name: 'msgSent', name: 'msgSent',
help: 'total number of messages sent' help: 'total number of messages sent'
...@@ -35,13 +37,14 @@ const failedLogins = new client.Counter({ ...@@ -35,13 +37,14 @@ const failedLogins = new client.Counter({
help: 'number of failed logins' help: 'number of failed logins'
}) })
//this registers the metrics in the registry we have created
register.registerMetric(registercounter) register.registerMetric(registercounter)
register.registerMetric(msgSent) register.registerMetric(msgSent)
register.registerMetric(msgLength) register.registerMetric(msgLength)
register.registerMetric(onlineUser) register.registerMetric(onlineUser)
register.registerMetric(failedLogins) register.registerMetric(failedLogins)
//exports the registry and the metrics to apply changes to metrics. e.g. inc the count for msgSent
exports.register = register exports.register = register
exports.msgSent = msgSent exports.msgSent = msgSent
exports.msgLength = msgLength exports.msgLength = msgLength
......
...@@ -88,6 +88,7 @@ app.get('/index', (req, res) => { ...@@ -88,6 +88,7 @@ app.get('/index', (req, res) => {
io.on('connection', (socket) => { io.on('connection', (socket) => {
socket.on('chat message', (msg) => { socket.on('chat message', (msg) => {
Metrics.msgSent.inc() Metrics.msgSent.inc()
//this returns the count of words for every msg sent
var length = msg.split(' ').filter(word=> word !=='').length var length = msg.split(' ').filter(word=> word !=='').length
Metrics.msgLength.observe(length) Metrics.msgLength.observe(length)
io.emit('chat message', msg); io.emit('chat message', msg);
...@@ -110,6 +111,7 @@ require(path.join(__dirname,'/app/routes/auth.routes'))(app); ...@@ -110,6 +111,7 @@ require(path.join(__dirname,'/app/routes/auth.routes'))(app);
require(path.join(__dirname,'/app/routes/user.routes'))(app); require(path.join(__dirname,'/app/routes/user.routes'))(app);
//exporting the collected metrics to prometheus metrics endpoint
app.get('/metrics', async (req, res) => { app.get('/metrics', async (req, res) => {
res.set('Content-Type', Metrics.register.contentType) res.set('Content-Type', Metrics.register.contentType)
res.end(await Metrics.register.metrics()) res.end(await Metrics.register.metrics())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment