Skip to content

Commit fe50860

Browse files
author
Daniel OBrien
committed
add tracking decorator
1 parent 6c0742d commit fe50860

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

ldai/tracker.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import time
12
from typing import Dict, Union
23
from ldclient import Context, LDClient
34
from ldai.types import BedrockTokenUsage, FeedbackKind, TokenUsage, UnderscoreTokenUsage
@@ -18,6 +19,16 @@ def get_track_data(self):
1819
def track_duration(self, duration: int) -> None:
1920
self.ld_client.track('$ld:ai:duration:total', self.context, self.get_track_data(), duration)
2021

22+
def track_duration_of(self, func):
23+
def wrapper(*args, **kwargs):
24+
start_time = time.time()
25+
result = func(*args, **kwargs)
26+
end_time = time.time()
27+
duration = int((end_time - start_time) * 1000) # duration in milliseconds
28+
self.track_duration(duration)
29+
return result
30+
return wrapper
31+
2132
def track_tokens(self, tokens: Union[TokenUsage, UnderscoreTokenUsage, BedrockTokenUsage]) -> None:
2233
token_metrics = tokens.to_metrics()
2334
if token_metrics['total'] > 0:

0 commit comments

Comments
 (0)