Add metrics

This commit is contained in:
2022-02-12 16:10:31 +03:00
parent d635578d23
commit 4d03bebb38
4 changed files with 1752 additions and 1 deletions

View File

@@ -0,0 +1,41 @@
export default class UsersCounter {
static bots: {[key: string]: Set<number>} = {};
static allUsers: Set<number> = new Set();
static take(userId: number, bot: string) {
const isExists = this.bots[bot];
if (!isExists) {
this.bots[bot] = new Set();
}
this.bots[bot].add(userId);
this.allUsers.add(userId);
}
static getAllUsersCount(): number {
return this.allUsers.size;
}
static getUsersByBots(): {[bot: string]: number} {
const result: {[bot: string]: number} = {};
Object.keys(this.bots).forEach((bot: string) => result[bot] = this.bots[bot].size);
return result;
}
static getMetrics(): string {
const lines = [];
lines.push(`all_users_count ${this.getAllUsersCount()}`);
const usersByBots = this.getUsersByBots();
Object.keys(usersByBots).forEach((bot: string) => {
lines.push(`users_count{bot="${bot}"} ${usersByBots[bot]}`)
});
return lines.join("\n");
}
}

View File

@@ -12,6 +12,7 @@ import * as Messages from "./messages";
import * as CallbackData from "./callback_data";
import * as BookLibrary from "./services/book_library";
import UsersCounter from '@/analytics/users_counter';
import { createOrUpdateUserSettings, getUserSettings } from './services/user_settings';
import { formatBook, formatAuthor, formatSequence, formatTranslator } from './format';
import { getCallbackArgs, getPaginatedMessage, getPrefixWithQueryCreator, getSearchArgs, registerLanguageSettingsCallback, registerPaginationCommand, registerRandomItemCallback } from './utils';
@@ -45,8 +46,9 @@ export async function createApprovedBot(token: string, state: BotState): Promise
last_name: user.last_name || '',
first_name: user.first_name,
username: user.username || '',
source: ctx.botInfo.username,
source: me.username,
});
UsersCounter.take(user.id, me.username);
}
await next();
});

View File

@@ -11,6 +11,8 @@ import { Telegraf } from 'telegraf';
import env from '@/config';
import getBot, { BotStatuses } from './factory/index';
import UsersCounter from '@/analytics/users_counter';
Sentry.init({
dsn: env.SENTRY_DSN,
@@ -126,6 +128,10 @@ export default class BotsManager {
res.send("Ok!");
});
application.get("/metrics", (req, res) => {
res.send(UsersCounter.getMetrics());
});
application.use((req: Request, res: Response, next: NextFunction) => this.handleUpdate(req, res, next));
this.server = application.listen(env.WEBHOOK_PORT);

1702
yarn.lock Normal file

File diff suppressed because it is too large Load Diff