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 10, 2020
1 parent 128453e commit 6eb13b2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions 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: Optional[typedefs.MValue] = None,
) -> Iterator[None]:
"""
Context manager for easily timing methods.
Expand All @@ -268,6 +269,8 @@ def timeit(
yield
finally:
value = (loop.time() - started_at) * 1000
if threshold_ms and value < threshold_ms:
return
self.timing(name, value=int(value), tags=tags, sample_rate=sample_rate)


Expand Down
1 change: 1 addition & 0 deletions examples/timeit.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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
Expand Down
8 changes: 8 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ 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 6eb13b2

Please sign in to comment.