Skip to content
This repository was archived by the owner on Jun 2, 2021. It is now read-only.

Commit 884aad3

Browse files
authored
Merge pull request #38 from tjstansell/max-payload-size
add check for payload size over 1MB
2 parents 9042783 + 6146063 commit 884aad3

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

newrelic_python_agent/agent.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ class NewRelicPythonAgent(helper.Controller):
4545
IGNORE_KEYS = ['license_key', 'proxy', 'endpoint', 'verify_ssl_cert',
4646
'poll_interval', 'wake_interval', 'newrelic_api_timeout', 'skip_newrelic_upload']
4747

48-
MAX_METRICS_PER_REQUEST = 10000
48+
MAX_SIZE_PER_REQUEST = 750*1024 # behavior is undetermined if POST is larger than 1MB, so max at 750KB
49+
MAX_METRICS_PER_REQUEST = 10000 # limit is 20,000 per POST
4950
PLATFORM_URL = 'https://platform-api.newrelic.com/platform/v1/metrics'
5051
WAKE_INTERVAL = 60
5152

@@ -311,6 +312,7 @@ def process_config_plugins(self):
311312

312313
def send_data_to_newrelic(self):
313314
"""Process the queue of metric plugin results"""
315+
size = 0
314316
metrics = 0
315317
components = list()
316318
while self.publish_queue.qsize():
@@ -322,11 +324,14 @@ def send_data_to_newrelic(self):
322324
for component in data:
323325
self.process_min_max_values(component)
324326
components.append(component)
327+
# track a rough approximation of payload size
328+
size += len(json.dumps(component, ensure_ascii=False))
325329
metrics += len(component['metrics'].keys())
326-
if metrics >= self.MAX_METRICS_PER_REQUEST:
330+
if metrics >= self.MAX_METRICS_PER_REQUEST or size >= self.MAX_SIZE_PER_REQUEST:
327331
self.send_components(components, metrics)
328332
components = list()
329333
metrics = 0
334+
size = 0
330335

331336
if metrics > 0:
332337
LOGGER.debug('Done, will send remainder of %i metrics', metrics)
@@ -341,13 +346,7 @@ def send_components(self, components, metrics):
341346
LOGGER.warning('No metrics to send to NewRelic this interval')
342347
return
343348

344-
if self.config.application.get('skip_newrelic_upload'):
345-
LOGGER.info('Not sending %i metrics to NewRelic', metrics)
346-
return
347-
348-
LOGGER.info('Sending %i metrics to NewRelic', metrics)
349349
body = {'agent': self.agent_data, 'components': components}
350-
LOGGER.debug(body)
351350

352351
s = StringIO()
353352
g = gzip.GzipFile(fileobj=s, mode='w')
@@ -356,8 +355,16 @@ def send_components(self, components, metrics):
356355
gzipped_body = s.getvalue()
357356
request_body = gzipped_body
358357

359-
LOGGER.debug('POST data size before compression: %i bytes', len(json.dumps(body, ensure_ascii=False)))
360-
LOGGER.debug('POST data size after compression: %i bytes', len(request_body))
358+
LOGGER.info('%sSending %i metrics for %i components to NewRelic (%i bytes compressed to %i bytes)',
359+
"NOT " if self.config.application.get('skip_newrelic_upload') else "",
360+
metrics,
361+
len(components),
362+
len(json.dumps(body, ensure_ascii=False)),
363+
len(request_body))
364+
LOGGER.debug(body)
365+
366+
if self.config.application.get('skip_newrelic_upload'):
367+
return
361368

362369
try:
363370
response = requests.post(self.endpoint,
@@ -367,9 +374,9 @@ def send_components(self, components, metrics):
367374
timeout=self.config.application.get('newrelic_api_timeout', 10),
368375
verify=self.config.application.get('verify_ssl_cert', True))
369376

370-
LOGGER.debug('Response: %s: %r',
371-
response.status_code,
372-
response.content.strip())
377+
LOGGER.info('Response: %s: %r',
378+
response.status_code,
379+
response.content.strip())
373380
except requests.ConnectionError as error:
374381
LOGGER.error('Error reporting stats: %s', error)
375382
except requests.Timeout as error:

0 commit comments

Comments
 (0)