Skip to content

Commit

Permalink
Make Labels Optional for CounterMetric::add (#1032)
Browse files Browse the repository at this point in the history
  • Loading branch information
astorm authored May 13, 2020
1 parent 6b2a9b7 commit 48de7ff
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/opentelemetry-api/src/metrics/Metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ export interface Counter extends UnboundMetric<BoundCounter> {
/**
* Adds the given value to the current value. Values cannot be negative.
*/
add(value: number, labels: Labels): void;
add(value: number, labels?: Labels): void;
}

export interface Measure extends UnboundMetric<BoundMeasure> {
/**
* Records the given value to this measure.
*/
record(value: number, labels: Labels): void;
record(value: number, labels?: Labels): void;

record(
value: number,
Expand Down
6 changes: 3 additions & 3 deletions packages/opentelemetry-metrics/src/Metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ export class CounterMetric extends Metric<BoundCounter> implements api.Counter {
/**
* Adds the given value to the current value. Values cannot be negative.
* @param value the value to add.
* @param labels key-values pairs that are associated with a specific metric
* @param [labels = {}] key-values pairs that are associated with a specific metric
* that you want to record.
*/
add(value: number, labels: api.Labels) {
add(value: number, labels: api.Labels = {}) {
this.bind(labels).add(value);
}
}
Expand Down Expand Up @@ -164,7 +164,7 @@ export class MeasureMetric extends Metric<BoundMeasure> implements api.Measure {
);
}

record(value: number, labels: api.Labels) {
record(value: number, labels: api.Labels = {}) {
this.bind(labels).record(value);
}
}
Expand Down
13 changes: 13 additions & 0 deletions packages/opentelemetry-metrics/test/Meter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ describe('Meter', () => {
);
});

it('should be able to call add with no labels', () => {
const counter = meter.createCounter('name', {
description: 'desc',
unit: '1',
disabled: false,
monotonic: true,
});
counter.add(1);
meter.collect();
const [record1] = meter.getBatcher().checkPointSet();
assert.strictEqual(record1.aggregator.toPoint().value, 1);
});

it('should pipe through resource', () => {
const counter = meter.createCounter('name') as CounterMetric;
assert.ok(counter.resource instanceof Resource);
Expand Down

0 comments on commit 48de7ff

Please sign in to comment.