Skip to content

Commit fba3bcd

Browse files
authored
adjust timer record method (#93)
Restore the ability to pass process.hrtime() directly to the record method, because this is the simplest way to record latency durations.
1 parent aba9018 commit fba3bcd

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nflx-spectator",
3-
"version": "3.0.1",
3+
"version": "3.0.2",
44
"license": "Apache-2.0",
55
"homepage": "https://github.com/Netflix/spectator-js",
66
"author": "Netflix Telemetry Engineering <[email protected]>",

src/meter/timer.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,28 @@ export class Timer extends Meter {
1111
super(id, writer, "t");
1212
}
1313

14-
record(seconds: number = 1): Promise<void> {
15-
if (seconds >= 0) {
16-
const line = `${this._meter_type_symbol}:${this._id.spectatord_id}:${seconds}`
14+
/**
15+
* @param {number|number[]} seconds
16+
* Number of seconds, which may be fractional, or an array of two numbers [seconds, nanoseconds],
17+
* which is the return value from process.hrtime(), and serves as a convenient means of recording
18+
* latency durations.
19+
*
20+
* start = process.hrtime();
21+
* // do work
22+
* registry.timer("eventLatency").record(process.hrtime(start));
23+
*
24+
*/
25+
record(seconds: number | number[]): Promise<void> {
26+
let elapsed: number;
27+
28+
if (seconds instanceof Array) {
29+
elapsed = seconds[0] + (seconds[1] / 1e9);
30+
} else {
31+
elapsed = seconds;
32+
}
33+
34+
if (elapsed >= 0) {
35+
const line = `${this._meter_type_symbol}:${this._id.spectatord_id}:${elapsed}`;
1736
return this._writer.write(line);
1837
} else {
1938
return new Promise((resolve: (value: void | PromiseLike<void>) => void): void => {

test/meter/timer.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,28 @@ describe("Timer Tests", (): void => {
2828
t.record(0);
2929
assert.equal("t:timer:0", writer.last_line());
3030
});
31+
32+
it("record latency from hrtime", (): void => {
33+
let nanos: number = 0;
34+
let round: number = 1;
35+
const f: NodeJS.HRTime = process.hrtime;
36+
Object.defineProperty(process, "hrtime", {
37+
get(): () => [number, number] {
38+
return (): [number, number] => {
39+
nanos += round * 1e6; // 1ms lag first time, 2ms second time, etc.
40+
++round;
41+
return [0, nanos];
42+
};
43+
}
44+
});
45+
46+
const start: [number, number] = process.hrtime();
47+
48+
const t = new Timer(tid, new MemoryWriter());
49+
const writer = t.writer() as MemoryWriter;
50+
t.record(process.hrtime(start)); // two calls to hrtime = 1ms + 2ms = 3ms
51+
assert.equal("t:timer:0.003", writer.last_line());
52+
53+
Object.defineProperty(process, "hrtime", f);
54+
});
3155
});

0 commit comments

Comments
 (0)