-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathuser_agent.py
83 lines (68 loc) · 2.85 KB
/
user_agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Copyright 2017-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
"""Placeholder docstring"""
from __future__ import absolute_import
import platform
import sys
import importlib_metadata
SDK_VERSION = importlib_metadata.version("sagemaker")
OS_NAME = platform.system() or "UnresolvedOS"
OS_VERSION = platform.release() or "UnresolvedOSVersion"
OS_NAME_VERSION = "{}/{}".format(OS_NAME, OS_VERSION)
PYTHON_VERSION = "Python/{}.{}.{}".format(
sys.version_info.major, sys.version_info.minor, sys.version_info.micro
)
def determine_prefix(user_agent=""):
"""Placeholder docstring"""
prefix = "AWS-SageMaker-Python-SDK/{}".format(SDK_VERSION)
if PYTHON_VERSION not in user_agent:
prefix = "{} {}".format(prefix, PYTHON_VERSION)
if OS_NAME_VERSION not in user_agent:
prefix = "{} {}".format(prefix, OS_NAME_VERSION)
try:
with open("/etc/opt/ml/sagemaker-notebook-instance-version.txt") as sagemaker_nbi_file:
prefix = "{} AWS-SageMaker-Notebook-Instance/{}".format(
prefix, sagemaker_nbi_file.read().strip()
)
except IOError:
# This file isn't expected to always exist, and we DO want to silently ignore failures.
pass
return prefix
def prepend_user_agent(client):
"""
Args:
client: boto3 client (e.g. sagemaker or sagemaker-runtime client)
Returns:
(str): Prepended user_agent string
"""
prefix = determine_prefix(client._client_config.user_agent)
if client._client_config.user_agent is None:
client._client_config.user_agent = prefix
else:
client._client_config.user_agent = "{} {}".format(prefix, client._client_config.user_agent)
return client._client_config.user_agent
def update_sdk_metrics(client, default_user_agent, sdk_metrics):
"""
Args:
client (boto3.*.Client): boto3 client (e.g. boto3.SageMaker.Client or
boto3.SageMakerRuntime.Client client)
default_user_agent (str): default user agent string without any call specific SDK metrics
sdk_metrics (sagemaker.SDKMetrics): an object of SDKMetrics specific to a call
"""
try:
client._client_config.user_agent = "{} {}".format(
default_user_agent, sdk_metrics.get_metrics_string()
)
except:
# Any telemetry is secondary and we DO want to silently ignore failures.
pass