|
122 | 122 | end
|
123 | 123 |
|
124 | 124 | describe '#track_bedrock_metrics' do
|
125 |
| - it 'tracks duration and tokens' do |
126 |
| - # TODO: Verify the $metadata field in bedrock result. I don't see anything like this in the docs. |
127 |
| - bedrock_result = { |
128 |
| - '$metadata': { httpStatusCode: 200 }, |
| 125 | + let(:bedrock_result) do |
| 126 | + { |
129 | 127 | usage: {
|
130 | 128 | total_tokens: 300,
|
131 | 129 | input_tokens: 200,
|
132 | 130 | output_tokens: 100,
|
133 | 131 | },
|
134 |
| - metrics: { |
135 |
| - latency_ms: 50, |
136 |
| - }, |
137 | 132 | }
|
| 133 | + end |
| 134 | + |
| 135 | + it 'tracks duration and tokens' do |
138 | 136 | expect(ld_client).to receive(:track).with(
|
139 | 137 | '$ld:ai:generation',
|
140 | 138 | context,
|
|
172 | 170 | 100
|
173 | 171 | )
|
174 | 172 |
|
175 |
| - result = tracker.track_bedrock_converse_metrics(bedrock_result) |
| 173 | + result = tracker.track_bedrock_converse_metrics { bedrock_result } |
176 | 174 | expect(result).to eq(bedrock_result)
|
177 | 175 | expect(tracker.summary).to be_a(LaunchDarkly::Server::AI::MetricSummary)
|
178 | 176 | expect(tracker.summary.usage).to be_a(LaunchDarkly::Server::AI::TokenUsage)
|
179 | 177 | expect(tracker.summary.usage.total).to eq(300)
|
180 | 178 | expect(tracker.summary.usage.input).to eq(200)
|
181 | 179 | expect(tracker.summary.usage.output).to eq(100)
|
182 |
| - expect(tracker.summary.duration).to eq(50) |
| 180 | + expect(tracker.summary.duration).to be_a(Integer) |
| 181 | + expect(tracker.summary.duration).to be >= 0 |
183 | 182 | expect(tracker.summary.success).to be true
|
184 | 183 | end
|
185 | 184 |
|
186 |
| - it 'tracks duration and tokens with error' do |
187 |
| - # Verify the $metadata field in bedrock result. I don't see anything like this in the docs. |
188 |
| - bedrock_result = { |
189 |
| - '$metadata': { httpStatusCode: 400 }, |
190 |
| - usage: { |
191 |
| - total_tokens: 300, |
192 |
| - input_tokens: 200, |
193 |
| - output_tokens: 100, |
194 |
| - }, |
195 |
| - metrics: { |
196 |
| - latency_ms: 50, |
197 |
| - }, |
198 |
| - } |
| 185 | + it 'tracks error for failed operation' do |
199 | 186 | expect(ld_client).to receive(:track).with(
|
200 | 187 | '$ld:ai:generation',
|
201 | 188 | context,
|
|
214 | 201 | tracker_flag_data,
|
215 | 202 | kind_of(Integer)
|
216 | 203 | )
|
217 |
| - expect(ld_client).to receive(:track).with( |
218 |
| - '$ld:ai:tokens:total', |
219 |
| - context, |
220 |
| - tracker_flag_data, |
221 |
| - 300 |
222 |
| - ) |
223 |
| - expect(ld_client).to receive(:track).with( |
224 |
| - '$ld:ai:tokens:input', |
225 |
| - context, |
226 |
| - tracker_flag_data, |
227 |
| - 200 |
228 |
| - ) |
229 |
| - expect(ld_client).to receive(:track).with( |
230 |
| - '$ld:ai:tokens:output', |
231 |
| - context, |
232 |
| - tracker_flag_data, |
233 |
| - 100 |
234 |
| - ) |
235 | 204 |
|
236 |
| - result = tracker.track_bedrock_converse_metrics(bedrock_result) |
237 |
| - expect(result).to eq(bedrock_result) |
238 |
| - expect(tracker.summary).to be_a(LaunchDarkly::Server::AI::MetricSummary) |
239 |
| - expect(tracker.summary.usage).to be_a(LaunchDarkly::Server::AI::TokenUsage) |
240 |
| - expect(tracker.summary.usage.total).to eq(300) |
241 |
| - expect(tracker.summary.usage.input).to eq(200) |
242 |
| - expect(tracker.summary.usage.output).to eq(100) |
243 |
| - expect(tracker.summary.duration).to eq(50) |
| 205 | + expect { tracker.track_bedrock_converse_metrics { raise 'test error' } }.to raise_error('test error') |
| 206 | + expect(tracker.summary.usage).to be_nil |
| 207 | + expect(tracker.summary.duration).to be_a(Integer) |
| 208 | + expect(tracker.summary.duration).to be >= 0 |
244 | 209 | expect(tracker.summary.success).to be false
|
245 | 210 | end
|
246 | 211 | end
|
|
299 | 264 | expect(tracker.summary.usage.total).to eq(300)
|
300 | 265 | expect(tracker.summary.usage.input).to eq(200)
|
301 | 266 | expect(tracker.summary.usage.output).to eq(100)
|
| 267 | + expect(tracker.summary.duration).to be_a(Integer) |
| 268 | + expect(tracker.summary.duration).to be >= 0 |
302 | 269 | expect(tracker.summary.success).to be true
|
303 | 270 | end
|
304 | 271 |
|
|
324 | 291 |
|
325 | 292 | expect { tracker.track_openai_metrics { raise 'test error' } }.to raise_error('test error')
|
326 | 293 | expect(tracker.summary.usage).to be_nil
|
| 294 | + expect(tracker.summary.duration).to be_a(Integer) |
| 295 | + expect(tracker.summary.duration).to be >= 0 |
| 296 | + expect(tracker.summary.success).to be false |
327 | 297 | end
|
328 | 298 | end
|
329 | 299 |
|
|
0 commit comments