1616
1717import { validateArrayElementsNotNull , validateNotNull } from '../common/validations' ;
1818import { MeasureUnit } from '../stats/types' ;
19- import { LabelKey } from './export/types' ;
19+ import { MetricProducer } from './export/metric-producer' ;
20+ import { LabelKey , Metric , MetricDescriptorType } from './export/types' ;
21+ import { Gauge } from './gauges/gauge' ;
22+ import { Meter } from './gauges/types' ;
2023
2124/**
2225 * Creates and manages application's set of metrics.
2326 */
2427export class MetricRegistry {
28+ private registeredMetrics : Map < string , Meter > = new Map ( ) ;
29+ private metricProducer : MetricProducer ;
30+
31+ private static readonly NAME = 'name' ;
32+ private static readonly DESCRIPTION = 'description' ;
33+ private static readonly UNIT = 'unit' ;
34+ private static readonly LABEL_KEY = 'labelKey' ;
35+ private static readonly LABEL_KEYS = 'labelKeys' ;
36+
37+ constructor ( ) {
38+ this . metricProducer = new MetricProducerForRegistry ( this . registeredMetrics ) ;
39+ }
40+
2541 /**
2642 * Builds a new Int64 gauge to be added to the registry. This is more
2743 * convenient form when you want to manually increase and decrease values as
@@ -31,16 +47,23 @@ export class MetricRegistry {
3147 * @param {string } description The description of the metric.
3248 * @param {MeasureUnit } unit The unit of the metric.
3349 * @param {LabelKey[] } labelKeys The list of the label keys.
50+ * @returns {Gauge } A Int64 Gauge metric.
3451 */
3552 addInt64Gauge (
3653 name : string , description : string , unit : MeasureUnit ,
37- labelKeys : LabelKey [ ] ) : void {
38- validateNotNull ( name , 'name' ) ;
39- validateNotNull ( description , 'description' ) ;
40- validateNotNull ( unit , 'unit' ) ;
54+ labelKeys : LabelKey [ ] ) : Gauge {
4155 validateArrayElementsNotNull (
42- validateNotNull ( labelKeys , 'labelKeys' ) , 'labelKey' ) ;
43- // TODO(mayurkale): Add Int64Gauge.
56+ validateNotNull ( labelKeys , MetricRegistry . LABEL_KEYS ) ,
57+ MetricRegistry . LABEL_KEY ) ;
58+
59+ const labelKeysCopy = Object . assign ( [ ] , labelKeys ) ;
60+ const int64Gauge = new Gauge (
61+ validateNotNull ( name , MetricRegistry . NAME ) ,
62+ validateNotNull ( description , MetricRegistry . DESCRIPTION ) ,
63+ validateNotNull ( unit , MetricRegistry . UNIT ) ,
64+ MetricDescriptorType . GAUGE_INT64 , labelKeysCopy ) ;
65+ this . registerMetric ( name , int64Gauge ) ;
66+ return int64Gauge ;
4467 }
4568
4669 /**
@@ -52,16 +75,23 @@ export class MetricRegistry {
5275 * @param {string } description The description of the metric.
5376 * @param {MeasureUnit } unit The unit of the metric.
5477 * @param {LabelKey[] } labelKeys The list of the label keys.
78+ * @returns {Gauge } A Double Gauge metric.
5579 */
5680 addDoubleGauge (
5781 name : string , description : string , unit : MeasureUnit ,
58- labelKeys : LabelKey [ ] ) : void {
59- validateNotNull ( name , 'name' ) ;
60- validateNotNull ( description , 'description' ) ;
61- validateNotNull ( unit , 'unit' ) ;
82+ labelKeys : LabelKey [ ] ) : Gauge {
6283 validateArrayElementsNotNull (
63- validateNotNull ( labelKeys , 'labelKeys' ) , 'labelKey' ) ;
64- // TODO(mayurkale): Add DoubleGauge.
84+ validateNotNull ( labelKeys , MetricRegistry . LABEL_KEYS ) ,
85+ MetricRegistry . LABEL_KEY ) ;
86+
87+ const labelKeysCopy = Object . assign ( [ ] , labelKeys ) ;
88+ const doubleGauge = new Gauge (
89+ validateNotNull ( name , MetricRegistry . NAME ) ,
90+ validateNotNull ( description , MetricRegistry . DESCRIPTION ) ,
91+ validateNotNull ( unit , MetricRegistry . UNIT ) ,
92+ MetricDescriptorType . GAUGE_DOUBLE , labelKeysCopy ) ;
93+ this . registerMetric ( name , doubleGauge ) ;
94+ return doubleGauge ;
6595 }
6696
6797 /**
@@ -77,11 +107,12 @@ export class MetricRegistry {
77107 addDerivedInt64Gauge (
78108 name : string , description : string , unit : MeasureUnit ,
79109 labelKeys : LabelKey [ ] ) : void {
80- validateNotNull ( name , 'name' ) ;
81- validateNotNull ( description , 'description' ) ;
82- validateNotNull ( unit , 'unit' ) ;
110+ validateNotNull ( name , MetricRegistry . NAME ) ;
111+ validateNotNull ( description , MetricRegistry . DESCRIPTION ) ;
112+ validateNotNull ( unit , MetricRegistry . UNIT ) ;
83113 validateArrayElementsNotNull (
84- validateNotNull ( labelKeys , 'labelKeys' ) , 'labelKey' ) ;
114+ validateNotNull ( labelKeys , MetricRegistry . LABEL_KEYS ) ,
115+ MetricRegistry . LABEL_KEY ) ;
85116 // TODO(mayurkale): Add Derived Int64Gauge.
86117 }
87118
@@ -98,11 +129,62 @@ export class MetricRegistry {
98129 addDerivedDoubleGauge (
99130 name : string , description : string , unit : MeasureUnit ,
100131 labelKeys : LabelKey [ ] ) : void {
101- validateNotNull ( name , 'name' ) ;
102- validateNotNull ( description , 'description' ) ;
103- validateNotNull ( unit , 'unit' ) ;
132+ validateNotNull ( name , MetricRegistry . NAME ) ;
133+ validateNotNull ( description , MetricRegistry . DESCRIPTION ) ;
134+ validateNotNull ( unit , MetricRegistry . UNIT ) ;
104135 validateArrayElementsNotNull (
105- validateNotNull ( labelKeys , 'labelKeys' ) , 'labelKey' ) ;
136+ validateNotNull ( labelKeys , MetricRegistry . LABEL_KEYS ) ,
137+ MetricRegistry . LABEL_KEY ) ;
106138 // TODO(mayurkale): Add Derived DoubleGauge.
107139 }
140+
141+ /**
142+ * Registers metric to register.
143+ *
144+ * @param {string } name The name of the metric.
145+ * @param {Meter } meter The metric to register.
146+ */
147+ private registerMetric ( name : string , meter : Meter ) : void {
148+ if ( this . registeredMetrics . has ( name ) ) {
149+ throw new Error (
150+ `A metric with the name ${ name } has already been registered.` ) ;
151+ }
152+ this . registeredMetrics . set ( name , meter ) ;
153+ }
154+
155+ /**
156+ * Gets a metric producer for registry.
157+ *
158+ * @returns {MetricProducer } The metric producer.
159+ */
160+ getMetricProducer ( ) : MetricProducer {
161+ return this . metricProducer ;
162+ }
163+ }
164+
165+ /**
166+ * A MetricProducerForRegistry is a producer that can be registered for
167+ * exporting using MetricProducerManager.
168+ *
169+ * TODO (mayurkale): Add MetricProducerManager, that Keeps a set of
170+ * MetricProducer that is used by exporters to determine the metrics that
171+ * need to be exported.
172+ */
173+ class MetricProducerForRegistry extends MetricProducer {
174+ private registeredMetrics : Map < string , Meter > ;
175+
176+ constructor ( registeredMetrics : Map < string , Meter > ) {
177+ super ( ) ;
178+ this . registeredMetrics = registeredMetrics ;
179+ }
180+
181+ /**
182+ * Gets a collection of produced Metric`s to be exported.
183+ *
184+ * @returns {Metric[] } The list of metrics.
185+ */
186+ getMetrics ( ) : Metric [ ] {
187+ return Array . from (
188+ this . registeredMetrics , ( [ _ , meter ] ) => meter . getMetric ( ) ) ;
189+ }
108190}
0 commit comments