Skip to content

Commit cd5273c

Browse files
committed
add telemetry to plugin
1 parent 9a47c83 commit cd5273c

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

Diff for: aws_advanced_python_wrapper/fastest_response_strategy_plugin.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
WrapperProperties)
3535
from aws_advanced_python_wrapper.utils.sliding_expiration_cache import \
3636
SlidingExpirationCacheWithCleanupThread
37+
from aws_advanced_python_wrapper.utils.telemetry.telemetry import (
38+
TelemetryContext, TelemetryCounter, TelemetryFactory, TelemetryGauge,
39+
TelemetryTraceLevel)
3740

3841
if TYPE_CHECKING:
3942
from aws_advanced_python_wrapper.driver_dialect import DriverDialect
@@ -160,6 +163,7 @@ def __init__(self, plugin_service: PluginService, host_info: HostInfo, props: Pr
160163
self._properties = props
161164
self._interval_ms = interval_ms
162165

166+
self._telemetry_factory = self._plugin_service.get_telemetry_factory()
163167
self._response_time: int = 0
164168
self._check_timestamp = datetime.now()
165169
self._lock: Lock = Lock()
@@ -172,6 +176,11 @@ def __init__(self, plugin_service: PluginService, host_info: HostInfo, props: Pr
172176

173177
self._executor: Executor = ThreadPoolExecutor(thread_name_prefix="HostResponseTimeMonitorExecutor")
174178

179+
# Report current response time (in milliseconds) to telemetry engine.
180+
# Report -1 if response time couldn't be measured.
181+
self._response_time_gauge = self._telemetry_factory.create_gauge("frt.response.time." + self._host_id,
182+
lambda: self._response_time if self._response_time != 2 ^ 31 else -1)
183+
175184
@property
176185
def response_time(self):
177186
return self._response_time
@@ -206,6 +215,9 @@ def _get_current_time(self):
206215
return time()
207216

208217
def run(self):
218+
context = self._telemetry_factory.open_telemetry_context(
219+
"node response time thread", TelemetryTraceLevel.TOP_LEVEL)
220+
context.set_attribute("url", self._host_info.url)
209221
try:
210222
while not self.is_stopped:
211223
try:
@@ -249,6 +261,8 @@ def run(self):
249261
except Exception:
250262
# Do nothing
251263
pass
264+
if context is not None:
265+
context.close_context()
252266

253267
def _open_connection(self):
254268
try:
@@ -282,8 +296,8 @@ def get_response_time(self, host_info: HostInfo) -> int:
282296
Return a response time in milliseconds to the host.
283297
Return 2 ^ 31 if response time is not available.
284298
285-
@param hostSpec the host details
286-
@return response time in milliseconds for a desired host. It should return 2 ^ 31
299+
:param host_info: the host details
300+
:return: response time in milliseconds for a desired host. It should return 2 ^ 31
287301
if response time couldn't be measured.
288302
"""
289303
...
@@ -307,6 +321,8 @@ def __init__(self, plugin_service: PluginService, props: Properties, interval_ms
307321
self._properties = props
308322
self._interval_ms = interval_ms
309323
self._hosts: Tuple[HostInfo, ...] = ()
324+
self._telemetry_factory = self._plugin_service.get_telemetry_factory()
325+
self._node_count_gauge = self._telemetry_factory.create_gauge("frt.nodes.count", lambda: len(self._monitoring_nodes))
310326

311327
def get_response_time(self, host_info: HostInfo) -> int:
312328
monitor: Optional[HostResponseTimeMonitor] = HostResponseTimeServiceImpl._monitoring_nodes.get(host_info.url)

0 commit comments

Comments
 (0)