-
Notifications
You must be signed in to change notification settings - Fork 821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: updates ValueRecorder to allow negative values #1373
Changes from 1 commit
2347435
3aebc6f
2c04e1d
d7057dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,6 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
import { CorrelationContext } from '../correlation_context/CorrelationContext'; | ||
import { SpanContext } from '../trace/span_context'; | ||
import { | ||
BoundBaseObserver, | ||
BoundCounter, | ||
|
@@ -51,12 +49,6 @@ export interface MetricOptions { | |
*/ | ||
disabled?: boolean; | ||
|
||
/** | ||
* (Measure only, default true) Asserts that this metric will only accept | ||
* non-negative values (e.g. disk usage). | ||
*/ | ||
absolute?: boolean; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ValueRecorder was the only thing using |
||
|
||
/** | ||
* Indicates the type of the recorded value. | ||
* @default {@link ValueType.DOUBLE} | ||
|
@@ -148,19 +140,6 @@ export interface ValueRecorder extends UnboundMetric<BoundValueRecorder> { | |
* Records the given value to this value recorder. | ||
*/ | ||
record(value: number, labels?: Labels): void; | ||
|
||
record( | ||
value: number, | ||
labels: Labels, | ||
correlationContext: CorrelationContext | ||
): void; | ||
|
||
record( | ||
value: number, | ||
labels: Labels, | ||
correlationContext: CorrelationContext, | ||
spanContext: SpanContext | ||
): void; | ||
} | ||
|
||
/** Base interface for the Observer metrics. */ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,42 +115,26 @@ export class BoundUpDownCounter extends BaseBoundInstrument | |
*/ | ||
export class BoundValueRecorder extends BaseBoundInstrument | ||
implements api.BoundValueRecorder { | ||
private readonly _absolute: boolean; | ||
|
||
constructor( | ||
labels: api.Labels, | ||
disabled: boolean, | ||
absolute: boolean, | ||
valueType: api.ValueType, | ||
logger: api.Logger, | ||
aggregator: Aggregator | ||
) { | ||
super(labels, logger, disabled, valueType, aggregator); | ||
this._absolute = absolute; | ||
} | ||
|
||
record( | ||
value: number, | ||
correlationContext?: api.CorrelationContext, | ||
spanContext?: api.SpanContext | ||
): void { | ||
if (this._absolute && value < 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the critical crux of the change. |
||
this._logger.error( | ||
`Absolute ValueRecorder cannot contain negative values for $${Object.values( | ||
this._labels | ||
)}` | ||
); | ||
return; | ||
} | ||
|
||
record(value: number): void { | ||
this.update(value); | ||
} | ||
} | ||
|
||
/** | ||
* BoundObserver is an implementation of the {@link BoundObserver} interface. | ||
*/ | ||
export class BoundObserver extends BaseBoundInstrument { | ||
export class BoundObserver extends BaseBoundInstrument | ||
implements api.BoundBaseObserver { | ||
constructor( | ||
labels: api.Labels, | ||
disabled: boolean, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -476,31 +476,6 @@ describe('Meter', () => { | |
assert.ok(valueRecorder instanceof Metric); | ||
}); | ||
|
||
it('should be absolute by default', () => { | ||
const valueRecorder = meter.createValueRecorder('name', { | ||
description: 'desc', | ||
unit: '1', | ||
disabled: false, | ||
}); | ||
assert.strictEqual( | ||
(valueRecorder as ValueRecorderMetric)['_absolute'], | ||
true | ||
); | ||
}); | ||
|
||
it('should be able to set absolute to false', () => { | ||
const valueRecorder = meter.createValueRecorder('name', { | ||
description: 'desc', | ||
unit: '1', | ||
disabled: false, | ||
absolute: false, | ||
}); | ||
assert.strictEqual( | ||
(valueRecorder as ValueRecorderMetric)['_absolute'], | ||
false | ||
); | ||
}); | ||
|
||
it('should pipe through resource', async () => { | ||
const valueRecorder = meter.createValueRecorder( | ||
'name' | ||
|
@@ -559,10 +534,12 @@ describe('Meter', () => { | |
assert.doesNotThrow(() => boundValueRecorder.record(10)); | ||
}); | ||
|
||
it('should not accept negative values by default', async () => { | ||
const valueRecorder = meter.createValueRecorder('name'); | ||
it('should not set the instrument data when disabled', async () => { | ||
const valueRecorder = meter.createValueRecorder('name', { | ||
disabled: true, | ||
}) as ValueRecorderMetric; | ||
const boundValueRecorder = valueRecorder.bind(labels); | ||
boundValueRecorder.record(-10); | ||
boundValueRecorder.record(10); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This diff makes things confusing but that test case was not touched, its just a shifting of code. |
||
|
||
await meter.collect(); | ||
const [record1] = meter.getBatcher().checkPointSet(); | ||
|
@@ -578,57 +555,30 @@ describe('Meter', () => { | |
); | ||
}); | ||
|
||
it('should not set the instrument data when disabled', async () => { | ||
const valueRecorder = meter.createValueRecorder('name', { | ||
disabled: true, | ||
}) as ValueRecorderMetric; | ||
it('should accept negative (and positive) values', async () => { | ||
const valueRecorder = meter.createValueRecorder('name'); | ||
const boundValueRecorder = valueRecorder.bind(labels); | ||
boundValueRecorder.record(10); | ||
boundValueRecorder.record(-10); | ||
boundValueRecorder.record(50); | ||
|
||
await meter.collect(); | ||
const [record1] = meter.getBatcher().checkPointSet(); | ||
assert.deepStrictEqual( | ||
record1.aggregator.toPoint().value as Distribution, | ||
{ | ||
count: 0, | ||
last: 0, | ||
max: -Infinity, | ||
min: Infinity, | ||
sum: 0, | ||
count: 2, | ||
last: 50, | ||
max: 50, | ||
min: -10, | ||
sum: 40, | ||
} | ||
); | ||
assert.ok( | ||
hrTimeToNanoseconds(record1.aggregator.toPoint().timestamp) > | ||
hrTimeToNanoseconds(performanceTimeOrigin) | ||
); | ||
}); | ||
|
||
it( | ||
'should accept negative (and positive) values when absolute is set' + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test case was modified to not pass any value for 'absolute' and then use existing assertions to assert both positive and negative values are accepted. |
||
' to false', | ||
async () => { | ||
const valueRecorder = meter.createValueRecorder('name', { | ||
absolute: false, | ||
}); | ||
const boundValueRecorder = valueRecorder.bind(labels); | ||
boundValueRecorder.record(-10); | ||
boundValueRecorder.record(50); | ||
|
||
await meter.collect(); | ||
const [record1] = meter.getBatcher().checkPointSet(); | ||
assert.deepStrictEqual( | ||
record1.aggregator.toPoint().value as Distribution, | ||
{ | ||
count: 2, | ||
last: 50, | ||
max: 50, | ||
min: -10, | ||
sum: 40, | ||
} | ||
); | ||
assert.ok( | ||
hrTimeToNanoseconds(record1.aggregator.toPoint().timestamp) > | ||
hrTimeToNanoseconds(performanceTimeOrigin) | ||
); | ||
} | ||
); | ||
|
||
it('should return same instrument on same label values', async () => { | ||
const valueRecorder = meter.createValueRecorder( | ||
'name' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These signatures appeared completely unused. When get to the
BoundValueRecorder
they were just ignored, so I removed them. Have an open question in Gitter but figured I'd start with removing and can always add back if necessary.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe these signatures came from the spec, but the spec on what to do with the correlations/span context was never finished.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, interesting. Feels like these should be left out until that is known but obviously I am new to the project and may not understand all the impacts.
I couldn't find any current usages. May be some usages in the wild outside of these repos (js/contrib) but we are also < 1.0 so figured was OK to have a breaking change to clean-up at this point.