Skip to content

Commit 09eb3f7

Browse files
committed
address feedback
1 parent 707344e commit 09eb3f7

File tree

5 files changed

+42
-31
lines changed

5 files changed

+42
-31
lines changed

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/sampler/aws_xray_sampling_client.py renamed to aws-opentelemetry-distro/src/amazon/opentelemetry/distro/sampler/_aws_xray_sampling_client.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
import requests
77

8-
from amazon.opentelemetry.distro.sampler.sampling_rule import SamplingRule
8+
from amazon.opentelemetry.distro.sampler._sampling_rule import _SamplingRule
99

1010
_logger = getLogger(__name__)
1111

1212

13-
class AwsXRaySamplingClient:
13+
class _AwsXRaySamplingClient:
1414
def __init__(self, endpoint=None, log_level=None):
1515
# Override default log level
1616
if log_level is not None:
@@ -27,22 +27,22 @@ def get_sampling_rules(self):
2727
try:
2828
xray_response = requests.post(url=self.__get_sampling_rules_endpoint, headers=headers, timeout=20)
2929
if xray_response is None:
30-
raise ValueError("GetSamplingRules response is None")
30+
_logger.error("GetSamplingRules response is None")
31+
return []
3132
sampling_rules_response = xray_response.json()
3233
if "SamplingRuleRecords" not in sampling_rules_response:
33-
raise ValueError(
34-
f"SamplingRuleRecords is missing in getSamplingRules response:{sampling_rules_response}"
34+
_logger.error(
35+
"SamplingRuleRecords is missing in getSamplingRules response: %s", sampling_rules_response
3536
)
37+
return []
3638

3739
sampling_rules_records = sampling_rules_response["SamplingRuleRecords"]
3840
for record in sampling_rules_records:
39-
sampling_rules.append(SamplingRule(**record["SamplingRule"]))
41+
sampling_rules.append(_SamplingRule(**record["SamplingRule"]))
4042

4143
except requests.exceptions.RequestException as req_err:
42-
_logger.exception("Request error occurred: %s", req_err)
44+
_logger.error("Request error occurred: %s", req_err)
4345
except json.JSONDecodeError as json_err:
44-
_logger.exception("Error in decoding JSON response: %s", json_err)
45-
except ValueError as ex:
46-
_logger.exception("Exception occurred: %s", ex)
46+
_logger.error("Error in decoding JSON response: %s", json_err)
4747

4848
return sampling_rules

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/sampler/sampling_rule.py renamed to aws-opentelemetry-distro/src/amazon/opentelemetry/distro/sampler/_sampling_rule.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Disable snake_case naming style so this class can match the sampling rules response from X-Ray
66
# pylint: disable=invalid-name
7-
class SamplingRule:
7+
class _SamplingRule:
88
def __init__(
99
self,
1010
Attributes=None,

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/sampler/aws_xray_remote_sampler.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
from threading import Timer
66
from typing import Optional, Sequence
77

8-
from amazon.opentelemetry.distro.sampler.aws_xray_sampling_client import AwsXRaySamplingClient
8+
from typing_extensions import override
9+
10+
from amazon.opentelemetry.distro.sampler._aws_xray_sampling_client import _AwsXRaySamplingClient
911
from opentelemetry.context import Context
1012
from opentelemetry.sdk.resources import Resource
1113
from opentelemetry.sdk.trace.sampling import ALWAYS_OFF, Sampler, SamplingResult
@@ -15,8 +17,8 @@
1517

1618
_logger = getLogger(__name__)
1719

18-
DEFAULT_RULES_POLLING_INTERVAL = 300
19-
DEFAULT_TARGET_POLLING_INTERVAL = 10
20+
DEFAULT_RULES_POLLING_INTERVAL_SECONDS = 300
21+
DEFAULT_TARGET_POLLING_INTERVAL_SECONDS = 10
2022
DEFAULT_SAMPLING_PROXY_ENDPOINT = "http://127.0.0.1:2000"
2123

2224

@@ -25,36 +27,44 @@ class AwsXRayRemoteSampler(Sampler):
2527
Remote Sampler for OpenTelemetry that gets sampling configurations from AWS X-Ray
2628
2729
Args:
28-
resource: OpenTelemetry Resource (Optional)
30+
resource: OpenTelemetry Resource (Required)
2931
endpoint: proxy endpoint for AWS X-Ray Sampling (Optional)
3032
polling_interval: Polling interval for getSamplingRules call (Optional)
3133
log_level: custom log level configuration for remote sampler (Optional)
3234
"""
3335

3436
__resource: Resource
3537
__polling_interval: int
36-
__xray_client: AwsXRaySamplingClient
38+
__xray_client: _AwsXRaySamplingClient
3739

3840
def __init__(
3941
self,
40-
resource=None,
42+
resource: Resource,
4143
endpoint=DEFAULT_SAMPLING_PROXY_ENDPOINT,
42-
polling_interval=DEFAULT_RULES_POLLING_INTERVAL,
44+
polling_interval=DEFAULT_RULES_POLLING_INTERVAL_SECONDS,
4345
log_level=None,
4446
):
4547
# Override default log level
4648
if log_level is not None:
4749
_logger.setLevel(log_level)
4850

49-
self.__xray_client = AwsXRaySamplingClient(endpoint, log_level=log_level)
51+
self.__xray_client = _AwsXRaySamplingClient(endpoint, log_level=log_level)
5052
self.__polling_interval = polling_interval
5153

5254
# pylint: disable=unused-private-member
53-
self.__resource = resource
54-
55-
self.__start_sampling_rule_poller()
55+
if resource is not None:
56+
self.__resource = resource
57+
else:
58+
_logger.warning("OTel Resource provided is `None`. Defaulting to empty resource")
59+
self.__resource = Resource.get_empty()
60+
61+
# Schedule the next rule poll now
62+
self._timer = Timer(0, self.__start_sampling_rule_poller)
63+
self._timer.daemon = True
64+
self._timer.start()
5665

5766
# pylint: disable=no-self-use
67+
@override
5868
def should_sample(
5969
self,
6070
parent_context: Optional["Context"],
@@ -64,13 +74,14 @@ def should_sample(
6474
attributes: Attributes = None,
6575
links: Sequence["Link"] = None,
6676
trace_state: "TraceState" = None,
67-
) -> "SamplingResult":
77+
) -> SamplingResult:
6878
# TODO: add sampling functionality
6979
return ALWAYS_OFF.should_sample(
7080
self, parent_context, trace_id, name, kind=kind, attributes=attributes, links=links, trace_state=trace_state
7181
)
7282

7383
# pylint: disable=no-self-use
84+
@override
7485
def get_description(self) -> str:
7586
description = "AwsXRayRemoteSampler{remote sampling with AWS X-Ray}"
7687
return description

aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/sampler/test_aws_xray_remote_sampler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from opentelemetry.sdk.resources import Resource
88

99

10-
class AwsXRayRemoteSamplerTest(TestCase):
10+
class TestAwsXRayRemoteSampler(TestCase):
1111
def test_create_remote_sampler_with_empty_resource(self):
1212
rs = AwsXRayRemoteSampler(resource=Resource.get_empty())
1313
self.assertIsNotNone(rs._timer)
@@ -40,6 +40,6 @@ def test_create_remote_sampler_with_all_fields_populated(self):
4040
self.assertEqual(
4141
rs._AwsXRayRemoteSampler__xray_client._AwsXRaySamplingClient__get_sampling_rules_endpoint,
4242
"http://abc.com/GetSamplingRules",
43-
) # "http://127.0.0.1:2000"
43+
)
4444
self.assertEqual(rs._AwsXRayRemoteSampler__resource.attributes["service.name"], "test-service-name")
4545
self.assertEqual(rs._AwsXRayRemoteSampler__resource.attributes["cloud.platform"], "test-cloud-platform")

aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/sampler/test_aws_xray_sampling_client.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,27 @@
66
from unittest import TestCase
77
from unittest.mock import patch
88

9-
from amazon.opentelemetry.distro.sampler.aws_xray_sampling_client import AwsXRaySamplingClient
9+
from amazon.opentelemetry.distro.sampler._aws_xray_sampling_client import _AwsXRaySamplingClient
1010

11-
SAMPLING_CLIENT_LOGGER_NAME = "amazon.opentelemetry.distro.sampler.aws_xray_sampling_client"
11+
SAMPLING_CLIENT_LOGGER_NAME = "amazon.opentelemetry.distro.sampler._aws_xray_sampling_client"
1212
_logger = getLogger(SAMPLING_CLIENT_LOGGER_NAME)
1313

1414
TEST_DIR = os.path.dirname(os.path.realpath(__file__))
1515
DATA_DIR = os.path.join(TEST_DIR, "data")
1616

1717

18-
class AwsXRaySamplingClientTest(TestCase):
18+
class TestAwsXRaySamplingClient(TestCase):
1919
@patch("requests.post")
2020
def test_get_no_sampling_rules(self, mock_post=None):
2121
mock_post.return_value.configure_mock(**{"json.return_value": {"SamplingRuleRecords": []}})
22-
client = AwsXRaySamplingClient("http://127.0.0.1:2000")
22+
client = _AwsXRaySamplingClient("http://127.0.0.1:2000")
2323
sampling_rules = client.get_sampling_rules()
2424
self.assertTrue(len(sampling_rules) == 0)
2525

2626
@patch("requests.post")
2727
def test_get_invalid_response(self, mock_post=None):
2828
mock_post.return_value.configure_mock(**{"json.return_value": {}})
29-
client = AwsXRaySamplingClient("http://127.0.0.1:2000")
29+
client = _AwsXRaySamplingClient("http://127.0.0.1:2000")
3030
with self.assertLogs(_logger, level="ERROR"):
3131
sampling_rules = client.get_sampling_rules()
3232
self.assertTrue(len(sampling_rules) == 0)
@@ -36,6 +36,6 @@ def test_get_two_sampling_rules(self, mock_post=None):
3636
with open(f"{DATA_DIR}/get-sampling-rules-response-sample.json", encoding="UTF-8") as file:
3737
mock_post.return_value.configure_mock(**{"json.return_value": json.load(file)})
3838
file.close()
39-
client = AwsXRaySamplingClient("http://127.0.0.1:2000")
39+
client = _AwsXRaySamplingClient("http://127.0.0.1:2000")
4040
sampling_rules = client.get_sampling_rules()
4141
self.assertTrue(len(sampling_rules) == 3)

0 commit comments

Comments
 (0)