Skip to content

Commit

Permalink
feat(condo) HOTFIX: add custom fetch.js (#4479)
Browse files Browse the repository at this point in the history
* fix(condo) HOTFIX: add custom fetch.js

* fix(condo) fix metrics.js

* fix(condo) fix linter issues

* fix(condo) remove stragne fetch calls

* fix(condo) remove stragne fetch calls

* fix(condo) add elapsedTime if error is caught
  • Loading branch information
toplenboren authored Mar 22, 2024
1 parent abe5502 commit 64bbba3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
45 changes: 45 additions & 0 deletions packages/keystone/fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const nodeFetch = require('node-fetch')

const { getExecutionContext } = require('./executionContext')
const { getLogger } = require('./logging')
const Mertrics = require('./metrics')

const logger = getLogger('fetch')

async function fetch (url, options) {

const urlObject = new URL(url)
const host = urlObject.hostname
const path = urlObject.pathname
const metricUrl = host.replace(/[^a-zA-Z0-9]/g, '')

const executionContext = getExecutionContext()
const parentReqId = executionContext.reqId

const startTime = Date.now()

try {
const response = await nodeFetch(url, options)

const endTime = Date.now()
const elapsedTime = endTime - startTime

logger.info({ msg: 'fetch: request successful', url, reqId: parentReqId, path, host, status: response.status, elapsedTime })
Mertrics.increment({ name: 'fetch.status.' + metricUrl, value: response.status, tags: { path } })
Mertrics.gauge({ name: 'fetch.time.' + metricUrl, value: elapsedTime, tags: { path } })

return response
} catch (error) {
const endTime = Date.now()
const elapsedTime = endTime - startTime

logger.error({ msg: 'fetch: failed with error', url, path, host, reqId: parentReqId, error, elapsedTime })

Mertrics.increment({ name: 'fetch.error.' + metricUrl, value: 1, tags: { path } })
throw error
}
}

module.exports = {
fetch,
}
20 changes: 13 additions & 7 deletions packages/keystone/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,29 @@ const StatsDClient = new StatsD({
globalTags: { hostname: HOSTNAME, command: process.argv[1] },
})

const gauge = ({ name, value }) => {
const gauge = ({ name, value, tags }) => {
validateName(name)
StatsDClient.gauge(name, value)
StatsDClient.gauge(name, value, tags)
}

const histogram = ({ name, value }) => {
const histogram = ({ name, value, tags }) => {
validateName(name)
StatsDClient.histogram(name, value)
StatsDClient.histogram(name, value, tags)
}

const count = ({ name, value }) => {
const increment = ({ name, value, tags }) => {
validateName(name)
StatsDClient.count(name, value)
StatsDClient.increment(name, value, tags)
}

const decrement = ({ name, value, tags }) => {
validateName(name)
StatsDClient.decrement(name, value, tags)
}

module.exports = {
gauge,
count,
increment,
decrement,
histogram,
}

0 comments on commit 64bbba3

Please sign in to comment.