Skip to content

Commit

Permalink
fix(mu): add error assertion for missing metrics dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
jrdn91 committed Jul 1, 2024
1 parent 67392aa commit 7278e99
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions servers/mu/src/domain/lib/with-timer-metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { swallow } from '../utils.js'
* need to be referenced as labels passed to the histogram itself. Any additional checks in this way are up to the implementation and are details of the specific implementation.
*/
export function withTimerMetrics ({ timer, startLabelsFrom = always({}), stopLabelsFrom = always({}), tracesFrom = always({}), logger = console.warn.bind(console) } = {}) {
if (!timer || !timer.startTimer || typeof timer.startTimer !== 'function') {
throw new Error('Timer must implement a startTimer function')
}
return (func) => (...funcArgs) => {
const startLabels = startLabelsFrom(...funcArgs)
const traces = tracesFrom(...funcArgs)
Expand Down
17 changes: 17 additions & 0 deletions servers/mu/src/domain/lib/with-timer-metrics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ import { withTimerMetrics } from './with-timer-metrics.js'
import assert from 'node:assert'

describe('withTimerMetrics', () => {
test('should throw an error if the timer dependency is invalid', () => {
assert.throws(() => withTimerMetrics({ timer: {} }), {
name: 'Error',
message: 'Timer must implement a startTimer function'
}, 'Timer must implement a startTimer function')

assert.throws(() => withTimerMetrics({ timer: { startTimer: 'not a function' } }), {
name: 'Error',
message: 'Timer must implement a startTimer function'
}, 'Timer startTimer property must be a function')

assert.throws(() => withTimerMetrics({}), {
name: 'Error',
message: 'Timer must implement a startTimer function'
}, 'Timer dependency is required')
})

test('should return the original function result', async () => {
const metricsFunction = withTimerMetrics({
timer: {
Expand Down

0 comments on commit 7278e99

Please sign in to comment.