44
55import sentry_sdk
66from sentry_sdk .data_collection import _ALL_HTTP_BODY_TYPES
7+ from sentry_sdk .utils import has_data_collection_enabled
78
89
910def test_kvcb_invalid_mode ():
1011 with pytest .raises (ValueError ):
11- sentry_sdk .init (data_collection = {"cookies" : {"mode" : "nope" }}) # type: ignore Purposely ignoring to test invalid option
12+ sentry_sdk .init (_experiments = {"data_collection" : { " cookies" : {"mode" : "nope" } }}) # type: ignore Purposely ignoring to test invalid option
1213
1314
1415def test_kvcb_from_dict_defaults_mode ():
15- sentry_sdk .init (data_collection = {"cookies" : {"mode" : "denylist" , "terms" : ["x" ]}})
16+ sentry_sdk .init (
17+ _experiments = {
18+ "data_collection" : {"cookies" : {"mode" : "denylist" , "terms" : ["x" ]}}
19+ }
20+ )
1621 client = sentry_sdk .get_client ()
1722 assert client .options ["data_collection" ]["cookies" ] == {
1823 "mode" : "denylist" ,
@@ -23,13 +28,13 @@ def test_kvcb_from_dict_defaults_mode():
2328def test_http_headers_collection_defaults ():
2429 default_terms = ["forwarded" , "-ip" , "remote-" , "via" , "-user" ]
2530
26- sentry_sdk .init (data_collection = {"http_headers" : {}}) # type: ignore Purposely ignoring to test invalid option
31+ sentry_sdk .init (_experiments = {"data_collection" : { " http_headers" : {} }}) # type: ignore Purposely ignoring to test invalid option
2732 client = sentry_sdk .get_client ()
2833 assert client .options ["data_collection" ]["http_headers" ]["request" ] == {
2934 "mode" : "denylist"
3035 }
3136
32- sentry_sdk .init (data_collection = {"http_headers" : "off" }) # type: ignore Purposely ignoring to test invalid option
37+ sentry_sdk .init (_experiments = {"data_collection" : { " http_headers" : "off" } }) # type: ignore Purposely ignoring to test invalid option
3338 client = sentry_sdk .get_client ()
3439 assert client .options ["data_collection" ]["http_headers" ]["request" ] == {
3540 "mode" : "denylist"
@@ -45,9 +50,11 @@ def test_http_headers_collection_defaults():
4550
4651def test_http_headers_use_default_in_setting_with_missing_config ():
4752 sentry_sdk .init (
48- data_collection = {
49- "http_headers" : {
50- "request" : {"mode" : "allowlist" , "terms" : ["x-id" ]},
53+ _experiments = {
54+ "data_collection" : {
55+ "http_headers" : {
56+ "request" : {"mode" : "allowlist" , "terms" : ["x-id" ]},
57+ }
5158 }
5259 }
5360 )
@@ -108,7 +115,7 @@ def _get(dc, path):
108115 id = "send_default_pii_false_collects_no_pii" ,
109116 ),
110117 pytest .param (
111- {"data_collection" : {}},
118+ {"_experiments" : { " data_collection" : {} }},
112119 {
113120 "user_info" : True ,
114121 "gen_ai.inputs" : True ,
@@ -121,7 +128,11 @@ def _get(dc, path):
121128 id = "explicit_data_collection_uses_spec_defaults" ,
122129 ),
123130 pytest .param (
124- {"data_collection" : {"user_info" : False , "http_bodies" : []}},
131+ {
132+ "_experiments" : {
133+ "data_collection" : {"user_info" : False , "http_bodies" : []}
134+ }
135+ },
125136 {
126137 "user_info" : False ,
127138 "http_bodies" : [],
@@ -132,7 +143,7 @@ def _get(dc, path):
132143 ),
133144 pytest .param (
134145 {
135- "data_collection" : {},
146+ "_experiments" : { " data_collection" : {} },
136147 "include_local_variables" : False ,
137148 "include_source_context" : False ,
138149 },
@@ -141,11 +152,13 @@ def _get(dc, path):
141152 ),
142153 pytest .param (
143154 {
144- "data_collection" : {
145- "cookies" : {"mode" : "off" },
146- "query_params" : {"mode" : "allowlist" , "terms" : ["page" ]},
147- "http_headers" : {"request" : {"mode" : "off" }},
148- "gen_ai" : {"inputs" : False , "outputs" : True },
155+ "_experiments" : {
156+ "data_collection" : {
157+ "cookies" : {"mode" : "off" },
158+ "query_params" : {"mode" : "allowlist" , "terms" : ["page" ]},
159+ "http_headers" : {"request" : {"mode" : "off" }},
160+ "gen_ai" : {"inputs" : False , "outputs" : True },
161+ }
149162 }
150163 },
151164 {
@@ -160,12 +173,14 @@ def _get(dc, path):
160173 ),
161174 pytest .param (
162175 {
163- "data_collection" : {
164- "cookies" : None ,
165- "http_headers" : None ,
166- "query_params" : None ,
167- "graphql" : None ,
168- "gen_ai" : None ,
176+ "_experiments" : {
177+ "data_collection" : {
178+ "cookies" : None ,
179+ "http_headers" : None ,
180+ "query_params" : None ,
181+ "graphql" : None ,
182+ "gen_ai" : None ,
183+ }
169184 }
170185 },
171186 {
@@ -180,7 +195,7 @@ def _get(dc, path):
180195 id = "none_values_fall_back_to_spec_defaults" ,
181196 ),
182197 pytest .param (
183- {"data_collection" : {}},
198+ {"_experiments" : { " data_collection" : {} }},
184199 {
185200 "graphql.document" : True ,
186201 "graphql.variables" : True ,
@@ -209,27 +224,27 @@ def _get(dc, path):
209224 id = "legacy_pii_on_collects_graphql_database_and_queues" ,
210225 ),
211226 pytest .param (
212- {"data_collection" : {"graphql" : {"variables" : False }}},
227+ {"_experiments" : { " data_collection" : {"graphql" : {"variables" : False } }}},
213228 {"graphql.document" : True , "graphql.variables" : False },
214229 id = "explicit_partial_graphql_fills_omitted" ,
215230 ),
216231 pytest .param (
217- {"data_collection" : {"frame_context_lines" : True }},
232+ {"_experiments" : { " data_collection" : {"frame_context_lines" : True } }},
218233 {"frame_context_lines" : 5 },
219234 id = "frame_context_lines_bool_fallback_true" ,
220235 ),
221236 pytest .param (
222- {"data_collection" : {"frame_context_lines" : False }},
237+ {"_experiments" : { " data_collection" : {"frame_context_lines" : False } }},
223238 {"frame_context_lines" : 0 },
224239 id = "frame_context_lines_bool_fallback_false" ,
225240 ),
226241 pytest .param (
227- {"data_collection" : {"frame_context_lines" : 3 }},
242+ {"_experiments" : { " data_collection" : {"frame_context_lines" : 3 } }},
228243 {"frame_context_lines" : 3 },
229244 id = "frame_context_lines_bool_fallback_3" ,
230245 ),
231246 pytest .param (
232- {"data_collection" : {"frame_context_lines" : 0 }},
247+ {"_experiments" : { " data_collection" : {"frame_context_lines" : 0 } }},
233248 {"frame_context_lines" : 0 },
234249 id = "frame_context_lines_bool_fallback_0" ,
235250 ),
@@ -245,7 +260,8 @@ def test_initialize_client_data_collection_overrides_send_default_pii_and_warns(
245260 with warnings .catch_warnings (record = True ) as caught :
246261 warnings .simplefilter ("always" )
247262 dc = _initialize_client_with_config (
248- send_default_pii = True , data_collection = {"user_info" : False }
263+ send_default_pii = True ,
264+ _experiments = {"data_collection" : {"user_info" : False }},
249265 )
250266 assert dc ["user_info" ] is False # data_collection wins
251267 assert any (issubclass (w .category , DeprecationWarning ) for w in caught )
@@ -269,7 +285,7 @@ def test_initialize_client_data_collection_overrides_send_default_pii_and_warns(
269285 id = "send_default_pii_enables_user_info" ,
270286 ),
271287 pytest .param (
272- {"data_collection" : {"user_info" : False }},
288+ {"_experiments" : { " data_collection" : {"user_info" : False } }},
273289 {"user_info" : False , "provided_by_user" : True },
274290 id = "explicit_data_collection_overrides_user_info" ,
275291 ),
@@ -279,7 +295,10 @@ def test_initialize_client_data_collection_overrides_send_default_pii_and_warns(
279295 id = "dsnless_spotlight_rederives_data_collection" ,
280296 ),
281297 pytest .param (
282- {"spotlight" : True , "data_collection" : {"user_info" : False }},
298+ {
299+ "spotlight" : True ,
300+ "_experiments" : {"data_collection" : {"user_info" : False }},
301+ },
283302 {"provided_by_user" : True , "user_info" : False },
284303 id = "dsnless_spotlight_respects_explicit_data_collection" ,
285304 ),
@@ -293,3 +312,23 @@ def test_client_data_collection_settings(init_kwargs, expected):
293312 assert client .should_send_default_pii () is value
294313 else :
295314 assert client .options ["data_collection" ][key ] is value
315+
316+
317+ def test_has_data_collection_enabled_gates_on_presence ():
318+ assert has_data_collection_enabled (None ) is False
319+ assert has_data_collection_enabled ({"_experiments" : {}}) is False
320+ assert (
321+ has_data_collection_enabled ({"_experiments" : {"data_collection" : {}}}) is True
322+ )
323+ assert (
324+ has_data_collection_enabled ({"_experiments" : {"data_collection" : None }}) is True
325+ )
326+
327+
328+ def test_no_experiments_data_collection_values_fall_back_to_send_default_pii_configuration ():
329+ sentry_sdk .init (send_default_pii = True )
330+ client = sentry_sdk .get_client ()
331+ dc = client .options ["data_collection" ]
332+ assert dc ["provided_by_user" ] is False
333+ assert dc ["user_info" ] is True
334+ assert has_data_collection_enabled (client .options ) is False
0 commit comments