Skip to content

Original metrics classes are not directly accessible #150

@beezz

Description

@beezz

This is not an issue, just an observation maybe.

Due to the _MetricWrapper, metric classes (Counter, Gauge, Histogram, Summary) are not directly accessible.

Example:

from unittest.mock import Mock
from prometheus_client import Gauge

set_mock = Mock()
Gauge.set = set_mock

g = Gauge('metric', 'Some metric')

g.set(1)
g.set(2)
g.set(3)
g.set(5)

set_mock.assert_called_once()

Output:

AssertionError: Expected 'mock' to have been called once. Called 0 times.

You can circumvent this issue by accessing the __class__ attribute of metric class instance.

Example:

Gauge('foo', 'bar').__class__.set = set_mock

I suggest to make them accessible the same way the functools.update_wrapper does, via __wrapped__ attribute (update_wrapper code) and of course document it.

I'll gladly prepare PR for this!

Thanks for your great work and have a nice day!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions