Introduced conditional tracing at runtime by defining set_tracing_active()
and is_tracing_active()
functions in the config module
Resolves #2090
I had craeted a test script that had two tracing enabled(which 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
https://github.com/user-attachments/assets/3872cb8f-7b55-4265-b8f4-8a6950af108a
Updated the docs of sdk python
/claim #2090
Mintu Gogoi
@Gmin2
Comet
@comet-ml