Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for multi-tenant Loki configuration #39

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions logging_loki/emitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ 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.

Arguments:
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.
Expand All @@ -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))

Expand Down
7 changes: 4 additions & 3 deletions logging_loki/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
):
"""
Expand All @@ -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.

"""
Expand All @@ -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."""
Expand Down