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
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
26 changes: 5 additions & 21 deletions src/sagemaker/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,18 @@
import re

import os
import pkg_resources
import sys
import time

import boto3
import botocore.session
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


logging.basicConfig()
LOGGER = logging.getLogger('sagemaker')
Expand Down Expand Up @@ -71,30 +68,17 @@ 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')
prepend_user_agent(self.sagemaker_client)

def _botocore_session_with_user_agent(self):
botocore_session = botocore.session.get_session()

# 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)

return botocore_session
self.sagemaker_runtime_client = sagemaker_runtime_client or self.boto_session.client('runtime.sagemaker')
prepend_user_agent(self.sagemaker_runtime_client)

@property
def boto_region_name(self):
Expand Down
32 changes: 32 additions & 0 deletions src/sagemaker/user_agent.py
Original file line number Diff line number Diff line change
@@ -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)