Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[analytics] Made suppression fixes #324

Merged
merged 2 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -140,19 +139,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