diff --git a/application/helpers/loki.js b/application/helpers/loki.js index 8277f06..8415333 100644 --- a/application/helpers/loki.js +++ b/application/helpers/loki.js @@ -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, diff --git a/application/helpers/prometheusmetrics.js b/application/helpers/prometheusmetrics.js index d0eb32f..a4b8d00 100644 --- a/application/helpers/prometheusmetrics.js +++ b/application/helpers/prometheusmetrics.js @@ -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 }; diff --git a/application/middlewares/loki.middleware.js b/application/middlewares/loki.middleware.js index a79260c..f9bbf1c 100644 --- a/application/middlewares/loki.middleware.js +++ b/application/middlewares/loki.middleware.js @@ -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, + // method: method, + route: url, responseTime: responseTime, - status: statusCode, + status_code: statusCode, // data: res.body.data, }, }); } else { winstonLoki.log.info({ - message: "success", + message: message, labels: { - method: method, - url: url, + // method: method, + route: url, responseTime: responseTime, - status: statusCode, + status_code: statusCode, // data: res.body.data, }, }); diff --git a/application/middlewares/prometheus.middleware.js b/application/middlewares/prometheus.middleware.js index daf49a8..3da3a5d 100644 --- a/application/middlewares/prometheus.middleware.js +++ b/application/middlewares/prometheus.middleware.js @@ -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); } diff --git a/env b/env index f1306e5..7a2a6d4 100644 --- a/env +++ b/env @@ -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= diff --git a/server/index.js b/server/index.js index 8802185..ac870cd 100644 --- a/server/index.js +++ b/server/index.js @@ -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);