Skip to content

Commit cb6555a

Browse files
authored
fix: updates ValueRecorder to allow negative values (#1373)
1 parent b88b95b commit cb6555a

File tree

8 files changed

+25
-141
lines changed

8 files changed

+25
-141
lines changed

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

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

17-
import { CorrelationContext } from '../correlation_context/CorrelationContext';
18-
import { SpanContext } from '../trace/span_context';
19-
2017
/** An Instrument for Counter Metric. */
2118
export interface BoundCounter {
2219
/**
@@ -31,18 +28,8 @@ export interface BoundValueRecorder {
3128
/**
3229
* Records the given value to this value recorder.
3330
* @param value to record.
34-
* @param correlationContext the correlationContext associated with the
35-
* values.
36-
* @param spanContext the {@link SpanContext} that identifies the {@link Span}
37-
* which the values are associated with.
3831
*/
3932
record(value: number): void;
40-
record(value: number, correlationContext: CorrelationContext): void;
41-
record(
42-
value: number,
43-
correlationContext: CorrelationContext,
44-
spanContext: SpanContext
45-
): void;
4633
}
4734

4835
/** An Instrument for Base Observer */

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

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

17-
import { CorrelationContext } from '../correlation_context/CorrelationContext';
18-
import { SpanContext } from '../trace/span_context';
1917
import {
2018
BoundBaseObserver,
2119
BoundCounter,
@@ -51,12 +49,6 @@ export interface MetricOptions {
5149
*/
5250
disabled?: boolean;
5351

54-
/**
55-
* (Measure only, default true) Asserts that this metric will only accept
56-
* non-negative values (e.g. disk usage).
57-
*/
58-
absolute?: boolean;
59-
6052
/**
6153
* Indicates the type of the recorded value.
6254
* @default {@link ValueType.DOUBLE}
@@ -148,19 +140,6 @@ export interface ValueRecorder extends UnboundMetric<BoundValueRecorder> {
148140
* Records the given value to this value recorder.
149141
*/
150142
record(value: number, labels?: Labels): void;
151-
152-
record(
153-
value: number,
154-
labels: Labels,
155-
correlationContext: CorrelationContext
156-
): void;
157-
158-
record(
159-
value: number,
160-
labels: Labels,
161-
correlationContext: CorrelationContext,
162-
spanContext: SpanContext
163-
): void;
164143
}
165144

166145
/** Base interface for the Observer metrics. */

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

+2-13
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,8 @@ export class NoopCounterMetric
142142
export class NoopValueRecorderMetric
143143
extends NoopMetric<BoundValueRecorder>
144144
implements ValueRecorder {
145-
record(
146-
value: number,
147-
labels: Labels,
148-
correlationContext?: CorrelationContext,
149-
spanContext?: SpanContext
150-
) {
151-
if (typeof correlationContext === 'undefined') {
152-
this.bind(labels).record(value);
153-
} else if (typeof spanContext === 'undefined') {
154-
this.bind(labels).record(value, correlationContext);
155-
} else {
156-
this.bind(labels).record(value, correlationContext, spanContext);
157-
}
145+
record(value: number, labels: Labels) {
146+
this.bind(labels).record(value);
158147
}
159148
}
160149

packages/opentelemetry-metrics/src/BoundInstrument.ts

+4-19
Original file line numberDiff line numberDiff line change
@@ -126,42 +126,27 @@ export class BoundUpDownCounter
126126
export class BoundValueRecorder
127127
extends BaseBoundInstrument
128128
implements api.BoundValueRecorder {
129-
private readonly _absolute: boolean;
130-
131129
constructor(
132130
labels: api.Labels,
133131
disabled: boolean,
134-
absolute: boolean,
135132
valueType: api.ValueType,
136133
logger: api.Logger,
137134
aggregator: Aggregator
138135
) {
139136
super(labels, logger, disabled, valueType, aggregator);
140-
this._absolute = absolute;
141137
}
142138

143-
record(
144-
value: number,
145-
correlationContext?: api.CorrelationContext,
146-
spanContext?: api.SpanContext
147-
): void {
148-
if (this._absolute && value < 0) {
149-
this._logger.error(
150-
`Absolute ValueRecorder cannot contain negative values for $${Object.values(
151-
this._labels
152-
)}`
153-
);
154-
return;
155-
}
156-
139+
record(value: number): void {
157140
this.update(value);
158141
}
159142
}
160143

161144
/**
162145
* BoundObserver is an implementation of the {@link BoundObserver} interface.
163146
*/
164-
export class BoundObserver extends BaseBoundInstrument {
147+
export class BoundObserver
148+
extends BaseBoundInstrument
149+
implements api.BoundBaseObserver {
165150
constructor(
166151
labels: api.Labels,
167152
disabled: boolean,

packages/opentelemetry-metrics/src/Meter.ts

-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ export class Meter implements api.Meter {
7777
const opt: api.MetricOptions = {
7878
logger: this._logger,
7979
...DEFAULT_METRIC_OPTIONS,
80-
absolute: true, // value recorders are defined as absolute by default
8180
...options,
8281
};
8382

packages/opentelemetry-metrics/src/ValueRecorderMetric.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ import { Metric } from './Metric';
2626
export class ValueRecorderMetric
2727
extends Metric<BoundValueRecorder>
2828
implements api.ValueRecorder {
29-
protected readonly _absolute: boolean;
30-
3129
constructor(
3230
name: string,
3331
options: api.MetricOptions,
@@ -42,14 +40,12 @@ export class ValueRecorderMetric
4240
resource,
4341
instrumentationLibrary
4442
);
45-
46-
this._absolute = options.absolute !== undefined ? options.absolute : true; // Absolute default is true
4743
}
44+
4845
protected _makeInstrument(labels: api.Labels): BoundValueRecorder {
4946
return new BoundValueRecorder(
5047
labels,
5148
this._disabled,
52-
this._absolute,
5349
this._valueType,
5450
this._logger,
5551
this._batcher.aggregatorFor(this._descriptor)

packages/opentelemetry-metrics/src/types.ts

-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ export const DEFAULT_CONFIG = {
5353
/** The default metric creation options value. */
5454
export const DEFAULT_METRIC_OPTIONS = {
5555
disabled: false,
56-
absolute: false,
5756
description: '',
5857
unit: '1',
5958
valueType: api.ValueType.DOUBLE,

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

+18-68
Original file line numberDiff line numberDiff line change
@@ -555,31 +555,6 @@ describe('Meter', () => {
555555
assert.ok(valueRecorder instanceof Metric);
556556
});
557557

558-
it('should be absolute by default', () => {
559-
const valueRecorder = meter.createValueRecorder('name', {
560-
description: 'desc',
561-
unit: '1',
562-
disabled: false,
563-
});
564-
assert.strictEqual(
565-
(valueRecorder as ValueRecorderMetric)['_absolute'],
566-
true
567-
);
568-
});
569-
570-
it('should be able to set absolute to false', () => {
571-
const valueRecorder = meter.createValueRecorder('name', {
572-
description: 'desc',
573-
unit: '1',
574-
disabled: false,
575-
absolute: false,
576-
});
577-
assert.strictEqual(
578-
(valueRecorder as ValueRecorderMetric)['_absolute'],
579-
false
580-
);
581-
});
582-
583558
it('should pipe through resource', async () => {
584559
const valueRecorder = meter.createValueRecorder(
585560
'name'
@@ -638,10 +613,12 @@ describe('Meter', () => {
638613
assert.doesNotThrow(() => boundValueRecorder.record(10));
639614
});
640615

641-
it('should not accept negative values by default', async () => {
642-
const valueRecorder = meter.createValueRecorder('name');
616+
it('should not set the instrument data when disabled', async () => {
617+
const valueRecorder = meter.createValueRecorder('name', {
618+
disabled: true,
619+
}) as ValueRecorderMetric;
643620
const boundValueRecorder = valueRecorder.bind(labels);
644-
boundValueRecorder.record(-10);
621+
boundValueRecorder.record(10);
645622

646623
await meter.collect();
647624
const [record1] = meter.getBatcher().checkPointSet();
@@ -657,57 +634,30 @@ describe('Meter', () => {
657634
);
658635
});
659636

660-
it('should not set the instrument data when disabled', async () => {
661-
const valueRecorder = meter.createValueRecorder('name', {
662-
disabled: true,
663-
}) as ValueRecorderMetric;
637+
it('should accept negative (and positive) values', async () => {
638+
const valueRecorder = meter.createValueRecorder('name');
664639
const boundValueRecorder = valueRecorder.bind(labels);
665-
boundValueRecorder.record(10);
640+
boundValueRecorder.record(-10);
641+
boundValueRecorder.record(50);
666642

667643
await meter.collect();
668644
const [record1] = meter.getBatcher().checkPointSet();
669645
assert.deepStrictEqual(
670646
record1.aggregator.toPoint().value as Distribution,
671647
{
672-
count: 0,
673-
last: 0,
674-
max: -Infinity,
675-
min: Infinity,
676-
sum: 0,
648+
count: 2,
649+
last: 50,
650+
max: 50,
651+
min: -10,
652+
sum: 40,
677653
}
678654
);
655+
assert.ok(
656+
hrTimeToNanoseconds(record1.aggregator.toPoint().timestamp) >
657+
hrTimeToNanoseconds(performanceTimeOrigin)
658+
);
679659
});
680660

681-
it(
682-
'should accept negative (and positive) values when absolute is set' +
683-
' to false',
684-
async () => {
685-
const valueRecorder = meter.createValueRecorder('name', {
686-
absolute: false,
687-
});
688-
const boundValueRecorder = valueRecorder.bind(labels);
689-
boundValueRecorder.record(-10);
690-
boundValueRecorder.record(50);
691-
692-
await meter.collect();
693-
const [record1] = meter.getBatcher().checkPointSet();
694-
assert.deepStrictEqual(
695-
record1.aggregator.toPoint().value as Distribution,
696-
{
697-
count: 2,
698-
last: 50,
699-
max: 50,
700-
min: -10,
701-
sum: 40,
702-
}
703-
);
704-
assert.ok(
705-
hrTimeToNanoseconds(record1.aggregator.toPoint().timestamp) >
706-
hrTimeToNanoseconds(performanceTimeOrigin)
707-
);
708-
}
709-
);
710-
711661
it('should return same instrument on same label values', async () => {
712662
const valueRecorder = meter.createValueRecorder(
713663
'name'

0 commit comments

Comments
 (0)