-
Notifications
You must be signed in to change notification settings - Fork 702
feat: introduced conditional tracing at runtime #2289
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
base: main
Are you sure you want to change the base?
Conversation
@Gmin2 few of your PRs are blocked. The NLTK one will be merged once we resolve an issue with our unit tests, they are failing due to PR being an outside contributor. Bear with us 🙏 |
sdks/python/src/opik/config.py
Outdated
True if tracing is active, False otherwise. | ||
""" | ||
global _tracing_active | ||
if os.environ.get("OPIK_TRACK_DISABLE", "").lower() in ("true", "1", "yes"): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- we should use opik.config.OpikConfig to get this information, it handles not only env vars, but the config file as well.
- If someone enables tracing via the python function, it should take priority over the environment variable.
@@ -273,6 +278,11 @@ def _tracked_sync( | |||
) -> Callable: | |||
@functools.wraps(func) | |||
def wrapper(*args, **kwargs) -> Any: # type: ignore | |||
from .. import config |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All imports should be moved to the top of the module.
sdks/python/src/opik/config.py
Outdated
@@ -34,6 +34,30 @@ | |||
|
|||
LOGGER = logging.getLogger(__name__) | |||
|
|||
_tracing_active = True # Default to enabled |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for the comment
@@ -152,6 +152,11 @@ def _decorate( | |||
So these spans can't be parents for other spans. This is usually the case LLM API calls | |||
8000 | with `stream=True`. | ||
""" | |||
from .. import config | |||
# Check if tracing is active at runtime | |||
if self.disabled or not config.is_tracing_active(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea behind the conditional tracing at runtime is that you can switch the parameter to on/off and your code will log/not log the data without any extra manipulations.
With that logic, if the tracing was disabled when the tracked function was initialized, after enabling the tracing - it will not work, because the function is not decorated.
So:
- the decorated objects should remain decorated
- the only part which will be enabled/disabled is the part that submits traces and spans
There must be proper and extensive unit tests working with fake_backend
fixture to test that it really works as expected (inside tests/unit/decorator).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Gmin2 I've updated this comment to change the expected behavior a bit, should be more straightforward now. The context manipulations will still happen, only the parts where the spans/traces are submitted via Opik client should be disabled.
@@ -23,6 +24,10 @@ def track_aisuite( | |||
Returns: | |||
The modified AISuite client with Opik tracking enabled. | |||
""" | |||
# Check if tracing is active | |||
if not is_tracing_active(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No integration should have this logic when trace_*
is called. The enable/disable tracing logic should live only in base_track_decorator.
sdks/python/src/opik/config.py
Outdated
@@ -34,6 +34,30 @@ | |||
|
|||
LOGGER = logging.getLogger(__name__) | |||
|
|||
_tracing_active = True # Default to enabled | |||
|
|||
def set_tracing_active(active: bool) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd move these functions to opik.decorator
namespace.
@Gmin2 are you still working on this? |
yes, on it |
Changes in this commit - A new global toggle controlled by `opik.set_tracing_active()` and `opik.is_tracing_active()`. - The core `@track` decorator logic has been refactored to always manage context but conditionally submit trace data based on the runtime toggle. This ensures functions remain correctly instrumented even when tracing is re-enabled at runtime. - The runtime toggle takes precedence over configuration from files or environment variables. - All library integration trackers now defer to the central decorator logic for consistency. - A comprehensive test suite has been added to validate the new functionality. - Added detailed uint test
Details
Introduced conditional tracing at runtime by defining
set_tracing_active()
andis_tracing_active()
functions in the config moduleIssues
Resolves #2090
Testing
I had craeted a test script that had two tracing enabled(which 8000 is by default) and one disabled
and see the logs
and see in the ui there is no traces of test 2
Screencast.from.2025-05-28.01-04-04.mp4
Documentation
Updated the docs of sdk python
/claim #2090