1616import polars ._plr as plr
1717from polars ._utils .monitoring import (
1818 MONITORING_ENV_VAR ,
19+ MONITORING_ORGANIZATION_ENV_VAR ,
1920 MONITORING_WORKSPACE_ENV_VAR ,
2021)
2122from polars .lazyframe .engine import StreamingEngine
@@ -81,7 +82,7 @@ def test_config_enable_monitoring_workspace() -> None:
8182 assert os .environ [MONITORING_WORKSPACE_ENV_VAR ] == "my-workspace"
8283
8384 _sample_lf ().collect ()
84- module .QueryCloudObserver .assert_called_with ("my-workspace" )
85+ module .QueryCloudObserver .assert_called_with (workspace = "my-workspace" )
8586
8687 # re-enabling without a workspace goes back to the default workspace
8788 pl .Config .enable_monitoring ()
@@ -96,6 +97,53 @@ def test_config_enable_monitoring_workspace() -> None:
9697 assert MONITORING_WORKSPACE_ENV_VAR not in os .environ
9798
9899
100+ def test_config_enable_monitoring_organization () -> None :
101+ """The configured organization is handed to the observer on construction."""
102+ module , _ = fake_cloud_observer ()
103+ with mock_module_import ("polars_cloud" , module , replace_if_exists = True ):
104+ pl .Config .enable_monitoring (organization = "my-org" )
105+ assert os .environ [MONITORING_ORGANIZATION_ENV_VAR ] == "my-org"
106+ assert MONITORING_WORKSPACE_ENV_VAR not in os .environ
107+
108+ _sample_lf ().collect ()
109+ module .QueryCloudObserver .assert_called_with (organization = "my-org" )
110+
111+ # workspace and organization are passed together
112+ pl .Config .enable_monitoring (workspace = "my-workspace" , organization = "my-org" )
113+ _sample_lf ().collect ()
114+ module .QueryCloudObserver .assert_called_with (
115+ workspace = "my-workspace" , organization = "my-org"
116+ )
117+
118+ # re-enabling without an organization goes back to the default organization
119+ pl .Config .enable_monitoring ()
120+ assert MONITORING_ORGANIZATION_ENV_VAR not in os .environ
121+
122+ _sample_lf ().collect ()
123+ module .QueryCloudObserver .assert_called_with ()
124+
125+ # disabling monitoring clears the organization as well
126+ pl .Config .enable_monitoring (organization = "my-org" )
127+ pl .Config .enable_monitoring (False )
128+ assert MONITORING_ORGANIZATION_ENV_VAR not in os .environ
129+
130+
131+ def test_config_scope_monitoring_organization () -> None :
132+ """The organization is restored together with the other Config options."""
133+ module , _ = fake_cloud_observer ()
134+ with mock_module_import ("polars_cloud" , module , replace_if_exists = True ):
135+ pl .Config .enable_monitoring (organization = "outer-org" )
136+
137+ with pl .Config ():
138+ pl .Config .enable_monitoring (organization = "inner-org" )
139+ _sample_lf ().collect ()
140+ module .QueryCloudObserver .assert_called_with (organization = "inner-org" )
141+
142+ assert os .environ [MONITORING_ORGANIZATION_ENV_VAR ] == "outer-org"
143+ _sample_lf ().collect ()
144+ module .QueryCloudObserver .assert_called_with (organization = "outer-org" )
145+
146+
99147def test_config_scope_monitoring_workspace () -> None :
100148 """The workspace is restored together with the other Config options."""
101149 module , _ = fake_cloud_observer ()
@@ -105,11 +153,11 @@ def test_config_scope_monitoring_workspace() -> None:
105153 with pl .Config ():
106154 pl .Config .enable_monitoring (workspace = "inner-workspace" )
107155 _sample_lf ().collect ()
108- module .QueryCloudObserver .assert_called_with ("inner-workspace" )
156+ module .QueryCloudObserver .assert_called_with (workspace = "inner-workspace" )
109157
110158 assert os .environ [MONITORING_WORKSPACE_ENV_VAR ] == "outer-workspace"
111159 _sample_lf ().collect ()
112- module .QueryCloudObserver .assert_called_with ("outer-workspace" )
160+ module .QueryCloudObserver .assert_called_with (workspace = "outer-workspace" )
113161
114162
115163def test_engine_monitoring_uses_configured_workspace () -> None :
@@ -119,7 +167,7 @@ def test_engine_monitoring_uses_configured_workspace() -> None:
119167 pl .Config .enable_monitoring (workspace = "my-workspace" )
120168 _sample_lf ().collect (engine = StreamingEngine (monitoring = True ))
121169
122- module .QueryCloudObserver .assert_called_with ("my-workspace" )
170+ module .QueryCloudObserver .assert_called_with (workspace = "my-workspace" )
123171
124172
125173def test_collect_calls_observer () -> None :
0 commit comments