Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset CollectorRegistry of Prometheus lib after each unit test

I have a class A that initializes a Counter in its init

from prometheus_client import Counter
class A:
    def __init__(self):
        self.my_counter = Counter('an_awesome_counter')
    def method_1(self):
        return 1
    def method_2(self):
        return 2

Then I write test class :

import unittest
import A

class ATests(unittest.TestCase):
    def setUp(self):
        self.a = A()
    def tearDown(self):
        self.a = None
    def method_1_test(self):
        ....
    def method_2_test(self):
        ....

Thing is, if I run the test separately, they are fine. Yet when I run them together (Run the whole ATests class), I have the error as:

ValueError: Duplicated timeseries in CollectorRegistry: {'an_awesome_counter'}

So it seems that the python environment isn't reset after each test run. I check the CollectorRegistry and there is a method to unregister collector, but it seems a bit ugly to do that.

I wonder if there is another way to solve this problem? Like forcing the test to run with new environment every time for example..

Thank you.

like image 658
Xitrum Avatar asked Nov 07 '22 15:11

Xitrum


1 Answers

For the moment, I moved a = A() out of setup(self) and turned it into a class variable as a workaround solution

like image 177
Xitrum Avatar answered Nov 12 '22 18:11

Xitrum