-
Notifications
You must be signed in to change notification settings - Fork 813
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[collector][emitter] Split metric payloads bigger than 2MB #3454
Merged
olivielpeau
merged 3 commits into
master
from
olivielpeau/split-collector-metrics-payloads
Aug 21, 2017
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,13 @@ | |
control_char_re = re.compile('[%s]' % re.escape(control_chars)) | ||
|
||
|
||
# Only enforced for the metrics API on our end, for now | ||
MAX_COMPRESSED_SIZE = 2 << 20 # 2MB, the backend should accept up to 3MB but let's be conservative here | ||
# maximum depth of recursive calls to payload splitting function, a bit arbitrary (we don't want too much | ||
# depth in the recursive calls, but we want to give up only when some metrics are clearly too large) | ||
MAX_SPLIT_DEPTH = 2 | ||
|
||
|
||
def remove_control_chars(s, log): | ||
if isinstance(s, str): | ||
sanitized = control_char_re.sub('', s) | ||
|
@@ -73,49 +80,134 @@ def sanitize_payload(item, log, sanitize_func): | |
|
||
return item | ||
|
||
def post_payload(url, message, agentConfig, log): | ||
|
||
def post_payload(url, message, serialize_func, agentConfig, log): | ||
log.debug('http_emitter: attempting postback to ' + string.split(url, "api_key=")[0]) | ||
|
||
try: | ||
try: | ||
payload = json.dumps(message) | ||
except UnicodeDecodeError: | ||
newmessage = sanitize_payload(message, log, remove_control_chars) | ||
try: | ||
payload = json.dumps(newmessage) | ||
except UnicodeDecodeError: | ||
log.info('Removing undecodable characters from payload') | ||
newmessage = sanitize_payload(newmessage, log, remove_undecodable_chars) | ||
payload = json.dumps(newmessage) | ||
except UnicodeDecodeError as ude: | ||
log.error('http_emitter: Unable to convert message to json %s', ude) | ||
payloads = serialize_func(message, MAX_COMPRESSED_SIZE, 0, log) | ||
except UnicodeDecodeError: | ||
log.exception('http_emitter: Unable to convert message to json') | ||
# early return as we can't actually process the message | ||
return | ||
except RuntimeError as rte: | ||
log.error('http_emitter: runtime error dumping message to json %s', rte) | ||
except RuntimeError: | ||
log.exception('http_emitter: runtime error dumping message to json') | ||
# early return as we can't actually process the message | ||
return | ||
except Exception as e: | ||
log.error('http_emitter: unknown exception processing message %s', e) | ||
except Exception: | ||
log.exception('http_emitter: unknown exception processing message') | ||
return | ||
|
||
zipped = zlib.compress(payload) | ||
for payload in payloads: | ||
try: | ||
headers = get_post_headers(agentConfig, payload) | ||
r = requests.post(url, data=payload, timeout=5, headers=headers) | ||
|
||
log.debug("payload_size=%d, compressed_size=%d, compression_ratio=%.3f" | ||
% (len(payload), len(zipped), float(len(payload))/float(len(zipped)))) | ||
r.raise_for_status() | ||
|
||
if r.status_code >= 200 and r.status_code < 205: | ||
log.debug("Payload accepted") | ||
|
||
except Exception: | ||
log.exception("Unable to post payload.") | ||
|
||
|
||
def serialize_payload(message, log): | ||
payload = "" | ||
try: | ||
headers = get_post_headers(agentConfig, zipped) | ||
r = requests.post(url, data=zipped, timeout=5, headers=headers) | ||
payload = json.dumps(message) | ||
except UnicodeDecodeError: | ||
newmessage = sanitize_payload(message, log, remove_control_chars) | ||
try: | ||
payload = json.dumps(newmessage) | ||
except UnicodeDecodeError: | ||
log.info('Removing undecodable characters from payload') | ||
newmessage = sanitize_payload(newmessage, log, remove_undecodable_chars) | ||
payload = json.dumps(newmessage) | ||
|
||
r.raise_for_status() | ||
return payload | ||
|
||
if r.status_code >= 200 and r.status_code < 205: | ||
log.debug("Payload accepted") | ||
|
||
except Exception: | ||
log.exception("Unable to post payload.") | ||
def serialize_and_compress_legacy_payload(legacy_payload, max_compressed_size, depth, log): | ||
""" | ||
Serialize and compress the legacy payload | ||
""" | ||
serialized_payload = serialize_payload(legacy_payload, log) | ||
zipped = zlib.compress(serialized_payload) | ||
log.debug("payload_size=%d, compressed_size=%d, compression_ratio=%.3f" | ||
% (len(serialized_payload), len(zipped), float(len(serialized_payload))/float(len(zipped)))) | ||
|
||
|
||
compressed_payloads = [zipped] | ||
|
||
if len(zipped) > max_compressed_size: | ||
# let's just log a warning for now, splitting the legacy payload is tricky | ||
log.warning("collector payload is above the limit of %dKB compressed", max_compressed_size/(1 << 10)) | ||
|
||
return compressed_payloads | ||
|
||
|
||
def serialize_and_compress_metrics_payload(metrics_payload, max_compressed_size, depth, log): | ||
""" | ||
Serialize and compress the metrics payload | ||
If the compressed payload is too big, we attempt to split it into smaller payloads and call this | ||
function recursively on each smaller payload | ||
""" | ||
compressed_payloads = [] | ||
|
||
serialized_payload = serialize_payload(metrics_payload, log) | ||
zipped = zlib.compress(serialized_payload) | ||
compression_ratio = float(len(serialized_payload))/float(len(zipped)) | ||
log.debug("payload_size=%d, compressed_size=%d, compression_ratio=%.3f" | ||
% (len(serialized_payload), len(zipped), compression_ratio)) | ||
|
||
if len(zipped) < max_compressed_size: | ||
compressed_payloads.append(zipped) | ||
else: | ||
series = metrics_payload["series"] | ||
|
||
if depth > MAX_SPLIT_DEPTH: | ||
log.error("Maximum depth of payload splitting reached, dropping the %d metrics in this chunk", len(series)) | ||
return compressed_payloads | ||
|
||
# Try to account for the compression when estimating the number of chunks needed to get small-enough chunks | ||
n_chunks = len(zipped)/max_compressed_size + 1 + int(compression_ratio/2) | ||
log.debug("payload is too big (%d bytes), splitting it in %d chunks", len(zipped), n_chunks) | ||
|
||
series_per_chunk = len(series)/n_chunks + 1 | ||
|
||
for i in range(n_chunks): | ||
# Create each chunk and make them go through this function recursively ; increment the `depth` of the recursive call | ||
compressed_payloads.extend( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🍰 |
||
serialize_and_compress_metrics_payload( | ||
{ | ||
"series": series[i*series_per_chunk:(i+1)*series_per_chunk] | ||
}, | ||
max_compressed_size, | ||
depth+1, | ||
log | ||
) | ||
) | ||
|
||
return compressed_payloads | ||
|
||
|
||
def serialize_and_compress_checkruns_payload(checkruns_payload, max_compressed_size, depth, log): | ||
""" | ||
Serialize and compress the checkruns payload | ||
""" | ||
serialized_payload = serialize_payload(checkruns_payload, log) | ||
zipped = zlib.compress(serialized_payload) | ||
log.debug("payload_size=%d, compressed_size=%d, compression_ratio=%.3f" | ||
% (len(serialized_payload), len(zipped), float(len(serialized_payload))/float(len(zipped)))) | ||
|
||
compressed_payloads = [zipped] | ||
|
||
if len(zipped) > max_compressed_size: | ||
# let's just log a warning for now, splitting the legacy payload is tricky | ||
log.warning("checkruns payload is above the limit of %dKB compressed", max_compressed_size/(1 << 10)) | ||
|
||
return compressed_payloads | ||
|
||
|
||
def split_payload(legacy_payload): | ||
|
@@ -154,6 +246,7 @@ def split_payload(legacy_payload): | |
|
||
return legacy_payload, metrics_payload, checkruns_payload | ||
|
||
|
||
def http_emitter(message, log, agentConfig, endpoint): | ||
api_key = message.get('apiKey') | ||
|
||
|
@@ -170,13 +263,13 @@ def http_emitter(message, log, agentConfig, endpoint): | |
legacy_payload, metrics_payload, checkruns_payload = split_payload(message) | ||
|
||
# Post legacy payload | ||
post_payload(legacy_url, legacy_payload, agentConfig, log) | ||
post_payload(legacy_url, legacy_payload, serialize_and_compress_legacy_payload, agentConfig, log) | ||
|
||
# Post metrics payload | ||
post_payload(metrics_endpoint, metrics_payload, agentConfig, log) | ||
post_payload(metrics_endpoint, metrics_payload, serialize_and_compress_metrics_payload, agentConfig, log) | ||
|
||
# Post check runs payload | ||
post_payload(checkruns_endpoint, checkruns_payload, agentConfig, log) | ||
post_payload(checkruns_endpoint, checkruns_payload, serialize_and_compress_checkruns_payload, agentConfig, log) | ||
|
||
|
||
def get_post_headers(agentConfig, payload): | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a really cool, elegant solution.
However, it took me a bit to understand everything that was going on in it. I think it could stand to use a few comments. I think it would be very easy to make a mistake in editing this function in the future without some added clarity, and I specified some of the places where I think it could stand to be clearer in my other comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very valid comment, I'm going to document this more