Skip to content

Commit ccff588

Browse files
committed
fix: observers should not expose bind/unbind method
1 parent 16ae2a7 commit ccff588

File tree

8 files changed

+57
-79
lines changed

8 files changed

+57
-79
lines changed

packages/opentelemetry-api/src/metrics/BoundInstrument.ts

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

1717
import { CorrelationContext } from '../correlation_context/CorrelationContext';
1818
import { SpanContext } from '../trace/span_context';
19-
import { ObserverResult } from './ObserverResult';
2019

2120
/** An Instrument for Counter Metric. */
2221
export interface BoundCounter {
@@ -45,14 +44,3 @@ export interface BoundMeasure {
4544
spanContext: SpanContext
4645
): void;
4746
}
48-
49-
/** Base interface for the Observer metrics. */
50-
export interface BoundObserver {
51-
/**
52-
* Sets callback for the observer. The callback is called once and then it
53-
* sets observers for values. The observers are called periodically to
54-
* retrieve the value.
55-
* @param callback
56-
*/
57-
setCallback(callback: (observerResult: ObserverResult) => void): void;
58-
}

packages/opentelemetry-api/src/metrics/Meter.ts

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

17-
import { Metric, MetricOptions } from './Metric';
18-
import { BoundCounter, BoundMeasure, BoundObserver } from './BoundInstrument';
17+
import { MetricOptions, Counter, Measure, Observer } from './Metric';
1918

2019
/**
2120
* An interface to allow the recording metrics.
@@ -30,7 +29,7 @@ export interface Meter {
3029
* @param name the name of the metric.
3130
* @param [options] the metric options.
3231
*/
33-
createMeasure(name: string, options?: MetricOptions): Metric<BoundMeasure>;
32+
createMeasure(name: string, options?: MetricOptions): Measure;
3433

3534
/**
3635
* Creates a new `Counter` metric. Generally, this kind of metric when the
@@ -39,12 +38,12 @@ export interface Meter {
3938
* @param name the name of the metric.
4039
* @param [options] the metric options.
4140
*/
42-
createCounter(name: string, options?: MetricOptions): Metric<BoundCounter>;
41+
createCounter(name: string, options?: MetricOptions): Counter;
4342

4443
/**
4544
* Creates a new `Observer` metric.
4645
* @param name the name of the metric.
4746
* @param [options] the metric options.
4847
*/
49-
createObserver(name: string, options?: MetricOptions): Metric<BoundObserver>;
48+
createObserver(name: string, options?: MetricOptions): Observer;
5049
}

packages/opentelemetry-api/src/metrics/Metric.ts

+27-19
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import { CorrelationContext } from '../correlation_context/CorrelationContext';
1818
import { SpanContext } from '../trace/span_context';
1919
import { ObserverResult } from './ObserverResult';
20+
import { BoundCounter, BoundMeasure } from './BoundInstrument';
2021

