@@ -30,44 +30,40 @@ flexible consumption patterns.
3030### Example
3131
3232``` mjs
33- import { counter , timer } from ' node:metrics' ;
33+ import { counter } from ' node:metrics' ;
3434
35- // Create a counter metric
35+ // Create counter metrics
3636const apiCalls = counter (' api.calls' , { service: ' web' });
37-
38- // Create a timer factory
39- const requestTimer = timer (' api.request.duration' , { service: ' web' });
37+ const requestDuration = counter (' api.request.duration.ms' , { service: ' web' });
4038
4139// Use metrics in your application
4240function handleRequest (req , res ) {
43- const timer = requestTimer . create ({ endpoint: req .url });
41+ const timer = requestDuration . createTimer ({ endpoint: req .url });
4442
4543 apiCalls .increment ();
4644
4745 // Process request...
4846
49- timer .stop ();
47+ timer .stop (); // Increments requestDuration with the elapsed time
5048}
5149```
5250
5351``` cjs
54- const { counter , timer } = require (' node:metrics' );
52+ const { counter } = require (' node:metrics' );
5553
56- // Create a counter metric
54+ // Create counter metrics
5755const apiCalls = counter (' api.calls' , { service: ' web' });
58-
59- // Create a timer factory
60- const requestTimer = timer (' api.request.duration' , { service: ' web' });
56+ const requestDuration = counter (' api.request.duration.ms' , { service: ' web' });
6157
6258// Use metrics in your application
6359function handleRequest (req , res ) {
64- const timer = requestTimer . create ({ endpoint: req .url });
60+ const timer = requestDuration . createTimer ({ endpoint: req .url });
6561
6662 apiCalls .increment ();
6763
6864 // Process request...
6965
70- timer .stop ();
66+ timer .stop (); // Increments requestDuration with the elapsed time
7167}
7268```
7369
@@ -117,27 +113,6 @@ const memory = gauge('memory.usage.bytes');
117113memory .reset (memoryUsage ().heapUsed );
118114```
119115
120- ### ` metrics.timer(name[, meta]) `
121-
122- <!-- YAML
123- added: REPLACEME
124- -->
125-
126- * ` name ` {string} The name of the timer metric.
127- * ` meta ` {Object} Optional metadata to attach to all reports.
128- * Returns: {metrics.TimerFactory}
129-
130- Creates a timer factory for measuring durations.
131-
132- ``` mjs
133- import { timer } from ' node:metrics' ;
134-
135- const dbQueryTimer = timer (' db.query.duration' );
136-
137- const t = dbQueryTimer .create ({ query: ' SELECT * FROM users' });
138- // Perform database query...
139- const duration = t .stop (); // Returns duration in milliseconds
140- ```
141116
142117### ` metrics.pullGauge(name, fn[, meta]) `
143118
@@ -389,6 +364,27 @@ errorCount.decrement(2, { errorType: 'timeout' }); // Decrement by 2 with metada
389364errorCount .decrement ({ errorType: ' timeout' }); // Decrement by 1 with metadata
390365```
391366
367+ #### ` counter.createTimer([meta]) `
368+
369+ <!-- YAML
370+ added: REPLACEME
371+ -->
372+
373+ * ` meta ` {Object} Additional metadata to include with the report.
374+ * Returns: {Timer}
375+
376+ Creates a timer that will increment this counter with its duration when stopped.
377+
378+ ``` mjs
379+ import { counter } from ' node:metrics' ;
380+
381+ const requestDuration = counter (' request.duration.ms' );
382+
383+ const timer = requestDuration .createTimer ({ endpoint: ' /api/users' });
384+ // Process request...
385+ const duration = timer .stop (); // Counter is incremented with duration
386+ ```
387+
392388### Class: ` Gauge `
393389
394390* Extends: {metrics.Metric}
@@ -431,15 +427,34 @@ memory.reset(memoryUsage().heapUsed); // Set to current memory usage
431427memory .reset (1024 , { source: ' system' }); // Set to 1024 with metadata
432428```
433429
434- ### Class: ` Timer `
430+ #### ` gauge.createTimer([meta]) `
435431
436- * Extends: {metrics.Metric}
432+ <!-- YAML
433+ added: REPLACEME
434+ -->
435+
436+ * ` meta ` {Object} Additional metadata to include with the report.
437+ * Returns: {Timer}
438+
439+ Creates a timer that will set this gauge to its duration when stopped.
440+
441+ ``` mjs
442+ import { gauge } from ' node:metrics' ;
443+
444+ const responseTime = gauge (' response.time.ms' );
445+
446+ const timer = responseTime .createTimer ({ endpoint: ' /api/users' });
447+ // Process request...
448+ const duration = timer .stop (); // Gauge is set to duration
449+ ```
450+
451+ ### Class: ` Timer `
437452
438453<!-- YAML
439454added: REPLACEME
440455-->
441456
442- A metric for measuring durations.
457+ A helper for measuring durations that reports the elapsed time via a callback when stopped .
443458
444459#### ` timer.start `
445460
@@ -449,45 +464,44 @@ added: REPLACEME
449464
450465* {number}
451466
452- The start time of the timer (milliseconds since epoch) .
467+ The start time of the timer (milliseconds since ` performance.timeOrigin ` ). This property is read-only .
453468
454469#### ` timer.end `
455470
456471<!-- YAML
457472added: REPLACEME
458473-->
459474
460- * {number}
475+ * {number|undefined }
461476
462- The end time of the timer (milliseconds since epoch ). Zero if timer is running.
477+ The end time of the timer (milliseconds since ` performance.timeOrigin ` ). ` undefined ` if timer is still running. This property is read-only .
463478
464479#### ` timer.duration `
465480
466481<!-- YAML
467482added: REPLACEME
468483-->
469484
470- * {number}
485+ * {number|undefined }
471486
472- The duration in milliseconds. Zero if timer is still running.
487+ The duration in milliseconds. ` undefined ` if timer is still running. This property is read-only .
473488
474- #### ` timer.stop([meta] ) `
489+ #### ` timer.stop() `
475490
476491<!-- YAML
477492added: REPLACEME
478493-->
479494
480- * ` meta ` {Object} Additional metadata for this report.
481495* Returns: {number} The duration in milliseconds.
482496
483497Stops the timer and reports the duration. Can only be called once.
484498
485499``` mjs
486- import { timer } from ' node:metrics' ;
500+ import { counter } from ' node:metrics' ;
487501
488- const dbQueryTimer = timer (' db.query.duration' );
502+ const dbQueryDuration = counter (' db.query.duration' );
489503
490- const t = dbQueryTimer . create ({ query: ' SELECT * FROM users' });
504+ const t = dbQueryDuration . createTimer ({ query: ' SELECT * FROM users' });
491505
492506// Perform database query...
493507
@@ -504,46 +518,18 @@ added: REPLACEME
504518Allows ` using ` syntax to automatically stop the timer when done.
505519
506520``` mjs
507- import { timer } from ' node:metrics' ;
521+ import { counter } from ' node:metrics' ;
508522
509- const dbQueryTimer = timer (' db.query.duration' );
523+ const dbQueryDuration = counter (' db.query.duration' );
510524
511525{
512- using t = dbQueryTimer . create ({ query: ' SELECT * FROM users' });
526+ using t = dbQueryDuration . createTimer ({ query: ' SELECT * FROM users' });
513527 // Perform database query...
514528
515529 // Timer is automatically stopped here
516530}
517531```
518532
519- ### Class: ` TimerFactory `
520-
521- * Extends: {metrics.Metric}
522-
523- <!-- YAML
524- added: REPLACEME
525- -->
526-
527- A factory for creating timer instances.
528-
529- #### ` timerFactory.create([meta]) `
530-
531- <!-- YAML
532- added: REPLACEME
533- -->
534-
535- * ` meta ` {Object} Additional metadata for this timer.
536- * Returns: {metrics.Timer}
537-
538- Creates a new timer instance with the specified metadata.
539-
540- ``` mjs
541- import { timer } from ' node:metrics' ;
542-
543- const dbQueryTimer = timer (' db.query.duration' );
544-
545- const t = dbQueryTimer .create ({ query: ' SELECT * FROM users' });
546- ```
547533
548534### Class: ` PullGauge `
549535
0 commit comments