Skip to content

Commit 4aca10e

Browse files
committed
refactor: use time.perf_counter_ns() instead of time.time_ns() for duration measurement
perf_counter_ns is monotonic and designed for elapsed-time measurement; time.time_ns reflects wall-clock time and can go backward due to NTP or clock adjustments.
1 parent 926598e commit 4aca10e

1 file changed

Lines changed: 11 additions & 11 deletions

File tree

packages/sdk/server-ai/src/ldai/tracker.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,11 @@ def track_duration_of(self, func, *, graph_key: Optional[str] = None):
156156
:param graph_key: When set, passed through to :meth:`track_duration`.
157157
:return: Result of the tracked function.
158158
"""
159-
start_ns = time.time_ns()
159+
start_ns = time.perf_counter_ns()
160160
try:
161161
result = func()
162162
finally:
163-
duration = (time.time_ns() - start_ns) // 1_000_000 # duration in milliseconds
163+
duration = (time.perf_counter_ns() - start_ns) // 1_000_000 # duration in milliseconds
164164
self.track_duration(duration, graph_key=graph_key)
165165

166166
return result
@@ -205,16 +205,16 @@ def track_metrics_of(
205205
:param graph_key: When set, include ``graphKey`` on emitted config-level events.
206206
:return: The result of the operation
207207
"""
208-
start_ns = time.time_ns()
208+
start_ns = time.perf_counter_ns()
209209
try:
210210
result = func()
211211
except Exception as err:
212-
duration = (time.time_ns() - start_ns) // 1_000_000
212+
duration = (time.perf_counter_ns() - start_ns) // 1_000_000
213213
self.track_duration(duration, graph_key=graph_key)
214214
self.track_error(graph_key=graph_key)
215215
raise err
216216

217-
duration = (time.time_ns() - start_ns) // 1_000_000
217+
duration = (time.perf_counter_ns() - start_ns) // 1_000_000
218218
self.track_duration(duration, graph_key=graph_key)
219219
return self._track_from_metrics_extractor(result, metrics_extractor, graph_key=graph_key)
220220

@@ -231,17 +231,17 @@ async def track_metrics_of_async(
231231
:param graph_key: When set, include ``graphKey`` on emitted config-level events.
232232
:return: The result of the operation
233233
"""
234-
start_ns = time.time_ns()
234+
start_ns = time.perf_counter_ns()
235235
result = None
236236
try:
237237
result = await func()
238238
except Exception as err:
239-
duration = (time.time_ns() - start_ns) // 1_000_000
239+
duration = (time.perf_counter_ns() - start_ns) // 1_000_000
240240
self.track_duration(duration, graph_key=graph_key)
241241
self.track_error(graph_key=graph_key)
242242
raise err
243243

244-
duration = (time.time_ns() - start_ns) // 1_000_000
244+
duration = (time.perf_counter_ns() - start_ns) // 1_000_000
245245
self.track_duration(duration, graph_key=graph_key)
246246
return self._track_from_metrics_extractor(result, metrics_extractor, graph_key=graph_key)
247247

@@ -351,16 +351,16 @@ def track_openai_metrics(self, func):
351351
:param func: Function to track.
352352
:return: Result of the tracked function.
353353
"""
354-
start_ns = time.time_ns()
354+
start_ns = time.perf_counter_ns()
355355
try:
356356
result = func()
357-
duration = (time.time_ns() - start_ns) // 1_000_000
357+
duration = (time.perf_counter_ns() - start_ns) // 1_000_000
358358
self.track_duration(duration)
359359
self.track_success()
360360
if hasattr(result, "usage") and hasattr(result.usage, "to_dict"):
361361
self.track_tokens(_openai_to_token_usage(result.usage.to_dict()))
362362
except Exception:
363-
duration = (time.time_ns() - start_ns) // 1_000_000
363+
duration = (time.perf_counter_ns() - start_ns) // 1_000_000
364364
self.track_duration(duration)
365365
self.track_error()
366366
raise

0 commit comments

Comments
 (0)