2122
/**
2223
* Options needed for metric creation
@@ -77,7 +78,18 @@ export enum ValueType {
7778
* Metric represents a base class for different types of metric
7879
* pre aggregations.
7980
*/
80-
export interface Metric<T> {
81+
export interface Metric {
82+
/**
83+
* Clears all bound instruments from the Metric.
84+
*/
85+
clear(): void;
86+
}
87+
88+
/**
89+
* UnboundMetric represents a base class for different types of metric
90+
* pre aggregations without label value bound yet.
91+
*/
92+
export interface UnboundMetric<T> extends Metric {
8193
/**
8294
* Returns a Instrument associated with specified Labels.
8395
* It is recommended to keep a reference to the Instrument instead of always
@@ -92,31 +104,16 @@ export interface Metric<T> {
92104
* @param labels key-values pairs that are associated with a specific metric.
93105
*/
94106
unbind(labels: Labels): void;
95-
96-
/**
97-
* Clears all timeseries from the Metric.
98-
*/
99-
clear(): void;
100107
}
101108

102-
export interface MetricUtils {
109+
export interface Counter extends UnboundMetric<BoundCounter> {
103110
/**
104111
* Adds the given value to the current value. Values cannot be negative.
105112
*/
106113
add(value: number, labels: Labels): void;
114+
}
107115

108-
/**
109-
* Sets a callback where user can observe value for certain labels
110-
* @param callback a function that will be called once to set observers
111-
* for values
112-
*/
113-
setCallback(callback: (observerResult: ObserverResult) => void): void;
114-
115-
/**
116-
* Sets the given value. Values can be negative.
117-
*/
118-
set(value: number, labels: Labels): void;
119-
116+
export interface Measure extends UnboundMetric<BoundMeasure> {
120117
/**
121118
* Records the given value to this measure.
122119
*/
@@ -136,6 +133,17 @@ export interface MetricUtils {
136133
): void;
137134
}
138135

136+
/** Base interface for the Observer metrics. */
137+
export interface Observer extends Metric {
138+
/**
139+
* Sets a callback where user can observe value for certain labels. The
140+
* observers are called periodically to retrieve the value.
141+
* @param callback a function that will be called once to set observers
142+
* for values
143+
*/
144+
setCallback(callback: (observerResult: ObserverResult) => void): void;
145+
}
146+
139147
/**
140148
* key-value pairs passed by the user.
141149
*/

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

+17-16
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,15 @@
1515
*/
1616

1717
import { Meter } from './Meter';
18-
import { MetricOptions, Metric, Labels, MetricUtils } from './Metric';
19-
import { BoundMeasure, BoundCounter, BoundObserver } from './BoundInstrument';
18+
import {
19+
MetricOptions,
20+
UnboundMetric,
21+
Labels,
22+
Counter,
23+
Measure,
24+
Observer,
25+
} from './Metric';
26+
import { BoundMeasure, BoundCounter } from './BoundInstrument';
2027
import { CorrelationContext } from '../correlation_context/CorrelationContext';
2128
import { SpanContext } from '../trace/span_context';
2229
import { ObserverResult } from './ObserverResult';
@@ -33,7 +40,7 @@ export class NoopMeter implements Meter {
3340
* @param name the name of the metric.
3441
* @param [options] the metric options.
3542
*/
36-
createMeasure(name: string, options?: MetricOptions): Metric<BoundMeasure> {
43+
createMeasure(name: string, options?: MetricOptions): Measure {
3744
return NOOP_MEASURE_METRIC;
3845
}
3946

@@ -42,7 +49,7 @@ export class NoopMeter implements Meter {
4249
* @param name the name of the metric.
4350
* @param [options] the metric options.
4451
*/
45-
createCounter(name: string, options?: MetricOptions): Metric<BoundCounter> {
52+
createCounter(name: string, options?: MetricOptions): Counter {
4653
return NOOP_COUNTER_METRIC;
4754
}
4855

@@ -51,12 +58,12 @@ export class NoopMeter implements Meter {
5158
* @param name the name of the metric.
5259
* @param [options] the metric options.
5360
*/
54-
createObserver(name: string, options?: MetricOptions): Metric<BoundObserver> {
61+
createObserver(name: string, options?: MetricOptions): Observer {
5562
return NOOP_OBSERVER_METRIC;
5663
}
5764
}
5865

59-
export class NoopMetric<T> implements Metric<T> {
66+
export class NoopMetric<T> implements UnboundMetric<T> {
6067
private readonly _instrument: T;
6168

6269
constructor(instrument: T) {
@@ -90,14 +97,14 @@ export class NoopMetric<T> implements Metric<T> {
9097
}
9198

9299
export class NoopCounterMetric extends NoopMetric<BoundCounter>
93-
implements Pick<MetricUtils, 'add'> {
100+
implements Counter {
94101
add(value: number, labels: Labels) {
95102
this.bind(labels).add(value);
96103
}
97104
}
98105

99106
export class NoopMeasureMetric extends NoopMetric<BoundMeasure>
100-
implements Pick<MetricUtils, 'record'> {
107+
implements Measure {
101108
record(
102109
value: number,
103110
labels: Labels,
@@ -114,8 +121,7 @@ export class NoopMeasureMetric extends NoopMetric<BoundMeasure>
114121
}
115122
}
116123

117-
export class NoopObserverMetric extends NoopMetric<BoundObserver>
118-
implements Pick<MetricUtils, 'setCallback'> {
124+
export class NoopObserverMetric extends NoopMetric<void> implements Observer {
119125
setCallback(callback: (observerResult: ObserverResult) => void): void {}
120126
}
121127

@@ -135,16 +141,11 @@ export class NoopBoundMeasure implements BoundMeasure {
135141
}
136142
}
137143

138-
export class NoopBoundObserver implements BoundObserver {
139-
setCallback(callback: (observerResult: ObserverResult) => void): void {}
140-
}
141-
142144
export const NOOP_METER = new NoopMeter();
143145
export const NOOP_BOUND_COUNTER = new NoopBoundCounter();
144146
export const NOOP_COUNTER_METRIC = new NoopCounterMetric(NOOP_BOUND_COUNTER);
145147

146148
export const NOOP_BOUND_MEASURE = new NoopBoundMeasure();
147149
export const NOOP_MEASURE_METRIC = new NoopMeasureMetric(NOOP_BOUND_MEASURE);
148150

149-
export const NOOP_BOUND_OBSERVER = new NoopBoundObserver();
150-
export const NOOP_OBSERVER_METRIC = new NoopObserverMetric(NOOP_BOUND_OBSERVER);
151+
export const NOOP_OBSERVER_METRIC = new NoopObserverMetric();

packages/opentelemetry-metrics/src/BoundInstrument.ts

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

1717
import * as types from '@opentelemetry/api';
1818
import { Aggregator } from './export/types';
19-
import { ObserverResult } from './ObserverResult';
2019

2120
/**
2221
* This class represent the base to BoundInstrument, which is responsible for generating
@@ -134,8 +133,7 @@ export class BoundMeasure extends BaseBoundInstrument
134133
/**
135134
* BoundObserver is an implementation of the {@link BoundObserver} interface.
136135
*/
137-
export class BoundObserver extends BaseBoundInstrument
138-
implements types.BoundObserver {
136+
export class BoundObserver extends BaseBoundInstrument {
139137
constructor(
140138
labels: types.Labels,
141139
disabled: boolean,
@@ -146,9 +144,4 @@ export class BoundObserver extends BaseBoundInstrument
146144
) {
147145
super(labels, logger, monotonic, disabled, valueType, aggregator);
148146
}
149-
150-
setCallback(callback: (observerResult: types.ObserverResult) => void): void {
151-
const observerResult = new ObserverResult();
152-
callback(observerResult);
153-
}
154147
}

packages/opentelemetry-metrics/src/Meter.ts

+3-12
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ export class Meter implements types.Meter {
5656
* @param name the name of the metric.
5757
* @param [options] the metric options.
5858
*/
59-
createMeasure(
60-
name: string,
61-
options?: types.MetricOptions
62-
): types.Metric<types.BoundMeasure> {
59+
createMeasure(name: string, options?: types.MetricOptions): types.Measure {
6360
if (!this._isValidName(name)) {
6461
this._logger.warn(
6562
`Invalid metric name ${name}. Defaulting to noop metric implementation.`
@@ -86,10 +83,7 @@ export class Meter implements types.Meter {
8683
* @param name the name of the metric.
8784
* @param [options] the metric options.
8885
*/
89-
createCounter(
90-
name: string,
91-
options?: types.MetricOptions
92-
): types.Metric<types.BoundCounter> {
86+
createCounter(name: string, options?: types.MetricOptions): types.Counter {
9387
if (!this._isValidName(name)) {
9488
this._logger.warn(
9589
`Invalid metric name ${name}. Defaulting to noop metric implementation.`
@@ -113,10 +107,7 @@ export class Meter implements types.Meter {
113107
* @param name the name of the metric.
114108
* @param [options] the metric options.
115109
*/
116-
createObserver(
117-
name: string,
118-
options?: types.MetricOptions
119-
): types.Metric<types.BoundObserver> {
110+
createObserver(name: string, options?: types.MetricOptions): types.Observer {
120111
if (!this._isValidName(name)) {
121112
this._logger.warn(
122113
`Invalid metric name ${name}. Defaulting to noop metric implementation.`

packages/opentelemetry-metrics/src/Metric.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { hashLabels } from './Utils';
3030

3131
/** This is a SDK implementation of {@link Metric} interface. */
3232
export abstract class Metric<T extends BaseBoundInstrument>
33-
implements api.Metric<T> {
33+
implements api.UnboundMetric<T> {
3434
protected readonly _monotonic: boolean;
3535
protected readonly _disabled: boolean;
3636
protected readonly _valueType: api.ValueType;
@@ -106,8 +106,7 @@ export abstract class Metric<T extends BaseBoundInstrument>
106106
}
107107

108108
/** This is a SDK implementation of Counter Metric. */
109-
export class CounterMetric extends Metric<BoundCounter>
110-
implements Pick<api.MetricUtils, 'add'> {
109+
export class CounterMetric extends Metric<BoundCounter> implements api.Counter {
111110
constructor(
112111
name: string,
113112
options: MetricOptions,
@@ -139,8 +138,7 @@ export class CounterMetric extends Metric<BoundCounter>
139138
}
140139
}
141140

142-
export class MeasureMetric extends Metric<BoundMeasure>
143-
implements Pick<api.MetricUtils, 'record'> {
141+
export class MeasureMetric extends Metric<BoundMeasure> implements api.Measure {
144142
protected readonly _absolute: boolean;
145143

146144
constructor(
@@ -172,7 +170,7 @@ export class MeasureMetric extends Metric<BoundMeasure>
172170

173171
/** This is a SDK implementation of Observer Metric. */
174172
export class ObserverMetric extends Metric<BoundObserver>
175-
implements Pick<api.MetricUtils, 'setCallback'> {
173+
implements api.Observer {
176174
private _observerResult = new ObserverResult();
177175

178176
constructor(

packages/opentelemetry-metrics/test/Batcher.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('Batcher', () => {
2424
let meter: Meter;
2525
let fooCounter: types.BoundCounter;
2626
let barCounter: types.BoundCounter;
27-
let counter: types.Metric<types.BoundCounter>;
27+
let counter: types.Counter;
2828
beforeEach(() => {
2929
meter = new MeterProvider({
3030
logger: new NoopLogger(),

0 commit comments

Comments
 (0)