Skip to content

Commit 036da81

Browse files
mayurkale22dyladan
authored andcommitted
Remove label set from metrics API (open-telemetry#915)
* metrics: remove LabeSet API * fix: tests * fix: lint
1 parent bdb4125 commit 036da81

File tree

5 files changed

+35
-61
lines changed

5 files changed

+35
-61
lines changed

api/src/metrics/Meter.ts

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

17-
import { Metric, MetricOptions, Labels, LabelSet } from './Metric';
17+
import { Metric, MetricOptions } from './Metric';
1818
import { BoundCounter, BoundMeasure, BoundObserver } from './BoundInstrument';
1919

2020
/**
@@ -47,12 +47,4 @@ export interface Meter {
4747
* @param [options] the metric options.
4848
*/
4949
createObserver(name: string, options?: MetricOptions): Metric<BoundObserver>;
50-
51-
/**
52-
* Provide a pre-computed re-useable LabelSet by
53-
* converting the unordered labels into a canonicalized
54-
* set of labels with an unique identifier, useful for pre-aggregation.
55-
* @param labels user provided unordered Labels.
56-
*/
57-
labels(labels: Labels): LabelSet;
5850
}

api/src/metrics/Metric.ts

+12-21
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,19 @@ export enum ValueType {
7979
*/
8080
export interface Metric<T> {
8181
/**
82-
* Returns a Instrument associated with specified LabelSet.
82+
* Returns a Instrument associated with specified Labels.
8383
* It is recommended to keep a reference to the Instrument instead of always
8484
* calling this method for every operations.
85-
* @param labels the canonicalized LabelSet used to associate with this
86-
* metric instrument.
85+
* @param labels key-values pairs that are associated with a specific metric
86+
* that you want to record.
8787
*/
88-
bind(labels: LabelSet): T;
88+
bind(labels: Labels): T;
8989

9090
/**
9191
* Removes the Instrument from the metric, if it is present.
92-
* @param labels the canonicalized LabelSet used to associate with this
93-
* metric instrument.
92+
* @param labels key-values pairs that are associated with a specific metric.
9493
*/
95-
unbind(labels: LabelSet): void;
94+
unbind(labels: Labels): void;
9695

9796
/**
9897
* Clears all timeseries from the Metric.
@@ -104,7 +103,7 @@ export interface MetricUtils {
104103
/**
105104
* Adds the given value to the current value. Values cannot be negative.
106105
*/
107-
add(value: number, labelSet: LabelSet): void;
106+
add(value: number, labels: Labels): void;
108107

109108
/**
110109
* Sets a callback where user can observe value for certain labels
@@ -116,22 +115,22 @@ export interface MetricUtils {
116115
/**
117116
* Sets the given value. Values can be negative.
118117
*/
119-
set(value: number, labelSet: LabelSet): void;
118+
set(value: number, labels: Labels): void;
120119

121120
/**
122121
* Records the given value to this measure.
123122
*/
124-
record(value: number, labelSet: LabelSet): void;
123+
record(value: number, labels: Labels): void;
125124

126125
record(
127126
value: number,
128-
labelSet: LabelSet,
127+
labels: Labels,
129128
correlationContext: CorrelationContext
130129
): void;
131130

132131
record(
133132
value: number,
134-
labelSet: LabelSet,
133+
labels: Labels,
135134
correlationContext: CorrelationContext,
136135
spanContext: SpanContext
137136
): void;
@@ -140,12 +139,4 @@ export interface MetricUtils {
140139
/**
141140
* key-value pairs passed by the user.
142141
*/
143-
export type Labels = Record<string, string>;
144-
145-
/**
146-
* Canonicalized labels with an unique string identifier.
147-
*/
148-
export interface LabelSet {
149-
identifier: string;
150-
labels: Labels;
151-
}
142+
export type Labels = { [key: string]: string };

api/src/metrics/NoopMeter.ts

+13-20
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import { Meter } from './Meter';
18-
import { MetricOptions, Metric, Labels, LabelSet, MetricUtils } from './Metric';
18+
import { MetricOptions, Metric, Labels, MetricUtils } from './Metric';
1919
import { BoundMeasure, BoundCounter, BoundObserver } from './BoundInstrument';
2020
import { CorrelationContext } from '../correlation_context/CorrelationContext';
2121
import { SpanContext } from '../trace/span_context';
@@ -54,10 +54,6 @@ export class NoopMeter implements Meter {
5454
createObserver(name: string, options?: MetricOptions): Metric<BoundObserver> {
5555
return NOOP_OBSERVER_METRIC;
5656
}
57-
58-
labels(labels: Labels): LabelSet {
59-
return NOOP_LABEL_SET;
60-
}
6157
}
6258

6359
export class NoopMetric<T> implements Metric<T> {
@@ -67,22 +63,21 @@ export class NoopMetric<T> implements Metric<T> {
6763
this._instrument = instrument;
6864
}
6965
/**
70-
* Returns a Bound Instrument associated with specified LabelSet.
66+
* Returns a Bound Instrument associated with specified Labels.
7167
* It is recommended to keep a reference to the Bound Instrument instead of
7268
* always calling this method for every operations.
73-
* @param labels the canonicalized LabelSet used to associate with this
74-
* metric instrument.
69+
* @param labels key-values pairs that are associated with a specific metric
70+
* that you want to record.
7571
*/
76-
bind(labels: LabelSet): T {
72+
bind(labels: Labels): T {
7773
return this._instrument;
7874
}
7975

8076
/**
8177
* Removes the Binding from the metric, if it is present.
82-
* @param labels the canonicalized LabelSet used to associate with this
83-
* metric instrument.
78+
* @param labels key-values pairs that are associated with a specific metric.
8479
*/
85-
unbind(labels: LabelSet): void {
80+
unbind(labels: Labels): void {
8681
return;
8782
}
8883

@@ -96,25 +91,25 @@ export class NoopMetric<T> implements Metric<T> {
9691

9792
export class NoopCounterMetric extends NoopMetric<BoundCounter>
9893
implements Pick<MetricUtils, 'add'> {
99-
add(value: number, labelSet: LabelSet) {
100-
this.bind(labelSet).add(value);
94+
add(value: number, labels: Labels) {
95+
this.bind(labels).add(value);
10196
}
10297
}
10398

10499
export class NoopMeasureMetric extends NoopMetric<BoundMeasure>
105100
implements Pick<MetricUtils, 'record'> {
106101
record(
107102
value: number,
108-
labelSet: LabelSet,
103+
labels: Labels,
109104
correlationContext?: CorrelationContext,
110105
spanContext?: SpanContext
111106
) {
112107
if (typeof correlationContext === 'undefined') {
113-
this.bind(labelSet).record(value);
108+
this.bind(labels).record(value);
114109
} else if (typeof spanContext === 'undefined') {
115-
this.bind(labelSet).record(value, correlationContext);
110+
this.bind(labels).record(value, correlationContext);
116111
} else {
117-
this.bind(labelSet).record(value, correlationContext, spanContext);
112+
this.bind(labels).record(value, correlationContext, spanContext);
118113
}
119114
}
120115
}
@@ -153,5 +148,3 @@ export const NOOP_MEASURE_METRIC = new NoopMeasureMetric(NOOP_BOUND_MEASURE);
153148

154149
export const NOOP_BOUND_OBSERVER = new NoopBoundObserver();
155150
export const NOOP_OBSERVER_METRIC = new NoopObserverMetric(NOOP_BOUND_OBSERVER);
156-
157-
export const NOOP_LABEL_SET = {} as LabelSet;

api/src/metrics/ObserverResult.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { LabelSet } from './Metric';
17+
import { Labels } from './Metric';
1818

1919
/**
2020
* Interface that is being used in function setCallback for Observer Metric
2121
*/
2222
export interface ObserverResult {
23-
observers: Map<LabelSet, Function>;
24-
observe(callback: Function, labelSet: LabelSet): void;
23+
observers: Map<Labels, Function>;
24+
observe(callback: Function, labels: Labels): void;
2525
}

api/test/noop-implementations/noop-meter.test.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import * as assert from 'assert';
1818
import {
19-
Labels,
2019
NoopMeterProvider,
2120
NOOP_BOUND_COUNTER,
2221
NOOP_BOUND_MEASURE,
@@ -28,24 +27,23 @@ describe('NoopMeter', () => {
2827
it('should not crash', () => {
2928
const meter = new NoopMeterProvider().getMeter('test-noop');
3029
const counter = meter.createCounter('some-name');
31-
const labels = {} as Labels;
32-
const labelSet = meter.labels(labels);
30+
const labels = {};
3331

3432
// ensure NoopMetric does not crash.
35-
counter.bind(labelSet).add(1);
36-
counter.unbind(labelSet);
33+
counter.bind(labels).add(1);
34+
counter.unbind(labels);
3735

3836
// ensure the correct noop const is returned
3937
assert.strictEqual(counter, NOOP_COUNTER_METRIC);
40-
assert.strictEqual(counter.bind(labelSet), NOOP_BOUND_COUNTER);
38+
assert.strictEqual(counter.bind(labels), NOOP_BOUND_COUNTER);
4139
counter.clear();
4240

4341
const measure = meter.createMeasure('some-name');
44-
measure.bind(labelSet).record(1);
42+
measure.bind(labels).record(1);
4543

4644
// ensure the correct noop const is returned
4745
assert.strictEqual(measure, NOOP_MEASURE_METRIC);
48-
assert.strictEqual(measure.bind(labelSet), NOOP_BOUND_MEASURE);
46+
assert.strictEqual(measure.bind(labels), NOOP_BOUND_MEASURE);
4947

5048
const options = {
5149
component: 'tests',

0 commit comments

Comments
 (0)