Skip to content

Commit

Permalink
Add timeout (#47)
Browse files Browse the repository at this point in the history
* Add timeout to send, send_batch

* Fix missing kwarg

* Run linter

* Add docs for missing param

Co-authored-by: Timothy Pansino <11214426+TimPansino@users.noreply.github.com>
Co-authored-by: Tim Pansino <timpansino@gmail.com>
  • Loading branch information
3 people committed Jul 9, 2022
1 parent f089f53 commit 0947469
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/newrelic_telemetry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,25 +204,27 @@ def _create_payload(self, items, common):

return self._compress_payload(payload)

def send(self, item):
def send(self, item, timeout=None):
"""Send a single item
:param item: The object to send
:type item: dict
:param timeout: (optional) a timeout in seconds for sending the request
:type timeout: int
:rtype: HTTPResponse
"""
return self.send_batch((item,))
return self.send_batch((item,), timeout=timeout)

def send_batch(self, items, common=None):
def send_batch(self, items, common=None, timeout=None):
"""Send a batch of items
:param items: An iterable of items to send to New Relic.
:type items: list or tuple
:param common: (optional) A map of attributes that will be set on each
item.
:type common: dict
:param timeout: (optional) a timeout in seconds for sending the request
:type timeout: int
:rtype: HTTPResponse
"""
# Specifying the headers argument overrides any base headers existing
Expand All @@ -233,7 +235,9 @@ def send_batch(self, items, common=None):
headers["x-request-id"] = str(uuid.uuid4())

payload = self._create_payload(items, common)
return self._pool.urlopen("POST", self.PATH, body=payload, headers=headers)
return self._pool.urlopen(
"POST", self.PATH, body=payload, headers=headers, timeout=timeout
)


class SpanClient(Client):
Expand Down Expand Up @@ -324,15 +328,17 @@ def _create_payload(self, items, common):

return self._compress_payload(payload)

def send_batch(self, items):
def send_batch(self, items, timeout=None):
"""Send a batch of items
:param items: An iterable of items to send to New Relic.
:type items: list or tuple
:param timeout: (optional) a timeout in seconds for sending the request
:type timeout: int
:rtype: HTTPResponse
"""
return super(EventClient, self).send_batch(items, None)
return super(EventClient, self).send_batch(items, None, timeout=timeout)


class LogClient(Client):
Expand Down

0 comments on commit 0947469

Please sign in to comment.