Skip to content
Merged
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
3 changes: 3 additions & 0 deletions escalite/escalite.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import time
import contextvars
import uuid
from contextlib import contextmanager
from typing import Any

Expand All @@ -15,6 +16,7 @@
ERROR_LOGS,
TIME_ELAPSED,
LOG_DATE,
ALERT_ID,
)

# Context variable for per-request logs
Expand All @@ -36,6 +38,7 @@ def start_logging():
Starts per-request logging by initializing the context variable.
"""
logs = {
ALERT_ID: str(uuid.uuid4()),
API_LOGS: {},
SERVICE_LOGS: {},
ERROR_LOGS: {},
Expand Down
1 change: 1 addition & 0 deletions escalite/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
LOG_DATE = "log_date"
LOG_LEVEL = Literal["info", "warning", "error", "debug", "critical"]
LOG_LEVELS = {"info": 20, "warning": 30, "error": 40, "debug": 10, "critical": 50}
ALERT_ID = "alert_id"
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "escalite"
version = "0.1.10"
version = "0.1.11"
description = "A Python library for collecting and dumping data from an api service."
authors = ["Rakibul Haq <haq.rakibul@gmail.com>"]
license = "MIT"
Expand Down
23 changes: 23 additions & 0 deletions tests/escalite/test_escalite.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import logging
import threading
import time
import uuid

import pytest
from escalite.escalite import Escalite
from escalite.utils.constants import ALERT_ID


class TestEscalite:
Expand Down Expand Up @@ -183,6 +185,7 @@ def test_escalite_get_all_logs_empty(self):
Escalite.end_logging()
logs = Escalite.get_all_logs()
assert logs == {
"alert_id": logs[ALERT_ID],
"log_level": "info",
"start_time": logs["start_time"],
"end_time": logs["end_time"],
Expand Down Expand Up @@ -302,3 +305,23 @@ def test_contextvar_is_isolated_across_threads(self, simulate_request):
for t in threads:
t.join()
assert results == [f"value_{i}" for i in range(num_threads)]

def test_alert_id_is_unique_and_valid(self):
Escalite.start_logging()
logs1 = Escalite.get_all_logs()
alert_id1 = logs1[ALERT_ID]
Escalite.end_logging()

Escalite.start_logging()
logs2 = Escalite.get_all_logs()
alert_id2 = logs2[ALERT_ID]
Escalite.end_logging()

# Check both are valid UUIDs
uuid_obj1 = uuid.UUID(alert_id1)
uuid_obj2 = uuid.UUID(alert_id2)
assert str(uuid_obj1) == alert_id1
assert str(uuid_obj2) == alert_id2

# Check they are unique
assert alert_id1 != alert_id2
Loading