Skip to content

Use custom user agent string at all times #1

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

Merged
merged 3 commits into from
Dec 5, 2017
Merged
Changes from 1 commit
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
32 changes: 13 additions & 19 deletions src/sagemaker/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,24 @@

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

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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If no OS name can be resolved, platform.system() will return an empty string. Can this be something like:

OS_NAME = platform.system() or 'UnresolvedOS'

Same for OS_VERSION

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done. Also moved everything to a new module.

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')
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we don't preserve the original user agent string as well. Why not just append the SageMaker string to the end?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default user agent string is 'Boto3/1.4.8 Python/3.6.3 Darwin/15.6.0 Botocore/1.8.6', which is why I just added the Boto and Botocore versions to the end. It needs to start with 'AWS-SageMaker' and be followed by 'language/version OS/version' for parsing. Appending the original user agent string here is a good call, though.


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):
Expand Down