Skip to content

Commit e0992c6

Browse files
authored
[analytics] Made suppression fixes (#324) (#325)
* Made suppression fixes * Suppress request logs while fetching external ip (cherry picked from commit 960b213)
1 parent 08fa9dc commit e0992c6

File tree

3 files changed

+42
-17
lines changed

3 files changed

+42
-17
lines changed

src/sparsezoo/analytics.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import asyncio
1615
import json
1716
import os
1817
import threading
@@ -25,13 +24,13 @@
2524
from requests import HTTPError
2625

2726
from sparsezoo.utils.gdpr import is_gdpr_country
27+
from sparsezoo.utils.helpers import disable_request_logs
2828
from sparsezoo.version import version as sparsezoo_version
2929

3030

3131
__all__ = ["GoogleAnalytics", "analytics_disabled", "sparsezoo_analytics"]
3232

3333

34-
_LOOP = asyncio.get_event_loop()
3534
_DEBUG = os.getenv("NM_DEBUG_ANALYTICS")
3635

3736

@@ -133,19 +132,19 @@ def _send_request():
133132
"Content-Type": "application/json",
134133
}
135134
data = json.dumps(payload)
136-
137-
try:
138-
response = requests.post(self._url, headers=headers, data=data)
139-
response.raise_for_status()
140-
body = response.content
141-
if _DEBUG:
142-
print(body)
143-
except HTTPError as http_error:
144-
if _DEBUG:
145-
print(http_error)
146-
147-
if raise_errors:
148-
raise http_error
135+
with disable_request_logs():
136+
try:
137+
response = requests.post(self._url, headers=headers, data=data)
138+
response.raise_for_status()
139+
body = response.content
140+
if _DEBUG:
141+
print(body)
142+
except HTTPError as http_error:
143+
if _DEBUG:
144+
print(http_error)
145+
146+
if raise_errors:
147+
raise http_error
149148

150149
thread = threading.Thread(target=_send_request)
151150
thread.start()

src/sparsezoo/utils/gdpr.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import contextlib
1516
from typing import Optional
1617

1718
import geocoder
1819
import requests
1920
from requests import HTTPError
2021

22+
from sparsezoo.utils.helpers import disable_request_logs
23+
2124

2225
__all__ = ["get_external_ip", "get_country_code", "is_gdpr_country"]
2326

@@ -58,7 +61,8 @@ def get_external_ip() -> Optional[str]:
5861
:return: the external ip of the machine, None if unable to get
5962
"""
6063
try:
61-
response = requests.get("https://ident.me")
64+
with disable_request_logs():
65+
response = requests.get("https://ident.me")
6266
external_ip = response.text.strip()
6367

6468
return external_ip
@@ -84,6 +88,8 @@ def is_gdpr_country() -> bool:
8488
:return: True if the country code of the machine is in the GDPR list,
8589
False otherwise
8690
"""
87-
country_code = get_country_code()
91+
with contextlib.redirect_stderr(None):
92+
# suppress geocoder error logging
93+
country_code = get_country_code()
8894

8995
return country_code is None or country_code in _GDPR_COUNTRY_CODES

src/sparsezoo/utils/helpers.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
# limitations under the License.
1414

1515
import glob
16+
import logging
1617
import os
18+
from contextlib import contextmanager
1719
from typing import Any
1820

1921

@@ -23,6 +25,7 @@
2325
"clean_path",
2426
"remove_tar_duplicates",
2527
"convert_to_bool",
28+
"disable_request_logs",
2629
]
2730

2831

@@ -87,3 +90,20 @@ def clean_path(path: str) -> str:
8790
:return: a cleaned version that expands the user path and creates an absolute path
8891
"""
8992
return os.path.abspath(os.path.expanduser(path))
93+
94+
95+
@contextmanager
96+
def disable_request_logs():
97+
"""
98+
Context manager for disabling logs for a requests session
99+
"""
100+
loggers = [logging.getLogger("requests"), logging.getLogger("urllib3")]
101+
102+
original_disabled_states = [logger.disabled for logger in loggers]
103+
for logger in loggers:
104+
logger.disabled = True
105+
106+
yield
107+
108+
for logger, original_disabled_state in zip(loggers, original_disabled_states):
109+
logger.disabled = original_disabled_state

0 commit comments

Comments
 (0)