Skip to content

Commit 25f16ef

Browse files
author
Roja Reddy Sareddy
committed
change: Allow telemetry only in supported regions
1 parent 92b706a commit 25f16ef

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed

src/sagemaker/telemetry/constants.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,38 @@ 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+
# Classic
49+
US_EAST_1 = "us-east-1" # IAD
50+
US_EAST_2 = "us-east-2" # CMH
51+
US_WEST_1 = "us-west-1" # SFO
52+
US_WEST_2 = "us-west-2" # PDX
53+
AP_NORTHEAST_1 = "ap-northeast-1" # NRT
54+
AP_NORTHEAST_2 = "ap-northeast-2" # ICN
55+
AP_NORTHEAST_3 = "ap-northeast-3" # KIX
56+
AP_SOUTH_1 = "ap-south-1" # BOM
57+
AP_SOUTHEAST_1 = "ap-southeast-1" # SIN
58+
AP_SOUTHEAST_2 = "ap-southeast-2" # SYD
59+
CA_CENTRAL_1 = "ca-central-1" # YUL
60+
EU_CENTRAL_1 = "eu-central-1" # FRA
61+
EU_NORTH_1 = "eu-north-1" # ARN
62+
EU_WEST_1 = "eu-west-1" # DUB
63+
EU_WEST_2 = "eu-west-2" # LHR
64+
EU_WEST_3 = "eu-west-3" # CDG
65+
SA_EAST_1 = "sa-east-1" # GRU
66+
# Opt-in
67+
AP_EAST_1 = "ap-east-1" # HKG
68+
AP_SOUTHEAST_3 = "ap-southeast-3" # CGK
69+
AF_SOUTH_1 = "af-south-1" # CPT
70+
EU_SOUTH_1 = "eu-south-1" # MXP
71+
ME_SOUTH_1 = "me-south-1" # BAH
72+
MX_CENTRAL_1 = "mx-central-1" # QRO
73+
AP_SOUTHEAST_7 = "ap-southeast-7" # BKK
74+
AP_SOUTH_2 = "ap-south-2" # HYD
75+
AP_SOUTHEAST_4 = "ap-southeast-4" # MEL
76+
EU_CENTRAL_2 = "eu-central-2" # ZRH
77+
EU_SOUTH_2 = "eu-south-2" # ZAZ
78+
IL_CENTRAL_1 = "il-central-1" # TLV
79+
ME_CENTRAL_1 = "me-central-1" # DXB

src/sagemaker/telemetry/telemetry_logging.py

Lines changed: 14 additions & 2 deletions
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,19 @@ 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+
194+
# Validate region if session exists
195+
if session:
196+
region = _get_region_or_default(session)
197+
try:
198+
Region(region)
199+
except ValueError:
200+
logger.debug(
201+
"Region not found in supported regions. Telemetry request will not be emitted."
202+
)
203+
return
204+
else: # telemetry will be sent to us-west-2 if no session available
205+
region = DEFAULT_AWS_REGION
194206
url = _construct_url(
195207
accountId,
196208
region,

tests/unit/sagemaker/telemetry/test_telemetry_logging.py

Lines changed: 36 additions & 0 deletions
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)