Skip to content

Instantly share code, notes, and snippets.

@mieciu
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mieciu/0cff51ac7796ad6da43a to your computer and use it in GitHub Desktop.
Save mieciu/0cff51ac7796ad6da43a to your computer and use it in GitHub Desktop.
Hosted graphite low-level but elegant feeding
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)
@mieciu
Copy link
Author

mieciu commented Jul 15, 2015

Python3 compliant OFC 🌵

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment