client_python icon indicating copy to clipboard operation
client_python copied to clipboard

Labels on function decorator that are defined at runtime

Open kequach opened this issue 4 years ago • 0 comments

Referring back to this thread: https://github.com/prometheus/client_python/issues/157 I wanted to know if there's a way to use the function decorator syntax with labels that will be instantiated at a later point? I need to fetch the label values from another service first. I am currently filling in some dummy values which will give me duplicated metrics:

inference_time_created{edge="EMPTY_SCOPE",line="EMPTY_SCOPE",module="EMPTY_SCOPE",plant="EMPTY_SCOPE",workload="EMPTY_SCOPE"} 1.6311126626366935e+09
inference_time_created{edge="ACTUAL_VALUE",line="ACTUAL_VALUE",module="ACTUAL_VALUE",plant="ACTUAL_VALUE",workload="ACTUAL_VALUE"} 1.6311126626709173e+09

So my question is if there's a way to not have the EMPTY_SCOPE metric on the endpoint.

This is the basic idea of what I'm doing:

scope_keys = ["plant", "edge", "line", "workload", "module"]
scope_values = ["EMPTY_SCOPE"] * 5
inference_time_created= Gauge('inference_time', 'How long (in seconds) does it take for an inference', scope_keys)
inference_time_created_labeled = inference_time_created.labels(*scope_values)

def set_metrics_scope(scope): # this function gets called later in the main code
    scope_values = scope
    inference_time_created_labeled = inference_time_created.labels(*scope_values)

@inference_time_created_labeled.time()
def inference():
    # do some calculations

kequach avatar Sep 08 '21 15:09 kequach