Skip to content

Commit

Permalink
Speed up usage logging
Browse files Browse the repository at this point in the history
  • Loading branch information
nnansters committed Nov 10, 2023
1 parent fcc72e7 commit bb28916
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions nannyml/usage_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ def __init__(self, write_key: Optional[str] = None, machine_metadata: Optional[D

segment_analytics.max_retries = 1

segment_analytics.timeout = 3
segment_analytics.max_retries = 1

if machine_metadata is not None:
self._identify(machine_metadata)

Expand Down Expand Up @@ -252,16 +255,17 @@ def _is_nannyml_cloud():


def _get_runtime_environment():
if _is_running_in_aks():
return 'aks'
if _is_running_in_eks():
return 'eks'
if _is_running_in_kubernetes():
return 'kubernetes'
if _is_running_in_notebook():
return 'notebook'
elif _is_running_in_docker():
return 'docker'
elif _is_running_in_notebook():
return 'notebook'
elif _is_running_in_kubernetes():
if _is_running_in_aks():
return 'aks'
elif _is_running_in_eks():
return 'eks'
else:
return 'kubernetes'
else:
return 'native'

Expand All @@ -286,7 +290,7 @@ def _is_running_in_aks():

try:
metadata = requests.get(
'http://169.254.169.254/metadata/instance?api-version=2021-02-01', headers={'Metadata': 'true'}
'http://169.254.169.254/metadata/instance?api-version=2021-02-01', headers={'Metadata': 'true'}, timeout=5
)
return metadata.status_code == 200
except Exception:
Expand All @@ -298,7 +302,9 @@ def _is_running_in_eks():

try:
token = requests.put(
'http://169.254.169.254/latest/api/token', headers={'X-aws-ec2-metadata-token-ttl-seconds': 21600}
'http://169.254.169.254/latest/api/token',
headers={'X-aws-ec2-metadata-token-ttl-seconds': 21600},
timeout=5,
).raw()

metadata = requests.get('http://169.254.169.254/latest/meta-data/', headers={'X-aws-ec2-metadata-token': token})
Expand Down

0 comments on commit bb28916

Please sign in to comment.