Skip to content

Commit

Permalink
fix: Add condition to flush after 1s
Browse files Browse the repository at this point in the history
This fixes a bug where if you emit an event every
900ms it'll never flush (until you get to 1000
events).
  • Loading branch information
bahlo committed Sep 30, 2024
1 parent c33459c commit eb325df
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/axiom_py/logging.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Logging contains the AxiomHandler and related methods to do with logging."""

from threading import Timer

from logging import Handler, NOTSET, getLogger, WARNING
import time

from .client import Client


Expand All @@ -13,6 +14,7 @@ class AxiomHandler(Handler):
dataset: str
buffer: list
interval: int
last_flush: float
timer: Timer

def __init__(self, client: Client, dataset: str, level=NOTSET, interval=1):
Expand All @@ -37,7 +39,10 @@ def __init__(self, client: Client, dataset: str, level=NOTSET, interval=1):
def emit(self, record):
"""Emit sends a log to Axiom."""
self.buffer.append(record.__dict__)
if len(self.buffer) >= 1000:
if (
len(self.buffer) >= 1000
or time.monotonic() - self.last_run > self.interval
):
self.flush()

# Restart timer
Expand All @@ -48,6 +53,8 @@ def emit(self, record):
def flush(self):
"""Flush sends all logs in the buffer to Axiom."""

self.last_flush = time.monotonic()

if len(self.buffer) == 0:
return

Expand Down

0 comments on commit eb325df

Please sign in to comment.