Last active
March 24, 2023 14:28
-
-
Save hnez/3fb252cb6c204a34e7396310997a0d30 to your computer and use it in GitHub Desktop.
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
import itertools | |
import socket | |
import time | |
import threading | |
import urllib.request | |
import bottle | |
def statsd_publisher(): | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
for i in itertools.count(): | |
msg = f'foo:{i}|c' | |
sock.sendto(msg.encode(), ("localhost", 8125)) | |
print(f"statsd:\n {msg}") | |
time.sleep(2) | |
def http_poller(): | |
while True: | |
try: | |
contents = urllib.request.urlopen("http://localhost:9103").read() | |
lines = contents.decode('utf-8').split('\n') | |
print("write_prometheus:\n " + '\n '.join(lines)) | |
except KeyboardInterrupt: | |
break | |
except Exception as e: | |
print("write_prometheus:", e) | |
time.sleep(5) | |
def http_server(): | |
def index(): | |
contents = bottle.request.body.read() | |
lines = contents.decode('utf-8').split('\n') | |
print("write_http:\n " + '\n '.join(lines)) | |
return [""] | |
app = bottle.Bottle() | |
app.post("/", callback=index) | |
app.run(host="localhost", port=9000, quiet=True) | |
threads = [ | |
threading.Thread(target=statsd_publisher), | |
threading.Thread(target=http_poller), | |
threading.Thread(target=http_server), | |
] | |
for t in threads: | |
t.start() | |
for t in threads: | |
t.join() |
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
statsd: | |
foo:0|c | |
write_prometheus: | |
# collectd/write_prometheus 5.12.0.301.g8380269+ at localhost | |
write_http: | |
PUTMETRIC collectd_statsd_derive_total type=COUNTER time=1679668094.413 interval=3.000 label:instance="localhost" label:statsd="foo" 0 | |
statsd: | |
foo:1|c | |
statsd: | |
foo:2|c | |
write_http: | |
PUTMETRIC collectd_statsd_derive_total type=COUNTER time=1679668097.413 interval=3.000 label:instance="localhost" label:statsd="foo" 3 | |
write_prometheus: | |
# HELP collectd_statsd_derive_total | |
# TYPE collectd_statsd_derive_total counter | |
collectd_statsd_derive_total{instance="localhost",statsd="foo"} 3 1679668097413 | |
# collectd/write_prometheus 5.12.0.301.g8380269+ at localhost | |
statsd: | |
foo:3|c | |
write_http: | |
PUTMETRIC collectd_statsd_derive_total type=COUNTER time=1679668100.413 interval=3.000 label:instance="localhost" label:statsd="foo" 6 | |
statsd: | |
foo:4|c | |
statsd: | |
foo:5|c | |
write_prometheus: | |
# HELP collectd_statsd_derive_total | |
# TYPE collectd_statsd_derive_total counter | |
collectd_statsd_derive_total{instance="localhost",statsd="foo"} 6 1679668100413 | |
# collectd/write_prometheus 5.12.0.301.g8380269+ at localhost | |
write_http: | |
PUTMETRIC collectd_statsd_derive_total type=COUNTER time=1679668103.413 interval=3.000 label:instance="localhost" label:statsd="foo" 15 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment