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("dotenv").config();
const { combine, timestamp, printf, colorize, align, json } = winston.format;
let lokiUrl = process.env.LOKI_URL;
const logLevels = {
fatal: 0,
@@ -41,7 +42,7 @@ if (
defaultMeta: { service: "topuponline" },
transports: [
new loki({
host: "http://localhost:3100",
host: lokiUrl,
labels: { app: "topuponline" },
json: true,
format: json(),
@@ -57,7 +58,7 @@ if (
log = winston.createLogger({
transports: [
new loki({
host: "http://localhost:3100",
host: lokiUrl,
labels: { app: "topuponline" },
handleExceptions: true,
handleRejections: true,
+1 -6
View File
@@ -3,12 +3,7 @@ const client = require("prom-client");
const topuponline_metrics = new client.Histogram({
name: "topuponline",
help: "this is the metric for getting request data based on code status",
labelNames: [
"route",
"status_code",
"issuer_name",
"method",
],
labelNames: ["route", "status_code", "issuer_name", "date", "month", "year"],
});
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 statusCode = res.body.code;
let method = req.method;
let message = res.body.message;
if (statusCode != "200" && statusCode != "204") {
if (statusCode != "00") {
winstonLoki.log.error({
message: "error",
message: message,
labels: {
method: method,
url: url,
responseTime: responseTime,
status: statusCode,
// method: method,
route: url,
// responseTime: responseTime,
status_code: statusCode,
// data: res.body.data,
},
});
} else {
winstonLoki.log.info({
message: "success",
message: message,
labels: {
method: method,
url: url,
responseTime: responseTime,
status: statusCode,
// method: method,
route: url,
// responseTime: responseTime,
status_code: statusCode,
// data: res.body.data,
},
});
@@ -1,28 +1,39 @@
const { topuponline_metrics } = require("../helpers/prometheusmetrics");
const moment = require("moment");
moment.locale("id");
const prometheusMiddleware = (req, res, next) => {
res.on("finish", () => {
let url = req.originalUrl;
let responseTime = Math.round(parseInt(res.getHeader("x-response-time")));
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") {
topuponline_metrics
if (statusCode != "00") {
topuponline_metrics
.labels({
issuer_name: "CMS",
method: method,
// method: method,
route: url,
status_code: statusCode,
date: date,
month: month,
year: year,
})
.observe(responseTime);
} else {
topuponline_metrics
topuponline_metrics
.labels({
issuer_name: "CMS",
method: method,
// method: method,
route: url,
status_code: statusCode,
date: date,
month: month,
year: year,
})
.observe(responseTime);
}
+6
View File
@@ -21,6 +21,12 @@ DB_PASSWORD_LOCAL=postgres
REDIS_URL=redis://localhost:6379
REDIS_PASSWORD=
# ==================================== LOKI =====================================
LOKI_URL=http://10.20.14.30:3100
# ==================================== APP PROMETHEUS =====================================
PROM_APP = topuponline
# ================================== SECRET KEY ==================================
# FOR PRODUCTION
#SECRET_KEY=U2FsdGVkX19keaNhiBfdNSWnKA2a8Z5deBu/KkU6BNg=
+2 -1
View File
@@ -26,7 +26,8 @@ const {
const app = express();
register.setDefaultLabels({ app: 'topuponline' });
let promApp = process.env.PROM_APP || "topuponline";
register.setDefaultLabels({ app: promApp });
client.collectDefaultMetrics({ register });
register.registerMetric(topuponline_metrics);