observe failed digest-score api call lantency in prometheus too

This commit is contained in:
Hongbo Wu
2024-07-01 12:05:48 +08:00
parent 550cef241c
commit ad88cbaea8
2 changed files with 6 additions and 6 deletions

View File

@ -248,7 +248,7 @@ const rankCandidates = async (
const scores = await scoreClient.getScores(data)
// update scores for candidates
candidates.forEach((item) => {
item.score = scores[item.id]['score'] || 0
item.score = scores[item.id].score || 0
})
// rank candidates by score in descending order

View File

@ -75,9 +75,9 @@ class ScoreClientImpl implements ScoreClient {
}
async getScores(data: ScoreApiRequestBody): Promise<ScoreApiResponse> {
try {
const start = Date.now()
const start = Date.now()
try {
const response = await axios.post<ScoreApiResponse>(this.apiUrl, data, {
headers: {
'Content-Type': 'application/json',
@ -85,9 +85,6 @@ class ScoreClientImpl implements ScoreClient {
timeout: 5000,
})
const duration = (Date.now() - start) / 1000 // in seconds
latency.observe(duration)
return response.data
} catch (error) {
logError(error)
@ -96,6 +93,9 @@ class ScoreClientImpl implements ScoreClient {
return {
[Object.keys(data.items)[0]]: { score: 0 },
}
} finally {
const duration = (Date.now() - start) / 1000 // in seconds
latency.observe(duration)
}
}
}