17
17
import { validateArrayElementsNotNull , validateNotNull } from '../common/validations' ;
18
18
import { MeasureUnit , } from '../stats/types' ;
19
19
import { BaseMetricProducer } from './export/base-metric-producer' ;
20
- import { LabelKey , Metric , MetricDescriptorType , MetricProducer } from './export/types' ;
20
+ import { Metric , MetricDescriptorType , MetricProducer } from './export/types' ;
21
21
import { DerivedGauge } from './gauges/derived-gauge' ;
22
22
import { Gauge } from './gauges/gauge' ;
23
- import { Meter } from './gauges/types' ;
23
+ import { Meter , MetricOptions } from './gauges/types' ;
24
24
25
25
/**
26
26
* Creates and manages application's set of metrics.
@@ -30,10 +30,10 @@ export class MetricRegistry {
30
30
private metricProducer : MetricProducer ;
31
31
32
32
private static readonly NAME = 'name' ;
33
- private static readonly DESCRIPTION = 'description' ;
34
- private static readonly UNIT = 'unit' ;
35
33
private static readonly LABEL_KEY = 'labelKey' ;
36
- private static readonly LABEL_KEYS = 'labelKeys' ;
34
+ private static readonly DEFAULT_DESCRIPTION = '' ;
35
+ private static readonly DEFAULT_UNIT = MeasureUnit . UNIT ;
36
+ private static readonly DEFAULT_LABEL_KEYS = [ ] ;
37
37
38
38
constructor ( ) {
39
39
this . metricProducer = new MetricProducerForRegistry ( this . registeredMetrics ) ;
@@ -45,23 +45,21 @@ export class MetricRegistry {
45
45
* per your service requirements.
46
46
*
47
47
* @param {string } name The name of the metric.
48
- * @param {string } description The description of the metric.
49
- * @param {MeasureUnit } unit The unit of the metric.
50
- * @param {LabelKey[] } labelKeys The list of the label keys.
48
+ * @param {MetricOptions } options The options for the metric.
51
49
* @returns {Gauge } A Int64 Gauge metric.
52
50
*/
53
- addInt64Gauge (
54
- name : string , description : string , unit : MeasureUnit ,
55
- labelKeys : LabelKey [ ] ) : Gauge {
56
- validateArrayElementsNotNull (
57
- validateNotNull ( labelKeys , MetricRegistry . LABEL_KEYS ) ,
58
- MetricRegistry . LABEL_KEY ) ;
59
-
51
+ addInt64Gauge ( name : string , options ?: MetricOptions ) : Gauge {
52
+ const description =
53
+ ( options && options . description ) || MetricRegistry . DEFAULT_DESCRIPTION ;
54
+ const unit = ( options && options . unit ) || MetricRegistry . DEFAULT_UNIT ;
55
+ const labelKeys =
56
+ ( options && options . labelKeys ) || MetricRegistry . DEFAULT_LABEL_KEYS ;
57
+ // TODO (mayurkale): Add support for constantLabels
58
+
59
+ validateArrayElementsNotNull ( labelKeys , MetricRegistry . LABEL_KEY ) ;
60
60
const labelKeysCopy = Object . assign ( [ ] , labelKeys ) ;
61
61
const int64Gauge = new Gauge (
62
- validateNotNull ( name , MetricRegistry . NAME ) ,
63
- validateNotNull ( description , MetricRegistry . DESCRIPTION ) ,
64
- validateNotNull ( unit , MetricRegistry . UNIT ) ,
62
+ validateNotNull ( name , MetricRegistry . NAME ) , description , unit ,
65
63
MetricDescriptorType . GAUGE_INT64 , labelKeysCopy ) ;
66
64
this . registerMetric ( name , int64Gauge ) ;
67
65
return int64Gauge ;
@@ -73,23 +71,21 @@ export class MetricRegistry {
73
71
* per your service requirements.
74
72
*
75
73
* @param {string } name The name of the metric.
76
- * @param {string } description The description of the metric.
77
- * @param {MeasureUnit } unit The unit of the metric.
78
- * @param {LabelKey[] } labelKeys The list of the label keys.
74
+ * @param {MetricOptions } options The options for the metric.
79
75
* @returns {Gauge } A Double Gauge metric.
80
76
*/
81
- addDoubleGauge (
82
- name : string , description : string , unit : MeasureUnit ,
83
- labelKeys : LabelKey [ ] ) : Gauge {
84
- validateArrayElementsNotNull (
85
- validateNotNull ( labelKeys , MetricRegistry . LABEL_KEYS ) ,
86
- MetricRegistry . LABEL_KEY ) ;
87
-
77
+ addDoubleGauge ( name : string , options ?: MetricOptions ) : Gauge {
78
+ const description =
79
+ ( options && options . description ) || MetricRegistry . DEFAULT_DESCRIPTION ;
80
+ const unit = ( options && options . unit ) || MetricRegistry . DEFAULT_UNIT ;
81
+ const labelKeys =
82
+ ( options && options . labelKeys ) || MetricRegistry . DEFAULT_LABEL_KEYS ;
83
+ // TODO (mayurkale): Add support for constantLabels
84
+
85
+ validateArrayElementsNotNull ( labelKeys , MetricRegistry . LABEL_KEY ) ;
88
86
const labelKeysCopy = Object . assign ( [ ] , labelKeys ) ;
89
87
const doubleGauge = new Gauge (
90
- validateNotNull ( name , MetricRegistry . NAME ) ,
91
- validateNotNull ( description , MetricRegistry . DESCRIPTION ) ,
92
- validateNotNull ( unit , MetricRegistry . UNIT ) ,
88
+ validateNotNull ( name , MetricRegistry . NAME ) , description , unit ,
93
89
MetricDescriptorType . GAUGE_DOUBLE , labelKeysCopy ) ;
94
90
this . registerMetric ( name , doubleGauge ) ;
95
91
return doubleGauge ;
@@ -101,23 +97,21 @@ export class MetricRegistry {
101
97
* per your service requirements.
102
98
*
103
99
* @param {string } name The name of the metric.
104
- * @param {string } description The description of the metric.
105
- * @param {MeasureUnit } unit The unit of the metric.
106
- * @param {LabelKey[] } labelKeys The list of the label keys.
100
+ * @param {MetricOptions } options The options for the metric.
107
101
* @returns {DerivedGauge } A Int64 DerivedGauge metric.
108
102
*/
109
- addDerivedInt64Gauge (
110
- name : string , description : string , unit : MeasureUnit ,
111
- labelKeys : LabelKey [ ] ) : DerivedGauge {
112
- validateArrayElementsNotNull (
113
- validateNotNull ( labelKeys , MetricRegistry . LABEL_KEYS ) ,
114
- MetricRegistry . LABEL_KEY ) ;
115
-
103
+ addDerivedInt64Gauge ( name : string , options ?: MetricOptions ) : DerivedGauge {
104
+ const description =
105
+ ( options && options . description ) || MetricRegistry . DEFAULT_DESCRIPTION ;
106
+ const unit = ( options && options . unit ) || MetricRegistry . DEFAULT_UNIT ;
107
+ const labelKeys =
108
+ ( options && options . labelKeys ) || MetricRegistry . DEFAULT_LABEL_KEYS ;
109
+ // TODO (mayurkale): Add support for constantLabels
110
+
111
+ validateArrayElementsNotNull ( labelKeys , MetricRegistry . LABEL_KEY ) ;
116
112
const labelKeysCopy = Object . assign ( [ ] , labelKeys ) ;
117
113
const derivedInt64Gauge = new DerivedGauge (
118
- validateNotNull ( name , MetricRegistry . NAME ) ,
119
- validateNotNull ( description , MetricRegistry . DESCRIPTION ) ,
120
- validateNotNull ( unit , MetricRegistry . UNIT ) ,
114
+ validateNotNull ( name , MetricRegistry . NAME ) , description , unit ,
121
115
MetricDescriptorType . GAUGE_INT64 , labelKeysCopy ) ;
122
116
this . registerMetric ( name , derivedInt64Gauge ) ;
123
117
return derivedInt64Gauge ;
@@ -129,23 +123,21 @@ export class MetricRegistry {
129
123
* per your service requirements.
130
124
*
131
125
* @param {string } name The name of the metric.
132
- * @param {string } description The description of the metric.
133
- * @param {MeasureUnit } unit The unit of the metric.
134
- * @param {LabelKey[] } labelKeys The list of the label keys.
126
+ * @param {MetricOptions } options The options for the metric.
135
127
* @returns {DerivedGauge } A Double DerivedGauge metric.
136
128
*/
137
- addDerivedDoubleGauge (
138
- name : string , description : string , unit : MeasureUnit ,
139
- labelKeys : LabelKey [ ] ) : DerivedGauge {
140
- validateArrayElementsNotNull (
141
- validateNotNull ( labelKeys , MetricRegistry . LABEL_KEYS ) ,
142
- MetricRegistry . LABEL_KEY ) ;
143
-
129
+ addDerivedDoubleGauge ( name : string , options ?: MetricOptions ) : DerivedGauge {
130
+ const description =
131
+ ( options && options . description ) || MetricRegistry . DEFAULT_DESCRIPTION ;
132
+ const unit = ( options && options . unit ) || MetricRegistry . DEFAULT_UNIT ;
133
+ const labelKeys =
134
+ ( options && options . labelKeys ) || MetricRegistry . DEFAULT_LABEL_KEYS ;
135
+ // TODO (mayurkale): Add support for constantLabels
136
+
137
+ validateArrayElementsNotNull ( labelKeys , MetricRegistry . LABEL_KEY ) ;
144
138
const labelKeysCopy = Object . assign ( [ ] , labelKeys ) ;
145
139
const derivedDoubleGauge = new DerivedGauge (
146
- validateNotNull ( name , MetricRegistry . NAME ) ,
147
- validateNotNull ( description , MetricRegistry . DESCRIPTION ) ,
148
- validateNotNull ( unit , MetricRegistry . UNIT ) ,
140
+ validateNotNull ( name , MetricRegistry . NAME ) , description , unit ,
149
141
MetricDescriptorType . GAUGE_DOUBLE , labelKeysCopy ) ;
150
142
this . registerMetric ( name , derivedDoubleGauge ) ;
151
143
return derivedDoubleGauge ;
0 commit comments