From d088834026364fa5f20506369124acbd298b5c24 Mon Sep 17 00:00:00 2001 From: luzzi <christopher.luzzi@student.reutlingen-university.de> Date: Tue, 29 Nov 2022 18:43:28 +0100 Subject: [PATCH] code commented --- application/Dockerfile | 9 +++++++++ application/metrics.js | 7 +++++-- application/server.js | 2 ++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/application/Dockerfile b/application/Dockerfile index a4c9aa6..3767c31 100644 --- a/application/Dockerfile +++ b/application/Dockerfile @@ -1,10 +1,19 @@ +# 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 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 +# 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*", "./"] 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 . . EXPOSE 3000 RUN chown -R node /usr/src/CLOUD-COMPUTING_2022 USER node +# When running the Image in a container the application will start CMD ["node", "server.js"] diff --git a/application/metrics.js b/application/metrics.js index 4909f3b..cee9465 100644 --- a/application/metrics.js +++ b/application/metrics.js @@ -1,14 +1,16 @@ const client = require('prom-client') +//creating a registry where the metrics are collected const register = new client.Registry() register.setDefaultLabels({ app: 'cloud-computing-chat' }) +//this enables collecting the defaul metrics the prom-client for node offers client.collectDefaultMetrics({register}) - +//here the metrics are defined const msgSent = new client.Counter({ name: 'msgSent', help: 'total number of messages sent' @@ -35,13 +37,14 @@ const failedLogins = new client.Counter({ help: 'number of failed logins' }) +//this registers the metrics in the registry we have created register.registerMetric(registercounter) register.registerMetric(msgSent) register.registerMetric(msgLength) register.registerMetric(onlineUser) 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.msgSent = msgSent exports.msgLength = msgLength diff --git a/application/server.js b/application/server.js index 977aad7..821e65d 100644 --- a/application/server.js +++ b/application/server.js @@ -88,6 +88,7 @@ app.get('/index', (req, res) => { io.on('connection', (socket) => { socket.on('chat message', (msg) => { Metrics.msgSent.inc() + //this returns the count of words for every msg sent var length = msg.split(' ').filter(word=> word !=='').length Metrics.msgLength.observe(length) io.emit('chat message', msg); @@ -110,6 +111,7 @@ require(path.join(__dirname,'/app/routes/auth.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) => { res.set('Content-Type', Metrics.register.contentType) res.end(await Metrics.register.metrics()) -- GitLab