generated from 8iq/nodejs-hackathon-boilerplate-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(condo) HOTFIX: add custom fetch.js (#4479)
* 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
1 parent
abe5502
commit 64bbba3
Showing
2 changed files
with
58 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters