-
Notifications
You must be signed in to change notification settings - Fork 9
feat: Add ClpKeyValuePairStreamHandler
which supports logging dictionary-type log events into CLP's key-value pair IR format.
#46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
51636a1
Implement kv pair handler
LinZhihao-723 e03b6d7
Add unit tests
LinZhihao-723 5a51940
Fix handlers
LinZhihao-723 919a15a
Add empty test
LinZhihao-723 a1ed017
Merge oss-main
LinZhihao-723 5a53fe5
Add loglevel timeout test cases
LinZhihao-723 048531d
Skip tests for macos
LinZhihao-723 ec36f20
Add tests for bad timeout dicts
LinZhihao-723 d414d65
Fix capitalization after :
LinZhihao-723 06608a7
Missing these two...
LinZhihao-723 096c3ce
Update `auto_generated_kv_pairs_utils` based on the review comments.
LinZhihao-723 e854fa1
Apply suggestions from code review
LinZhihao-723 b310993
Apply code review comments
LinZhihao-723 b039278
Apply black
LinZhihao-723 715961b
Fix utc offset calculation.
LinZhihao-723 5d8cd33
Use io_open
LinZhihao-723 f270b39
Apply code review comments
LinZhihao-723 1bcf7d7
Empty test
LinZhihao-723 2f03629
Merge branch 'oss-main' into kv-pair-handler
LinZhihao-723 fe77d91
Apply suggestions from code review
LinZhihao-723 5ab34d4
Fix version specifier
LinZhihao-723 9f96dda
Fix linter...
LinZhihao-723 47d3af7
Update 's docstring
LinZhihao-723 306fba6
Update src/clp_logging/handlers.py
LinZhihao-723 1ea69de
Apply code review comments
LinZhihao-723 cb8be0a
Apply code review comment for missed renaming
LinZhihao-723 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,68 @@ | ||
import logging | ||
from typing import Any, Dict | ||
|
||
from clp_logging.utils import Timestamp | ||
|
||
TIMESTAMP_KEY: str = "timestamp" | ||
TIMESTAMP_UNIX_MILLISECS_KEY: str = "unix_millisecs" | ||
TIMESTAMP_UTC_OFFSET_SECS_KEY: str = "utc_offset_secs" | ||
|
||
LEVEL_KEY: str = "level" | ||
LEVEL_NUM_KEY: str = "num" | ||
LEVEL_NAME_KEY: str = "name" | ||
|
||
SOURCE_LOCATION_KEY: str = "source_location" | ||
SOURCE_LOCATION_PATH_KEY: str = "path" | ||
SOURCE_LOCATION_LINE_KEY: str = "line" | ||
|
||
|
||
class AutoGeneratedKeyValuePairsBuffer: | ||
""" | ||
A reusable buffer for auto-generated key-value pairs. | ||
|
||
This buffer maintains a predefined dictionary for common metadata fields, to | ||
enable efficient reuse without creating new dictionaries for each log event. | ||
""" | ||
|
||
def __init__(self) -> None: | ||
self._buf: Dict[str, Any] = { | ||
TIMESTAMP_KEY: { | ||
TIMESTAMP_UNIX_MILLISECS_KEY: None, | ||
TIMESTAMP_UTC_OFFSET_SECS_KEY: None, | ||
}, | ||
LEVEL_KEY: { | ||
LEVEL_NUM_KEY: None, | ||
LEVEL_NAME_KEY: None, | ||
}, | ||
SOURCE_LOCATION_KEY: { | ||
SOURCE_LOCATION_PATH_KEY: None, | ||
SOURCE_LOCATION_LINE_KEY: None, | ||
}, | ||
} | ||
|
||
def generate(self, ts: Timestamp, record: logging.LogRecord) -> Dict[str, Any]: | ||
""" | ||
Generates the auto-generated key-value pairs by populating the | ||
underlying buffer with the given log event metadata. | ||
|
||
:param ts: The timestamp assigned to the log event. | ||
:param record: The LogRecord containing metadata for the log event. | ||
:return: The populated underlying buffer as the auto-generated key-value | ||
pairs. | ||
""" | ||
|
||
self._buf[TIMESTAMP_KEY][TIMESTAMP_UNIX_MILLISECS_KEY] = ts.get_unix_ts() | ||
self._buf[TIMESTAMP_KEY][TIMESTAMP_UTC_OFFSET_SECS_KEY] = ts.get_utc_offset() | ||
|
||
# NOTE: We don't add all the metadata contained in `record`. Instead, we only add the | ||
# following fields: | ||
# - Log level | ||
# - Source location | ||
|
||
self._buf[LEVEL_KEY][LEVEL_NUM_KEY] = record.levelno | ||
self._buf[LEVEL_KEY][LEVEL_NAME_KEY] = record.levelname | ||
|
||
self._buf[SOURCE_LOCATION_KEY][SOURCE_LOCATION_PATH_KEY] = record.pathname | ||
self._buf[SOURCE_LOCATION_KEY][SOURCE_LOCATION_LINE_KEY] = record.lineno | ||
|
||
return self._buf |
This file contains hidden or 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 hidden or 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,44 @@ | ||
from __future__ import annotations | ||
|
||
import time | ||
from math import floor | ||
|
||
|
||
class Timestamp: | ||
""" | ||
A timestamp represented as a Unix timestamp and a timezone offset from UTC. | ||
""" | ||
|
||
@staticmethod | ||
def now() -> Timestamp: | ||
""" | ||
:return: A `Timestamp` instance representing the current time. | ||
""" | ||
ts: float = time.time() | ||
return Timestamp( | ||
unix_ts=floor(ts * 1000), | ||
utc_offset=time.localtime(ts).tm_gmtoff, | ||
) | ||
|
||
def __init__(self, unix_ts: int, utc_offset: int): | ||
""" | ||
Initializes a `Timestamp` instance with the given time. | ||
|
||
:param unix_ts: Unix timestamp in milliseconds. | ||
:param utc_offset: The number of seconds the timezone is ahead of | ||
(positive) or behind (negative) UTC. | ||
""" | ||
self._utc_offset: int = utc_offset | ||
self._unix_ts: int = unix_ts | ||
|
||
def get_unix_ts(self) -> int: | ||
""" | ||
:return: The Unix timestamp in milliseconds. | ||
""" | ||
return self._unix_ts | ||
|
||
def get_utc_offset(self) -> int: | ||
""" | ||
:return: The number of seconds the timezone is ahead of (positive) or behind (negative) UTC. | ||
""" | ||
return self._utc_offset |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.