diff --git a/dogstatsd.py b/dogstatsd.py index a91c8690f3..d5e4be3d18 100755 --- a/dogstatsd.py +++ b/dogstatsd.py @@ -19,6 +19,7 @@ import signal import socket import sys +import zlib from time import time import threading from urllib import urlencode @@ -41,9 +42,17 @@ FLUSH_LOGGING_INITIAL = 10 FLUSH_LOGGING_COUNT = 5 EVENT_CHUNK_SIZE = 50 +COMPRESS_THRESHOLD = 1024 def serialize_metrics(metrics): - return json.dumps({"series" : metrics}) + serialized = json.dumps({"series" : metrics}) + if len(serialized) > COMPRESS_THRESHOLD: + headers = {'Content-Type': 'application/json', + 'Content-Encoding': 'deflate'} + serialized = zlib.compress(serialized) + else: + headers = {'Content-Type': 'application/json'} + return serialized, headers def serialize_event(event): return json.dumps(event) @@ -146,8 +155,7 @@ def flush(self): def submit(self, metrics): # Copy and pasted from dogapi, because it's a bit of a pain to distribute python # dependencies with the agent. - body = serialize_metrics(metrics) - headers = {'Content-Type':'application/json'} + body, headers = serialize_metrics(metrics) method = 'POST' params = {}