diff --git a/logging_loki/emitter.py b/logging_loki/emitter.py index 949ceea..932556c 100644 --- a/logging_loki/emitter.py +++ b/logging_loki/emitter.py @@ -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..8930309 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 logging_loki 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."""