@@ -28,11 +28,19 @@ const {
2828
2929const {
3030 codes : {
31- ERR_INVALID_ARG_TYPE ,
3231 ERR_INVALID_ARG_VALUE ,
3332 } ,
3433} = require ( 'internal/errors' ) ;
3534
35+ const {
36+ kValidateObjectAllowNullable,
37+ validateNumber,
38+ validateObject,
39+ validateOneOf,
40+ validateString,
41+ validateFunction,
42+ } = require ( 'internal/validators' ) ;
43+
3644const {
3745 channel,
3846 hasChannel,
@@ -61,6 +69,10 @@ class MetricReport {
6169 * @param {object } [meta] Additional metadata to include with the report.
6270 */
6371 constructor ( type , name , value , meta ) {
72+ validateString ( type , 'type' ) ;
73+ validateString ( name , 'name' ) ;
74+ validateNumber ( value , 'value' ) ;
75+ validateObject ( meta , 'meta' , kValidateObjectAllowNullable ) ;
6476 this . #type = type ;
6577 this . #name = name ;
6678 this . #value = value ;
@@ -125,14 +137,11 @@ class Metric {
125137 * @param {object } [meta] Additional metadata to include with the metric.
126138 */
127139 constructor ( type , name , meta ) {
128- if ( ! metricTypeNames . includes ( type ) ) {
129- throw new ERR_INVALID_ARG_VALUE ( 'type' , type , wrongTypeErr ) ;
130- }
131- if ( typeof name !== 'string' || ! name ) {
132- throw new ERR_INVALID_ARG_TYPE ( 'name' , [ 'string' ] , name ) ;
133- }
134- if ( meta !== undefined && typeof meta !== 'object' ) {
135- throw new ERR_INVALID_ARG_TYPE ( 'meta' , [ 'object' , 'undefined' ] , meta ) ;
140+ validateOneOf ( type , 'type' , [ 'gauge' , 'counter' , 'pullGauge' ] ) ;
141+ validateString ( name , 'name' ) ;
142+ validateObject ( meta , 'meta' , kValidateObjectAllowNullable ) ;
143+ if ( name === '' ) {
144+ throw new ERR_INVALID_ARG_VALUE ( 'name' , name , 'must not be empty' ) ;
136145 }
137146
138147 this . #type = type ;
@@ -342,9 +351,7 @@ class Timer {
342351 * @param {Function } reportCallback The function to call with the duration when stopped.
343352 */
344353 constructor ( report ) {
345- if ( typeof report !== 'function' ) {
346- throw new ERR_INVALID_ARG_TYPE ( 'report' , [ 'function' ] , report ) ;
347- }
354+ validateFunction ( report , 'report' ) ;
348355 this . #report = report ;
349356 this . #start = performance . now ( ) ;
350357 this . #end = undefined ;
@@ -399,7 +406,7 @@ class Timer {
399406 * A gauge which updates its value by calling a function when sampled.
400407 */
401408class PullGauge extends Metric {
402- #fn ;
409+ #puller ;
403410 #value;
404411
405412 /**
@@ -408,14 +415,10 @@ class PullGauge extends Metric {
408415 * @param {Function } fn The function to call to get the gauge value.
409416 * @param {object } [meta] Additional metadata to include with the metric.
410417 */
411- constructor ( name , fn , meta ) {
418+ constructor ( name , puller , meta ) {
412419 super ( 'pullGauge' , name , meta ) ;
413-
414- if ( typeof fn !== 'function' ) {
415- throw new ERR_INVALID_ARG_TYPE ( 'fn' , [ 'function' ] , fn ) ;
416- }
417-
418- this . #fn = fn ;
420+ validateFunction ( puller , 'puller' ) ;
421+ this . #puller = puller ;
419422 this . #value = 0 ;
420423 }
421424
@@ -432,56 +435,32 @@ class PullGauge extends Metric {
432435 * @param {object } [meta] Additional metadata to include with the report.
433436 */
434437 sample ( meta ) {
435- const value = this . #fn ( ) ;
438+ const value = this . #puller ( ) ;
436439 this . #value = value ;
437440 this . report ( value , meta ) ;
438441 return value ;
439442 }
440443}
441444
442-
443- // Map of metric types to their constructors.
444- const metricTypes = {
445- counter : Counter ,
446- gauge : Gauge ,
447- pullGauge : PullGauge ,
448- } ;
449-
450- const metricTypeNames = ObjectKeys ( metricTypes ) ;
451- const wrongTypeErr = `must be one of: ${ metricTypeNames . join ( ', ' ) } ` ;
452-
453- /**
454- * Create a function to directly create a metric of a specific type.
455- * @param {string } type The type of metric to create.
456- * @returns {Function } A function which creates a metric of the specified type.
457- * @private
458- */
459- function direct ( type ) {
460- if ( ! metricTypeNames . includes ( type ) ) {
461- throw new ERR_INVALID_ARG_VALUE ( 'type' , type , wrongTypeErr ) ;
462- }
463- const Type = metricTypes [ type ] ;
464-
465- return function makeMetricType ( name , ...args ) {
466- return new Type ( name , ...args ) ;
467- } ;
468- }
469-
470445/**
471446 * Create a counter metric.
472447 * @param {string } name The name of the counter.
473448 * @param {object } [meta] Additional metadata to include with the report.
474449 * @returns {Counter } The counter metric.
475450 */
476- const counter = direct ( 'counter' ) ;
451+ function counter ( name , meta ) {
452+ return new Counter ( name , meta ) ;
453+ }
477454
478455/**
479456 * Create a gauge metric.
480457 * @param {string } name The name of the gauge.
481458 * @param {object } [meta] Additional metadata to include with the report.
482459 * @returns {Gauge } The gauge metric.
483460 */
484- const gauge = direct ( 'gauge' ) ;
461+ function gauge ( name , meta ) {
462+ return new Gauge ( name , meta ) ;
463+ }
485464
486465/**
487466 * Create a raw metric.
@@ -501,7 +480,9 @@ function metric(type, name, meta) {
501480 * @param {object } [meta] Additional metadata to include with the report.
502481 * @returns {PullGauge } The pull gauge metric.
503482 */
504- const pullGauge = direct ( 'pullGauge' ) ;
483+ function pullGauge ( name , puller , meta ) {
484+ return new PullGauge ( name , puller , meta ) ;
485+ }
505486
506487module . exports = {
507488 MetricReport,
0 commit comments