Skip to content

Commit

Permalink
Add prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
kraftwerk28 committed Dec 20, 2022
1 parent b119e7c commit 2bf196f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
FROM node:16.0.0-alpine AS dev-deps
WORKDIR /opt/build
COPY package.json package-lock.json ./
RUN npm ci --no-audit --silent
RUN npm ci --no-audit

FROM node:16.0.0-alpine AS prod-deps
WORKDIR /opt/app
COPY package.json package-lock.json ./
RUN npm ci --no-audit --only prod --silent
RUN npm ci --no-audit --only prod

FROM dev-deps AS build
WORKDIR /opt/build
Expand Down
43 changes: 35 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Telegraf } from "telegraf";
import util from "util";
import * as path from "path";
import * as crypto from "crypto";
import { createServer } from "http";

import { Composer } from "./composer";
import { Ctx } from "./types";
Expand All @@ -10,6 +11,7 @@ import { initLogger, log } from "./logger";
import { noop, regexp } from "./utils";
import * as middlewares from "./middlewares";
import * as commands from "./commands";
import { registerPromHandlers, getRawMetrics } from "./prometheus";

async function main() {
if (process.env.NODE_ENV === "development") {
Expand Down Expand Up @@ -40,6 +42,8 @@ async function main() {
const username = botInfo.username;
const composer2 = new Composer();

registerPromHandlers(bot);

bot
.use(composer2)
.use(middlewares.getDbChat)
Expand Down Expand Up @@ -196,16 +200,39 @@ async function main() {
break;
case "production": {
log.info("Launching in webhook mode");
const { WEBHOOK_PATH, WEBHOOK_DOMAIN, WEBHOOK_PORT, SERVER_PORT } =
const { WEBHOOK_DOMAIN, WEBHOOK_PORT, SERVER_PORT, BOT_TOKEN } =
process.env;
await bot.launch({
dropPendingUpdates: true,
webhook: {
hookPath: WEBHOOK_PATH,
domain: `${WEBHOOK_DOMAIN}:${WEBHOOK_PORT}`,
port: parseInt(SERVER_PORT!),
},
const proxyPrefix = "/blcklstbot";
const webhookCallback = bot.webhookCallback(
`${proxyPrefix}/${BOT_TOKEN}`,
);
await bot.telegram.setWebhook(
`https://${WEBHOOK_DOMAIN}:${WEBHOOK_PORT}${proxyPrefix}/${BOT_TOKEN}`,
{ drop_pending_updates: true },
);
const server = createServer(async (req, res) => {
if (req.url === `${proxyPrefix}/metrics`) {
try {
const raw = await getRawMetrics();
return res
.writeHead(200, { "Content-Type": "text/plain" })
.end(raw);
} catch (err) {
return res.writeHead(500).end((err as Error).message);
}
}
return webhookCallback(req, res);
});
server.listen(SERVER_PORT);

// await bot.launch({
// dropPendingUpdates: true,
// webhook: {
// hookPath: WEBHOOK_PATH,
// domain: `${WEBHOOK_DOMAIN}:${WEBHOOK_PORT}`,
// port: parseInt(SERVER_PORT!),
// },
// });
break;
}
default:
Expand Down
31 changes: 31 additions & 0 deletions src/prometheus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Counter, register } from "prom-client";
import type { Telegraf } from "telegraf";
import type { MessageSubType } from "telegraf/typings/telegram-types";
import type { Ctx } from "./types";

const promMessageCounter = new Counter({
name: "tg_messages_total",
help: "Message counter",
labelNames: ["media_type"],
});

const promUpdateTypes: MessageSubType[] = [
"text",
"photo",
"sticker",
"photo",
"video",
];

export function registerPromHandlers(bot: Telegraf<Ctx>) {
for (const msgSubType of promUpdateTypes) {
bot.on(msgSubType, (_ctx, next) => {
promMessageCounter.inc({ media_type: msgSubType });
return next();
});
}
}

export function getRawMetrics() {
return register.metrics();
}

0 comments on commit 2bf196f

Please sign in to comment.