Skip to content

Commit

Permalink
[analytics] Made suppression fixes (#324) (#325)
Browse files Browse the repository at this point in the history
* Made suppression fixes

* Suppress request logs while fetching external ip

(cherry picked from commit 960b213)
  • Loading branch information
rahul-tuli committed Jun 1, 2023
1 parent 08fa9dc commit e0992c6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
29 changes: 14 additions & 15 deletions src/sparsezoo/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import asyncio
import json
import os
import threading
Expand All @@ -25,13 +24,13 @@
from requests import HTTPError

from sparsezoo.utils.gdpr import is_gdpr_country
from sparsezoo.utils.helpers import disable_request_logs
from sparsezoo.version import version as sparsezoo_version


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


_LOOP = asyncio.get_event_loop()
_DEBUG = os.getenv("NM_DEBUG_ANALYTICS")


Expand Down Expand Up @@ -133,19 +132,19 @@ def _send_request():
"Content-Type": "application/json",
}
data = json.dumps(payload)

try:
response = requests.post(self._url, headers=headers, data=data)
response.raise_for_status()
body = response.content
if _DEBUG:
print(body)
except HTTPError as http_error:
if _DEBUG:
print(http_error)

if raise_errors:
raise http_error
with disable_request_logs():
try:
response = requests.post(self._url, headers=headers, data=data)
response.raise_for_status()
body = response.content
if _DEBUG:
print(body)
except HTTPError as http_error:
if _DEBUG:
print(http_error)

if raise_errors:
raise http_error

thread = threading.Thread(target=_send_request)
thread.start()
Expand Down
10 changes: 8 additions & 2 deletions src/sparsezoo/utils/gdpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import contextlib
from typing import Optional

import geocoder
import requests
from requests import HTTPError

from sparsezoo.utils.helpers import disable_request_logs


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

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

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

return country_code is None or country_code in _GDPR_COUNTRY_CODES
20 changes: 20 additions & 0 deletions src/sparsezoo/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
# limitations under the License.

import glob
import logging
import os
from contextlib import contextmanager
from typing import Any


Expand All @@ -23,6 +25,7 @@
"clean_path",
"remove_tar_duplicates",
"convert_to_bool",
"disable_request_logs",
]


Expand Down Expand Up @@ -87,3 +90,20 @@ def clean_path(path: str) -> str:
:return: a cleaned version that expands the user path and creates an absolute path
"""
return os.path.abspath(os.path.expanduser(path))


@contextmanager
def disable_request_logs():
"""
Context manager for disabling logs for a requests session
"""
loggers = [logging.getLogger("requests"), logging.getLogger("urllib3")]

original_disabled_states = [logger.disabled for logger in loggers]
for logger in loggers:
logger.disabled = True

yield

for logger, original_disabled_state in zip(loggers, original_disabled_states):
logger.disabled = original_disabled_state

0 comments on commit e0992c6

Please sign in to comment.