From fdef161d6c2343c8715e9f86f35daa5a553be69c Mon Sep 17 00:00:00 2001 From: Eugenio Marzo Date: Tue, 6 Jul 2021 09:06:39 +0200 Subject: [PATCH] preparing metrics visualization --- html5/index.html | 34 +++++++++++++++++++++++++++--- html5/kubeinvaders.js | 48 ++++++++++++++++++++++++++++++++----------- scripts/metrics.lua | 39 +++-------------------------------- scripts/pod.lua | 14 +++++++++++-- 4 files changed, 82 insertions(+), 53 deletions(-) diff --git a/html5/index.html b/html5/index.html index 339fbe4..9df363c 100644 --- a/html5/index.html +++ b/html5/index.html @@ -69,14 +69,42 @@
-
- Responsive image +
+
+
+
+
+ Stats +
+
+

Deleted Pod Total: 0

+

Chaos Jobs Total: 0

+
+
+
+
+ Responsive image +
+
+
+
+ Speed +
+
+
Latest speed of return to the desired state
+
+

0

+
+
+
+
+
-
+
diff --git a/html5/kubeinvaders.js b/html5/kubeinvaders.js index e327146..b3f87ac 100644 --- a/html5/kubeinvaders.js +++ b/html5/kubeinvaders.js @@ -92,18 +92,37 @@ function contains(a, obj) { return false; } +function getMetrics() { + var oReq = new XMLHttpRequest(); + oReq.onload = function () { + console.log(this.responseText); + var lines = this.responseText.split('\n'); + for (var i = 0;i < lines.length;i++){ + metric = lines[i].split(' '); + if (metric[0] == "chaos_node_jobs_total") { + chaos_jobs_total + $('#chaos_jobs_total').text(metric[1]); + } + else if (metric[0] == "deleted_pods_total") { + $('#deleted_pods_total').text(metric[1]); + } + } + };; + oReq.open("GET", "https://ENDPOINT_PLACEHOLDER/metrics"); + oReq.send(); +} + function getNamespaces() { var oReq = new XMLHttpRequest(); oReq.onload = function () { namespaces = this.responseText; - namespaces = namespaces.split(","); - namespace = namespaces[namespaces_index]; + namespaces = namespaces.split(","); + namespace = namespaces[namespaces_index]; };; oReq.open("GET", "https://ENDPOINT_PLACEHOLDER/kube/namespaces"); oReq.send(); } - function getEndpoint() { var oReq = new XMLHttpRequest(); oReq.onload = function () { @@ -150,7 +169,7 @@ function startChaosNode(node_name) { oReq.onload = function () { //console.log(JSON.parse(this.responseText)) };; - $('#alert_placeholder').replaceWith(''); + $('#alert_placeholder').replaceWith(''); oReq.open("GET", "https://ENDPOINT_PLACEHOLDER/kube/chaos/nodes?nodename=" + node_name + "&namespace=" + namespace); oReq.send(); } @@ -160,7 +179,7 @@ function deletePods(pod_name) { oReq.onload = function () { //console.log(JSON.parse(this.responseText)) };; - $('#alert_placeholder').replaceWith(''); + $('#alert_placeholder').replaceWith(''); oReq.open("GET", "https://ENDPOINT_PLACEHOLDER/kube/pods?action=delete&pod_name=" + pod_name + "&namespace=" + namespace); oReq.send(); } @@ -210,6 +229,7 @@ window.setInterval(function getKubeItems() { function keyDownHandler(e) { if (!modal_opened) { + e.preventDefault(); if(e.key == "Right" || e.key == "ArrowRight") { rightPressed = true; //console.log("Go right"); @@ -237,13 +257,13 @@ function keyDownHandler(e) { else if(e.keyCode == 83) { if (shuffle) { shuffle = false; - $('#alert_placeholder').replaceWith(''); + $('#alert_placeholder').replaceWith(''); //console.log("Deactivate shuffle"); } else { shuffle = true //console.log("Activate shuffle"); - $('#alert_placeholder').replaceWith(''); + $('#alert_placeholder').replaceWith(''); } } else if(e.keyCode == 32) { @@ -259,7 +279,7 @@ function keyDownHandler(e) { namespaces_index = 0; } namespace = namespaces[namespaces_index]; - $('#alert_placeholder').replaceWith(''); + $('#alert_placeholder').replaceWith(''); aliens = []; pods = []; } @@ -274,22 +294,22 @@ function keyDownHandler(e) { else if(e.keyCode == 67) { if (chaos_nodes) { chaos_nodes = false; - $('#alert_placeholder').replaceWith(''); + $('#alert_placeholder').replaceWith(''); } else { chaos_nodes = true - $('#alert_placeholder').replaceWith(''); + $('#alert_placeholder').replaceWith(''); } } else if(e.keyCode == 80) { if (chaos_pods) { chaos_pods = false; - $('#alert_placeholder').replaceWith(''); + $('#alert_placeholder').replaceWith(''); } else { chaos_pods = true - $('#alert_placeholder').replaceWith(''); + $('#alert_placeholder').replaceWith(''); } } } @@ -528,5 +548,9 @@ window.setInterval(function setAliens() { } }, 1000) +window.setInterval(function metrics() { + getMetrics() +}, 2000) + getEndpoint(); getNamespaces(); diff --git a/scripts/metrics.lua b/scripts/metrics.lua index d524060..640a10e 100644 --- a/scripts/metrics.lua +++ b/scripts/metrics.lua @@ -1,43 +1,10 @@ local redis = require "resty.redis" local arg = ngx.req.get_uri_args() local incr = 0 -if ngx.var.request_method == "DELETE" then - local red = redis:new() - local okredis, errredis = red:connect("unix:/tmp/redis.sock") - - if okredis then - ngx.log(ngx.ERR, "Connection to Redis is ok") - else - ngx.log(ngx.ERR, "Connection to Redis is not ok") - ngx.log(ngx.ERR, errredis) - end - -- Count the total of deleted pods - local res, err = red:get("deleted_pods_total") - - if res == ngx.null then - ngx.say(err) - red:set("deleted_pods_total", 1) - else - incr = res + 1 - red:set("deleted_pods_total", incr) - end - -- TODO: Make a better regular expression! - local namespace_pods = string.match(ngx.var.request_uri, '^.*/namespaces/(.*)/.*$') - local namespace = namespace_pods:gsub("/pods", "") - - -- Count the total of deleted pods for namespace - local res, err = red:get("deleted_pods_total_on_" .. namespace) - - if res == ngx.null then - red:set("deleted_pods_total_on_" .. namespace, 1) - else - incr = res + 1 - red:set("deleted_pods_total_on_" .. namespace, incr) - end - +-- TO DO put all metrics into this metrics.lua. Actually in pod.lua are written other metrics in Redis -elseif ngx.var.request_method == "GET" and string.match(ngx.var.request_uri, "^.*/chaos[-]node.*$") then +if ngx.var.request_method == "GET" and string.match(ngx.var.request_uri, "^.*/chaos[-]node.*$") then local red = redis:new() local okredis, errredis = red:connect("unix:/tmp/redis.sock") @@ -52,7 +19,7 @@ elseif ngx.var.request_method == "GET" and string.match(ngx.var.request_uri, "^. if chaos_node_res == ngx.null then ngx.say(err) - red:set("chaos_node_jobs_total", 0) + red:set("chaos_node_jobs_total", 1) else local incr = chaos_node_res + 1 local res, err = red:set("chaos_node_jobs_total",incr) diff --git a/scripts/pod.lua b/scripts/pod.lua index 796b746..ba39f9f 100644 --- a/scripts/pod.lua +++ b/scripts/pod.lua @@ -25,7 +25,6 @@ ngx.header['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS' ngx.header['Access-Control-Allow-Headers'] = 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' ngx.header['Access-Control-Expose-Headers'] = 'Content-Length,Content-Range'; - if action == "delete" then local red = redis:new() local okredis, errredis = red:connect("unix:/tmp/redis.sock") @@ -38,6 +37,18 @@ if action == "delete" then end -- Count the total of deleted pods + local res, err = red:get("deleted_pods_total") + + if res == ngx.null then + ngx.say(err) + ngx.log(ngx.ERR, "deleted_pods_total is not present on Redis. Creating it..") + red:set("deleted_pods_total", 1) + else + incr = res + 1 + ngx.log(ngx.ERR, "deleted_pods_total is present on Redis. Incrementing it..") + red:set("deleted_pods_total", incr) + end + -- Count the total of deleted pods for namespace local res, err = red:get("deleted_pods_total_on_" .. namespace) @@ -49,7 +60,6 @@ if action == "delete" then end end - if action == "list" then url = k8s_url.. "/api/v1/namespaces/" .. namespace .. "/pods"