@@ -62,8 +62,13 @@ interface GaugeEntry {
6262 readonly extractor : ValueExtractor ;
6363}
6464
65- export type AccessorInterface = LengthAttributeInterface | LengthMethodInterface |
66- SizeAttributeInterface | SizeMethodInterface | ToValueInterface ;
65+ interface AccessorFunction {
66+ ( ) : number ;
67+ }
68+
69+ export type AccessorInterface =
70+ LengthAttributeInterface | LengthMethodInterface | SizeAttributeInterface |
71+ SizeMethodInterface | ToValueInterface | AccessorFunction ;
6772
6873/**
6974 * DerivedGauge metric
@@ -145,19 +150,23 @@ export class DerivedGauge implements types.Meter {
145150
146151 /**
147152 * Creates a TimeSeries. The value of a single point in the TimeSeries is
148- * observed from a obj. The ValueExtractor is invoked whenever
153+ * observed from a obj or a function . The ValueExtractor is invoked whenever
149154 * metrics are collected, meaning the reported value is up-to-date.
150155 *
151156 * @param {LabelValue[] } labelValues The list of the label values.
152- * @param obj The obj to get the size or length or value from. If multiple
153- * options are available, the value (ToValueInterface) takes precedence
154- * first, followed by length and size. e.g value -> length -> size.
157+ * @param objOrFn obj The obj to get the size or length or value from. If
158+ * multiple options are available, the value (ToValueInterface) takes
159+ * precedence first, followed by length and size. e.g value -> length ->
160+ * size.
161+ * fn is the function that will be called to get the current value
162+ * of the gauge.
155163 */
156- createTimeSeries ( labelValues : LabelValue [ ] , obj : AccessorInterface ) : void {
164+ createTimeSeries ( labelValues : LabelValue [ ] , objOrFn : AccessorInterface ) :
165+ void {
157166 validateArrayElementsNotNull (
158167 validateNotNull ( labelValues , DerivedGauge . LABEL_VALUES ) ,
159168 DerivedGauge . LABEL_VALUE ) ;
160- validateNotNull ( obj , DerivedGauge . OBJECT ) ;
169+ validateNotNull ( objOrFn , DerivedGauge . OBJECT ) ;
161170
162171 const hash = hashLabelValues ( labelValues ) ;
163172 if ( this . registeredPoints . has ( hash ) ) {
@@ -167,16 +176,18 @@ export class DerivedGauge implements types.Meter {
167176 throw new Error ( DerivedGauge . ERROR_MESSAGE_INVALID_SIZE ) ;
168177 }
169178
170- if ( DerivedGauge . isToValueInterface ( obj ) ) {
171- this . extractor = ( ) => obj . getValue ( ) ;
172- } else if ( DerivedGauge . isLengthAttributeInterface ( obj ) ) {
173- this . extractor = ( ) => obj . length ;
174- } else if ( DerivedGauge . isLengthMethodInterface ( obj ) ) {
175- this . extractor = ( ) => obj . length ( ) ;
176- } else if ( DerivedGauge . isSizeAttributeInterface ( obj ) ) {
177- this . extractor = ( ) => obj . size ;
178- } else if ( DerivedGauge . isSizeMethodInterface ( obj ) ) {
179- this . extractor = ( ) => obj . size ( ) ;
179+ if ( objOrFn instanceof Function ) {
180+ this . extractor = objOrFn ;
181+ } else if ( DerivedGauge . isToValueInterface ( objOrFn ) ) {
182+ this . extractor = ( ) => objOrFn . getValue ( ) ;
183+ } else if ( DerivedGauge . isLengthAttributeInterface ( objOrFn ) ) {
184+ this . extractor = ( ) => objOrFn . length ;
185+ } else if ( DerivedGauge . isLengthMethodInterface ( objOrFn ) ) {
186+ this . extractor = ( ) => objOrFn . length ( ) ;
187+ } else if ( DerivedGauge . isSizeAttributeInterface ( objOrFn ) ) {
188+ this . extractor = ( ) => objOrFn . size ;
189+ } else if ( DerivedGauge . isSizeMethodInterface ( objOrFn ) ) {
190+ this . extractor = ( ) => objOrFn . size ( ) ;
180191 } else {
181192 throw new Error ( DerivedGauge . ERROR_MESSAGE_UNKNOWN_INTERFACE ) ;
182193 }
0 commit comments