Last active
August 29, 2015 14:24
-
-
Save mieciu/0cff51ac7796ad6da43a to your computer and use it in GitHub Desktop.
Hosted graphite low-level but elegant feeding
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 contextlib import contextmanager | |
import socket | |
import os | |
HOSTED_GRAPHITE = ("carbon.hostedgraphite.com", 2003) | |
API_KEY = "ASDFASDF" | |
metric_name = "mieciu.head.request" | |
metric_value = 140 | |
@contextmanager | |
def tcp_connection_to(*args, **kwargs): | |
s = socket.create_connection(*args, **kwargs) | |
yield s | |
s.close() | |
def send_metric(name, value): | |
with tcp_connection_to(HOSTED_GRAPHITE) as conn: | |
conn.send(bytes("{}.{} {}\n".format(API_KEY, name, value), 'UTF-8')) | |
send_metric(metric_name, metric_value) | |
send_metric(metric_name, 120) | |
send_metric("mieciu.head.dummy", 4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Python3 compliant OFC 🌵