cyberdelia / metrology

A library to easily measure what's going on in your python.
https://metrology.readthedocs.org
MIT License
309 stars 25 forks source link

Retrieving an existing gauge, and possibly replacing it. #39

Open miracle2k opened 6 years ago

miracle2k commented 6 years ago

This is currently based on https://github.com/cyberdelia/metrology/pull/38, but that needn't be the case.

There are a couple elements here.

First, there is a bug in the existing code. Calling Metrology.gauge('name', my_gauge_instance) twice fails the second time, because the isinstance call here (https://github.com/cyberdelia/metrology/blob/master/metrology/registry.py#L70) will be called with my_gauge_instance instead of a class object, which fails.

I also run into the case of having a gauge that measures the length of a queue object, and the queue object will be replaced during the lifetime of the program, which means the gauge itself has to be replaced as well (it is not feasible in my case to implement a gauge subclass that can be pointed to a different queue).

Since:

  1. The gauge api requires a custom gauge instance to be passed Metrology.gauge('foo', gauge_instance)

  2. Two calls to these line of code currently return an error.

  3. The proper behaviour of

    Metrology.gauge('foo', gauge_instance1) Metrology.gauge('foo', gauge_instance2)

    not currently beging defined, it made sense to me to implement it such that the latter call will replace the metric in the registry.