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

Commit 42338a2

Browse files
authored
Plug-in derived-gauges into the Metric-Registry (#228)
1 parent 614e29b commit 42338a2

File tree

2 files changed

+161
-13
lines changed

2 files changed

+161
-13
lines changed

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717
import {validateArrayElementsNotNull, validateNotNull} from '../common/validations';
1818
import {MeasureUnit} from '../stats/types';
19+
1920
import {MetricProducer} from './export/metric-producer';
2021
import {LabelKey, Metric, MetricDescriptorType} from './export/types';
22+
import {DerivedGauge} from './gauges/derived-gauge';
2123
import {Gauge} from './gauges/gauge';
2224
import {Meter} from './gauges/types';
2325

@@ -103,17 +105,23 @@ export class MetricRegistry {
103105
* @param {string} description The description of the metric.
104106
* @param {MeasureUnit} unit The unit of the metric.
105107
* @param {LabelKey[]} labelKeys The list of the label keys.
108+
* @returns {DerivedGauge} A Int64 DerivedGauge metric.
106109
*/
107110
addDerivedInt64Gauge(
108111
name: string, description: string, unit: MeasureUnit,
109-
labelKeys: LabelKey[]): void {
110-
validateNotNull(name, MetricRegistry.NAME);
111-
validateNotNull(description, MetricRegistry.DESCRIPTION);
112-
validateNotNull(unit, MetricRegistry.UNIT);
112+
labelKeys: LabelKey[]): DerivedGauge {
113113
validateArrayElementsNotNull(
114114
validateNotNull(labelKeys, MetricRegistry.LABEL_KEYS),
115115
MetricRegistry.LABEL_KEY);
116-
// TODO(mayurkale): Add Derived Int64Gauge.
116+
117+
const labelKeysCopy = Object.assign([], labelKeys);
118+
const derivedInt64Gauge = new DerivedGauge(
119+
validateNotNull(name, MetricRegistry.NAME),
120+
validateNotNull(description, MetricRegistry.DESCRIPTION),
121+
validateNotNull(unit, MetricRegistry.UNIT),
122+
MetricDescriptorType.GAUGE_INT64, labelKeysCopy);
123+
this.registerMetric(name, derivedInt64Gauge);
124+
return derivedInt64Gauge;
117125
}
118126

119127
/**
@@ -125,17 +133,23 @@ export class MetricRegistry {
125133
* @param {string} description The description of the metric.
126134
* @param {MeasureUnit} unit The unit of the metric.
127135
* @param {LabelKey[]} labelKeys The list of the label keys.
136+
* @returns {DerivedGauge} A Double DerivedGauge metric.
128137
*/
129138
addDerivedDoubleGauge(
130139
name: string, description: string, unit: MeasureUnit,
131-
labelKeys: LabelKey[]): void {
132-
validateNotNull(name, MetricRegistry.NAME);
133-
validateNotNull(description, MetricRegistry.DESCRIPTION);
134-
validateNotNull(unit, MetricRegistry.UNIT);
140+
labelKeys: LabelKey[]): DerivedGauge {
135141
validateArrayElementsNotNull(
136142
validateNotNull(labelKeys, MetricRegistry.LABEL_KEYS),
137143
MetricRegistry.LABEL_KEY);
138-
// TODO(mayurkale): Add Derived DoubleGauge.
144+
145+
const labelKeysCopy = Object.assign([], labelKeys);
146+
const derivedDoubleGauge = new DerivedGauge(
147+
validateNotNull(name, MetricRegistry.NAME),
148+
validateNotNull(description, MetricRegistry.DESCRIPTION),
149+
validateNotNull(unit, MetricRegistry.UNIT),
150+
MetricDescriptorType.GAUGE_DOUBLE, labelKeysCopy);
151+
this.registerMetric(name, derivedDoubleGauge);
152+
return derivedDoubleGauge;
139153
}
140154

141155
/**

packages/opencensus-core/test/test-metric-registry.ts

Lines changed: 137 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const LABEL_KEYS: LabelKey[] = [{key: 'code', description: 'desc'}];
2626
const LABEL_KEYS_WITH_NULL: LabelKey[] =
2727
[{key: 'code', description: 'desc'}, null];
2828
const LABEL_VALUES_200: LabelValue[] = [{value: '200'}];
29+
const LABEL_VALUES_400: LabelValue[] = [{value: '400'}];
2930

3031
describe('addInt64Gauge', () => {
3132
const oldProcessHrtime = process.hrtime;
@@ -174,12 +175,12 @@ describe('addDoubleGauge', () => {
174175
}, /^Error: labelKey elements should not be a NULL$/);
175176
});
176177
it('should return a metric', () => {
177-
const int64Gauge = registry.addDoubleGauge(
178+
const doubleGauge = registry.addDoubleGauge(
178179
METRIC_NAME, METRIC_DESCRIPTION, UNIT, LABEL_KEYS);
179-
const pointEntry = int64Gauge.getOrCreateTimeSeries(LABEL_VALUES_200);
180+
const pointEntry = doubleGauge.getOrCreateTimeSeries(LABEL_VALUES_200);
180181
pointEntry.add(5.5);
181182

182-
const pointEntry1 = int64Gauge.getOrCreateTimeSeries(LABEL_VALUES_200);
183+
const pointEntry1 = doubleGauge.getOrCreateTimeSeries(LABEL_VALUES_200);
183184
pointEntry1.set(0.7);
184185

185186
const metrics = registry.getMetricProducer().getMetrics();
@@ -272,6 +273,39 @@ describe('addDerivedInt64Gauge', () => {
272273
METRIC_NAME, METRIC_DESCRIPTION, UNIT, LABEL_KEYS_WITH_NULL);
273274
}, /^Error: labelKey elements should not be a NULL$/);
274275
});
276+
it('should return a metric', () => {
277+
const map = new Map();
278+
map.set('key', 'value');
279+
const derivedInt64Gauge = registry.addDerivedInt64Gauge(
280+
METRIC_NAME, METRIC_DESCRIPTION, UNIT, LABEL_KEYS);
281+
derivedInt64Gauge.createTimeSeries(LABEL_VALUES_200, map);
282+
map.set('key1', 'value1');
283+
284+
const metrics = registry.getMetricProducer().getMetrics();
285+
assert.strictEqual(metrics.length, 1);
286+
const [{descriptor, timeseries}] = metrics;
287+
assert.deepStrictEqual(descriptor, {
288+
name: METRIC_NAME,
289+
description: METRIC_DESCRIPTION,
290+
'labelKeys': LABEL_KEYS,
291+
unit: UNIT,
292+
type: MetricDescriptorType.GAUGE_INT64
293+
});
294+
assert.strictEqual(timeseries.length, 1);
295+
const [{points}] = timeseries;
296+
const [point] = points;
297+
assert.equal(point.value, 2);
298+
assert.deepStrictEqual(point.timestamp, {seconds: 1000, nanos: 1e7});
299+
});
300+
301+
it('should throw an error when the register same metric', () => {
302+
registry.addDerivedInt64Gauge(
303+
METRIC_NAME, METRIC_DESCRIPTION, UNIT, LABEL_KEYS);
304+
assert.throws(() => {
305+
registry.addDerivedInt64Gauge(
306+
METRIC_NAME, METRIC_DESCRIPTION, UNIT, LABEL_KEYS);
307+
}, /^Error: A metric with the name metric-name has already been registered.$/);
308+
});
275309
});
276310

277311
describe('addDerivedDoubleGauge', () => {
@@ -339,4 +373,104 @@ describe('addDerivedDoubleGauge', () => {
339373
METRIC_NAME, METRIC_DESCRIPTION, UNIT, LABEL_KEYS_WITH_NULL);
340374
}, /^Error: labelKey elements should not be a NULL$/);
341375
});
376+
it('should return a metric', () => {
377+
class QueueManager {
378+
getValue(): number {
379+
return 0.7;
380+
}
381+
}
382+
const derivedDoubleGauge = registry.addDerivedDoubleGauge(
383+
METRIC_NAME, METRIC_DESCRIPTION, UNIT, LABEL_KEYS);
384+
derivedDoubleGauge.createTimeSeries(LABEL_VALUES_200, new QueueManager());
385+
386+
const metrics = registry.getMetricProducer().getMetrics();
387+
assert.strictEqual(metrics.length, 1);
388+
const [{descriptor, timeseries}] = metrics;
389+
assert.deepStrictEqual(descriptor, {
390+
name: METRIC_NAME,
391+
description: METRIC_DESCRIPTION,
392+
'labelKeys': LABEL_KEYS,
393+
unit: UNIT,
394+
type: MetricDescriptorType.GAUGE_DOUBLE
395+
});
396+
assert.strictEqual(timeseries.length, 1);
397+
const [{points}] = timeseries;
398+
const [point] = points;
399+
assert.equal(point.value, 0.7);
400+
assert.deepStrictEqual(point.timestamp, {seconds: 1000, nanos: 1e7});
401+
});
402+
403+
it('should throw an error when the register same metric', () => {
404+
registry.addDerivedDoubleGauge(
405+
METRIC_NAME, METRIC_DESCRIPTION, UNIT, LABEL_KEYS);
406+
assert.throws(() => {
407+
registry.addDerivedDoubleGauge(
408+
METRIC_NAME, METRIC_DESCRIPTION, UNIT, LABEL_KEYS);
409+
}, /^Error: A metric with the name metric-name has already been registered.$/);
410+
});
411+
});
412+
413+
describe('Add multiple gauges', () => {
414+
const oldProcessHrtime = process.hrtime;
415+
let registry: MetricRegistry;
416+
417+
before(() => {
418+
registry = new MetricRegistry();
419+
process.hrtime = () => [1000, 1e7];
420+
});
421+
422+
after(() => {
423+
process.hrtime = oldProcessHrtime;
424+
});
425+
426+
it('should return metrics', () => {
427+
const int64Gauge = registry.addInt64Gauge(
428+
'metric-name1', METRIC_DESCRIPTION, UNIT, LABEL_KEYS);
429+
int64Gauge.getOrCreateTimeSeries(LABEL_VALUES_200).add(100);
430+
431+
const doubleGauge = registry.addDoubleGauge(
432+
'metric-name2', METRIC_DESCRIPTION, UNIT, LABEL_KEYS);
433+
doubleGauge.getOrCreateTimeSeries(LABEL_VALUES_200).add(5.5);
434+
435+
const arr = new Array(5).fill('test');
436+
const derivedInt64Gauge = registry.addDerivedInt64Gauge(
437+
'metric-name3', METRIC_DESCRIPTION, UNIT, LABEL_KEYS);
438+
derivedInt64Gauge.createTimeSeries(LABEL_VALUES_400, {
439+
size: () => arr.length,
440+
});
441+
442+
const metrics = registry.getMetricProducer().getMetrics();
443+
assert.strictEqual(metrics.length, 3);
444+
const [{descriptor: descriptor1, timeseries: timeseries1}, {descriptor: descriptor2, timeseries: timeseries2}, {descriptor: descriptor3, timeseries: timeseries3}] = metrics;
445+
assert.deepStrictEqual(descriptor1, {
446+
name: 'metric-name1',
447+
description: METRIC_DESCRIPTION,
448+
'labelKeys': LABEL_KEYS,
449+
unit: UNIT,
450+
type: MetricDescriptorType.GAUGE_INT64
451+
});
452+
assert.deepStrictEqual(descriptor2, {
453+
name: 'metric-name2',
454+
description: METRIC_DESCRIPTION,
455+
'labelKeys': LABEL_KEYS,
456+
unit: UNIT,
457+
type: MetricDescriptorType.GAUGE_DOUBLE
458+
});
459+
assert.deepStrictEqual(descriptor3, {
460+
name: 'metric-name3',
461+
description: METRIC_DESCRIPTION,
462+
'labelKeys': LABEL_KEYS,
463+
unit: UNIT,
464+
type: MetricDescriptorType.GAUGE_INT64
465+
});
466+
assert.strictEqual(timeseries1.length, 1);
467+
assert.strictEqual(timeseries1[0].points.length, 1);
468+
assert.equal(timeseries1[0].points[0].value, 100);
469+
assert.strictEqual(timeseries2.length, 1);
470+
assert.strictEqual(timeseries2[0].points.length, 1);
471+
assert.equal(timeseries2[0].points[0].value, 5.5);
472+
assert.strictEqual(timeseries3.length, 1);
473+
assert.strictEqual(timeseries3[0].points.length, 1);
474+
assert.equal(timeseries3[0].points[0].value, 5);
475+
});
342476
});

0 commit comments

Comments
 (0)