Skip to content

Commit

Permalink
[OPIK-673] implement OPIK_TRACK_DISABLE configuration (#962)
Browse files Browse the repository at this point in the history
* Implement OPIK_TRACK_DISABLE configuration, add unit tests

* Add disabled mode for track

* Fix lint errors

* Update test names
  • Loading branch information
alexkuzmik authored Dec 30, 2024
1 parent b4c108b commit a338601
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
8 changes: 8 additions & 0 deletions sdks/python/src/opik/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ def settings_customise_sources(
If enabled, TLS verification is enabled for all HTTP requests.
"""

track_disable: bool = False
"""
If set to True, then `@track` decorator and `track_LIBRARY(...)` integrations do not log any data.
Any other API will continue working.
We do not recommend disable tracking unless you only use tracking functionalities in your project because
it might lead to unexpected results for the features that rely on spans/traces created.
"""

@property
def config_file_fullpath(self) -> pathlib.Path:
config_file_path = os.getenv("OPIK_CONFIG_PATH", CONFIG_FILE_PATH_DEFAULT)
Expand Down
10 changes: 9 additions & 1 deletion sdks/python/src/opik/decorator/base_track_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
error_info_collector,
)
from ..api_objects import opik_client, helpers, span, trace
from .. import context_storage, logging_messages, datetime_helpers
from .. import context_storage, logging_messages, datetime_helpers, config

LOGGER = logging.getLogger(__name__)

Expand All @@ -50,6 +50,11 @@ def __init__(self) -> None:
self.provider: Optional[str] = None
""" Name of the LLM provider. Used in subclasses in integrations track decorators. """

@functools.cached_property
def disabled(self) -> bool:
config_ = config.OpikConfig()
return config_.track_disable

def track(
self,
name: Optional[Union[Callable, str]] = None,
Expand Down Expand Up @@ -368,6 +373,9 @@ def _after_call(
generators_trace_to_end: Optional[trace.TraceData] = None,
flush: bool = False,
) -> None:
if self.disabled:
return

try:
if output is not None:
end_arguments = self._end_span_inputs_preprocessor(
Expand Down
67 changes: 67 additions & 0 deletions sdks/python/tests/unit/decorator/test_track_disabled_mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from opik import opik_context
from opik.decorator import tracker

from ...testlib import patch_environ


def test_track_disabled_mode__nothing_logged__happyflow(fake_backend):
tracker_instance = tracker.OpikTrackDecorator()

with patch_environ({"OPIK_TRACK_DISABLE": "true"}):

@tracker_instance.track
def f_inner():
return 42

@tracker_instance.track
def f_outer():
f_inner()

f_outer()
tracker.flush_tracker()

assert len(fake_backend.trace_trees) == 0
assert len(fake_backend.span_trees) == 0


def test_track_disabled_mode__get_current_span_and_trace_called__spans_and_trace_exist__but_nothing_logged(
fake_backend,
):
"""
SpanData and TraceData are still returned intentionally to make sure that scripts
which called them continue working and don't fail because they started return `None`
in disabled mode.
"""
tracker_instance = tracker.OpikTrackDecorator()

with patch_environ({"OPIK_TRACK_DISABLE": "true"}):

@tracker_instance.track
def f():
assert opik_context.get_current_span_data() is not None
assert opik_context.get_current_trace_data() is not None

f()
tracker.flush_tracker()

assert len(fake_backend.trace_trees) == 0
assert len(fake_backend.span_trees) == 0


def test_track_disabled_mode__update_current_span_and_trace_called__no_errors_raised__nothing_logged(
fake_backend,
):
tracker_instance = tracker.OpikTrackDecorator()

with patch_environ({"OPIK_TRACK_DISABLE": "true"}):

@tracker_instance.track
def f():
opik_context.update_current_span(name="some-name")
opik_context.update_current_trace(name="some-name")

f()
tracker.flush_tracker()

assert len(fake_backend.trace_trees) == 0
assert len(fake_backend.span_trees) == 0

0 comments on commit a338601

Please sign in to comment.