Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(metrics): add registerMetric and getMetrics #437

Merged
merged 15 commits into from
Nov 1, 2019
Merged
4 changes: 4 additions & 0 deletions packages/opentelemetry-metrics/src/Meter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ export class Meter implements types.Meter {
metric: Metric<T>
): void {
if (this._metrics.has(name)) {
mayurkale22 marked this conversation as resolved.
Show resolved Hide resolved
// @todo: decide how to handle already registered metric
mayurkale22 marked this conversation as resolved.
Show resolved Hide resolved
// 1: Replace the old registered metric by the new
// 2. Throw error
// 3. skip duplicate metric (current approach)
this._logger.error(
`A metric with the name ${name} has already been registered.`
);
Expand Down
24 changes: 24 additions & 0 deletions packages/opentelemetry-metrics/test/Meter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,30 @@ describe('Meter', () => {
});
});

describe('.registerMetric()', () => {
it('skip already registered Metric', () => {
const counter1 = meter.createCounter('name1') as CounterMetric;
counter1.getHandle(labelValues).add(10);

// should skip below metric
const counter2 = meter.createCounter('name1', {
valueType: types.ValueType.INT,
}) as CounterMetric;
counter2.getHandle(labelValues).add(500);

assert.strictEqual(meter.getMetrics().length, 1);
const [{ descriptor, timeseries }] = meter.getMetrics();
assert.deepStrictEqual(descriptor.name, 'name1');
assert.deepStrictEqual(
descriptor.type,
MetricDescriptorType.COUNTER_DOUBLE
);
assert.strictEqual(timeseries.length, 1);
assert.strictEqual(timeseries[0].points.length, 1);
assert.strictEqual(timeseries[0].points[0].value, 10);
});
});

describe('names', () => {
it('should create counter with valid names', () => {
const counter1 = meter.createCounter('name1');
Expand Down