@@ -26,10 +26,15 @@ public class LdAiConfigTracker : IDisposable
2626
2727 private readonly string _key ;
2828
29+ private readonly LdValue _trackData ;
30+
2931 private const string Duration = "$ld:ai:duration:total" ;
3032 private const string FeedbackPositive = "$ld:ai:feedback:user:positive" ;
3133 private const string FeedbackNegative = "$ld:ai:feedback:user:negative" ;
3234 private const string Generation = "$ld:ai:generation" ;
35+ private const string TokenTotal = "$ld:ai:tokens:total" ;
36+ private const string TokenInput = "$ld:ai:tokens:input" ;
37+ private const string TokenOutput = "$ld:ai:tokens:output" ;
3338
3439 /// <summary>
3540 ///
@@ -45,11 +50,7 @@ public LdAiConfigTracker(ILaunchDarklyClient client, LdAiConfig config, Context
4550 _key = key ?? throw new ArgumentNullException ( nameof ( key ) ) ;
4651 _context = context ;
4752 Config = config ?? throw new ArgumentNullException ( nameof ( config ) ) ;
48- }
49-
50- private LdValue GetTrackData ( )
51- {
52- return LdValue . ObjectFrom ( new Dictionary < string , LdValue >
53+ _trackData = LdValue . ObjectFrom ( new Dictionary < string , LdValue >
5354 {
5455 { "versionKey" , LdValue . Of ( Config . VersionKey ) } ,
5556 { "configKey" , LdValue . Of ( _key ) }
@@ -61,7 +62,7 @@ private LdValue GetTrackData()
6162 /// </summary>
6263 /// <param name="duration"></param>
6364 public void TrackDuration ( float duration ) =>
64- _client . Track ( Duration , _context , GetTrackData ( ) , duration ) ;
65+ _client . Track ( Duration , _context , _trackData , duration ) ;
6566
6667
6768 /// <summary>
@@ -71,12 +72,18 @@ public void TrackDuration(float duration) =>
7172 /// <typeparam name="T"></typeparam>
7273 /// <returns></returns>
7374 public async Task < T > TrackDurationOfTask < T > ( Task < T > task )
75+ {
76+ var result = await MeasureDurationOfTaskMs ( task ) ;
77+ TrackDuration ( result . Item2 ) ;
78+ return result . Item1 ;
79+ }
80+
81+ private static async Task < Tuple < T , long > > MeasureDurationOfTaskMs < T > ( Task < T > task )
7482 {
7583 var sw = Stopwatch . StartNew ( ) ;
7684 var result = await task ;
7785 sw . Stop ( ) ;
78- TrackDuration ( sw . ElapsedMilliseconds ) ;
79- return result ;
86+ return Tuple . Create ( result , sw . ElapsedMilliseconds ) ;
8087 }
8188
8289 /// <summary>
@@ -89,10 +96,10 @@ public void TrackFeedback(Feedback feedback)
8996 switch ( feedback )
9097 {
9198 case Feedback . Positive :
92- _client . Track ( FeedbackPositive , _context , GetTrackData ( ) , 1 ) ;
99+ _client . Track ( FeedbackPositive , _context , _trackData , 1 ) ;
93100 break ;
94101 case Feedback . Negative :
95- _client . Track ( FeedbackNegative , _context , GetTrackData ( ) , 1 ) ;
102+ _client . Track ( FeedbackNegative , _context , _trackData , 1 ) ;
96103 break ;
97104 default :
98105 throw new ArgumentOutOfRangeException ( nameof ( feedback ) , feedback , null ) ;
@@ -104,7 +111,48 @@ public void TrackFeedback(Feedback feedback)
104111 /// </summary>
105112 public void TrackSuccess ( )
106113 {
107- _client . Track ( Generation , _context , GetTrackData ( ) , 1 ) ;
114+ _client . Track ( Generation , _context , _trackData , 1 ) ;
115+ }
116+
117+
118+ /// <summary>
119+ ///
120+ /// </summary>
121+ /// <param name="request"></param>
122+ /// <returns></returns>
123+ public async Task < ProviderResponse > TrackRequest ( Task < ProviderResponse > request )
124+ {
125+ var ( result , durationMs ) = await MeasureDurationOfTaskMs ( request ) ;
126+ TrackSuccess ( ) ;
127+
128+ TrackDuration ( result . Statistics ? . LatencyMs ?? durationMs ) ;
129+
130+ if ( result . Usage != null )
131+ {
132+ TrackTokens ( result . Usage . Value ) ;
133+ }
134+
135+ return result ;
136+ }
137+
138+ /// <summary>
139+ ///
140+ /// </summary>
141+ /// <param name="usage"></param>
142+ public void TrackTokens ( Usage usage )
143+ {
144+ if ( usage . Total is > 0 )
145+ {
146+ _client . Track ( TokenTotal , _context , _trackData , usage . Total . Value ) ;
147+ }
148+ if ( usage . Input is > 0 )
149+ {
150+ _client . Track ( TokenInput , _context , _trackData , usage . Input . Value ) ;
151+ }
152+ if ( usage . Output is > 0 )
153+ {
154+ _client . Track ( TokenOutput , _context , _trackData , usage . Output . Value ) ;
155+ }
108156 }
109157
110158
0 commit comments