-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from axiomhq/arne/structlog
Structlog support
- Loading branch information
Showing
6 changed files
with
95 additions
and
6 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
File renamed without changes.
File renamed without changes.
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,26 @@ | ||
from axiom_py import Client | ||
from axiom_py.structlog import AxiomProcessor | ||
import structlog | ||
|
||
|
||
def main(): | ||
client = Client() | ||
|
||
structlog.configure( | ||
processors=[ | ||
structlog.contextvars.merge_contextvars, | ||
structlog.processors.add_log_level, | ||
structlog.processors.StackInfoRenderer(), | ||
structlog.dev.set_exc_info, | ||
structlog.processors.TimeStamper(fmt="iso", key="_time"), | ||
AxiomProcessor(client, "my-dataset"), | ||
structlog.dev.ConsoleRenderer(), | ||
] | ||
) | ||
|
||
log = structlog.get_logger() | ||
log.info("hello", who="world") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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,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 |