-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(metrics): Forward received_at field to Kafka (#3561)
- Loading branch information
1 parent
9d3c91c
commit 5b8198b
Showing
7 changed files
with
154 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .time import time_after, time_within, time_within_delta | ||
|
||
__all__ = ["time_after", "time_within", "time_within_delta"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from datetime import timedelta, datetime, timezone | ||
|
||
|
||
class _WithinBounds: | ||
|
||
def __init__(self, lower_bound, upper_bound): | ||
self._lower_bound = lower_bound | ||
self._upper_bound = upper_bound | ||
|
||
def __eq__(self, other): | ||
assert isinstance(other, int) | ||
return self._lower_bound <= other <= self._upper_bound | ||
|
||
def __str__(self): | ||
return f"{self._lower_bound} <= x <= {self._upper_bound}" | ||
|
||
|
||
def time_after(lower_bound): | ||
upper_bound = int(datetime.now(tz=timezone.utc).timestamp()) | ||
return time_within(lower_bound, upper_bound) | ||
|
||
|
||
def time_within(lower_bound, upper_bound): | ||
assert lower_bound <= upper_bound | ||
return _WithinBounds(lower_bound, upper_bound) | ||
|
||
|
||
def time_within_delta(timestamp, delta=None): | ||
if delta is None: | ||
delta = timedelta(seconds=5) | ||
|
||
lower_bound = (datetime.fromtimestamp(timestamp) - delta).timestamp() | ||
upper_bound = (datetime.fromtimestamp(timestamp) + delta).timestamp() | ||
|
||
return _WithinBounds(lower_bound, upper_bound) |
Oops, something went wrong.