Skip to content

Commit

Permalink
studio: Skip data event if no STUDIO_TOKEN.
Browse files Browse the repository at this point in the history
When `_inside_dvc_exp` (user running `dvc exp run`), we were incorrectly trying to send the `data` event if `STUDIO_TOKEN` wasn't set, logging unnecesary warnings.
  • Loading branch information
daavoo committed Jan 4, 2023
1 parent de57fbd commit 7764ef0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/dvclive/live.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@
)

try:
from dvc_studio_client.env import STUDIO_TOKEN
from dvc_studio_client.post_live_metrics import post_live_metrics
except ImportError:
post_live_metrics = None
STUDIO_TOKEN = None

logging.basicConfig()
logger = logging.getLogger("dvclive")
Expand Down Expand Up @@ -149,6 +151,14 @@ def _init_dvc(self):
)

def _init_studio(self):
if post_live_metrics is not None:
if not os.getenv(STUDIO_TOKEN, None):
logger.debug("Skipping `studio` report.")
self._studio_events_to_skip.add("start")
self._studio_events_to_skip.add("data")
self._studio_events_to_skip.add("done")
return

if not self._dvc_repo:
logger.debug("`studio` report can't be used without a DVC Repo.")
self._studio_events_to_skip.add("start")
Expand Down
20 changes: 20 additions & 0 deletions tests/test_studio.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,23 @@ def test_post_to_studio_skip_on_env_var(tmp_dir, mocker, monkeypatch):
live.next_step()

assert mocked_post.call_count == 0


@pytest.mark.studio
def test_post_to_studio_skip_if_no_token(tmp_dir, mocker, monkeypatch):
dvc_repo = mocker.MagicMock()
dvc_repo.scm.get_rev.return_value = "f" * 40
mocker.patch("dvclive.live.get_dvc_repo", return_value=dvc_repo)

mocked_post = mocker.patch(
"dvclive.live.post_live_metrics", return_value=None
)

monkeypatch.setenv(DVC_EXP_BASELINE_REV, "foo")
monkeypatch.setenv(DVC_EXP_NAME, "bar")

with Live() as live:
live.log_metric("foo", 1)
live.next_step()

assert mocked_post.call_count == 0

0 comments on commit 7764ef0

Please sign in to comment.