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 integration_tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@ def _load_config():
os.environ.get("TASKBADGER_PROJECT", ""),
os.environ.get("TASKBADGER_API_KEY", ""),
systems=[CelerySystemIntegration()],
tags={
"env": "integration",
},
)
print(f"\nIntegration tests configuration:\n {badger.mug.Badger.current.settings}\n")
2 changes: 2 additions & 0 deletions integration_tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ def test_basics():
task = badger.create_task("test basics", data=data)
task.success(100)
assert task.status == StatusEnum.SUCCESS
assert task.tags == {"env": "integration"}

fresh = badger.get_task(task.id)
assert fresh.status == StatusEnum.SUCCESS
assert fresh.value == 100
assert fresh.data == data
assert fresh.tags == {"env": "integration"}
1 change: 1 addition & 0 deletions integration_tests/test_celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def test_celery(celery_session_app, celery_session_worker):
assert tb_task.status == StatusEnum.SUCCESS
assert tb_task.value == 100
assert tb_task.data == {"result": a + b}
assert tb_task.tags == {"env": "integration"}


def test_celery_auto_track(celery_session_app, celery_session_worker):
Expand Down
15 changes: 9 additions & 6 deletions taskbadger/mug.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import dataclasses
from contextlib import ContextDecorator
from contextvars import ContextVar
from copy import deepcopy
from typing import Union

from taskbadger.internal import AuthenticatedClient
Expand Down Expand Up @@ -87,8 +88,8 @@ def __init__(self):

def __enter__(self):
self.stack.append((self.context, self.tags))
self.context = self.context.copy()
self.tags = self.tags.copy()
self.context = deepcopy(self.context)
self.tags = deepcopy(self.tags)
return self

def __exit__(self, *args):
Expand Down Expand Up @@ -116,17 +117,19 @@ def current(cls):

class Badger(metaclass=MugMeta):
def __init__(self, settings_or_mug=None):
self._session = ReentrantSession()
self._scope = Scope()

if isinstance(settings_or_mug, Badger):
self.settings = settings_or_mug.settings
self._scope.context = deepcopy(settings_or_mug._scope.context)
self._scope.tags = deepcopy(settings_or_mug._scope.tags)
else:
self.settings = settings_or_mug

self._session = ReentrantSession()
self._scope = Scope()

def bind(self, settings, tags=None):
self.settings = settings
self.scope().tags = tags or {}
self._scope.tags = tags or {}

def session(self) -> ReentrantSession:
return self._session
Expand Down
11 changes: 10 additions & 1 deletion tests/test_session.py → tests/test_mug.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@pytest.fixture(autouse=True)
def _bind_settings():
Badger.current.bind(Settings("https://taskbadger.net", "token", "org", "proj"))
Badger.current.bind(Settings("https://taskbadger.net", "token", "org", "proj"), tags={"env": "test"})


def test_session_singleton():
Expand All @@ -19,6 +19,8 @@ def test_session_singleton():
assert session.stack == []
assert session == Badger.current.session()

assert Badger.current.scope().tags == {"env": "test"}


def test_session_global():
session = Session()
Expand Down Expand Up @@ -90,3 +92,10 @@ def run(self):
with session as client:
assert client is not None
self.clients.append(client)

assert Badger.current.scope().tags == {"env": "test"}
with Badger.current.scope() as scope:
scope.tag({"thread": self.name})
assert scope.tags == {"env": "test", "thread": self.name}

assert Badger.current.scope().tags == {"env": "test"}
10 changes: 6 additions & 4 deletions tests/test_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,21 @@ def test_scope_context():
with scope:
assert scope.stack == [({}, {})]
scope.context["foo"] = "bar"
scope.context["nit"] = [1]
scope.tags["name"] = "value"
with scope:
assert scope.stack == [({}, {}), ({"foo": "bar"}, {"name": "value"})]
assert scope.context == {"foo": "bar"}
assert scope.stack == [({}, {}), ({"foo": "bar", "nit": [1]}, {"name": "value"})]
assert scope.context == {"foo": "bar", "nit": [1]}
assert scope.tags == {"name": "value"}
scope.context["bar"] = "bazz"
scope.context["nit"].append(2)
scope.tags["bar"] = "bazz"
with scope:
assert scope.context == {"foo": "bar", "bar": "bazz"}
assert scope.context == {"foo": "bar", "bar": "bazz", "nit": [1, 2]}
assert scope.tags == {"name": "value", "bar": "bazz"}
scope.context.clear()
scope.tags.clear()
assert scope.context == {"foo": "bar"}
assert scope.context == {"foo": "bar", "nit": [1]}
assert scope.tags == {"name": "value"}
assert scope.stack == [({}, {})]
assert scope.context == {}
Expand Down