|
14 | 14 |
|
15 | 15 | import polars as pl |
16 | 16 | import polars._plr as plr |
17 | | -from polars._utils.monitoring import MONITORING_ENV_VAR |
| 17 | +from polars._utils.monitoring import ( |
| 18 | + MONITORING_ENV_VAR, |
| 19 | + MONITORING_WORKSPACE_ENV_VAR, |
| 20 | +) |
18 | 21 | from polars.lazyframe.engine import StreamingEngine |
19 | 22 | from tests.unit.conftest import mock_module_import |
20 | 23 |
|
@@ -70,6 +73,55 @@ def test_config_enable_monitoring() -> None: |
70 | 73 | assert observer.on_query_started.call_count == 2 |
71 | 74 |
|
72 | 75 |
|
| 76 | +def test_config_enable_monitoring_workspace() -> None: |
| 77 | + """The configured workspace is handed to the observer on construction.""" |
| 78 | + module, _ = fake_cloud_observer() |
| 79 | + with mock_module_import("polars_cloud", module, replace_if_exists=True): |
| 80 | + pl.Config.enable_monitoring(workspace="my-workspace") |
| 81 | + assert os.environ[MONITORING_WORKSPACE_ENV_VAR] == "my-workspace" |
| 82 | + |
| 83 | + _sample_lf().collect() |
| 84 | + module.QueryCloudObserver.assert_called_with("my-workspace") |
| 85 | + |
| 86 | + # re-enabling without a workspace goes back to the default workspace |
| 87 | + pl.Config.enable_monitoring() |
| 88 | + assert MONITORING_WORKSPACE_ENV_VAR not in os.environ |
| 89 | + |
| 90 | + _sample_lf().collect() |
| 91 | + module.QueryCloudObserver.assert_called_with() |
| 92 | + |
| 93 | + # disabling monitoring clears the workspace as well |
| 94 | + pl.Config.enable_monitoring(workspace="my-workspace") |
| 95 | + pl.Config.enable_monitoring(False) |
| 96 | + assert MONITORING_WORKSPACE_ENV_VAR not in os.environ |
| 97 | + |
| 98 | + |
| 99 | +def test_config_scope_monitoring_workspace() -> None: |
| 100 | + """The workspace is restored together with the other Config options.""" |
| 101 | + module, _ = fake_cloud_observer() |
| 102 | + with mock_module_import("polars_cloud", module, replace_if_exists=True): |
| 103 | + pl.Config.enable_monitoring(workspace="outer-workspace") |
| 104 | + |
| 105 | + with pl.Config(): |
| 106 | + pl.Config.enable_monitoring(workspace="inner-workspace") |
| 107 | + _sample_lf().collect() |
| 108 | + module.QueryCloudObserver.assert_called_with("inner-workspace") |
| 109 | + |
| 110 | + assert os.environ[MONITORING_WORKSPACE_ENV_VAR] == "outer-workspace" |
| 111 | + _sample_lf().collect() |
| 112 | + module.QueryCloudObserver.assert_called_with("outer-workspace") |
| 113 | + |
| 114 | + |
| 115 | +def test_engine_monitoring_uses_configured_workspace() -> None: |
| 116 | + """An engine-level override still reports to the configured workspace.""" |
| 117 | + module, _ = fake_cloud_observer() |
| 118 | + with mock_module_import("polars_cloud", module, replace_if_exists=True): |
| 119 | + pl.Config.enable_monitoring(workspace="my-workspace") |
| 120 | + _sample_lf().collect(engine=StreamingEngine(monitoring=True)) |
| 121 | + |
| 122 | + module.QueryCloudObserver.assert_called_with("my-workspace") |
| 123 | + |
| 124 | + |
73 | 125 | def test_collect_calls_observer() -> None: |
74 | 126 | """Running a query with monitoring on invokes the observer callbacks.""" |
75 | 127 | module, observer = fake_cloud_observer() |
|
0 commit comments