Skip to content
This repository was archived by the owner on Feb 4, 2022. It is now read-only.

Commit a7f653b

Browse files
committed
refactor(calculateDurationMs): move this method to common utils
1 parent 916f396 commit a7f653b

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

lib/connection/apm.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
const KillCursor = require('../connection/commands').KillCursor;
33
const GetMore = require('../connection/commands').GetMore;
4-
const process = require('process');
4+
const calculateDurationInMs = require('../utils').calculateDurationInMs;
55

66
/** Commands that we want to redact because of the sensitive nature of their contents */
77
const SENSITIVE_COMMANDS = new Set([
@@ -18,11 +18,6 @@ const SENSITIVE_COMMANDS = new Set([
1818

1919
// helper methods
2020
const extractCommandName = command => Object.keys(command)[0];
21-
const calculateDurationInMs = started => {
22-
const hrtime = process.hrtime(started);
23-
return (hrtime[0] * 1e9 + hrtime[1]) / 1e6;
24-
};
25-
2621
const namespace = command => command.ns;
2722
const databaseName = command => command.ns.split('.')[0];
2823
const collectionName = command => command.ns.split('.')[1];

lib/utils.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,28 @@
22

33
const crypto = require('crypto');
44

5+
/**
6+
* Generate a UUIDv4
7+
*/
58
const uuidV4 = () => {
69
const result = crypto.randomBytes(16);
710
result[6] = (result[6] & 0x0f) | 0x40;
811
result[8] = (result[8] & 0x3f) | 0x80;
912
return result;
1013
};
1114

15+
/**
16+
* Returns the duration calculated from two high resolution timers in milliseconds
17+
*
18+
* @param {Object} started A high resolution timestamp created from `process.hrtime()`
19+
* @returns {Number} The duration in milliseconds
20+
*/
21+
const calculateDurationInMs = started => {
22+
const hrtime = process.hrtime(started);
23+
return (hrtime[0] * 1e9 + hrtime[1]) / 1e6;
24+
};
25+
1226
module.exports = {
13-
uuidV4: uuidV4
27+
uuidV4,
28+
calculateDurationInMs
1429
};

0 commit comments

Comments
 (0)