1414 * limitations under the License.
1515 */
1616
17+ import { getTimestampWithProcessHRTime } from '../common/time-util' ;
1718import { validateArrayElementsNotNull , validateDuplicateKeys , validateMapElementNotNull , validateNotNull } from '../common/validations' ;
1819import { MeasureUnit } from '../stats/types' ;
1920import { Cumulative } from './cumulative/cumulative' ;
21+ import { DerivedCumulative } from './cumulative/derived-cumulative' ;
2022import { BaseMetricProducer } from './export/base-metric-producer' ;
21- import { Metric , MetricDescriptorType , MetricProducer } from './export/types' ;
23+ import { LabelKey , LabelValue , Metric , MetricDescriptorType , MetricProducer , Timestamp } from './export/types' ;
2224import { DerivedGauge } from './gauges/derived-gauge' ;
2325import { Gauge } from './gauges/gauge' ;
2426import { Meter , MetricOptions } from './types' ;
@@ -61,9 +63,7 @@ export class MetricRegistry {
6163 MetricRegistry . DEFAULT_CONSTANT_LABEL ;
6264 // TODO(mayurkale): Add support for resource
6365
64- validateArrayElementsNotNull ( labelKeys , MetricRegistry . LABEL_KEY ) ;
65- validateMapElementNotNull ( constantLabels , MetricRegistry . CONSTANT_LABELS ) ;
66- validateDuplicateKeys ( labelKeys , constantLabels ) ;
66+ this . validateLables ( labelKeys , constantLabels ) ;
6767
6868 const labelKeysCopy = Object . assign ( [ ] , labelKeys ) ;
6969 const int64Gauge = new Gauge (
@@ -92,9 +92,7 @@ export class MetricRegistry {
9292 MetricRegistry . DEFAULT_CONSTANT_LABEL ;
9393 // TODO(mayurkale): Add support for resource
9494
95- validateArrayElementsNotNull ( labelKeys , MetricRegistry . LABEL_KEY ) ;
96- validateMapElementNotNull ( constantLabels , MetricRegistry . CONSTANT_LABELS ) ;
97- validateDuplicateKeys ( labelKeys , constantLabels ) ;
95+ this . validateLables ( labelKeys , constantLabels ) ;
9896
9997 const labelKeysCopy = Object . assign ( [ ] , labelKeys ) ;
10098 const doubleGauge = new Gauge (
@@ -123,9 +121,7 @@ export class MetricRegistry {
123121 MetricRegistry . DEFAULT_CONSTANT_LABEL ;
124122 // TODO(mayurkale): Add support for resource
125123
126- validateArrayElementsNotNull ( labelKeys , MetricRegistry . LABEL_KEY ) ;
127- validateMapElementNotNull ( constantLabels , MetricRegistry . CONSTANT_LABELS ) ;
128- validateDuplicateKeys ( labelKeys , constantLabels ) ;
124+ this . validateLables ( labelKeys , constantLabels ) ;
129125
130126 const labelKeysCopy = Object . assign ( [ ] , labelKeys ) ;
131127 const derivedInt64Gauge = new DerivedGauge (
@@ -154,9 +150,7 @@ export class MetricRegistry {
154150 MetricRegistry . DEFAULT_CONSTANT_LABEL ;
155151 // TODO(mayurkale): Add support for resource
156152
157- validateArrayElementsNotNull ( labelKeys , MetricRegistry . LABEL_KEY ) ;
158- validateMapElementNotNull ( constantLabels , MetricRegistry . CONSTANT_LABELS ) ;
159- validateDuplicateKeys ( labelKeys , constantLabels ) ;
153+ this . validateLables ( labelKeys , constantLabels ) ;
160154
161155 const labelKeysCopy = Object . assign ( [ ] , labelKeys ) ;
162156 const derivedDoubleGauge = new DerivedGauge (
@@ -185,9 +179,7 @@ export class MetricRegistry {
185179 MetricRegistry . DEFAULT_CONSTANT_LABEL ;
186180 // TODO(mayurkale): Add support for resource
187181
188- validateArrayElementsNotNull ( labelKeys , MetricRegistry . LABEL_KEY ) ;
189- validateMapElementNotNull ( constantLabels , MetricRegistry . CONSTANT_LABELS ) ;
190- validateDuplicateKeys ( labelKeys , constantLabels ) ;
182+ this . validateLables ( labelKeys , constantLabels ) ;
191183
192184 const labelKeysCopy = Object . assign ( [ ] , labelKeys ) ;
193185 const int64Cumulative = new Cumulative (
@@ -216,9 +208,7 @@ export class MetricRegistry {
216208 MetricRegistry . DEFAULT_CONSTANT_LABEL ;
217209 // TODO(mayurkale): Add support for resource
218210
219- validateArrayElementsNotNull ( labelKeys , MetricRegistry . LABEL_KEY ) ;
220- validateMapElementNotNull ( constantLabels , MetricRegistry . CONSTANT_LABELS ) ;
221- validateDuplicateKeys ( labelKeys , constantLabels ) ;
211+ this . validateLables ( labelKeys , constantLabels ) ;
222212
223213 const labelKeysCopy = Object . assign ( [ ] , labelKeys ) ;
224214 const doubleCumulative = new Cumulative (
@@ -228,6 +218,66 @@ export class MetricRegistry {
228218 return doubleCumulative ;
229219 }
230220
221+ /**
222+ * Builds a new derived Int64 Cumulative to be added to the registry.
223+ *
224+ * @param name The name of the metric.
225+ * @param options The options for the metric.
226+ * @returns A Int64 DerivedCumulative metric.
227+ */
228+ addDerivedInt64Cumulative ( name : string , options ?: MetricOptions ) :
229+ DerivedCumulative {
230+ const description =
231+ ( options && options . description ) || MetricRegistry . DEFAULT_DESCRIPTION ;
232+ const unit = ( options && options . unit ) || MetricRegistry . DEFAULT_UNIT ;
233+ const labelKeys =
234+ ( options && options . labelKeys ) || MetricRegistry . DEFAULT_LABEL_KEYS ;
235+ const constantLabels = ( options && options . constantLabels ) ||
236+ MetricRegistry . DEFAULT_CONSTANT_LABEL ;
237+ // TODO(mayurkale): Add support for resource
238+
239+ this . validateLables ( labelKeys , constantLabels ) ;
240+
241+ const labelKeysCopy = Object . assign ( [ ] , labelKeys ) ;
242+ const startTime : Timestamp = getTimestampWithProcessHRTime ( ) ;
243+ const derivedInt64Cumulative = new DerivedCumulative (
244+ validateNotNull ( name , MetricRegistry . NAME ) , description , unit ,
245+ MetricDescriptorType . CUMULATIVE_INT64 , labelKeysCopy , constantLabels ,
246+ startTime ) ;
247+ this . registerMetric ( name , derivedInt64Cumulative ) ;
248+ return derivedInt64Cumulative ;
249+ }
250+
251+ /**
252+ * Builds a new derived Double Cumulative to be added to the registry.
253+ *
254+ * @param name The name of the metric.
255+ * @param options The options for the metric.
256+ * @returns A Double DerivedCumulative metric.
257+ */
258+ addDerivedDoubleCumulative ( name : string , options ?: MetricOptions ) :
259+ DerivedCumulative {
260+ const description =
261+ ( options && options . description ) || MetricRegistry . DEFAULT_DESCRIPTION ;
262+ const unit = ( options && options . unit ) || MetricRegistry . DEFAULT_UNIT ;
263+ const labelKeys =
264+ ( options && options . labelKeys ) || MetricRegistry . DEFAULT_LABEL_KEYS ;
265+ const constantLabels = ( options && options . constantLabels ) ||
266+ MetricRegistry . DEFAULT_CONSTANT_LABEL ;
267+ // TODO(mayurkale): Add support for resource
268+
269+ this . validateLables ( labelKeys , constantLabels ) ;
270+
271+ const labelKeysCopy = Object . assign ( [ ] , labelKeys ) ;
272+ const startTime : Timestamp = getTimestampWithProcessHRTime ( ) ;
273+ const derivedDoubleCumulative = new DerivedCumulative (
274+ validateNotNull ( name , MetricRegistry . NAME ) , description , unit ,
275+ MetricDescriptorType . CUMULATIVE_DOUBLE , labelKeysCopy , constantLabels ,
276+ startTime ) ;
277+ this . registerMetric ( name , derivedDoubleCumulative ) ;
278+ return derivedDoubleCumulative ;
279+ }
280+
231281 /**
232282 * Registers metric to register.
233283 *
@@ -250,6 +300,14 @@ export class MetricRegistry {
250300 getMetricProducer ( ) : MetricProducer {
251301 return this . metricProducer ;
252302 }
303+
304+ /** Validates labelKeys and constantLabels. */
305+ private validateLables (
306+ labelKeys : LabelKey [ ] , constantLabels : Map < LabelKey , LabelValue > ) {
307+ validateArrayElementsNotNull ( labelKeys , MetricRegistry . LABEL_KEY ) ;
308+ validateMapElementNotNull ( constantLabels , MetricRegistry . CONSTANT_LABELS ) ;
309+ validateDuplicateKeys ( labelKeys , constantLabels ) ;
310+ }
253311}
254312
255313/**
0 commit comments