Skip to content

Commit 668c3aa

Browse files
xiao-lixmayurkale22
authored andcommitted
feat: direct calling of metric instruments (#507)
* feat: direct calling of metric instruments * fix: add interface in types->metric * fix: linting * fix: linting & docs * fix: add overloads for record in measure metric * fix: linting * fix: linting * fix: linting
1 parent 5792593 commit 668c3aa

File tree

5 files changed

+107
-18
lines changed

5 files changed

+107
-18
lines changed

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

+36-7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
Meter,
2222
Metric,
2323
MetricOptions,
24+
MetricUtils,
2425
MeasureHandle,
2526
SpanContext,
2627
LabelSet,
@@ -110,6 +111,38 @@ export class NoopMetric<T> implements Metric<T> {
110111
}
111112
}
112113

114+
export class NoopCounterMetric extends NoopMetric<CounterHandle>
115+
implements Pick<MetricUtils, 'add'> {
116+
add(value: number, labelSet: LabelSet) {
117+
this.getHandle(labelSet).add(value);
118+
}
119+
}
120+
121+
export class NoopGaugeMetric extends NoopMetric<GaugeHandle>
122+
implements Pick<MetricUtils, 'set'> {
123+
set(value: number, labelSet: LabelSet) {
124+
this.getHandle(labelSet).set(value);
125+
}
126+
}
127+
128+
export class NoopMeasureMetric extends NoopMetric<MeasureHandle>
129+
implements Pick<MetricUtils, 'record'> {
130+
record(
131+
value: number,
132+
labelSet: LabelSet,
133+
distContext?: DistributedContext,
134+
spanContext?: SpanContext
135+
) {
136+
if (typeof distContext === 'undefined') {
137+
this.getHandle(labelSet).record(value);
138+
} else if (typeof spanContext === 'undefined') {
139+
this.getHandle(labelSet).record(value, distContext);
140+
} else {
141+
this.getHandle(labelSet).record(value, distContext, spanContext);
142+
}
143+
}
144+
}
145+
113146
export class NoopCounterHandle implements CounterHandle {
114147
add(value: number): void {
115148
return;
@@ -133,16 +166,12 @@ export class NoopMeasureHandle implements MeasureHandle {
133166
}
134167

135168
export const NOOP_GAUGE_HANDLE = new NoopGaugeHandle();
136-
export const NOOP_GAUGE_METRIC = new NoopMetric<GaugeHandle>(NOOP_GAUGE_HANDLE);
169+
export const NOOP_GAUGE_METRIC = new NoopGaugeMetric(NOOP_GAUGE_HANDLE);
137170

138171
export const NOOP_COUNTER_HANDLE = new NoopCounterHandle();
139-
export const NOOP_COUNTER_METRIC = new NoopMetric<CounterHandle>(
140-
NOOP_COUNTER_HANDLE
141-
);
172+
export const NOOP_COUNTER_METRIC = new NoopCounterMetric(NOOP_COUNTER_HANDLE);
142173

143174
export const NOOP_MEASURE_HANDLE = new NoopMeasureHandle();
144-
export const NOOP_MEASURE_METRIC = new NoopMetric<MeasureHandle>(
145-
NOOP_MEASURE_HANDLE
146-
);
175+
export const NOOP_MEASURE_METRIC = new NoopMeasureMetric(NOOP_MEASURE_HANDLE);
147176

148177
export const NOOP_LABEL_SET = {} as LabelSet;

packages/opentelemetry-metrics/src/LabelSet.ts

-9
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,3 @@ export class LabelSet implements types.LabelSet {
2828
this.labels = labels;
2929
}
3030
}
31-
32-
/**
33-
* Type guard to remove nulls from arrays
34-
*
35-
* @param value value to be checked for null equality
36-
*/
37-
export function notNull<T>(value: T | null): value is T {
38-
return value !== null;
39-
}

packages/opentelemetry-metrics/src/Metric.ts

+22-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ export abstract class Metric<T extends BaseHandle> implements types.Metric<T> {
121121
}
122122

123123
/** This is a SDK implementation of Counter Metric. */
124-
export class CounterMetric extends Metric<CounterHandle> {
124+
export class CounterMetric extends Metric<CounterHandle>
125+
implements Pick<types.MetricUtils, 'add'> {
125126
constructor(
126127
name: string,
127128
options: MetricOptions,
@@ -145,10 +146,20 @@ export class CounterMetric extends Metric<CounterHandle> {
145146
this._onUpdate
146147
);
147148
}
149+
150+
/**
151+
* Adds the given value to the current value. Values cannot be negative.
152+
* @param value the value to add.
153+
* @param labelSet the canonicalized LabelSet used to associate with this metric's handle.
154+
*/
155+
add(value: number, labelSet: types.LabelSet) {
156+
this.getHandle(labelSet).add(value);
157+
}
148158
}
149159

150160
/** This is a SDK implementation of Gauge Metric. */
151-
export class GaugeMetric extends Metric<GaugeHandle> {
161+
export class GaugeMetric extends Metric<GaugeHandle>
162+
implements Pick<types.MetricUtils, 'set'> {
152163
constructor(
153164
name: string,
154165
options: MetricOptions,
@@ -172,4 +183,13 @@ export class GaugeMetric extends Metric<GaugeHandle> {
172183
this._onUpdate
173184
);
174185
}
186+
187+
/**
188+
* Sets the given value. Values can be negative.
189+
* @param value the new value.
190+
* @param labelSet the canonicalized LabelSet used to associate with this metric's handle.
191+
*/
192+
set(value: number, labelSet: types.LabelSet) {
193+
this.getHandle(labelSet).set(value);
194+
}
175195
}

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

+16
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ describe('Meter', () => {
7676
assert.ok(counter instanceof Metric);
7777
});
7878

79+
it('should be able to call add() directly on counter', () => {
80+
const counter = meter.createCounter('name') as CounterMetric;
81+
counter.add(10, labelSet);
82+
assert.strictEqual(counter.getHandle(labelSet)['_data'], 10);
83+
counter.add(10, labelSet);
84+
assert.strictEqual(counter.getHandle(labelSet)['_data'], 20);
85+
});
86+
7987
describe('.getHandle()', () => {
8088
it('should create a counter handle', () => {
8189
const counter = meter.createCounter('name') as CounterMetric;
@@ -229,6 +237,14 @@ describe('Meter', () => {
229237
assert.ok(gauge instanceof Metric);
230238
});
231239

240+
it('should be able to call set() directly on gauge', () => {
241+
const gauge = meter.createGauge('name') as GaugeMetric;
242+
gauge.set(10, labelSet);
243+
assert.strictEqual(gauge.getHandle(labelSet)['_data'], 10);
244+
gauge.set(250, labelSet);
245+
assert.strictEqual(gauge.getHandle(labelSet)['_data'], 250);
246+
});
247+
232248
describe('.getHandle()', () => {
233249
it('should create a gauge handle', () => {
234250
const gauge = meter.createGauge('name') as GaugeMetric;

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

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

17+
import { DistributedContext } from '../distributed_context/DistributedContext';
18+
import { SpanContext } from '../trace/span_context';
19+
1720
/**
1821
* Options needed for metric creation
1922
*/
@@ -99,6 +102,36 @@ export interface Metric<T> {
99102
setCallback(fn: () => void): void;
100103
}
101104

105+
export interface MetricUtils {
106+
/**
107+
* Adds the given value to the current value. Values cannot be negative.
108+
*/
109+
add(value: number, labelSet: LabelSet): void;
110+
111+
/**
112+
* Sets the given value. Values can be negative.
113+
*/
114+
set(value: number, labelSet: LabelSet): void;
115+
116+
/**
117+
* Records the given value to this measure.
118+
*/
119+
record(value: number, labelSet: LabelSet): void;
120+
121+
record(
122+
value: number,
123+
labelSet: LabelSet,
124+
distContext: DistributedContext
125+
): void;
126+
127+
record(
128+
value: number,
129+
labelSet: LabelSet,
130+
distContext: DistributedContext,
131+
spanContext: SpanContext
132+
): void;
133+
}
134+
102135
/**
103136
* key-value pairs passed by the user.
104137
*/

0 commit comments

Comments
 (0)