Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mojaloop/#3537): add metrics for cache hits and duplicate result #314

Merged
merged 2 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/data/cachedDatabase.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const util = require('util')
const Database = require('./database.js')
const Cache = require('memory-cache').Cache
const ErrorHandler = require('@mojaloop/central-services-error-handling')
const Metrics = require('@mojaloop/central-services-metrics')

const { getStackOrInspect } = require('../lib/util')

Expand Down Expand Up @@ -100,6 +101,11 @@ class CachedDatabase extends Database {
// }

async getCacheValue (type, params) {
const histTimer = Metrics.getHistogram(
'database_get_cache_value',
'database_getCacheValue - Metrics for database cache',
['success', 'queryName', 'hit']
).startTimer()
try {
let value = this.cacheGet(type, params)

Expand All @@ -108,13 +114,16 @@ class CachedDatabase extends Database {
this.writeLog(`Cache miss for ${type}: ${util.inspect(params)}`)
value = await super[type].apply(this, params)
this.cachePut(type, params, value)
histTimer({ success: true, queryName: type, hit: false })
} else {
this.writeLog(`Cache hit for ${type} ${util.inspect(params)}: ${value}`)
histTimer({ success: true, queryName: type, hit: true })
}

return value
} catch (err) {
this.writeLog(`Error in getCacheValue: ${getStackOrInspect(err)}`)
histTimer({ success: false, queryName: type, hit: false })
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}
Expand Down
42 changes: 22 additions & 20 deletions src/model/quotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class QuotesModel {
const histTimer = Metrics.getHistogram(
'model_quote',
'validateQuoteRequest - Metrics for quote model',
['success', 'queryName']
['success', 'queryName', 'duplicateResult']
).startTimer()
const envConfig = new Config()
// note that the framework should validate the form of the request
Expand Down Expand Up @@ -204,7 +204,7 @@ class QuotesModel {
const histTimer = Metrics.getHistogram(
'model_quote',
'handleQuoteRequest - Metrics for quote model',
['success', 'queryName']
['success', 'queryName', 'duplicateResult']
).startTimer()
const envConfig = new Config()
// accumulate enum ids
Expand Down Expand Up @@ -396,7 +396,7 @@ class QuotesModel {
const histTimer = Metrics.getHistogram(
'model_quote',
'forwardQuoteRequest - Metrics for quote model',
['success', 'queryName']
['success', 'queryName', 'duplicateResult']
).startTimer()
let endpoint
const fspiopSource = headers[ENUM.Http.Headers.FSPIOP.SOURCE]
Expand Down Expand Up @@ -460,7 +460,7 @@ class QuotesModel {
const histTimer = Metrics.getHistogram(
'model_quote',
'handleQuoteRequestResend - Metrics for quote model',
['success', 'queryName']
['success', 'queryName', 'duplicateResult']
).startTimer()
try {
const fspiopSource = headers[ENUM.Http.Headers.FSPIOP.SOURCE]
Expand Down Expand Up @@ -506,7 +506,7 @@ class QuotesModel {
const histTimer = Metrics.getHistogram(
'model_quote',
'handleQuoteUpdate - Metrics for quote model',
['success', 'queryName']
['success', 'queryName', 'duplicateResult']
).startTimer()
let txn = null
const fspiopSource = headers[ENUM.Http.Headers.FSPIOP.SOURCE]
Expand Down Expand Up @@ -659,7 +659,7 @@ class QuotesModel {
const histTimer = Metrics.getHistogram(
'model_quote',
'forwardQuoteUpdate - Metrics for quote model',
['success', 'queryName']
['success', 'queryName', 'duplicateResult']
).startTimer()
let endpoint = null
const fspiopSource = headers[ENUM.Http.Headers.FSPIOP.SOURCE]
Expand Down Expand Up @@ -723,7 +723,7 @@ class QuotesModel {
const histTimer = Metrics.getHistogram(
'model_quote',
'handleQuoteUpdateResend - Metrics for quote model',
['success', 'queryName']
['success', 'queryName', 'duplicateResult']
).startTimer()
try {
const fspiopSource = headers[ENUM.Http.Headers.FSPIOP.SOURCE]
Expand Down Expand Up @@ -769,7 +769,7 @@ class QuotesModel {
const histTimer = Metrics.getHistogram(
'model_quote',
'handleQuoteError - Metrics for quote model',
['success', 'queryName']
['success', 'queryName', 'duplicateResult']
).startTimer()
let txn = null
const envConfig = new Config()
Expand Down Expand Up @@ -824,7 +824,7 @@ class QuotesModel {
const histTimer = Metrics.getHistogram(
'model_quote',
'handleQuoteGet - Metrics for quote model',
['success', 'queryName']
['success', 'queryName', 'duplicateResult']
).startTimer()
const fspiopSource = headers[ENUM.Http.Headers.FSPIOP.SOURCE]
let childSpan
Expand Down Expand Up @@ -863,7 +863,7 @@ class QuotesModel {
const histTimer = Metrics.getHistogram(
'model_quote',
'forwardQuoteGet - Metrics for quote model',
['success', 'queryName']
['success', 'queryName', 'duplicateResult']
).startTimer()
let endpoint

Expand Down Expand Up @@ -920,7 +920,7 @@ class QuotesModel {
const histTimer = Metrics.getHistogram(
'model_quote',
'handleException - Metrics for quote model',
['success', 'queryName']
['success', 'queryName', 'duplicateResult']
).startTimer()
// is this exception already wrapped as an API spec compatible type?
const fspiopError = ErrorHandler.ReformatFSPIOPError(error)
Expand Down Expand Up @@ -953,7 +953,7 @@ class QuotesModel {
const histTimer = Metrics.getHistogram(
'model_quote',
'sendErrorCallback - Metrics for quote model',
['success', 'queryName']
['success', 'queryName', 'duplicateResult']
).startTimer()
const envConfig = new Config()
const fspiopDest = headers[ENUM.Http.Headers.FSPIOP.DESTINATION]
Expand Down Expand Up @@ -1082,7 +1082,7 @@ class QuotesModel {
const histTimer = Metrics.getHistogram(
'model_quote',
'checkDuplicateQuoteRequest - Metrics for quote model',
['success', 'queryName']
['success', 'queryName', 'duplicateResult']
).startTimer()
try {
// calculate a SHA-256 of the request
Expand All @@ -1094,7 +1094,7 @@ class QuotesModel {

if (!dupchk) {
// no existing record for this quoteId found
histTimer({ success: true, queryName: 'quote_checkDuplicateQuoteRequest' })
histTimer({ success: true, queryName: 'quote_checkDuplicateQuoteRequest', duplicateResult: 'none' })
return {
isResend: false,
isDuplicateId: false
Expand All @@ -1103,14 +1103,14 @@ class QuotesModel {

if (dupchk.hash === hash) {
// hash matches, this is a resend
histTimer({ success: true, queryName: 'quote_checkDuplicateQuoteRequest' })
histTimer({ success: true, queryName: 'quote_checkDuplicateQuoteRequest', duplicateResult: 'resend' })
return {
isResend: true,
isDuplicateId: true
}
}

histTimer({ success: true, queryName: 'quote_checkDuplicateQuoteRequest' })
histTimer({ success: true, queryName: 'quote_checkDuplicateQuoteRequest', duplicateResult: 'duplicate' })
// if we get here then this is a duplicate id but not a resend e.g. hashes dont match.
return {
isResend: false,
Expand All @@ -1119,7 +1119,7 @@ class QuotesModel {
} catch (err) {
// internal-error
this.writeLog(`Error in checkDuplicateQuoteRequest: ${getStackOrInspect(err)}`)
histTimer({ success: false, queryName: 'quote_checkDuplicateQuoteRequest' })
histTimer({ success: false, queryName: 'quote_checkDuplicateQuoteRequest', duplicateResult: 'error' })
throw ErrorHandler.ReformatFSPIOPError(err)
}
}
Expand All @@ -1135,7 +1135,7 @@ class QuotesModel {
const histTimer = Metrics.getHistogram(
'model_quote',
'checkDuplicateQuoteResponse - Metrics for quote model',
['success', 'queryName']
['success', 'queryName', 'duplicateResult']
).startTimer()
try {
// calculate a SHA-256 of the request
Expand All @@ -1147,6 +1147,7 @@ class QuotesModel {

if (!dupchk) {
// no existing record for this quoteId found
histTimer({ success: true, queryName: 'quote_checkDuplicateQuoteResponse', duplicateResult: 'none' })
return {
isResend: false,
isDuplicateId: false
Expand All @@ -1155,22 +1156,23 @@ class QuotesModel {

if (dupchk.hash === hash) {
// hash matches, this is a resend
histTimer({ success: true, queryName: 'quote_checkDuplicateQuoteResponse', duplicateResult: 'resend' })
return {
isResend: true,
isDuplicateId: true
}
}

// if we get here then this is a duplicate id but not a resend e.g. hashes dont match.
histTimer({ success: true, queryName: 'quote_checkDuplicateQuoteResponse' })
histTimer({ success: true, queryName: 'quote_checkDuplicateQuoteResponse', duplicateResult: 'duplicate' })
return {
isResend: false,
isDuplicateId: true
}
} catch (err) {
// internal-error
this.writeLog(`Error in checkDuplicateQuoteResponse: ${getStackOrInspect(err)}`)
histTimer({ success: false, queryName: 'quote_checkDuplicateQuoteResponse' })
histTimer({ success: false, queryName: 'quote_checkDuplicateQuoteResponse', duplicateResult: 'error' })
throw ErrorHandler.ReformatFSPIOPError(err)
}
}
Expand Down
Loading