Skip to content

Commit 3d26f82

Browse files
authored
Remove label set from metrics API (#915)
* metrics: remove LabeSet API * fix: tests * fix: lint
1 parent 802882d commit 3d26f82

File tree

26 files changed

+186
-274
lines changed

26 files changed

+186
-274
lines changed

examples/metrics/metrics/observer.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ function getCpuUsage() {
2828
}
2929

3030
otelCpuUsage.setCallback((observerResult) => {
31-
observerResult.observe(getCpuUsage, meter.labels({ pid: process.pid, core: '1' }));
32-
observerResult.observe(getCpuUsage, meter.labels({ pid: process.pid, core: '2' }));
33-
observerResult.observe(getCpuUsage, meter.labels({ pid: process.pid, core: '3' }));
34-
observerResult.observe(getCpuUsage, meter.labels({ pid: process.pid, core: '4' }));
31+
observerResult.observe(getCpuUsage, { pid: process.pid, core: '1' });
32+
observerResult.observe(getCpuUsage, { pid: process.pid, core: '2' });
33+
observerResult.observe(getCpuUsage, { pid: process.pid, core: '3' });
34+
observerResult.observe(getCpuUsage, { pid: process.pid, core: '4' });
3535
});

examples/prometheus/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ const nonMonotonicCounter = meter.createCounter('non_monotonic_counter', {
3131
description: 'Example of a non-monotonic counter',
3232
});
3333

34-
setInterval(() => {
35-
const labels = meter.labels({ pid: process.pid });
34+
const labels = { pid: process.pid };
3635

36+
setInterval(() => {
3737
monotonicCounter.bind(labels).add(1);
3838
nonMonotonicCounter.bind(labels).add(Math.random() > 0.5 ? 1 : -1);
3939
}, 1000);

getting-started/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,8 @@ const boundInstruments = new Map();
262262
module.exports.countAllRequests = () => {
263263
return (req, res, next) => {
264264
if (!boundInstruments.has(req.path)) {
265-
const labelSet = meter.labels({ route: req.path });
266-
const boundCounter = requestCount.bind(labelSet);
265+
const labels = { route: req.path };
266+
const boundCounter = requestCount.bind(labels);
267267
boundInstruments.set(req.path, boundCounter);
268268
}
269269
@@ -328,8 +328,8 @@ const boundInstruments = new Map();
328328
module.exports.countAllRequests = () => {
329329
return (req, res, next) => {
330330
if (!boundInstruments.has(req.path)) {
331-
const labelSet = meter.labels({ route: req.path });
332-
const boundCounter = requestCount.bind(labelSet);
331+
const labels = { route: req.path };
332+
const boundCounter = requestCount.bind(labels);
333333
boundInstruments.set(req.path, boundCounter);
334334
}
335335

getting-started/monitored-example/monitoring.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ const boundInstruments = new Map();
2828
module.exports.countAllRequests = () => {
2929
return (req, res, next) => {
3030
if (!boundInstruments.has(req.path)) {
31-
const labelSet = meter.labels({ route: req.path });
32-
const boundCounter = requestCount.bind(labelSet);
31+
const labels = { route: req.path };
32+
const boundCounter = requestCount.bind(labels);
3333
boundInstruments.set(req.path, boundCounter);
3434
}
3535

getting-started/ts-example/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ const handles = new Map();
261261
export const countAllRequests = () => {
262262
return (req, res, next) => {
263263
if (!handles.has(req.path)) {
264-
const labelSet = meter.labels({ route: req.path });
265-
const handle = requestCount.bind(labelSet);
264+
const labels = { route: req.path };
265+
const handle = requestCount.bind(labels);
266266
handles.set(req.path, handle);
267267
}
268268
@@ -326,8 +326,8 @@ const handles = new Map();
326326
export const countAllRequests = () => {
327327
return (req, res, next) => {
328328
if (!handles.has(req.path)) {
329-
const labelSet = meter.labels({ route: req.path });
330-
const handle = requestCount.bind(labelSet);
329+
const labels = { route: req.path };
330+
const handle = requestCount.bind(labels);
331331
handles.set(req.path, handle);
332332
}
333333

getting-started/ts-example/monitoring.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ const handles = new Map();
2727
export const countAllRequests = () => {
2828
return (req, res, next) => {
2929
if (!handles.has(req.path)) {
30-
const labelSet = meter.labels({ route: req.path });
31-
const handle = requestCount.bind(labelSet);
30+
const labels = { route: req.path };
31+
const handle = requestCount.bind(labels);
3232
handles.set(req.path, handle);
3333
}
3434

packages/opentelemetry-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
}

packages/opentelemetry-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 };

packages/opentelemetry-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;

packages/opentelemetry-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
}

packages/opentelemetry-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',

packages/opentelemetry-exporter-prometheus/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ const meter = new MeterProvider({
3636

3737
// Now, start recording data
3838
const counter = meter.createCounter('metric_name');
39-
counter.add(10, meter.labels({ [key]: 'value' }));
39+
counter.add(10, { [key]: 'value' });
4040

4141
// Record data using Instrument: It is recommended to keep a reference to the Bound Instrument instead of
4242
// always calling `bind()` method for every operations.
43-
const boundCounter = counter.bind(meter.labels({ [key]: 'value' }));
43+
const boundCounter = counter.bind({ [key]: 'value' });
4444
boundCounter.add(10);
4545

4646
// .. some other work

packages/opentelemetry-exporter-prometheus/src/prometheus.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,8 @@ export class PrometheusExporter implements MetricExporter {
155155
// TODO: only counter and gauge are implemented in metrics so far
156156
}
157157

158-
private _getLabelValues(keys: string[], values: types.LabelSet) {
158+
private _getLabelValues(keys: string[], labels: types.Labels) {
159159
const labelValues: labelValues = {};
160-
const labels = values.labels;
161160
for (let i = 0; i < keys.length; i++) {
162161
if (labels[keys[i]] !== null) {
163162
labelValues[keys[i]] = labels[keys[i]];

0 commit comments

Comments
 (0)