Skip to content

Commit 6f0084f

Browse files
chore: rename Metric Handle to Bound Instrument (#634) (#638)
* chore: fix renames in metrics test * chore: fix formatting * chore: rename BaseInstrument to BaseBoundInstrument Co-authored-by: Mayur Kale <[email protected]>
1 parent 53c40c1 commit 6f0084f

File tree

18 files changed

+288
-281
lines changed

18 files changed

+288
-281
lines changed

examples/prometheus/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ setInterval(() => {
4646

4747
currentMonotonicGaugeValue += Math.random();
4848

49-
monotonicCounter.getHandle(labels).add(1);
50-
nonMonotonicCounter.getHandle(labels).add(Math.random() > 0.5 ? 1 : -1);
51-
monotonicGauge.getHandle(labels).set(currentMonotonicGaugeValue);
49+
monotonicCounter.bind(labels).add(1);
50+
nonMonotonicCounter.bind(labels).add(Math.random() > 0.5 ? 1 : -1);
51+
monotonicGauge.bind(labels).set(currentMonotonicGaugeValue);
5252
nonMonotonicGauge
53-
.getHandle(labels)
53+
.bind(labels)
5454
.set(Math.random() > 0.5 ? Math.random() * 10 : -Math.random() * 10);
5555
}, 1000);

getting-started/README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -227,17 +227,17 @@ const requestCount = meter.createCounter("requests", {
227227
description: "Count all incoming requests"
228228
});
229229
230-
const handles = new Map();
230+
const boundInstruments = new Map();
231231
232232
module.exports.countAllRequests = () => {
233233
return (req, res, next) => {
234-
if (!handles.has(req.path)) {
234+
if (!boundInstruments.has(req.path)) {
235235
const labelSet = meter.labels({ route: req.path });
236-
const handle = requestCount.getHandle(labelSet);
237-
handles.set(req.path, handle);
236+
const boundCounter = requestCount.bind(labelSet);
237+
boundInstruments.set(req.path, boundCounter);
238238
}
239239
240-
handles.get(req.path).add(1);
240+
boundInstruments.get(req.path).add(1);
241241
next();
242242
};
243243
};
@@ -253,7 +253,7 @@ app.use(countAllRequests());
253253

254254
Now, when we make requests to our service our meter will count all requests.
255255

256-
**Note**: Creating a new `labelSet` and `handle` on every request is not ideal as creating the `labelSet` can often be an expensive operation. This is why handles are created and stored in a `Map` according to the route key.
256+
**Note**: Creating a new `labelSet` and `binding` on every request is not ideal as creating the `labelSet` can often be an expensive operation. This is why instruments are created and stored in a `Map` according to the route key.
257257

258258
#### Initialize and register a metrics exporter
259259
Counting metrics is only useful if we can export them somewhere that we can see them. For this, we're going to use prometheus. Creating and registering a metrics exporter is much like the tracing exporter above. First we will need to install the prometheus exporter.
@@ -287,17 +287,17 @@ const requestCount = meter.createCounter("requests", {
287287
description: "Count all incoming requests"
288288
});
289289
290-
const handles = new Map();
290+
const boundInstruments = new Map();
291291
292292
module.exports.countAllRequests = () => {
293293
return (req, res, next) => {
294-
if (!handles.has(req.path)) {
294+
if (!boundInstruments.has(req.path)) {
295295
const labelSet = meter.labels({ route: req.path });
296-
const handle = requestCount.getHandle(labelSet);
297-
handles.set(req.path, handle);
296+
const boundCounter = requestCount.bind(labelSet);
297+
boundInstruments.set(req.path, boundCounter);
298298
}
299299
300-
handles.get(req.path).add(1);
300+
boundInstruments.get(req.path).add(1);
301301
next();
302302
};
303303
};

getting-started/monitored-example/monitoring.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ const requestCount = meter.createCounter("requests", {
2222
description: "Count all incoming requests"
2323
});
2424

25-
const handles = new Map();
25+
const boundInstruments = new Map();
2626

2727
module.exports.countAllRequests = () => {
2828
return (req, res, next) => {
29-
if (!handles.has(req.path)) {
29+
if (!boundInstruments.has(req.path)) {
3030
const labelSet = meter.labels({ route: req.path });
31-
const handle = requestCount.getHandle(labelSet);
32-
handles.set(req.path, handle);
31+
const boundCounter = requestCount.bind(labelSet);
32+
boundInstruments.set(req.path, boundCounter);
3333
}
3434

35-
handles.get(req.path).add(1);
35+
boundInstruments.get(req.path).add(1);
3636
next();
3737
};
3838
};

packages/opentelemetry-core/src/metrics/NoopMeter.ts

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
*/
1616

1717
import {
18-
CounterHandle,
18+
BoundCounter,
1919
DistributedContext,
20-
GaugeHandle,
20+
BoundGauge,
2121
Meter,
2222
Metric,
2323
MetricOptions,
2424
MetricUtils,
25-
MeasureHandle,
25+
BoundMeasure,
2626
SpanContext,
2727
LabelSet,
2828
Labels,
@@ -40,7 +40,7 @@ export class NoopMeter implements Meter {
4040
* @param name the name of the metric.
4141
* @param [options] the metric options.
4242
*/
43-
createMeasure(name: string, options?: MetricOptions): Metric<MeasureHandle> {
43+
createMeasure(name: string, options?: MetricOptions): Metric<BoundMeasure> {
4444
return NOOP_MEASURE_METRIC;
4545
}
4646

@@ -49,7 +49,7 @@ export class NoopMeter implements Meter {
4949
* @param name the name of the metric.
5050
* @param [options] the metric options.
5151
*/
52-
createCounter(name: string, options?: MetricOptions): Metric<CounterHandle> {
52+
createCounter(name: string, options?: MetricOptions): Metric<BoundCounter> {
5353
return NOOP_COUNTER_METRIC;
5454
}
5555

@@ -58,7 +58,7 @@ export class NoopMeter implements Meter {
5858
* @param name the name of the metric.
5959
* @param [options] the metric options.
6060
*/
61-
createGauge(name: string, options?: MetricOptions): Metric<GaugeHandle> {
61+
createGauge(name: string, options?: MetricOptions): Metric<BoundGauge> {
6262
return NOOP_GAUGE_METRIC;
6363
}
6464

@@ -68,33 +68,33 @@ export class NoopMeter implements Meter {
6868
}
6969

7070
export class NoopMetric<T> implements Metric<T> {
71-
private readonly _handle: T;
71+
private readonly _instrument: T;
7272

73-
constructor(handle: T) {
74-
this._handle = handle;
73+
constructor(instrument: T) {
74+
this._instrument = instrument;
7575
}
7676
/**
77-
* Returns a Handle associated with specified LabelSet.
78-
* It is recommended to keep a reference to the Handle instead of always
77+
* Returns a Bound Instrument associated with specified LabelSet.
78+
* It is recommended to keep a reference to the Bound Instrument instead of always
7979
* calling this method for every operations.
80-
* @param labels the canonicalized LabelSet used to associate with this metric handle.
80+
* @param labels the canonicalized LabelSet used to associate with this metric instrument.
8181
*/
82-
getHandle(labels: LabelSet): T {
83-
return this._handle;
82+
bind(labels: LabelSet): T {
83+
return this._instrument;
8484
}
8585

8686
/**
87-
* Returns a Handle for a metric with all labels not set.
87+
* Returns a Bound Instrument for a metric with all labels not set.
8888
*/
89-
getDefaultHandle(): T {
90-
return this._handle;
89+
getDefaultBound(): T {
90+
return this._instrument;
9191
}
9292

9393
/**
94-
* Removes the Handle from the metric, if it is present.
95-
* @param labels the canonicalized LabelSet used to associate with this metric handle.
94+
* Removes the Binding from the metric, if it is present.
95+
* @param labels the canonicalized LabelSet used to associate with this metric instrument.
9696
*/
97-
removeHandle(labels: LabelSet): void {
97+
unbind(labels: LabelSet): void {
9898
// @todo: implement this method
9999
return;
100100
}
@@ -111,21 +111,21 @@ export class NoopMetric<T> implements Metric<T> {
111111
}
112112
}
113113

114-
export class NoopCounterMetric extends NoopMetric<CounterHandle>
114+
export class NoopCounterMetric extends NoopMetric<BoundCounter>
115115
implements Pick<MetricUtils, 'add'> {
116116
add(value: number, labelSet: LabelSet) {
117-
this.getHandle(labelSet).add(value);
117+
this.bind(labelSet).add(value);
118118
}
119119
}
120120

121-
export class NoopGaugeMetric extends NoopMetric<GaugeHandle>
121+
export class NoopGaugeMetric extends NoopMetric<BoundGauge>
122122
implements Pick<MetricUtils, 'set'> {
123123
set(value: number, labelSet: LabelSet) {
124-
this.getHandle(labelSet).set(value);
124+
this.bind(labelSet).set(value);
125125
}
126126
}
127127

128-
export class NoopMeasureMetric extends NoopMetric<MeasureHandle>
128+
export class NoopMeasureMetric extends NoopMetric<BoundMeasure>
129129
implements Pick<MetricUtils, 'record'> {
130130
record(
131131
value: number,
@@ -134,28 +134,28 @@ export class NoopMeasureMetric extends NoopMetric<MeasureHandle>
134134
spanContext?: SpanContext
135135
) {
136136
if (typeof distContext === 'undefined') {
137-
this.getHandle(labelSet).record(value);
137+
this.bind(labelSet).record(value);
138138
} else if (typeof spanContext === 'undefined') {
139-
this.getHandle(labelSet).record(value, distContext);
139+
this.bind(labelSet).record(value, distContext);
140140
} else {
141-
this.getHandle(labelSet).record(value, distContext, spanContext);
141+
this.bind(labelSet).record(value, distContext, spanContext);
142142
}
143143
}
144144
}
145145

146-
export class NoopCounterHandle implements CounterHandle {
146+
export class NoopBoundCounter implements BoundCounter {
147147
add(value: number): void {
148148
return;
149149
}
150150
}
151151

152-
export class NoopGaugeHandle implements GaugeHandle {
152+
export class NoopBoundGauge implements BoundGauge {
153153
set(value: number): void {
154154
return;
155155
}
156156
}
157157

158-
export class NoopMeasureHandle implements MeasureHandle {
158+
export class NoopBoundMeasure implements BoundMeasure {
159159
record(
160160
value: number,
161161
distContext?: DistributedContext,
@@ -165,13 +165,13 @@ export class NoopMeasureHandle implements MeasureHandle {
165165
}
166166
}
167167

168-
export const NOOP_GAUGE_HANDLE = new NoopGaugeHandle();
169-
export const NOOP_GAUGE_METRIC = new NoopGaugeMetric(NOOP_GAUGE_HANDLE);
168+
export const NOOP_BOUND_GAUGE = new NoopBoundGauge();
169+
export const NOOP_GAUGE_METRIC = new NoopGaugeMetric(NOOP_BOUND_GAUGE);
170170

171-
export const NOOP_COUNTER_HANDLE = new NoopCounterHandle();
172-
export const NOOP_COUNTER_METRIC = new NoopCounterMetric(NOOP_COUNTER_HANDLE);
171+
export const NOOP_BOUND_COUNTER = new NoopBoundCounter();
172+
export const NOOP_COUNTER_METRIC = new NoopCounterMetric(NOOP_BOUND_COUNTER);
173173

174-
export const NOOP_MEASURE_HANDLE = new NoopMeasureHandle();
175-
export const NOOP_MEASURE_METRIC = new NoopMeasureMetric(NOOP_MEASURE_HANDLE);
174+
export const NOOP_BOUND_MEASURE = new NoopBoundMeasure();
175+
export const NOOP_MEASURE_METRIC = new NoopMeasureMetric(NOOP_BOUND_MEASURE);
176176

177177
export const NOOP_LABEL_SET = {} as LabelSet;

packages/opentelemetry-core/test/metrics/NoopMeter.test.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
import * as assert from 'assert';
1818
import {
1919
NoopMeter,
20-
NOOP_GAUGE_HANDLE,
20+
NOOP_BOUND_GAUGE,
2121
NOOP_GAUGE_METRIC,
22-
NOOP_COUNTER_HANDLE,
22+
NOOP_BOUND_COUNTER,
2323
NOOP_COUNTER_METRIC,
24-
NOOP_MEASURE_HANDLE,
24+
NOOP_BOUND_MEASURE,
2525
NOOP_MEASURE_METRIC,
2626
} from '../../src/metrics/NoopMeter';
2727
import { Labels } from '@opentelemetry/types';
@@ -37,20 +37,20 @@ describe('NoopMeter', () => {
3737
counter.setCallback(() => {
3838
assert.fail('callback occurred');
3939
});
40-
counter.getHandle(labelSet).add(1);
41-
counter.getDefaultHandle().add(1);
42-
counter.removeHandle(labelSet);
40+
counter.bind(labelSet).add(1);
41+
counter.getDefaultBound().add(1);
42+
counter.unbind(labelSet);
4343

4444
// ensure the correct noop const is returned
4545
assert.strictEqual(counter, NOOP_COUNTER_METRIC);
46-
assert.strictEqual(counter.getHandle(labelSet), NOOP_COUNTER_HANDLE);
47-
assert.strictEqual(counter.getDefaultHandle(), NOOP_COUNTER_HANDLE);
46+
assert.strictEqual(counter.bind(labelSet), NOOP_BOUND_COUNTER);
47+
assert.strictEqual(counter.getDefaultBound(), NOOP_BOUND_COUNTER);
4848
counter.clear();
4949

5050
const measure = meter.createMeasure('some-name');
51-
measure.getDefaultHandle().record(1);
52-
measure.getDefaultHandle().record(1, { key: { value: 'value' } });
53-
measure.getDefaultHandle().record(
51+
measure.getDefaultBound().record(1);
52+
measure.getDefaultBound().record(1, { key: { value: 'value' } });
53+
measure.getDefaultBound().record(
5454
1,
5555
{ key: { value: 'value' } },
5656
{
@@ -61,16 +61,16 @@ describe('NoopMeter', () => {
6161

6262
// ensure the correct noop const is returned
6363
assert.strictEqual(measure, NOOP_MEASURE_METRIC);
64-
assert.strictEqual(measure.getDefaultHandle(), NOOP_MEASURE_HANDLE);
65-
assert.strictEqual(measure.getHandle(labelSet), NOOP_MEASURE_HANDLE);
64+
assert.strictEqual(measure.getDefaultBound(), NOOP_BOUND_MEASURE);
65+
assert.strictEqual(measure.bind(labelSet), NOOP_BOUND_MEASURE);
6666

6767
const gauge = meter.createGauge('some-name');
68-
gauge.getDefaultHandle().set(1);
68+
gauge.getDefaultBound().set(1);
6969

7070
// ensure the correct noop const is returned
7171
assert.strictEqual(gauge, NOOP_GAUGE_METRIC);
72-
assert.strictEqual(gauge.getDefaultHandle(), NOOP_GAUGE_HANDLE);
73-
assert.strictEqual(gauge.getHandle(labelSet), NOOP_GAUGE_HANDLE);
72+
assert.strictEqual(gauge.getDefaultBound(), NOOP_BOUND_GAUGE);
73+
assert.strictEqual(gauge.bind(labelSet), NOOP_BOUND_GAUGE);
7474

7575
const options = {
7676
component: 'tests',

packages/opentelemetry-exporter-prometheus/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ meter.addExporter(exporter);
3636
const counter = meter.createCounter('metric_name');
3737
counter.add(10, meter.labels({ [key]: 'value' }));
3838

39-
// Record data using Handle: It is recommended to keep a reference to the Handle instead of
40-
// always calling `getHandle()` method for every operations.
41-
const handle = counter.getHandle(meter.labels({ [key]: 'value' }));
42-
handle.add(10);
39+
// Record data using Instrument: It is recommended to keep a reference to the Bound Instrument instead of
40+
// always calling `bind()` method for every operations.
41+
const boundCounter = counter.bind(meter.labels({ [key]: 'value' }));
42+
boundCounter.add(10);
4343

4444
// .. some other work
4545

0 commit comments

Comments
 (0)