wounky
wounky

Reputation: 97

How to push a gauge with multiple metrics to Prometheus? Problem with gateway

Exposing a prometheus gauge with multiple metrics via a Pushgateway throws an error of "Gauge" object has no attribute '_value'. Once the line throwing an error is commented, push_to_gateway() throws

urlopen error [WinError 10061] No connection could be made because the target machine actively refused it

I do not get any response while trying to enter http://localhost:9091/.


I tried to expose metrics via a standard method through HTTP client. However, python code will only run on demand, and with parameters. It will run for a few seconds and exit. I decided to try a Pushgateway. I tried to follow the guide in https://github.com/prometheus/client_python#exporting-to-a-pushgateway. I installed prometheus locally and ran the client after editing yaml file by replacing scrape config with:

scrape_configs:
 - job_name: pushgateway
   honor_labels: true
   static_configs:
    - targets:
      - localhost:9091

Actual code:

# ( Class attribute )
iv_registry = CollectorRegistry()
# Gauge to be passed to the Prometheus     
iv_gauge = Gauge(ic_gauge_name,
                     ic_gauge_docu_labels, ic_labels_list, registry=iv_registry)


    def __create_gauge(self):
        """Fill gauge to be passed to the prometheus and graffana"""
        try:               
            # Set labels and assign a metric
            self.iv_gauge.labels( label1 = "AAA", label2 =  "BBB", label3 = "CCC"  ).set(4)                          
            self.iv_gauge.labels( label1 = "AAA", label2 =  "BBB", label3 = "DDD"  ).set(0)                          

            # expose in a batch mode
            self.iv_gauge.set_to_current_time() # Does it have to be here? My gauge itself does not have any _value, on matrics with labels store _values
            push_to_gateway('localhost:9091', job='batchA', registry=self.iv_registry)

I would like to see a gauge with different metrics pushed to Pushgateway, scraped by Prometheus and for local tests, to be able to display it somewhere on localhost like it is done with http://localhost:8000/ via HTTP server exposure. Please find sample below.

TYPE gauge_name gauge
gauge_name{label1="AAA",label2="BBB",label3="CCC"} 4.0
gauge_name{label1="AAA",label2="BBB",label3="DDD"} 0.0

Upvotes: 2

Views: 8490

Answers (1)

samuel161
samuel161

Reputation: 281

Paste the following into a Python interpreter:

from prometheus_client import CollectorRegistry, Gauge, push_to_gateway

registry = CollectorRegistry()
g = Gauge('job_last_success_unixtime', 'Last time a batch job successfully finished', registry=registry)
g.set_to_current_time()
push_to_gateway('localhost:9091', job='batchA', registry=registry)

Upvotes: -1

Related Questions