8000 feat: introduced conditional tracing at runtime by Gmin2 · Pull Request #2289 · comet-ml/opik · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

Gmin2
Copy link
Contributor
@Gmin2 Gmin2 commented May 27, 2025

Details

Introduced conditional tracing at runtime by defining set_tracing_active() and is_tracing_active() functions in the config module

Issues

Resolves #2090

Testing

I had craeted a test script that had two tracing enabled(which 8000 is by default) and one disabled

#test_dynamic_tracing.py
import opik
import time
import random

opik.configure(use_local=True)

@opik.track
def sample_function(input_data):
    print(f"Processing: {input_data}")
    time.sleep(0.5)  # Simulate work
    return f"Processed {input_data}"

# Test 1: Verify tracing is enabled by default
print("\n--- Test 1: Default behavior (tracing enabled) ---")
print(f"Is tracing active? {opik.is_tracing_active()}")
result = sample_function("test1")
print(f"Result: {result}")
print("Check Opik UI to confirm trace was created")
time.sleep(2)  

# Test 2: Disable tracing
print("\n--- Test 2: Disable tracing ---")
opik.set_tracing_active(False)
print(f"Is tracing active? {opik.is_tracing_active()}")
result = sample_function("test2")
print(f"Result: {result}")
print("Check Opik UI to confirm NO trace was created")
time.sleep(2)  

# Test 3: Re-enable tracing
print("\n--- Test 3: Re-enable tracing ---")
opik.set_tracing_active(True)
print(f"Is tracing active? {opik.is_tracing_active()}")
result = sample_function("test3")
print(f"Result: {result}")
print("Check Opik UI to confirm trace was created")
time.sleep(2)  

and see the logs

opik/sdks/python git:dynamic-tracing*  
(venv) ❯ python3 test_dynamic_tracing.py
Found local Opik instance on: http://localhost:5173/, do you want to use it? (Y/n)Y
OPIK: Configuration saved to file: /home/mintu-ubuntu/.opik.config

--- Test 1: Default behavior (tracing enabled) ---
Is tracing active? True
Processing: test1
OPIK: Started logging traces to the "Default Project" project at http://localhost:5173/api/v1/session/redirect/projects/?trace_id=0197133d-021a-7cc6-b421-0aabe1cecf2e&path=aHR0cDovL2xvY2FsaG9zdDo1MTczL2FwaS8=.
Result: Processed test1
Check Opik UI to confirm trace was created

--- Test 2: Disable tracing ---
Is tracing active? False
Processing: test2
Result: Processed test2
Check Opik UI to confirm NO trace was created

--- Test 3: Re-enable tracing ---
Is tracing active? True
Processing: test3
Result: Processed test3
Check Opik UI to confirm trace was created

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

@Gmin2 Gmin2 requested a review from a team as a code owner May 27, 2025 19:42
@Nimrod007 Nimrod007 temporarily deployed to external-contributors May 29, 2025 13:07 — with GitHub Actions Inactive
@Nimrod007 Nimrod007 temporarily deployed to external-contributors May 29, 2025 13:07 — with GitHub Actions Inactive
@Nimrod007 Nimrod007 temporarily deployed to external-contributors May 29, 2025 13:07 — with GitHub Actions Inactive
@Nimrod007 Nimrod007 temporarily deployed to external-contributors May 29, 2025 13:07 — with GitHub Actions Inactive
@vincentkoc
Copy link
Collaborator

@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 🙏

True if tracing is active, False otherwise.
"""
global _tracing_active
if os.environ.get("OPIK_TRACK_DISABLE", "").lower() in ("true", "1", "yes"):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. we should use opik.config.OpikConfig to get this information, it handles not only env vars, but the config file as well.
  2. 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
Copy link
Collaborator
@alexkuzmik alexkuzmik May 30, 2025

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.

@@ -34,6 +34,30 @@

LOGGER = logging.getLogger(__name__)

_tracing_active = True # Default to enabled
Copy link
Collaborator

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

8000
@@ -152,6 +152,11 @@ def _decorate(
So these spans can't be parents for other spans. This is usually the case LLM API calls
with `stream=True`.
"""
from .. import config
# Check if tracing is active at runtime
if self.disabled or not config.is_tracing_active():
Copy link
Collaborator
@alexkuzmik alexkuzmik May 30, 2025

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).

Copy link
Collaborator

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():
Copy link
Collaborator

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.

@@ -34,6 +34,30 @@

LOGGER = logging.getLogger(__name__)

_tracing_active = True # Default to enabled

def set_tracing_active(active: bool) -> None:
Copy link
Collaborator

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.

@vincentkoc
Copy link
Collaborator

@Gmin2 are you still working on this?

@Gmin2
Copy link
Contributor Author
Gmin2 commented Jun 18, 2025

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
@Gmin2 Gmin2 force-pushed the dynamic-tracing branch from fc66690 to e8d915e Compare June 20, 2025 06:49
@Gmin2 Gmin2 requested a review from a team as a code owner June 20, 2025 06:49
@Gmin2 Gmin2 requested a review from alexkuzmik June 20, 2025 07:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FR]: Conditional tracing at runtime
4 participants
0