Passed
Push — master ( 795b66...a17000 )
by Zhenyu
01:23
created

src/decorators/event-metrics.js   A

Complexity

Total Complexity 13
Complexity/F 1.3

Size

Lines of Code 50
Function Count 10

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 39
mnd 3
bc 3
fnc 10
dl 0
loc 50
rs 10
bpm 0.3
cpm 1.3
noi 0
c 0
b 0
f 0
1
import addHooks from './helpers/add-hooks';
2
3
/*
4
  a decorator to timing action execution time in both success/error cases
5
  and send metrics using the client attached in context
6
 */
7
const metricsTimer = ({ parseLabel = () => {} } = {}) =>
8
  addHooks({
9
    bypassHook: (p, m, { metrics }) => !metrics,
10
    storageHook: (p, m, c, action) => {
11
      const { metrics } = c;
12
      const timer = metrics.find({ action, type: 'timer' });
13
      const stopTimer = timer.start({ ...m, ...parseLabel(p, m, c) });
14
      return { stopTimer };
15
    },
16
    afterHook: (r, p, m, c, a, { stopTimer }) => {
17
      stopTimer();
18
    },
19
    errorHook: (e, p, m, c, a, { stopTimer }) => {
20
      stopTimer();
21
    },
22
  });
23
24
export default metricsTimer;
25