2021-07-28

Unable to get query or JSON keys from Prometheus HTTP Server

I want to get a certain metric: zcash_difficulty_gauge from the Prometheus HTTP Server but I am facing these errors: i) JSON not available ii) Request URL does not return the metric, instead returns the whole metrics page.

My code:

query = "http://localhost:8094/api/v1/query\?query\=zcash_difficulty_gauge"
response = requests.get(query)

Then I face JSON error for the following command:

In [39]: response.json()
---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-39-34c975fa6377> in <module>
----> 1 response.json()

~/workspace/source/graphstore/k8s/gcp/neo4j/venv/lib/python3.8/site-packages/requests/models.py in json(self, **kwargs)
    908                     # used.
    909                     pass
--> 910         return complexjson.loads(self.text, **kwargs)
    911
    912     @property

/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/json/__init__.py in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    355             parse_int is None and parse_float is None and
    356             parse_constant is None and object_pairs_hook is None and not kw):
--> 357         return _default_decoder.decode(s)
    358     if cls is None:
    359         cls = JSONDecoder

/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/json/decoder.py in decode(self, s, _w)
    335
    336         """
--> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338         end = _w(s, end).end()
    339         if end != len(s):

/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/json/decoder.py in raw_decode(self, s, idx)
    353             obj, end = self.scan_once(s, idx)
    354         except StopIteration as err:
--> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
    356         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Even if I try the curl command:

curl http://localhost:8094/api/v1/query\?query\=zcash_difficulty_gauge

I get the whole page in return and not just the metric that I want.

The URL: localhost:8094 is the whole page and it is as follows:

# HELP python_gc_objects_collected_total Objects collected during gc
# TYPE python_gc_objects_collected_total counter
python_gc_objects_collected_total{generation="0"} 314.0
python_gc_objects_collected_total{generation="1"} 58.0
python_gc_objects_collected_total{generation="2"} 0.0
# HELP python_gc_objects_uncollectable_total Uncollectable object found during GC
# TYPE python_gc_objects_uncollectable_total counter
python_gc_objects_uncollectable_total{generation="0"} 0.0
python_gc_objects_uncollectable_total{generation="1"} 0.0
python_gc_objects_uncollectable_total{generation="2"} 0.0
# HELP python_gc_collections_total Number of times this generation was collected
# TYPE python_gc_collections_total counter
python_gc_collections_total{generation="0"} 36.0
python_gc_collections_total{generation="1"} 3.0
python_gc_collections_total{generation="2"} 0.0
# HELP python_info Python platform information
# TYPE python_info gauge
python_info{implementation="CPython",major="3",minor="8",patchlevel="2",version="3.8.2"} 1.0
# HELP zcash_difficulty_gauge zcash_difficulty_gauge gauge
# TYPE zcash_difficulty_gauge gauge
zcash_difficulty_gauge 1237.0

My server script is:

from typing import Counter
from prometheus_client import start_http_server, Gauge
from time import sleep


start_http_server(8094)

ZCASH_DIFFICULTY_GAUGE = Gauge('zcash_difficulty_gauge', 'zcash_difficulty_gauge gauge')
counter = 0
while True:
    sleep(10)
    ZCASH_DIFFICULTY_GAUGE.set(1004 + counter)
    counter += 1

I have tried reading the documents but it seems I am doing the right thing basis these docs. I also tried to understand what another similar post says but the solution seems to what I am doing right now.

How do I get the particular metric using Python?



from Recent Questions - Stack Overflow https://ift.tt/2UU52oi
https://ift.tt/eA8V8J

No comments:

Post a Comment