Llandy3d / pytheus

experimenting with a new prometheus client for python

Home Page:https://pythe.us/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Registry module for collecting metrics

Llandy3d opened this issue · comments

Info: https://prometheus.io/docs/instrumenting/writing_clientlibs/#overall-structure

We should have a CollectorRegistry as described in the link above, that will allow for metrics to be registered and unregistered.
A metric should be able to be registered to multiple registries, and by default it should be automatic the registration to the default registry.

The approach used in python libraries is to have a global variable that will be imported and shared, each newly created metric will automatically add itself to this global registry if not forbidden to.

It's a good approach for usability, but might have some drawbacks when you want to customize this default registry possibly adding custom functionality, users might find themselves having to create a different registry and manually specify this new registry on metrics creatiion. It could get more complicated depending on third party libraries offering metrics depending on their availability on specifying a custom registry.

To remedy that, an RegistryProxy object could be considered, as the global object, delegating all the operation to the actual registry but with the distinction of allowing the user to set a custom registry that will be used on startup. This would give flexibility of customizing the registry behaviour while keeping the nicety of metrics registering by default on creation to the default "proxy".

A main concept here is the one of Collector, a registry in the end is a collector, an object that provides a collect() function returning zero or more metrics/samples.
A python protocol would be a good candidate to describe this behaviour for example:

from typing import Protocol

class Collector(Protocol):

    def collect(self) -> ...:
        ...