Class AlertRule

java.lang.Object
org.acemq.rabbitmq.admin.alerts.AlertRule

public final class AlertRule extends Object
A condition worth waking somebody for, written once and used twice.

 AlertRule backlog = Alert.named("queue-backing-up")
         .on("rabbitmq_detailed_queue_messages_ready")
         .above(1000)
         .lasting(Duration.ofMinutes(5))
         .severity(Severity.WARNING)
         .because("a queue is deep and not draining")
         .build();

 List<AlertEvent> firing = backlog.evaluate(metrics);   // now, in this process
 String yaml = backlog.toPrometheusRule();              // and for Prometheus
 

Why both

The same rule evaluates in-process and renders as a Prometheus alerting rule. That is the point of the class: a deployment gate, a health endpoint and the on-call alert otherwise express the same intention in three places, in two languages, and drift apart — and the way you find out is an incident that nothing paged for because the threshold in Prometheus was changed a year ago and the health check was not.

What this is not

Not a replacement for Prometheus and Alertmanager. Evaluating here reads one scrape, in one process, with no history, no deduplication, no silencing and no routing. Alert.lasting(Duration) cannot be honoured by a single snapshot at all — it is carried into the generated rule, where a real time-series database can evaluate it, and evaluate(org.acemq.rabbitmq.admin.metrics.MetricsSnapshot) says so rather than pretending.

Use the in-process evaluation for a check with a clear answer now: is this deployment safe, is this broker healthy, should this CLI print red. Use the generated rule for the things that need to be true over time.

  • Method Details

    • evaluate

      public List<AlertEvent> evaluate(MetricsSnapshot metrics)
      Checks this rule against one scrape.
      Parameters:
      metrics - a snapshot
      Returns:
      one event per label combination that breached — so one per queue, not one for the broker. An empty list means nothing is firing
    • isFiring

      public boolean isFiring(MetricsSnapshot metrics)
      Parameters:
      metrics - a snapshot
      Returns:
      whether anything breached
    • toPrometheusRule

      public String toPrometheusRule()
      Renders this rule as Prometheus alerting-rule YAML.

      The half that evaluate(org.acemq.rabbitmq.admin.metrics.MetricsSnapshot) cannot do: for: needs history, and this is where Alert.lasting(Duration) becomes real.

      Returns:
      a YAML fragment for a groups[].rules[] list
    • toAlertName

      public String toAlertName()
      Returns:
      the rule's name in the form Prometheus conventionally uses
    • name

      public String name()
    • metric

      public String metric()
    • companionMetric

      public String companionMetric()
      Returns:
      the metric that must read zero alongside metric(), or null if there is none
    • severity

      public Severity severity()
    • because

      public String because()
      Returns:
      why this matters, in a sentence somebody woken at 3am can act on
    • conditionText

      public String conditionText()
      Returns:
      how the threshold reads, for a message
    • lasting

      public Duration lasting()
      Returns:
      how long it must hold. Zero means "now", and only the generated rule can honour it
    • toString

      public String toString()
      Overrides:
      toString in class Object