-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
139 additions
and
29 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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,55 @@ | ||
import { getMetricClient } from '../get-metric-client'; | ||
import { wrap } from '../shimmer'; | ||
|
||
export const instrumentFetch = () => { | ||
const client = getMetricClient(); | ||
|
||
const counter = new client.Counter({ | ||
name: 'fetch_requests_total', | ||
help: 'Monitor the number of fetch requests made by the application to external services', | ||
labelNames: ['method', 'status', 'origin'] | ||
}); | ||
|
||
const histogram = new client.Histogram({ | ||
name: 'fetch_duration_milliseconds', | ||
help: 'Monitor the duration of fetch requests made by the application to external services', | ||
labelNames: ['method', 'status', 'origin'] | ||
}); | ||
|
||
wrap(globalThis, 'fetch', (originalFetch): typeof globalThis.fetch => { | ||
return async function ( | ||
this: typeof originalFetch, | ||
...args: Parameters<typeof originalFetch> | ||
) { | ||
const start = process.hrtime.bigint(); | ||
const result = originalFetch.apply(this, args); | ||
|
||
if (result instanceof Promise) { | ||
await result; | ||
} | ||
const end = process.hrtime.bigint(); | ||
const duration = Number(end - start) / 1e6; | ||
|
||
const origin = new URL(args[0].toString()).origin; | ||
|
||
counter.inc({ | ||
method: args[1]?.method ?? 'GET', | ||
// @ts-ignore | ||
status: result.status as string, | ||
origin: origin.toString() | ||
}); | ||
|
||
histogram.observe( | ||
{ | ||
method: args[1]?.method ?? 'GET', | ||
// @ts-ignore | ||
status: result.status as string, | ||
origin: origin.toString() | ||
}, | ||
duration | ||
); | ||
|
||
return result; | ||
} as typeof 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import promClient from 'prom-client'; | ||
|
||
export function getMetricClient() { | ||
return promClient; | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { default as OpenAPM, getMetricClient } from './OpenAPM'; | ||
export { getMetricClient } from './get-metric-client'; | ||
export { default as OpenAPM } from './OpenAPM'; | ||
export { setOpenAPMLabels } from './async-local-storage.http'; | ||
export type { OpenAPMOptions } from './OpenAPM'; |
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
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