client_python
client_python copied to clipboard
Failed to push using push_to_gateway and basic_auth_handler.
After collecting data with Prometheus, it can be pushed to a remote Prometheus server. However, direct pushing fails, returning the following error:
File "/usr/local/lib/python3.10/dist-packages/prometheus_client/exposition.py", line 529, in push_to_gateway
_use_gateway('PUT', gateway, job, registry, grouping_key, timeout, handler)
File "/usr/local/lib/python3.10/dist-packages/prometheus_client/exposition.py", line 621, in _use_gateway
handler(
File "/usr/local/lib/python3.10/dist-packages/prometheus_client/exposition.py", line 444, in handle
default_handler(url, method, timeout, headers, data)()
File "/usr/local/lib/python3.10/dist-packages/prometheus_client/exposition.py", line 381, in handle
resp = build_opener(base_handler).open(request, timeout=timeout)
File "/usr/lib/python3.10/urllib/request.py", line 525, in open
response = meth(req, response)
File "/usr/lib/python3.10/urllib/request.py", line 634, in http_response
response = self.parent.error(
File "/usr/lib/python3.10/urllib/request.py", line 563, in error
return self._call_chain(*args)
File "/usr/lib/python3.10/urllib/request.py", line 496, in _call_chain
result = func(*args)
File "/usr/lib/python3.10/urllib/request.py", line 643, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request
code:
def prometheus_auth_handler(url, method, timeout, headers, data):
username = '123213'
password = '12312'
return basic_auth_handler(url, method, timeout, headers, data, username, password)
resp = requests.post(forta_url, headers=headers, data=payload)
info = resp.text
info = json.loads(info)
for i in info['data']['getMultiSLAs']:
avg_uptime = i['slaAvgOverTime']['uptime']['avgUptime']
avg_SLA = i['slaAvgOverTime']['sla']['avgSLA']
validatorAddr = i['id']
validatorName = forta_ids[validatorAddr]
print(f'validatorCheck{{chainName="{chainName}",validatorName="{validatorName}",validatorAddr="{validatorAddr}", validator_info_type="avg_uptime"}} {avg_uptime}')
print(f'validatorCheck{{chainName="{chainName}",validatorName="{validatorName}",validatorAddr="{validatorAddr}", validator_info_type="avg_SLA"}} {avg_SLA}')
forta_gauge.labels(chainName=chainName, validatorName=validatorName, validatorAddr=validatorAddr, validator_info_type='avg_uptime').set(avg_uptime)
forta_gauge.labels(chainName=chainName, validatorName=validatorName, validatorAddr=validatorAddr, validator_info_type='avg_SLA').set(avg_SLA)
push_to_gateway('http://ip:8429/api/v1/write', job='forta', registry=registry, handler=prometheus_auth_handler)
Can we only push to the gateway now? Is it possible to write to the Prometheus database remotely?
This library does not support pushing directly to Prometheus and does not plan to, it is best to either have Prometheus (or an agent) scrape your service if it is long running, and use push_gateway if your service is short running.