public static Quota upperBound(double upperBound) { return new Quota(upperBound, true); }
@Test public void testQuotasEquality() { final Quota quota1 = Quota.upperBound(10.5); final Quota quota2 = Quota.lowerBound(10.5); assertFalse("Quota with different upper values shouldn't be equal", quota1.equals(quota2)); final Quota quota3 = Quota.lowerBound(10.5); assertTrue("Quota with same upper and bound values should be equal", quota2.equals(quota3)); }
public void checkQuotas(long timeMs) { for (KafkaMetric metric : this.metrics.values()) { MetricConfig config = metric.config(); if (config != null) { Quota quota = config.quota(); if (quota != null) { double value = metric.measurableValue(timeMs); if (!quota.acceptable(value)) { throw new QuotaViolationException(metric.metricName(), value, quota.bound()); } } } } }
@Test public void testQuotas() { Sensor sensor = metrics.sensor("test"); sensor.add(metrics.metricName("test1.total", "grp1"), new Total(), new MetricConfig().quota(Quota.upperBound(5.0))); sensor.add(metrics.metricName("test2.total", "grp1"), new Total(), new MetricConfig().quota(Quota.lowerBound(0.0))); sensor.record(5.0); try { sensor.record(1.0); fail("Should have gotten a quota violation."); } catch (QuotaViolationException e) { // this is good } assertEquals(6.0, (Double) metrics.metrics().get(metrics.metricName("test1.total", "grp1")).metricValue(), EPS); sensor.record(-6.0); try { sensor.record(-1.0); fail("Should have gotten a quota violation."); } catch (QuotaViolationException e) { // this is good } }
final Metrics metrics = new Metrics(new MetricConfig().quota(Quota.upperBound(Double.MAX_VALUE))
/** * Check if we have violated our quota for any metric that has a configured quota * @param timeMs */ private void checkQuotas(long timeMs) { for (int i = 0; i < this.metrics.size(); i++) { KafkaMetric metric = this.metrics.get(i); MetricConfig config = metric.config(); if (config != null) { Quota quota = config.quota(); if (quota != null) { if (!quota.acceptable(metric.value(timeMs))) throw new QuotaViolationException(metric.metricName() + " is in violation of its quota of " + quota.bound()); } } } }
public static Quota lowerBound(double lowerBound) { return new Quota(lowerBound, false); }
public static Quota lessThan(double upperBound) { return new Quota(upperBound, true); }
public static Quota moreThan(double lowerBound) { return new Quota(lowerBound, false); }