diff --git a/logging_loki/emitter.py b/logging_loki/emitter.py index 31230b9..8f375cf 100644 --- a/logging_loki/emitter.py +++ b/logging_loki/emitter.py @@ -46,7 +46,8 @@ def __init__(self, props_to_labels: Optional[list[str]] = None, level_tag: Optional[str] = const.level_tag, logger_tag: Optional[str] = const.logger_tag, - verify: Union[bool, str] = True + verify: Union[bool, str] = True, + replace_timestamp: Optional[bool] = False, ): """ Create new Loki emitter. @@ -75,6 +76,8 @@ def __init__(self, self.logger_tag: str = logger_tag # verify param to be past to requests, can be a bool (to enable/disable SSL verification) or a path to a CA bundle self.verify = verify + # Flag to control if the log timestamp should be replaced with the current time + self.replace_timestamp = replace_timestamp self._session: Optional[requests.Session] = None self._lock = threading.Lock() @@ -147,8 +150,7 @@ def build_tags(self, record: logging.LogRecord, line: str) -> Dict[str, Any]: def build_payload(self, record: logging.LogRecord, line: str) -> dict: """Build JSON payload with a log entry.""" labels = self.build_tags(record, line) - ns = 1e9 - ts = str(int(time.time() * ns)) + ts = str(time.time_ns()) if self.replace_timestamp else str(int(record.created * 1e9)) line = json.dumps(record, default=lambda obj: obj.__dict__) if self.as_json else line diff --git a/logging_loki/handlers.py b/logging_loki/handlers.py index 018f856..9cc5e72 100644 --- a/logging_loki/handlers.py +++ b/logging_loki/handlers.py @@ -71,7 +71,8 @@ def __init__( props_to_labels: Optional[list[str]] = None, level_tag: Optional[str] = const.level_tag, logger_tag: Optional[str] = const.logger_tag, - verify: Union[bool, str] = True + verify: Union[bool, str] = True, + replace_timestamp: Optional[bool] = False, ): """ Create new Loki logging handler. @@ -86,10 +87,10 @@ def __init__( level_tag: Label name indicating logging level. logger_tag: Label name indicating logger name. verify: Either a boolean, in which case it controls whether we verify the server's TLS certificate, or a string, in which case it must be a path to a CA bundle to use. - + replace_timestamp: If enabled, the timestamp in the log line will be replaced with `time.time_ns()`. Be careful when using this option with `batching` enabled, as the logs will be sent in batches, and the timestamp will be the time of the batch, not the time of the log. """ super().__init__() - self.emitter = LokiEmitter(url, tags, headers, auth, as_json, props_to_labels, level_tag, logger_tag, verify) + self.emitter = LokiEmitter(url, tags, headers, auth, as_json, props_to_labels, level_tag, logger_tag, verify, replace_timestamp=replace_timestamp) def handleError(self, exc: Exception): # noqa: N802 """Close emitter and let default handler take actions on error."""