Reset CollectorRegistry of Prometheus lib after each unit test

2024/10/2 22:20:15

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 1def method_2(self):return 2

Then I write test class :

import unittest
import Aclass ATests(unittest.TestCase):def setUp(self):self.a = A()def tearDown(self):self.a = Nonedef 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.

Answer

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

https://en.xdnf.cn/q/70801.html

Related Q&A

Why does Django ORM allow me to omit parameters for NOT NULL fields when creating an object?

Stupid question time. I seem to be able to create an object for a Django model even though I omit a column that was defined as NOT NULL and I dont understand why. Heres my model:class Movie(models.Mo…

Where should i do the django validations for objects and fields?

Im creating a django application which uses both the Django Rest Framework and the plain django-views as entrypoint for users.I want to do validation both independant fields of my models, and on object…

Why does bytes.fromhex() treat some hex values strangely?

Im trying to use the socket library in Python to send bytes of two hex digits to a piece of hardware programmed to accept them. To create the bytes from a user-entered string of hex digits, Im trying …

Extracting beats out of MP3 music with Python

What kind of solutions are there to analyze beats out of MP3 music in Python? The purpose of this would be to use rhythm information to time the keyframes of generated animation, export animation as v…

Switch to popup in python using selenium

How do I switch to a popup window in the below selenium program. Ive looked up for all possible solutions but havent been able to get my head around them. Please help!!from selenium import webdriver fr…

Segmentation fault while redirecting sys.stdout to Tkinter.Text widget

Im in the process of building a GUI-based application with Python/Tkinter that builds on top of the existing Python bdb module. In this application, I want to silence all stdout/stderr from the consol…

Why do pandas and dask perform better when importing from CSV compared to HDF5?

I am working with a system that currently operates with large (>5GB) .csv files. To increase performance, I am testing (A) different methods to create dataframes from disk (pandas VS dask) as well a…

is there any pool for ThreadingMixIn and ForkingMixIn for SocketServer?

I was trying to make an http proxy using BaseHttpServer which is based on SocketServer which got 2 asynchronous Mixins (ThreadingMixIn and ForkingMixIn)the problem with those two that they work on each…

Python - Read data from netCDF file with time as seconds since beginning of measurement

I need to extract values from a netCDf file. I am pretty new to python and even newer this file format. I need to extract time series data at a specific location (lat, lon). I have found that there is …

PyQt Multiline Text Input Box

I am working with PyQt and am attempting to build a multiline text input box for users. However, when I run the code below, I get a box that only allows for a single line of text to be entered. How to …