From 949633218eb95c1bbb58ce5d1fab5bfbea10f376 Mon Sep 17 00:00:00 2001 From: Ivan Cavanaugh Date: Tue, 14 May 2024 14:49:06 +0200 Subject: [PATCH 1/3] Added support for multi-tenant Loki configuration --- logging_loki/emitter.py | 9 ++++++--- logging_loki/handlers.py | 7 ++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/logging_loki/emitter.py b/logging_loki/emitter.py index 949ceea..0afd059 100644 --- a/logging_loki/emitter.py +++ b/logging_loki/emitter.py @@ -15,7 +15,7 @@ import requests import rfc3339 -from logging_loki import const +from classes.loki_logging import const BasicAuth = Optional[Tuple[str, str]] @@ -30,7 +30,7 @@ class LokiEmitter(abc.ABC): label_replace_with = const.label_replace_with session_class = requests.Session - def __init__(self, url: str, tags: Optional[dict] = None, auth: BasicAuth = None): + def __init__(self, url: str, tags: Optional[dict] = None, auth: BasicAuth = None, tenant_id: str = None): """ Create new Loki emitter. @@ -38,6 +38,7 @@ def __init__(self, url: str, tags: Optional[dict] = None, auth: BasicAuth = None url: Endpoint used to send log entries to Loki (e.g. `https://my-loki-instance/loki/api/v1/push`). tags: Default tags added to every log record. auth: Optional tuple with username and password for basic HTTP authentication. + tenant_id: Optional tenant id when Loki is configured as a multi-tenant system """ #: Tags that will be added to all records handled by this handler. @@ -46,13 +47,15 @@ def __init__(self, url: str, tags: Optional[dict] = None, auth: BasicAuth = None self.url = url #: Optional tuple with username and password for basic authentication. self.auth = auth + #: Optional tenant id when Loki is configured as a multi-tenant system + self.tenant_id = tenant_id self._session: Optional[requests.Session] = None def __call__(self, record: logging.LogRecord, line: str): """Send log record to Loki.""" payload = self.build_payload(record, line) - resp = self.session.post(self.url, json=payload) + resp = self.session.post(self.url, json=payload, headers={"X-Scope-OrgID": self.tenant_id}) if resp.status_code != self.success_response_code: raise ValueError("Unexpected Loki API response status code: {0}".format(resp.status_code)) diff --git a/logging_loki/handlers.py b/logging_loki/handlers.py index 74a55cb..1e64b9c 100644 --- a/logging_loki/handlers.py +++ b/logging_loki/handlers.py @@ -9,8 +9,7 @@ from typing import Optional from typing import Type -from logging_loki import const -from logging_loki import emitter +from classes.loki_logging import emitter, const class LokiQueueHandler(QueueHandler): @@ -41,6 +40,7 @@ def __init__( url: str, tags: Optional[dict] = None, auth: Optional[emitter.BasicAuth] = None, + tenant_id: Optional[str] = None, version: Optional[str] = None, ): """ @@ -50,6 +50,7 @@ def __init__( url: Endpoint used to send log entries to Loki (e.g. `https://my-loki-instance/loki/api/v1/push`). tags: Default tags added to every log record. auth: Optional tuple with username and password for basic HTTP authentication. + tenant_id: Optional tenant id when Loki system is configured as a multi-tenant system version: Version of Loki emitter to use. """ @@ -67,7 +68,7 @@ def __init__( version = version or const.emitter_ver if version not in self.emitters: raise ValueError("Unknown emitter version: {0}".format(version)) - self.emitter = self.emitters[version](url, tags, auth) + self.emitter = self.emitters[version](url, tags, auth, tenant_id) def handleError(self, record): # noqa: N802 """Close emitter and let default handler take actions on error.""" From 655e154711f0cfac847c285f85269b0ac5e83a22 Mon Sep 17 00:00:00 2001 From: Ivan Cavanaugh Date: Tue, 14 May 2024 14:55:07 +0200 Subject: [PATCH 2/3] Added support for multi-tenant Loki configuration --- logging_loki/emitter.py | 2 +- logging_loki/handlers.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/logging_loki/emitter.py b/logging_loki/emitter.py index 0afd059..4ca7614 100644 --- a/logging_loki/emitter.py +++ b/logging_loki/emitter.py @@ -15,7 +15,7 @@ import requests import rfc3339 -from classes.loki_logging import const +from loki_logging import const BasicAuth = Optional[Tuple[str, str]] diff --git a/logging_loki/handlers.py b/logging_loki/handlers.py index 1e64b9c..21f3525 100644 --- a/logging_loki/handlers.py +++ b/logging_loki/handlers.py @@ -9,7 +9,7 @@ from typing import Optional from typing import Type -from classes.loki_logging import emitter, const +from loki_logging import emitter, const class LokiQueueHandler(QueueHandler): From 511981422b22338bad5ca29606b4e8ce459111b1 Mon Sep 17 00:00:00 2001 From: Ivan Cavanaugh Date: Tue, 14 May 2024 14:56:57 +0200 Subject: [PATCH 3/3] Added support for multi-tenant Loki configuration --- logging_loki/emitter.py | 2 +- logging_loki/handlers.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/logging_loki/emitter.py b/logging_loki/emitter.py index 4ca7614..932556c 100644 --- a/logging_loki/emitter.py +++ b/logging_loki/emitter.py @@ -15,7 +15,7 @@ import requests import rfc3339 -from loki_logging import const +from logging_loki import const BasicAuth = Optional[Tuple[str, str]] diff --git a/logging_loki/handlers.py b/logging_loki/handlers.py index 21f3525..8930309 100644 --- a/logging_loki/handlers.py +++ b/logging_loki/handlers.py @@ -9,7 +9,7 @@ from typing import Optional from typing import Type -from loki_logging import emitter, const +from logging_loki import emitter, const class LokiQueueHandler(QueueHandler):