Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use local varaiable containing a string to reference the name of a Global Variable

I have a script in which I’m trying to iterate through a list of strings, then take the value of the current variable, convert it to upper-case, then use use the result as a variable name. The end result will be to set the value of a prometheus metric, which is a global variable.

from prometheus_client import start_http_server, Gauge


# List of strings (global variables will be equal but upper-case)
MEASUREMENTS = ['x', 'y', 'z']

# Prometheus Metrics set as global variables
X = Gauge('some_value', 'some_description', ['label1', 'label2']
Y = Gauge('some_value', 'some_description', ['label1', 'label2']
Z = Gauge('some_value', 'some_description', ['label1', 'label2']

...

    # Iterate through list of strings
    for measurement in MEASUREMENTS:                                    
        metric = getattr(some_status, measurement)  # Will be an integer                                    
        upper_measurement = measurement.upper()     # Will equal the global variable name                                 
        [?UPPER_MEASUREMENT?].labels(label1=value_P1, label_2=value).set(metric)  # This is where I need to reference the global variable name

Effectively this:

[?UPPER_MEASUREMENT?].labels(label1=value_P1, label_2=value).set(metric)

Needs to be:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

X.labels(label1=value_P1, label_2=value).set(metric)

I tried:

upper_measurement.labels(label1=value_P1, label_2=value).set(metric)

But of course that will result in an error since it’s a string:

Error: 'str' object has no attribute 'labels'

Not quite sure where to go from here. I could just write a big ugly block setting each metric, but I’d rather avoid that if possible.

>Solution :

Define your metrics in a dictionary:

METRICS = {
    key: Gauge('some_value', 'some_description', ['label1', 'label2']) 
    for key in ['x', 'y', 'z']
}

and then you can easily update all the metrics by iterating over the dictionary:

for key, metric in METRICS.items():                                    
    metric.labels(label1=value_P1, label_2=value).set(getattr(some_status, key))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading