From bbb05d4c28a4fa9fa6b0655d82c10d1919c17d2e Mon Sep 17 00:00:00 2001 From: Lauren Yu <6631887+laurenyu@users.noreply.github.com> Date: Fri, 1 Dec 2017 10:27:36 -0800 Subject: [PATCH 1/2] Use custom user agent string even if there is a user-supplied boto session or SageMaker client --- src/sagemaker/session.py | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/src/sagemaker/session.py b/src/sagemaker/session.py index c078f4af83..ca7a93478d 100644 --- a/src/sagemaker/session.py +++ b/src/sagemaker/session.py @@ -17,11 +17,12 @@ import os import pkg_resources +import platform import sys import time import boto3 -import botocore.session +import botocore import json import six from botocore.exceptions import ClientError @@ -29,8 +30,11 @@ from sagemaker.utils import name_from_image import sagemaker.logs -SDK_VERSION = pkg_resources.require('sagemaker')[0].version +SDK_VERSION = pkg_resources.require('sagemaker')[0].version +OS_NAME = platform.system() +OS_VERSION = platform.release() +PYTHON_VERSION = '{}.{}.{}'.format(sys.version_info.major, sys.version_info.minor, sys.version_info.micro) logging.basicConfig() LOGGER = logging.getLogger('sagemaker') @@ -71,30 +75,20 @@ def __init__(self, boto_session=None, sagemaker_client=None, sagemaker_runtime_c If not provided, one will be created using this instance's ``boto_session``. """ self._default_bucket = None - - if boto_session is None: - botocore_session = self._botocore_session_with_user_agent() - self.boto_session = boto3.Session(botocore_session=botocore_session) - else: - self.boto_session = boto_session + self.boto_session = boto_session or boto3.Session() region = self.boto_session.region_name if region is None: raise ValueError('Must setup local AWS configuration with a region supported by SageMaker.') - self.sagemaker_client = sagemaker_client or self.boto_session.client('sagemaker') - self.sagemaker_runtime_client = sagemaker_runtime_client or self.boto_session.client('runtime.sagemaker') - - def _botocore_session_with_user_agent(self): - botocore_session = botocore.session.get_session() + user_agent_string = 'AWS-SageMaker-Python-SDK/{} Python/{} {}/{} Boto3/{} Botocore/{}'\ + .format(SDK_VERSION, PYTHON_VERSION, OS_NAME, OS_VERSION, boto3.__version__, botocore.__version__) - # Some information is appended automatically, so this creates a user agent string that looks like - # 'AWS-SageMaker-Python-SDK/1.0/1.7.45 Python/3.6.2 Darwin/15.6.0 Boto3/1.4.7 Botocore/1.7.45' - botocore_session.user_agent_name = 'AWS-SageMaker-Python-SDK/{}'.format(SDK_VERSION) - botocore_session.user_agent_extra = 'Boto3/{} Botocore/{}'.format(boto3.__version__, - botocore_session.user_agent_version) + self.sagemaker_client = sagemaker_client or self.boto_session.client('sagemaker') + self.sagemaker_client._client_config.user_agent = user_agent_string - return botocore_session + self.sagemaker_runtime_client = sagemaker_runtime_client or self.boto_session.client('runtime.sagemaker') + self.sagemaker_runtime_client._client_config.user_agent = user_agent_string @property def boto_region_name(self): From aec864db43fe408c22b4dff1b10acde97570260a Mon Sep 17 00:00:00 2001 From: Lauren Yu <6631887+laurenyu@users.noreply.github.com> Date: Mon, 4 Dec 2017 14:20:24 -0800 Subject: [PATCH 2/2] Move user agent string construction to its own module --- src/sagemaker/session.py | 16 +++------------- src/sagemaker/user_agent.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 13 deletions(-) create mode 100644 src/sagemaker/user_agent.py diff --git a/src/sagemaker/session.py b/src/sagemaker/session.py index ca7a93478d..5effcb06c3 100644 --- a/src/sagemaker/session.py +++ b/src/sagemaker/session.py @@ -16,26 +16,19 @@ import re import os -import pkg_resources -import platform import sys import time import boto3 -import botocore import json import six from botocore.exceptions import ClientError +from sagemaker.user_agent import prepend_user_agent from sagemaker.utils import name_from_image import sagemaker.logs -SDK_VERSION = pkg_resources.require('sagemaker')[0].version -OS_NAME = platform.system() -OS_VERSION = platform.release() -PYTHON_VERSION = '{}.{}.{}'.format(sys.version_info.major, sys.version_info.minor, sys.version_info.micro) - logging.basicConfig() LOGGER = logging.getLogger('sagemaker') LOGGER.setLevel(logging.INFO) @@ -81,14 +74,11 @@ def __init__(self, boto_session=None, sagemaker_client=None, sagemaker_runtime_c if region is None: raise ValueError('Must setup local AWS configuration with a region supported by SageMaker.') - user_agent_string = 'AWS-SageMaker-Python-SDK/{} Python/{} {}/{} Boto3/{} Botocore/{}'\ - .format(SDK_VERSION, PYTHON_VERSION, OS_NAME, OS_VERSION, boto3.__version__, botocore.__version__) - self.sagemaker_client = sagemaker_client or self.boto_session.client('sagemaker') - self.sagemaker_client._client_config.user_agent = user_agent_string + prepend_user_agent(self.sagemaker_client) self.sagemaker_runtime_client = sagemaker_runtime_client or self.boto_session.client('runtime.sagemaker') - self.sagemaker_runtime_client._client_config.user_agent = user_agent_string + prepend_user_agent(self.sagemaker_runtime_client) @property def boto_region_name(self): diff --git a/src/sagemaker/user_agent.py b/src/sagemaker/user_agent.py new file mode 100644 index 0000000000..b32d29c217 --- /dev/null +++ b/src/sagemaker/user_agent.py @@ -0,0 +1,32 @@ +# Copyright 2017 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. +import pkg_resources +import platform +import sys + +import boto3 +import botocore + +SDK_VERSION = pkg_resources.require('sagemaker')[0].version +OS_NAME = platform.system() or 'UnresolvedOS' +OS_VERSION = platform.release() or 'UnresolvedOSVersion' +PYTHON_VERSION = '{}.{}.{}'.format(sys.version_info.major, sys.version_info.minor, sys.version_info.micro) + + +def prepend_user_agent(client): + prefix = 'AWS-SageMaker-Python-SDK/{} Python/{} {}/{} Boto3/{} Botocore/{}'\ + .format(SDK_VERSION, PYTHON_VERSION, OS_NAME, OS_VERSION, boto3.__version__, botocore.__version__) + 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)