Skip to content

Commit 90e9c9f

Browse files
rsareddy0329Roja Reddy Sareddy
and
Roja Reddy Sareddy
authored
change: Allow telemetry only in supported regions (#5009)
* change: Allow telemetry only in supported regions * change: Allow telemetry only in supported regions * change: Allow telemetry only in supported regions * change: Allow telemetry only in supported regions * change: Allow telemetry only in supported regions --------- Co-authored-by: Roja Reddy Sareddy <[email protected]>
1 parent c753da0 commit 90e9c9f

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed

src/sagemaker/telemetry/constants.py

+37
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,40 @@ class Status(Enum):
4242
def __str__(self): # pylint: disable=E0307
4343
"""Return the status name."""
4444
return self.name
45+
46+
47+
class Region(str, Enum):
48+
"""Telemetry: List of all supported AWS regions."""
49+
50+
# Classic
51+
US_EAST_1 = "us-east-1" # IAD
52+
US_EAST_2 = "us-east-2" # CMH
53+
US_WEST_1 = "us-west-1" # SFO
54+
US_WEST_2 = "us-west-2" # PDX
55+
AP_NORTHEAST_1 = "ap-northeast-1" # NRT
56+
AP_NORTHEAST_2 = "ap-northeast-2" # ICN
57+
AP_NORTHEAST_3 = "ap-northeast-3" # KIX
58+
AP_SOUTH_1 = "ap-south-1" # BOM
59+
AP_SOUTHEAST_1 = "ap-southeast-1" # SIN
60+
AP_SOUTHEAST_2 = "ap-southeast-2" # SYD
61+
CA_CENTRAL_1 = "ca-central-1" # YUL
62+
EU_CENTRAL_1 = "eu-central-1" # FRA
63+
EU_NORTH_1 = "eu-north-1" # ARN
64+
EU_WEST_1 = "eu-west-1" # DUB
65+
EU_WEST_2 = "eu-west-2" # LHR
66+
EU_WEST_3 = "eu-west-3" # CDG
67+
SA_EAST_1 = "sa-east-1" # GRU
68+
# Opt-in
69+
AP_EAST_1 = "ap-east-1" # HKG
70+
AP_SOUTHEAST_3 = "ap-southeast-3" # CGK
71+
AF_SOUTH_1 = "af-south-1" # CPT
72+
EU_SOUTH_1 = "eu-south-1" # MXP
73+
ME_SOUTH_1 = "me-south-1" # BAH
74+
MX_CENTRAL_1 = "mx-central-1" # QRO
75+
AP_SOUTHEAST_7 = "ap-southeast-7" # BKK
76+
AP_SOUTH_2 = "ap-south-2" # HYD
77+
AP_SOUTHEAST_4 = "ap-southeast-4" # MEL
78+
EU_CENTRAL_2 = "eu-central-2" # ZRH
79+
EU_SOUTH_2 = "eu-south-2" # ZAZ
80+
IL_CENTRAL_1 = "il-central-1" # TLV
81+
ME_CENTRAL_1 = "me-central-1" # DXB

src/sagemaker/telemetry/telemetry_logging.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from sagemaker.telemetry.constants import (
2828
Feature,
2929
Status,
30+
Region,
3031
DEFAULT_AWS_REGION,
3132
)
3233
from sagemaker.user_agent import SDK_VERSION, process_studio_metadata_file
@@ -189,8 +190,16 @@ def _send_telemetry_request(
189190
"""Make GET request to an empty object in S3 bucket"""
190191
try:
191192
accountId = _get_accountId(session) if session else "NotAvailable"
192-
# telemetry will be sent to us-west-2 if no session availale
193-
region = _get_region_or_default(session) if session else DEFAULT_AWS_REGION
193+
region = _get_region_or_default(session)
194+
195+
try:
196+
Region(region) # Validate the region
197+
except ValueError:
198+
logger.warning(
199+
"Region not found in supported regions. Telemetry request will not be emitted."
200+
)
201+
return
202+
194203
url = _construct_url(
195204
accountId,
196205
region,
@@ -268,6 +277,7 @@ def _get_region_or_default(session):
268277

269278
def _get_default_sagemaker_session():
270279
"""Return the default sagemaker session"""
280+
271281
boto_session = boto3.Session(region_name=DEFAULT_AWS_REGION)
272282
sagemaker_session = Session(boto_session=boto_session)
273283

tests/unit/sagemaker/telemetry/test_telemetry_logging.py

+36
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,39 @@ def test_get_default_sagemaker_session_with_no_region(self):
300300
assert "Must setup local AWS configuration with a region supported by SageMaker." in str(
301301
context.exception
302302
)
303+
304+
@patch("sagemaker.telemetry.telemetry_logging._get_accountId")
305+
@patch("sagemaker.telemetry.telemetry_logging._get_region_or_default")
306+
def test_send_telemetry_request_valid_region(self, mock_get_region, mock_get_accountId):
307+
"""Test to verify telemetry request is sent when region is valid"""
308+
mock_get_accountId.return_value = "testAccountId"
309+
mock_session = MagicMock()
310+
311+
# Test with valid region
312+
mock_get_region.return_value = "us-east-1"
313+
with patch(
314+
"sagemaker.telemetry.telemetry_logging._requests_helper"
315+
) as mock_requests_helper:
316+
_send_telemetry_request(1, [1, 2], mock_session)
317+
# Assert telemetry request was sent
318+
mock_requests_helper.assert_called_once_with(
319+
"https://sm-pysdk-t-us-east-1.s3.us-east-1.amazonaws.com/telemetry?"
320+
"x-accountId=testAccountId&x-status=1&x-feature=1,2",
321+
2,
322+
)
323+
324+
@patch("sagemaker.telemetry.telemetry_logging._get_accountId")
325+
@patch("sagemaker.telemetry.telemetry_logging._get_region_or_default")
326+
def test_send_telemetry_request_invalid_region(self, mock_get_region, mock_get_accountId):
327+
"""Test to verify telemetry request is not sent when region is invalid"""
328+
mock_get_accountId.return_value = "testAccountId"
329+
mock_session = MagicMock()
330+
331+
# Test with invalid region
332+
mock_get_region.return_value = "invalid-region"
333+
with patch(
334+
"sagemaker.telemetry.telemetry_logging._requests_helper"
335+
) as mock_requests_helper:
336+
_send_telemetry_request(1, [1, 2], mock_session)
337+
# Assert telemetry request was not sent
338+
mock_requests_helper.assert_not_called()

0 commit comments

Comments
 (0)