tdlight-team/tdlight-telegram-bot-api

Enhance stats

MarcoBuster opened this issue · 8 comments

Statistics can be helpful in many ways such as monitoring the instance and implement load balancers.

  • Currently, the stats format is easy to read for an human but hard to parse for a script so we need to add an option to retrieve them as JSON (with backwards compatibility).
  • @andrew-ld suggests to implement a public method to read (own) bot statistics from the public endpoint, like this: https://botapi.example.org/botID:TOKEN/getStats with a command line option or environment option to enable/disable it.

Progress tracker

  • JSON format
  • Toggle to hide sensible data
  • Per-bot public statistics
  • Documentation

I'm currently working on 1, albeit using the usual JsonBuilder to get the stats.
That way we can easily extract and re-use that for any bot method.

I'm also adding a toggle to hide the sensible data (bot token so far) from it, so it can be served public (json or not).

image

Hmmm, I'm stuck with the bots.

It starts at

for (auto top_bot_id : top_bot_ids) {
auto *client_info = clients_.get(top_bot_id.second);
CHECK(client_info);
auto bot_info = client_info->client_->get_actor_unsafe()->get_bot_info();
sb << "\n";
sb << "id\t" << bot_info.id_ << "\n";

goes through all the bots connected (which could be a very long list)
and writes as many as possible to the buffer, until that buffer is full:
if (sb.is_error()) {
break;
}
}

However I haven't find a way to really mimic that with how the Json works.

I can probably have that write to the buffer as well, similar to how the text does it,
but in case of that overflow we would end up without the closing tags (]}) which is rather suboptimal.

I would say for a first implementation that probably wouldn't be an issue for most, as most of those self hosted instances will run with only a few bots, and if it get's too long you'll just have to fall back to the the non-json version for now.

Awesome work, thank you. Keep us updated!

Well I'm less in control of that buffer than I thought I would be when using the regular json stuff.
I think I will reduce the "bots" array to just a list of id's which then could be queried independently, over the normal api or something

While add it I also added a CLI flag to disable the display of sensible data (bot tokens + Webhook urls), which enables the stats to be hosted public.

If someone already wants to give it a try, 374928c seems to be a working one.
There are still a few fields missing.
Regarding the buffer size problem, I now show only the internal id + the ranking if it's more than 100 active bots.