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

Commit cb79b9c

Browse files
authored
Drops whole measurement list if some of value is negative (#208)
1 parent 8fd0927 commit cb79b9c

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

packages/opencensus-core/src/stats/stats.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,27 @@ export class Stats {
114114
return {name, unit, type: MeasureType.INT64, description};
115115
}
116116

117+
/**
118+
* Verifies whether all measurements has positive value
119+
* @param measurements A list of measurements
120+
* @returns {boolean} Whether values is positive
121+
*/
122+
private hasNegativeValue(measurements: Measurement[]): boolean {
123+
return measurements.some(measurement => measurement.value < 0);
124+
}
125+
117126
/**
118127
* Updates all views with the new measurements.
119128
* @param measurements A list of measurements to record
120129
*/
121130
record(...measurements: Measurement[]) {
122-
for (const measurement of measurements) {
123-
if (measurement.value < 0) {
124-
this.logger.warn(`Dropping value ${
125-
measurement.value}, value to record must be non-negative.`);
126-
break;
127-
}
131+
if (this.hasNegativeValue(measurements)) {
132+
this.logger.warn(`Dropping measurments ${measurements}, value to record
133+
must be non-negative.`);
134+
return;
135+
}
128136

137+
for (const measurement of measurements) {
129138
const views = this.registeredViews[measurement.measure.name];
130139
if (!views) {
131140
break;

packages/opencensus-core/test/test-stats.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,14 @@ describe('Stats', () => {
176176
assert.strictEqual(aggregationData.value, measurement2.value);
177177
});
178178

179-
it('should stop record on negative value of multiple measurement', () => {
180-
const measurments = [
181-
{measure, tags, value: 1}, {measure, tags, value: -1},
182-
{measure, tags, value: 1}
183-
];
184-
stats.record(...measurments);
185-
assert.equal(testExporter.recordedMeasurements.length, 1);
186-
});
179+
it('should skip whole multiple measurment if one of value is negative',
180+
() => {
181+
const measurments = [
182+
{measure, tags, value: 1}, {measure, tags, value: -1},
183+
{measure, tags, value: 1}
184+
];
185+
stats.record(...measurments);
186+
assert.equal(testExporter.recordedMeasurements.length, 0);
187+
});
187188
});
188189
});

0 commit comments

Comments
 (0)