mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-05 12:46:27 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0437eaab25 | |||
| 4a98a1c5cd |
@@ -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,
|
||||
|
||||
@@ -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,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);
|
||||
}
|
||||
|
||||
@@ -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
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user