Compare commits

...

2 Commits

Author SHA1 Message Date
tedbearr 0437eaab25 update middleware monitoring 2023-04-12 15:42:31 +07:00
tedbearr 4a98a1c5cd update middleware monitoring 2023-04-12 14:48:13 +07:00
6 changed files with 41 additions and 26 deletions
+3 -2
View File
@@ -4,6 +4,7 @@ const loki = require("winston-loki");
require("winston-daily-rotate-file"); require("winston-daily-rotate-file");
require("dotenv").config(); require("dotenv").config();
const { combine, timestamp, printf, colorize, align, json } = winston.format; const { combine, timestamp, printf, colorize, align, json } = winston.format;
let lokiUrl = process.env.LOKI_URL;
const logLevels = { const logLevels = {
fatal: 0, fatal: 0,
@@ -41,7 +42,7 @@ if (
defaultMeta: { service: "topuponline" }, defaultMeta: { service: "topuponline" },
transports: [ transports: [
new loki({ new loki({
host: "http://localhost:3100", host: lokiUrl,
labels: { app: "topuponline" }, labels: { app: "topuponline" },
json: true, json: true,
format: json(), format: json(),
@@ -57,7 +58,7 @@ if (
log = winston.createLogger({ log = winston.createLogger({
transports: [ transports: [
new loki({ new loki({
host: "http://localhost:3100", host: lokiUrl,
labels: { app: "topuponline" }, labels: { app: "topuponline" },
handleExceptions: true, handleExceptions: true,
handleRejections: true, handleRejections: true,
+1 -6
View File
@@ -3,12 +3,7 @@ const client = require("prom-client");
const topuponline_metrics = new client.Histogram({ const topuponline_metrics = new client.Histogram({
name: "topuponline", name: "topuponline",
help: "this is the metric for getting request data based on code status", help: "this is the metric for getting request data based on code status",
labelNames: [ labelNames: ["route", "status_code", "issuer_name", "date", "month", "year"],
"route",
"status_code",
"issuer_name",
"method",
],
}); });
module.exports = { topuponline_metrics }; module.exports = { topuponline_metrics };
+12 -11
View File
@@ -12,26 +12,27 @@ const lokiMiddleware = (req, res, next) => {
let responseTime = Math.round(parseInt(res.getHeader("x-response-time"))); let responseTime = Math.round(parseInt(res.getHeader("x-response-time")));
let statusCode = res.body.code; let statusCode = res.body.code;
let method = req.method; let method = req.method;
let message = res.body.message;
if (statusCode != "200" && statusCode != "204") { if (statusCode != "00") {
winstonLoki.log.error({ winstonLoki.log.error({
message: "error", message: message,
labels: { labels: {
method: method, // method: method,
url: url, route: url,
responseTime: responseTime, // responseTime: responseTime,
status: statusCode, status_code: statusCode,
// data: res.body.data, // data: res.body.data,
}, },
}); });
} else { } else {
winstonLoki.log.info({ winstonLoki.log.info({
message: "success", message: message,
labels: { labels: {
method: method, // method: method,
url: url, route: url,
responseTime: responseTime, // responseTime: responseTime,
status: statusCode, status_code: statusCode,
// data: res.body.data, // data: res.body.data,
}, },
}); });
@@ -1,28 +1,39 @@
const { topuponline_metrics } = require("../helpers/prometheusmetrics"); const { topuponline_metrics } = require("../helpers/prometheusmetrics");
const moment = require("moment");
moment.locale("id");
const prometheusMiddleware = (req, res, next) => { const prometheusMiddleware = (req, res, next) => {
res.on("finish", () => { res.on("finish", () => {
let url = req.originalUrl; let url = req.originalUrl;
let responseTime = Math.round(parseInt(res.getHeader("x-response-time"))); let responseTime = Math.round(parseInt(res.getHeader("x-response-time")));
let statusCode = res.body.code; let statusCode = res.body.code;
let method = req.method; // let method = req.method;
let date = moment().format("D");
let month = moment().format("M");
let year = moment().format("Y");
if (statusCode != "200" && statusCode != "204") { if (statusCode != "00") {
topuponline_metrics topuponline_metrics
.labels({ .labels({
issuer_name: "CMS", issuer_name: "CMS",
method: method, // method: method,
route: url, route: url,
status_code: statusCode, status_code: statusCode,
date: date,
month: month,
year: year,
}) })
.observe(responseTime); .observe(responseTime);
} else { } else {
topuponline_metrics topuponline_metrics
.labels({ .labels({
issuer_name: "CMS", issuer_name: "CMS",
method: method, // method: method,
route: url, route: url,
status_code: statusCode, status_code: statusCode,
date: date,
month: month,
year: year,
}) })
.observe(responseTime); .observe(responseTime);
} }
+6
View File
@@ -21,6 +21,12 @@ DB_PASSWORD_LOCAL=postgres
REDIS_URL=redis://localhost:6379 REDIS_URL=redis://localhost:6379
REDIS_PASSWORD= REDIS_PASSWORD=
# ==================================== LOKI =====================================
LOKI_URL=http://10.20.14.30:3100
# ==================================== APP PROMETHEUS =====================================
PROM_APP = topuponline
# ================================== SECRET KEY ================================== # ================================== SECRET KEY ==================================
# FOR PRODUCTION # FOR PRODUCTION
#SECRET_KEY=U2FsdGVkX19keaNhiBfdNSWnKA2a8Z5deBu/KkU6BNg= #SECRET_KEY=U2FsdGVkX19keaNhiBfdNSWnKA2a8Z5deBu/KkU6BNg=
+2 -1
View File
@@ -26,7 +26,8 @@ const {
const app = express(); const app = express();
register.setDefaultLabels({ app: 'topuponline' }); let promApp = process.env.PROM_APP || "topuponline";
register.setDefaultLabels({ app: promApp });
client.collectDefaultMetrics({ register }); client.collectDefaultMetrics({ register });
register.registerMetric(topuponline_metrics); register.registerMetric(topuponline_metrics);