Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit cbd1d99

Browse files
authored
Add MetricOptions to simplify all addXGauge (#458)
* Add MetricOptions to simplify all addXGauge * Add TODO and fix stackdriver exporter tests
1 parent 26eb382 commit cbd1d99

File tree

4 files changed

+177
-94
lines changed

4 files changed

+177
-94
lines changed

packages/opencensus-core/src/metrics/gauges/types.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {Metric, TimeSeries, Timestamp} from '../export/types';
17+
import {MeasureUnit} from '../../stats/types';
18+
import {LabelKey, LabelValue, Metric, TimeSeries, Timestamp} from '../export/types';
1819

1920
export interface Meter {
2021
/**
@@ -48,3 +49,18 @@ export interface Point {
4849
*/
4950
getTimeSeries(timestamp: Timestamp): TimeSeries;
5051
}
52+
53+
/** Options for every metric added to the MetricRegistry. */
54+
export interface MetricOptions {
55+
/** The description of the metric. */
56+
readonly description?: string;
57+
/** The unit of the metric. */
58+
readonly unit?: MeasureUnit;
59+
/** The list of the label keys. */
60+
readonly labelKeys?: LabelKey[];
61+
/** The map of constant labels for the Metric. */
62+
readonly constantLabels?: Map<LabelKey, LabelValue>;
63+
64+
// TODO(mayurkale): Add resource information.
65+
// https://github.com/census-instrumentation/opencensus-specs/pull/248
66+
}

packages/opencensus-core/src/metrics/metric-registry.ts

+49-57
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
import {validateArrayElementsNotNull, validateNotNull} from '../common/validations';
1818
import {MeasureUnit,} from '../stats/types';
1919
import {BaseMetricProducer} from './export/base-metric-producer';
20-
import {LabelKey, Metric, MetricDescriptorType, MetricProducer} from './export/types';
20+
import {Metric, MetricDescriptorType, MetricProducer} from './export/types';
2121
import {DerivedGauge} from './gauges/derived-gauge';
2222
import {Gauge} from './gauges/gauge';
23-
import {Meter} from './gauges/types';
23+
import {Meter, MetricOptions} from './gauges/types';
2424

2525
/**
2626
* Creates and manages application's set of metrics.
@@ -30,10 +30,10 @@ export class MetricRegistry {
3030
private metricProducer: MetricProducer;
3131

3232
private static readonly NAME = 'name';
33-
private static readonly DESCRIPTION = 'description';
34-
private static readonly UNIT = 'unit';
3533
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 = [];
3737

3838
constructor() {
3939
this.metricProducer = new MetricProducerForRegistry(this.registeredMetrics);
@@ -45,23 +45,21 @@ export class MetricRegistry {
4545
* per your service requirements.
4646
*
4747
* @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.
5149
* @returns {Gauge} A Int64 Gauge metric.
5250
*/
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);
6060
const labelKeysCopy = Object.assign([], labelKeys);
6161
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,
6563
MetricDescriptorType.GAUGE_INT64, labelKeysCopy);
6664
this.registerMetric(name, int64Gauge);
6765
return int64Gauge;
@@ -73,23 +71,21 @@ export class MetricRegistry {
7371
* per your service requirements.
7472
*
7573
* @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.
7975
* @returns {Gauge} A Double Gauge metric.
8076
*/
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);
8886
const labelKeysCopy = Object.assign([], labelKeys);
8987
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,
9389
MetricDescriptorType.GAUGE_DOUBLE, labelKeysCopy);
9490
this.registerMetric(name, doubleGauge);
9591
return doubleGauge;
@@ -101,23 +97,21 @@ export class MetricRegistry {
10197
* per your service requirements.
10298
*
10399
* @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.
107101
* @returns {DerivedGauge} A Int64 DerivedGauge metric.
108102
*/
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);
116112
const labelKeysCopy = Object.assign([], labelKeys);
117113
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,
121115
MetricDescriptorType.GAUGE_INT64, labelKeysCopy);
122116
this.registerMetric(name, derivedInt64Gauge);
123117
return derivedInt64Gauge;
@@ -129,23 +123,21 @@ export class MetricRegistry {
129123
* per your service requirements.
130124
*
131125
* @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.
135127
* @returns {DerivedGauge} A Double DerivedGauge metric.
136128
*/
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);
144138
const labelKeysCopy = Object.assign([], labelKeys);
145139
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,
149141
MetricDescriptorType.GAUGE_DOUBLE, labelKeysCopy);
150142
this.registerMetric(name, derivedDoubleGauge);
151143
return derivedDoubleGauge;

0 commit comments

Comments
 (0)