Metrics
Added metrics for grafana
This commit is contained in:
parent
c4048f412b
commit
d07ade36a3
4
bot.js
4
bot.js
@ -133,6 +133,7 @@ bot.use(async (ctx, next) => {
|
|||||||
bot.context.config = require('./ctxconfig.json')
|
bot.context.config = require('./ctxconfig.json')
|
||||||
let id = ctx.from.id
|
let id = ctx.from.id
|
||||||
let username = ctx.from.username
|
let username = ctx.from.username
|
||||||
|
global.messagesCounter.inc({ type: ctx.updateType });
|
||||||
if (username == null) username = ctx.from.id
|
if (username == null) username = ctx.from.id
|
||||||
switch (ctx.updateType) {
|
switch (ctx.updateType) {
|
||||||
case `message`:
|
case `message`:
|
||||||
@ -201,11 +202,12 @@ bot.use(async (ctx, next) => {
|
|||||||
|
|
||||||
return Promise.race([timeoutPromise, nextPromise])
|
return Promise.race([timeoutPromise, nextPromise])
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
|
global.errorsCounter.inc(); // Увеличиваем счётчик ошибок
|
||||||
if (error.message === 'timeout') {
|
if (error.message === 'timeout') {
|
||||||
console.error('timeout', ctx.update)
|
console.error('timeout', ctx.update)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
global.errorsCounter.inc(); // Увеличиваем счётчик ошибок
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
|
39
index.js
39
index.js
@ -1,12 +1,51 @@
|
|||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
|
|
||||||
const sequelize = require('./db'); // Подключение базы данных
|
const sequelize = require('./db'); // Подключение базы данных
|
||||||
|
const { register, Counter } = require('prom-client'); // Подключаем библиотеку для мониторинга
|
||||||
|
|
||||||
|
// Создаём метрики
|
||||||
|
const messagesCounter = new Counter({
|
||||||
|
name: 'bot_messages_total',
|
||||||
|
help: 'Total number of messages received by the bot',
|
||||||
|
labelNames: ['type'],
|
||||||
|
});
|
||||||
|
|
||||||
|
const errorsCounter = new Counter({
|
||||||
|
name: 'bot_errors_total',
|
||||||
|
help: 'Total number of errors occurred in the bot',
|
||||||
|
});
|
||||||
|
|
||||||
// Настраиваем глобальные переменные (опционально)
|
// Настраиваем глобальные переменные (опционально)
|
||||||
global.path = require('path');
|
global.path = require('path');
|
||||||
global.config = require('./config'); // Конфигурация
|
global.config = require('./config'); // Конфигурация
|
||||||
global.database = sequelize; // База данных
|
global.database = sequelize; // База данных
|
||||||
global.utils = require('./utils');
|
global.utils = require('./utils');
|
||||||
|
global.messagesCounter = messagesCounter
|
||||||
|
global.errorsCounter = errorsCounter
|
||||||
|
|
||||||
|
////Metrics
|
||||||
|
|
||||||
|
const express = require('express');
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
|
||||||
|
// Эндпоинт для метрик
|
||||||
|
app.get('/metrics', async (req, res) => {
|
||||||
|
try {
|
||||||
|
res.set('Content-Type', register.contentType);
|
||||||
|
res.end(await register.metrics());
|
||||||
|
} catch (err) {
|
||||||
|
res.status(500).end(err.message);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Запускаем HTTP-сервер на порту 3000
|
||||||
|
app.listen(3003, () => {
|
||||||
|
console.log('Metrics server is running on http://localhost:3003/metrics');
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
////
|
||||||
|
|
||||||
// Инициализация бота
|
// Инициализация бота
|
||||||
require('./bot')
|
require('./bot')
|
10036
json/logs.json
10036
json/logs.json
File diff suppressed because it is too large
Load Diff
34
package-lock.json
generated
34
package-lock.json
generated
@ -22,6 +22,7 @@
|
|||||||
"pg": "^8.6.0",
|
"pg": "^8.6.0",
|
||||||
"pg-hstore": "^2.3.3",
|
"pg-hstore": "^2.3.3",
|
||||||
"pm2": "^5.3.0",
|
"pm2": "^5.3.0",
|
||||||
|
"prom-client": "^15.1.3",
|
||||||
"sequelize": "^6.6.2",
|
"sequelize": "^6.6.2",
|
||||||
"shortid": "^2.2.16",
|
"shortid": "^2.2.16",
|
||||||
"telegraf": "^4.13.1",
|
"telegraf": "^4.13.1",
|
||||||
@ -45,6 +46,14 @@
|
|||||||
"npm": ">=9.0.0"
|
"npm": ">=9.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@opentelemetry/api": {
|
||||||
|
"version": "1.9.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@opentelemetry/api/-/api-1.9.0.tgz",
|
||||||
|
"integrity": "sha512-3giAOQvZiH5F9bMlMiv8+GSPMeqg0dbaeo58/0SlA9sxSqZhnUtxzX9/2FzyhS9sWQf5S0GJE0AKBrFqjpeYcg==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=8.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@pm2/agent": {
|
"node_modules/@pm2/agent": {
|
||||||
"version": "2.0.4",
|
"version": "2.0.4",
|
||||||
"resolved": "https://registry.npmjs.org/@pm2/agent/-/agent-2.0.4.tgz",
|
"resolved": "https://registry.npmjs.org/@pm2/agent/-/agent-2.0.4.tgz",
|
||||||
@ -442,6 +451,11 @@
|
|||||||
"url": "https://github.com/sponsors/sindresorhus"
|
"url": "https://github.com/sponsors/sindresorhus"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/bintrees": {
|
||||||
|
"version": "1.0.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/bintrees/-/bintrees-1.0.2.tgz",
|
||||||
|
"integrity": "sha512-VOMgTMwjAaUG580SXn3LacVgjurrbMme7ZZNYGSSV7mmtY6QQRh0Eg3pwIcntQ77DErK1L0NxkbetjcoXzVwKw=="
|
||||||
|
},
|
||||||
"node_modules/blessed": {
|
"node_modules/blessed": {
|
||||||
"version": "0.1.81",
|
"version": "0.1.81",
|
||||||
"resolved": "https://registry.npmjs.org/blessed/-/blessed-0.1.81.tgz",
|
"resolved": "https://registry.npmjs.org/blessed/-/blessed-0.1.81.tgz",
|
||||||
@ -2284,6 +2298,18 @@
|
|||||||
"node": ">=0.10.0"
|
"node": ">=0.10.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/prom-client": {
|
||||||
|
"version": "15.1.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/prom-client/-/prom-client-15.1.3.tgz",
|
||||||
|
"integrity": "sha512-6ZiOBfCywsD4k1BN9IX0uZhF+tJkV8q8llP64G5Hajs4JOeVLPCwpPVcpXy3BwYiUGgyJzsJJQeOIv7+hDSq8g==",
|
||||||
|
"dependencies": {
|
||||||
|
"@opentelemetry/api": "^1.4.0",
|
||||||
|
"tdigest": "^0.1.1"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": "^16 || ^18 || >=20"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/promptly": {
|
"node_modules/promptly": {
|
||||||
"version": "2.2.0",
|
"version": "2.2.0",
|
||||||
"resolved": "https://registry.npmjs.org/promptly/-/promptly-2.2.0.tgz",
|
"resolved": "https://registry.npmjs.org/promptly/-/promptly-2.2.0.tgz",
|
||||||
@ -2937,6 +2963,14 @@
|
|||||||
"url": "https://www.buymeacoffee.com/systeminfo"
|
"url": "https://www.buymeacoffee.com/systeminfo"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/tdigest": {
|
||||||
|
"version": "0.1.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/tdigest/-/tdigest-0.1.2.tgz",
|
||||||
|
"integrity": "sha512-+G0LLgjjo9BZX2MfdvPfH+MKLCrxlXSYec5DaPYP1fe6Iyhf0/fSmJ0bFiZ1F8BT6cGXl2LpltQptzjXKWEkKA==",
|
||||||
|
"dependencies": {
|
||||||
|
"bintrees": "1.0.2"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/telegraf": {
|
"node_modules/telegraf": {
|
||||||
"version": "4.16.3",
|
"version": "4.16.3",
|
||||||
"resolved": "https://registry.npmjs.org/telegraf/-/telegraf-4.16.3.tgz",
|
"resolved": "https://registry.npmjs.org/telegraf/-/telegraf-4.16.3.tgz",
|
||||||
|
@ -24,6 +24,7 @@
|
|||||||
"pg": "^8.6.0",
|
"pg": "^8.6.0",
|
||||||
"pg-hstore": "^2.3.3",
|
"pg-hstore": "^2.3.3",
|
||||||
"pm2": "^5.3.0",
|
"pm2": "^5.3.0",
|
||||||
|
"prom-client": "^15.1.3",
|
||||||
"sequelize": "^6.6.2",
|
"sequelize": "^6.6.2",
|
||||||
"shortid": "^2.2.16",
|
"shortid": "^2.2.16",
|
||||||
"telegraf": "^4.13.1",
|
"telegraf": "^4.13.1",
|
||||||
|
1
rpg.js
1
rpg.js
@ -30,6 +30,7 @@ const rpg = new Composer();
|
|||||||
rpg.use(async (ctx, next) => {
|
rpg.use(async (ctx, next) => {
|
||||||
if (ctx.update.edited_channel_post) return console.log(`[RPG] Channel post updated`);
|
if (ctx.update.edited_channel_post) return console.log(`[RPG] Channel post updated`);
|
||||||
let id = ctx.from.id
|
let id = ctx.from.id
|
||||||
|
global.messagesCounter.inc({ type: ctx.updateType });
|
||||||
let username = ctx.from.username;
|
let username = ctx.from.username;
|
||||||
if (username == null) username = ctx.from.id;
|
if (username == null) username = ctx.from.id;
|
||||||
const currentTime = utils.getCurrentTime();
|
const currentTime = utils.getCurrentTime();
|
||||||
|
Loading…
Reference in New Issue
Block a user