Created
January 27, 2013 22:33
-
-
Save psobot/4651010 to your computer and use it in GitHub Desktop.
An absurdly simple, non-blocking emitter for Square's Cube data collection framework.
This file contains hidden or 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
""" | |
Simple, asynchronous, nonblocking UDP emitter for Cube metrics. | |
""" | |
import json | |
import socket | |
from datetime import datetime | |
class Emitter(object): | |
def __init__(self, destination='127.0.0.1', port=1180): | |
self.destination = destination | |
self.port = port | |
def send(self, event_type, event_data={}, **kwargs): | |
if not isinstance(event_data, dict): | |
event_data = {"value": event_data} | |
event = dict(type=event_type, data=event_data) | |
event["time"] = kwargs.get("time", datetime.utcnow().isoformat()) | |
if 'id' in kwargs: | |
event["id"] = kwargs.get("id") | |
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0) | |
self.socket.connect((self.destination, self.port)) | |
self.socket.send(json.dumps(event)) | |
self.socket.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment