Skip to content
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

Check for inconsistent gauge fields for summaries #36

Merged
merged 5 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ plugins {
}

group 'com.dynatrace.metric.util'
version = '2.0.1'
version = '2.1.0'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ private ValidationMessages() {}
static final String GAUGE_NAN_MESSAGE = "NaN values: min: %f, max %f, sum: %f, count: %d";
static final String GAUGE_MIN_GREATER_MAX_MESSAGE =
"max < min: min: %f, max: %f, sum: %f, count: %d";
static final String GAUGE_INCONSISTENT_FIELDS_MESSAGE =
"inconsistent gauge fields: min <= avg <= max doesn't hold: min: %f, max: %f, sum: %f, count: %d, avg: %f, tolerance: %f";

static final String VALUE_NAN_MESSAGE = "Metric value was NaN";
static final String VALUE_INFINITE_MESSAGE = "Metric value was infinite (%f)";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

/** Offers validation methods for metric-line values */
final class NumberValueValidator {
private static final double COMPARISON_ABSOLUTE_TOLERANCE = 0.000001D;

private NumberValueValidator() {}

Expand Down Expand Up @@ -59,9 +60,48 @@ static BooleanResultMessage isSummaryValid(double min, double max, double sum, l
String.format(
ValidationMessages.GAUGE_MIN_GREATER_MAX_MESSAGE, min, max, sum, count));
}

double avg = sum / count;
if (!(lessOrEqualWithAbsoluteTolerance(min, avg)
&& lessOrEqualWithAbsoluteTolerance(avg, max))) {
// in this case the min <= avg <= max does not hold
return BooleanResultMessage.newInvalid(
() ->
String.format(
ValidationMessages.GAUGE_INCONSISTENT_FIELDS_MESSAGE,
min,
max,
sum,
count,
avg,
COMPARISON_ABSOLUTE_TOLERANCE));
}

return BooleanResultMessage.newValid();
}

private static boolean lessOrEqualWithAbsoluteTolerance(double val1, double val2) {
// equals
if (Double.valueOf(val1).equals(Double.valueOf(val2))) {
return true;
}

// not equal, two cases:
// val1 < val2: should always evaluate to true
// val1 slightly < val2 -> val1 - val2 == negative | result <= tolerance -> true
// val1 much < val2 -> val1 - val2 == negative | result <= tolerance -> true
//
// val1 > val2: should only evaluate to true if
// - val1 is bigger than val2 but within the tolerance:
// val1 slightly > val2 -> val1 - val2 == small number | result <= tolerance -> true
// val1 much > val2 -> val1 - val2 == larger number | result > tolerance -> false
if ((val1 - val2) <= COMPARISON_ABSOLUTE_TOLERANCE) {
return true;
}

return false;
}

/**
* Checks if the value of a gauge or delta of a counter is not a number, or is +/- infinity.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,33 @@ void testValueValidator() {
assertFalse(NumberValueValidator.isValueValid(Double.POSITIVE_INFINITY).isValid());
}

@Test
void testInconsistentGaugeFields() {
// min > avg
assertFalse(NumberValueValidator.isSummaryValid(2.5, 3, 6, 3).isValid());
assertFalse(NumberValueValidator.isSummaryValid(5, 3, 3, 1).isValid());
assertFalse(NumberValueValidator.isSummaryValid(-2, -2, -5, 2).isValid());

// max < avg
assertFalse(NumberValueValidator.isSummaryValid(2, 2, 5, 3).isValid());
assertFalse(NumberValueValidator.isSummaryValid(10.5, 10.6, 21.3, 1).isValid());
assertFalse(NumberValueValidator.isSummaryValid(-5.3, -4.1, -5, 2).isValid());

// test tolerances (tolerance == 0.000001);
// avg == 2; min <= avg does not hold (but is within tolerance).
assertTrue(NumberValueValidator.isSummaryValid(2.0000001, 3, 4, 2).isValid());
// avg == -2; min <= avg does not hold (but is within tolerance)
assertTrue(NumberValueValidator.isSummaryValid(-1.999999, -1, -4, 2).isValid());

// avg == 2; max >= avg does not hold (but is within tolerance)
assertTrue(NumberValueValidator.isSummaryValid(1.5, 1.999999, 4, 2).isValid());
// avg == -2; max >= avg does not hold (but is within tolerance)
assertTrue(NumberValueValidator.isSummaryValid(-2.5, -2.0000001, -4, 2).isValid());
}

@Test
void testSummaryValidator() {
assertTrue(() -> NumberValueValidator.isSummaryValid(10.5, 10.6, 21.3, 1).isValid());
assertTrue(() -> NumberValueValidator.isSummaryValid(-1.3, 4.1, 5, 2).isValid());
assertTrue(() -> NumberValueValidator.isSummaryValid(-5.3, -4.1, -5, 2).isValid());

assertTrue(NumberValueValidator.isSummaryValid(0, 0, 0, 0).isValid());
assertFalse(NumberValueValidator.isSummaryValid(0, 0, 0, -1).isValid());
Expand Down
Loading