Skip to content

Feature request - public registry._names_to_collectors access #1071

Open

Description

@Lancetnik

Now we are working on Prometheus support in FastStream and faced with a problem. Shared registry between different middlewares leads to MetricsContainer duplication. So, we would like to have a public API to chech registry for already registered objects.

Can you please provide public access to this property some way?

https://github.com/prometheus/client_python/blob/master/prometheus_client/registry.py#L31

I think, the easiest way to save old API and provide us with a new one - add a public property like in the following snipper

class CollectorRegistry(Collector):
    def __init__(self):
        self._names_to_collectors: Dict[str, Collector] = {}

    @property
    def names_to_collectors(self):
        return self._names_to_collectors

Activity

  1. Lancetnik commented on Nov 19, 2024

    @Lancetnik
    Author
  2. csmarchbanks commented on Nov 19, 2024

    @csmarchbanks
    Member

    Is it not sufficient to call collect() and see what all is returned to get a list of all metrics?

    My concern with adding that function is that not all registries are instances of CollectorRegistry, some applications may use their own implementation. Not to mention things like custom collectors.

  3. Lancetnik commented on Nov 20, 2024

    @Lancetnik
    Author

    To clarify - we just want to avoid double metrics registration

    counter_name = "..."
    
    counter = (
        registry._names_to_collectors.get(counter_name)
    ) or Counter(
        name=counter_name,
        ...
        registry=registry,
    )

    https://github.com/airtai/faststream/blob/main/faststream/prometheus/container.py#L40

  4. Lancetnik commented on Nov 20, 2024

    @Lancetnik
    Author

    Well, seems like we can't create any metric with custom CollectorRegistry implementation - all registry options are Optional[CollectorRegistry] = REGISTRY type

    https://github.com/prometheus/client_python/blob/master/prometheus_client/metrics.py#L132

    So, the initial point about CollectorRegistry property still actual

  5. draincoder commented on Nov 20, 2024

    @draincoder

    Is it not sufficient to call collect() and see what all is returned to get a list of all metrics?

    collect() returns only non-zero metrics and resets their state. In addition, we need to get an instance of the metric by the name of the metric.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions