Skip to content

Commit

Permalink
added timer to trigger send if no incoming transactions trigger it
Browse files Browse the repository at this point in the history
This should alleviate a point of confusion when users expected that we
send data every x seconds. Instead, we only sent data every x seconds
if there were incoming transactions. If no transactions were happening,
no data was sent (including existing transactions on the queue).

This PR aligns the behaviour with user expectations. The trade-offs are:

 * slightly more resource consumption due to the timer thread
 * collection can either happen on the main thread, or on the timer
   thread, depending on whether the timer or an incoming transaction
   triggers collection.
  • Loading branch information
beniwohli committed Oct 2, 2017
1 parent 8a8eaa8 commit 4f79e4c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
7 changes: 7 additions & 0 deletions elasticapm/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import os
import platform
import socket
import threading
import sys
import time
import warnings
Expand Down Expand Up @@ -112,6 +113,8 @@ def __init__(self, config=None, **defaults):
self.config.disable_send = True
return

self._send_timer = None

self._transport_class = import_string(self.config.transport_class)
self._transports = {}

Expand Down Expand Up @@ -485,6 +488,8 @@ def handle_transport_fail(self, exception=None, **kwargs):
self.state.set_fail()

def _traces_collect(self):
if self._send_timer and self._send_timer.is_alive() and not self._send_timer == threading.current_thread():
self._send_timer.cancel()
transactions = self.instrumentation_store.get_all()
if not transactions:
return
Expand All @@ -495,6 +500,8 @@ def _traces_collect(self):
api_path = defaults.TRANSACTIONS_API_PATH

self.send(server=self.config.server + api_path, **data)
self._send_timer = threading.Timer(self.config.traces_send_frequency, self._traces_collect)
self._send_timer.start()

def get_app_info(self):
language_version = platform.python_version()
Expand Down
5 changes: 5 additions & 0 deletions elasticapm/transport/http_urllib3.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import os
import logging

import certifi
import urllib3
Expand All @@ -11,6 +12,9 @@
from elasticapm.utils import compat


logger = logging.getLogger(__name__)


class Urllib3Transport(HTTPTransport):

scheme = ['http', 'https']
Expand Down Expand Up @@ -46,6 +50,7 @@ def send(self, data, headers, timeout=None):
response = self.http.urlopen(
'POST', url, body=data, headers=headers, timeout=timeout, preload_content=False
)
logger.info('Sent request, url=%s size=%.2fkb status=%s', url, len(data) / 1024.0, response.status)
except Exception as e:
print_trace = True
if isinstance(e, MaxRetryError) and isinstance(e.reason, TimeoutError):
Expand Down

0 comments on commit 4f79e4c

Please sign in to comment.