Skip to content

Best way to avoid ValueError: Duplicated timeseries in CollectorRegistry #745

@4815162342lost

Description

@4815162342lost

Hello,

I have dynamic metric names (new metrics will be added several times in per day). And if i try to create same double metrics i get an Exception:
ValueError: Duplicated timeseries in CollectorRegistry:

But i do not understand, how can i check if metric already exists. Could you please help me?

For example, code:

promet_metricks = {}
def get_beer_can_name_from_onlime_shop_function():
    ''''In real program this huge function to get beers from online shop and parse available beers'''
    return ("Baltika", "Heinz", "Giguli", "Vodka_lite")

def prom():
    beer_cans = get_beer_can_name_from_onlime_shop_function()
    for current_beer_can in beer_cans:
        promet_metricks[current_beer_can] = prometheus_client.Counter(current_beer_can, "Counter can of beer which drink my dad")
        promet_metricks[current_beer_can].inc()
    time.sleep(3)

while True:
    prom()

How can i check that metric already exists and i should not create them.

Currently i am using

try:
    promet_metricks[current_beer_can] = prometheus_client.Counter(current_beer_can, "Counter can of beer which drink my dad")
except ValueError:
    pass

But i do not think that it is best solution.

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