-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathstructlog.py
42 lines (33 loc) · 1.06 KB
/
structlog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""Structlog contains the AxiomProcessor for structlog."""
from typing import List
import time
import atexit
from .client import Client
class AxiomProcessor:
"""A processor for sending structlogs to Axiom."""
client: Client
dataset: str
buffer: List[object]
interval: int
last_run: float
def __init__(self, client: Client, dataset: str, interval=1):
self.client = client
self.dataset = dataset
self.buffer = []
self.last_run = time.monotonic()
self.interval = interval
atexit.register(self.flush)
def flush(self):
self.last_run = time.monotonic()
if len(self.buffer) == 0:
return
self.client.ingest_events(self.dataset, self.buffer)
self.buffer = []
def __call__(self, logger: object, method_name: str, event_dict: object):
self.buffer.append(event_dict.copy())
if (
len(self.buffer) >= 1000
or time.monotonic() - self.last_run > self.interval
):
self.flush()
return event_dict