Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Commit

Permalink
Drops whole measurement list if some of value is negative (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
isaikevych committed Nov 29, 2018
1 parent 8fd0927 commit cb79b9c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
21 changes: 15 additions & 6 deletions packages/opencensus-core/src/stats/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,27 @@ export class Stats {
return {name, unit, type: MeasureType.INT64, description};
}

/**
* Verifies whether all measurements has positive value
* @param measurements A list of measurements
* @returns {boolean} Whether values is positive
*/
private hasNegativeValue(measurements: Measurement[]): boolean {
return measurements.some(measurement => measurement.value < 0);
}

/**
* Updates all views with the new measurements.
* @param measurements A list of measurements to record
*/
record(...measurements: Measurement[]) {
for (const measurement of measurements) {
if (measurement.value < 0) {
this.logger.warn(`Dropping value ${
measurement.value}, value to record must be non-negative.`);
break;
}
if (this.hasNegativeValue(measurements)) {
this.logger.warn(`Dropping measurments ${measurements}, value to record
must be non-negative.`);
return;
}

for (const measurement of measurements) {
const views = this.registeredViews[measurement.measure.name];
if (!views) {
break;
Expand Down
17 changes: 9 additions & 8 deletions packages/opencensus-core/test/test-stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,14 @@ describe('Stats', () => {
assert.strictEqual(aggregationData.value, measurement2.value);
});

it('should stop record on negative value of multiple measurement', () => {
const measurments = [
{measure, tags, value: 1}, {measure, tags, value: -1},
{measure, tags, value: 1}
];
stats.record(...measurments);
assert.equal(testExporter.recordedMeasurements.length, 1);
});
it('should skip whole multiple measurment if one of value is negative',
() => {
const measurments = [
{measure, tags, value: 1}, {measure, tags, value: -1},
{measure, tags, value: 1}
];
stats.record(...measurments);
assert.equal(testExporter.recordedMeasurements.length, 0);
});
});
});

0 comments on commit cb79b9c

Please sign in to comment.