Created
January 17, 2021 02:08
-
-
Save f41gh7/85b2eb895bb63b93ce46ef73448c62d0 to your computer and use it in GitHub Desktop.
VictoriaMetrics as push gateway.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from prometheus_client import Counter, start_http_server | |
from threading import Thread | |
import requests as re | |
import time | |
JOB_NAME = 'test' | |
INSTANCE = 'localhost' | |
def scrape_and_send(local_url: str, vm_url: str, scrape_interval: int): | |
s = re.Session() | |
scrape_cnt = Counter('self_scrapes_ok_total', 'some help') | |
push_cnt = Counter('self_vm_push_ok_total', 'some help') | |
vm_url_parametrized = f"{vm_url}?extra_label=job={JOB_NAME}&extra_label=instance={INSTANCE}" | |
while True: | |
try: | |
time.sleep(scrape_interval) | |
with s.get(local_url, timeout=3) as local_data: | |
if local_data.status_code != 200: | |
print(f"bad status code, want 200, got {local_data.status_code}") | |
continue | |
scrape_cnt.inc() | |
with s.post(vm_url_parametrized, data=local_data.text, timeout=5) as vm_resp: | |
if vm_resp.status_code != 204: | |
print(f"bad status code, want 204, got {vm_resp.status_code}") | |
continue | |
push_cnt.inc() | |
except Exception as e: | |
print(f"error occurred: {e}") | |
def push_to_vm(local_url: str, vm_url: str, scrape_interval=None) -> Thread: | |
if not scrape_interval: | |
# scrape interval 10s by default | |
scrape_interval = 10 | |
th = Thread(target=scrape_and_send, args=(local_url, vm_url, scrape_interval,)) | |
th.start() | |
return th | |
push_to_vm("http://localhost:8000", "http://localhost:8428/api/v1/import/prometheus") | |
start_http_server(8000) |
vanyakosmos
commented
Jan 17, 2021
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment