From 0f937061b7f28e3a122845eb2a5e0d8092c791f1 Mon Sep 17 00:00:00 2001
From: Alexander Tim Hobelsberger
 <alexander_tim.hobelsberger@student.reutlingen-university.de>
Date: Thu, 24 Nov 2022 18:30:01 +0100
Subject: [PATCH] Custom Metrics

---
 index.js                  | 24 +++++++++++++++++++++++-
 prometheus/prometheus.yml |  2 +-
 routes/login.js           |  5 ++++-
 3 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/index.js b/index.js
index 981bcc1..e397940 100644
--- a/index.js
+++ b/index.js
@@ -77,6 +77,23 @@ const messageGroup = new client.Counter({
   help: 'Counts the amount of times group message is used'
 });
 
+const clientActiveUsers = new client.Gauge({
+  name: 'node_active_users',
+  help: 'Active Users'
+});
+
+const successfullLogIns = new client.Counter({
+  name: 'node_successful_logins',
+  help: 'Successfull LogIns total'
+});
+global.SUCCESSFULLLOG = successfullLogIns;
+
+const notSuccessfullLogIns = new client.Counter({
+  name: 'node_not_successful_logins',
+  help: 'Not successfull LogIns total'
+});
+global.NOTSUCCESSFULLLOG = notSuccessfullLogIns;
+
 const histogram = new client.Histogram({
   name: 'node_request_duration_seconds',
   help: 'Own Metric: Histogram for the duration in seconds.',
@@ -89,6 +106,10 @@ register.registerMetric(messageAll);
 register.registerMetric(messagePrivate);
 register.registerMetric(messageGroup);
 register.registerMetric(histogram);
+register.registerMetric(successfullLogIns);
+register.registerMetric(notSuccessfullLogIns);
+register.registerMetric(clientActiveUsers);
+
 
 app.use(bodyParser.json());
 //Routes
@@ -132,12 +153,13 @@ app.get('/login', (req, res) => {
 io.on('connection', (socket) => {
   
   socket.on('userLogin', async function(response) {
-    //clientConnectionCounter.inc(1);
+    clientActiveUsers.inc(1);
     console.log("Connection");
     await addUserToActiveUsers(response, socket);
   });
   
   socket.on('disconnect', async function(response) {
+    clientActiveUsers.dec(1);
     await deleteUserFromActiveUsers(response, socket);
   });
 
diff --git a/prometheus/prometheus.yml b/prometheus/prometheus.yml
index aa390c1..4a0cd22 100644
--- a/prometheus/prometheus.yml
+++ b/prometheus/prometheus.yml
@@ -14,5 +14,5 @@ scrape_configs:
     scrape_interval: 5s
     # scheme: https
     static_configs:
-      - targets: ['192.168.2.34:5000']
+      - targets: ['192.168.0.230:5000']
   
\ No newline at end of file
diff --git a/routes/login.js b/routes/login.js
index e035328..69039af 100644
--- a/routes/login.js
+++ b/routes/login.js
@@ -32,21 +32,24 @@ router.post('/', async(req, res) => {
         if(user){
             bcrypt.compare(password, user.password, function (err, result) {
                 if(err){
+                    NOTSUCCESSFULLLOG.inc(1);
                     res.json(err);
                 }
                 if(result){
+                    SUCCESSFULLLOG.inc(1);
                     const user = { name: name }; 
-
                     const accessToken = jwt.sign(user, process.env.ACCESS_TOKEN_SECRET)
                     res.json({
                         statusCode: '200',
                         accessToken: accessToken,
                     })
                 }else{
+                    NOTSUCCESSFULLLOG.inc(1);
                     res.status(401);
                 }
             })
         }else{
+            NOTSUCCESSFULLLOG.inc(1);
             res.status(401);
         }
     })
-- 
GitLab