Skip to content

Commit

Permalink
Merge pull request #893 from DataDog/dogstatsd-compression
Browse files Browse the repository at this point in the history
Dogstatsd compression
  • Loading branch information
remh committed Apr 7, 2014
2 parents de14dac + 15b02fb commit 5382845
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions dogstatsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import signal
import socket
import sys
import zlib
from time import time
import threading
from urllib import urlencode
Expand All @@ -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)
Expand Down Expand Up @@ -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 = {}
Expand Down

0 comments on commit 5382845

Please sign in to comment.