Skip to content

Commit 3150c32

Browse files
committed
feat(http): use high resolution timer for metrics
1 parent 03437f1 commit 3150c32

File tree

5 files changed

+64
-8
lines changed

5 files changed

+64
-8
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
"build/src/**"
4141
],
4242
"dependencies": {
43-
"@linzjs/metrics": "^6.21.1",
4443
"find-my-way": "^7.0.0",
4544
"pino": "^8.3.0",
4645
"ulid": "^2.3.0"

src/http/router.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ export class Router {
112112
}
113113

114114
return Promise.race(requestHandles).then((res) => {
115-
console.log('RaceDone', requestHandles);
116115
req.abort.abort();
117116
return finalize(req, res);
118117
});

src/metric.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { performance } from 'perf_hooks';
2+
3+
/** Number of decimal places to keep in timers */
4+
const TruncateTimers = 4;
5+
/**
6+
* Utility to record some metrics about the execution of the function
7+
*
8+
* TODO this should be replaced by open telemetry
9+
*/
10+
export class Metrics {
11+
/**
12+
* Start time of all timers
13+
*/
14+
timers: Map<string, { start: number; duration?: number }> = new Map();
15+
16+
getTime(): number {
17+
return performance.now();
18+
}
19+
20+
/**
21+
* Start a timer at the current time
22+
* @param timeName name of timer to start
23+
*/
24+
public start(timeName: string): void {
25+
const existing = this.timers.get(timeName);
26+
if (existing != null && existing.duration == null) {
27+
throw new Error(`Duplicate startTime for "${timeName}"`);
28+
}
29+
this.timers.set(timeName, { start: this.getTime() });
30+
}
31+
32+
/**
33+
* End the timer, returning the duration in milliseconds
34+
* @param timeName timer to end
35+
*/
36+
public end(timeName: string): number {
37+
const timer = this.timers.get(timeName);
38+
if (timer == null) throw new Error(`Missing startTime information for "${timeName}"`);
39+
const duration = this.getTime() - timer.start;
40+
timer.duration = Number(duration.toFixed(TruncateTimers));
41+
return duration;
42+
}
43+
44+
/** Get list of all timers that have run */
45+
public get metrics(): Record<string, number> | undefined {
46+
if (this.timers.size === 0) return undefined;
47+
const output: Record<string, number> = {};
48+
for (const [key, timer] of this.timers.entries()) {
49+
if (timer.duration != null) output[key] = timer.duration;
50+
}
51+
return output;
52+
}
53+
54+
/** Get a list of timers that never finished */
55+
public get unfinished(): string[] | undefined {
56+
const st: string[] = [];
57+
for (const [key, timer] of this.timers.entries()) {
58+
if (timer.duration == null) st.push(key);
59+
}
60+
if (st.length === 0) return undefined;
61+
return st;
62+
}
63+
}

src/request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Metrics } from '@linzjs/metrics';
1+
import { Metrics } from './metric.js';
22
import { Context } from 'aws-lambda';
33
import { ulid } from 'ulid';
44
import { LogType } from './log.js';

yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@
5252
resolved "https://registry.yarnpkg.com/@humanwhocodes/object-schema/-/object-schema-1.2.1.tgz#b520529ec21d8e5945a1851dfd1c32e94e39ff45"
5353
integrity sha512-ZnQMnLV4e7hDlUvw8H+U8ASL02SS2Gn6+9Ac3wGGLIe7+je2AeAOxPY+izIPJDfFDb7eDjev0Us8MO1iFRN8hA==
5454

55-
"@linzjs/metrics@^6.21.1":
56-
version "6.21.1"
57-
resolved "https://registry.yarnpkg.com/@linzjs/metrics/-/metrics-6.21.1.tgz#58d008c6b9c26400ea9934e2b07bc7f3dbcdd6d7"
58-
integrity sha512-3QupitQN91FwxBN+EEmlBRIcLEwoOG/RK+T2yZtIHuFxXmaPJabnVFnJG62/uzlMZw8gRwmyixUg5qTVuSnziQ==
59-
6055
"@linzjs/style@^3.9.0":
6156
version "3.9.0"
6257
resolved "https://registry.yarnpkg.com/@linzjs/style/-/style-3.9.0.tgz#a4df77d78dc5434e57ba8106ca188b1b8bf03143"

0 commit comments

Comments
 (0)