Skip to content

Commit

Permalink
[feat] add threshold_ms for conditional timing metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Aviram Hassan committed Dec 9, 2020
1 parent 128453e commit f97ba6d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion aiodogstatsd/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ def timeit(
*,
tags: Optional[typedefs.MTags] = None,
sample_rate: Optional[typedefs.MSampleRate] = None,
threshold_ms: typedefs.MValue = 0
) -> Iterator[None]:
"""
Context manager for easily timing methods.
Expand All @@ -268,7 +269,8 @@ def timeit(
yield
finally:
value = (loop.time() - started_at) * 1000
self.timing(name, value=int(value), tags=tags, sample_rate=sample_rate)
if value > threshold_ms:
self.timing(name, value=int(value), tags=tags, sample_rate=sample_rate)


class DatagramProtocol(asyncio.DatagramProtocol):
Expand Down
2 changes: 2 additions & 0 deletions examples/timeit.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ async def main():
)
await client.connect()

# Use threshold_ms for setting a threshold for sending the timing metric.
with client.timeit("fire"):
# Do action we want to time
pass


await client.close()


Expand Down
6 changes: 6 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ async def test_timeit(self, statsd_client, statsd_server, wait_for, mocker):
loop.return_value.time.return_value = 1.0
with statsd_client.timeit("test_timer", tags={"and": "robin"}):
loop.return_value.time.return_value = 2.0

# This shouldn't be logged.
loop.return_value.time.return_value = 1.0
with statsd_client.timeit("test_timer", tags={"and": "robin"}, threshold_ms=3000.0):
loop.return_value.time.return_value = 2.0

async with udp_server:
await wait_for(collected)
assert collected == [b"test_timer:1000|ms|#whoami:batman,and:robin"]

0 comments on commit f97ba6d

Please sign in to comment.