Forum teuk.org

Prometheus metrics, WHOIS refactor & test suite

in Mediabot · started by TeuK · 1w ago

TeuK · 1w ago

Mediabot 3.2-dev — metrics, WHOIS refactor and test suite

Mediabot 3.2-dev moves forward on three important areas: Prometheus metrics, WHOIS-based user resolution, and automated testing.

This round of work is not just cosmetic. It finishes metrics that were still incomplete in 3.1, cleans up part of the legacy WHOIS pipeline, and adds a dedicated test suite for the metrics layer.

Prometheus metrics now properly wired

Mediabot::Metrics had already declared a large set of metrics, but several of them were still effectively dormant. In practice, they existed but were never updated outside of initialisation, which made the exported /metrics output incomplete and misleading for Grafana.

This has now been corrected.

Ten previously inactive metrics are now wired into real execution paths across multiple modules, including login handling, helpers, admin commands, the main bot logic, and mediabot.pl. As a result, the Prometheus endpoint now reflects much more of what the bot is actually doing in real time. :contentReference[oaicite:1]{index=1} :contentReference[oaicite:2]{index=2}

This includes things like:

  • authentication successes and failures
  • outgoing PRIVMSG and NOTICE
  • database query errors
  • restart and jump actions
  • command errors
  • incoming messages
  • joined and parted channels
  • live nick counts per channel
  • current timer count

One useful side effect is that Grafana dashboards built on top of these metrics now become much more trustworthy and informative. :contentReference[oaicite:3]{index=3}

Metrics logging bug fixed

A bug was also fixed in Mediabot::Metrics::_log().

The metrics layer was sending string levels such as "INFO" and "ERROR" to Mediabot::Log->log(), while the logger expects integers. Perl silently coerced those strings to 0, so the log level handling was wrong in practice.

_log() now normalises both string and numeric levels correctly, which makes internal Metrics logging behave as intended. :contentReference[oaicite:4]{index=4}

WHOIS user resolution cleaned up

The old WHOIS-related code was still relying on getNickInfoWhois(), a legacy helper returning eight positional scalar values. It worked, but it was not a clean fit anymore for the more object-oriented direction of the codebase.

A new helper, get_user_from_whois($hostmask), has now been introduced in Helpers.pm. It uses the same matching logic as before, but returns a proper Mediabot::User object instead of a raw positional list. The WHOIS-triggered handlers in mediabot.pl now use this new approach. :contentReference[oaicite:5]{index=5}

This is a healthier direction for the project: more consistent APIs, less fragile code, and fewer legacy-style return values to keep mentally aligned.

Switch.pm edge case handled

During the WHOIS refactor, one ugly Switch.pm parsing edge case also surfaced.

Using nested if {} blocks with my declarations inside case blocks generated invalid Perl after Switch.pm source filtering. This has been worked around cleanly by replacing those nested blocks with ternary assignments, avoiding extra braces inside the case body. :contentReference[oaicite:6]{index=6}

It is the kind of detail that is easy to underestimate, but documenting it matters because it explains why the code now looks the way it does.

New metrics test suite

A dedicated test file, t/cases/05_metrics.t, has been added to cover Mediabot::Metrics more seriously.

The new suite checks:

  • enabled vs disabled behaviour
  • declare/set/get logic
  • increment and accumulation
  • labelled metrics
  • multi-label handling
  • undeclared metric safety
  • Prometheus rendering
  • escaping rules
  • uptime evolution
  • _log() regression coverage
  • set_build_info() behaviour

The nice part is that this test suite does not require a live IRC connection or a database, which makes it much more practical for repeatable validation. :contentReference[oaicite:7]{index=7}

Files touched

This development pass updated the following areas:

  • Mediabot/Metrics.pm
  • Mediabot/LoginCommands.pm
  • Mediabot/Helpers.pm
  • Mediabot/AdminCommands.pm
  • Mediabot/Mediabot.pm
  • mediabot.pl
  • t/cases/05_metrics.t

Overall

This is a good 3.2-dev step forward.

The metrics side is now much closer to being genuinely useful in production. The WHOIS path is cleaner and more consistent with the rest of the object-oriented codebase. And the new test coverage is exactly the kind of work that helps the project grow without breaking itself every time something gets modernised.

In other words: less dead instrumentation, less legacy glue, more structure.

That is the right direction for Mediabot.

You must be logged in to reply.