Design an alerting engine with pluggable rule types and notification channels
Company: Datadog
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design an alerting engine that supports different rule types (Threshold, Change, Anomaly) and different notification channels (Slack, PagerDuty, Email).
The engine ingests metric data points over time and evaluates configured rules against the data. When a rule triggers, alerts are sent through all notification channels attached to that rule.
A Threshold rule triggers when the latest metric value exceeds a configured threshold. A Change rule triggers when the absolute difference between the two most recent data points exceeds a configured change amount. An Anomaly rule triggers when the latest value deviates from the running mean by more than a configured number of standard deviations (requires at least 2 data points).
The design should allow new rule types to be added without modifying the core engine logic.
Implement an `AlertingEngine` class with these methods:
- `add_rule(rule_id: str, rule_type: str, metric: str, config: str) -> bool` adds a new alerting rule with the given ID, type (Threshold, Change, or Anomaly), target metric, and JSON config string. Returns `True` if added, `False` if `rule_id` already exists.
- `remove_rule(rule_id: str) -> bool` removes a rule by its ID. Returns `True` if removed, `False` if not found.
- `add_channel(channel_id: str, channel_type: str) -> bool` registers a notification channel with the given ID and type (Slack, PagerDuty, or Email). Returns `True` if added, `False` if `channel_id` already exists.
- `attach_channel(rule_id: str, channel_id: str) -> bool` attaches a notification channel to a rule. Returns `True` if attached, `False` if the rule or channel is not found or the channel is already attached.
- `ingest(metric: str, timestamp: int, value: float) -> None` ingests a data point `(timestamp, value)` for the specified metric.
- `evaluate() -> List[str]` evaluates all rules against the current metric data. Returns a list of alert strings in the format `rule_id:channel_type` for each triggered rule-channel pair, or `rule_id:NO_CHANNEL` if a triggered rule has no channels attached.
- `get_alerts() -> List[str]` returns all historical triggered alert strings.
- `get_rule_count() -> int` returns the number of currently active rules.
- `get_metric_data_count(metric: str) -> int` returns the number of data points ingested for the given metric.
Example 1:
```text
Input:
operations = ["AlertingEngine","add_channel","add_channel","add_rule","attach_channel","attach_channel","ingest","ingest","ingest","evaluate","get_alerts"]
arguments =
[
[],
["ch1","Slack"],
["ch2","Email"],
["r1","Threshold","cpu","{\"threshold\": 90.0}"],
["r1","ch1"],
["r1","ch2"],
["cpu",1,85],
["cpu",2,92],
["cpu",3,95],
[],
[]
]
Output: [null,true,true,true,true,true,null,null,null,["r1:Slack","r1:Email"],["r1:Slack","r1:Email"]]
```
The engine gets a Slack channel, an Email channel and a Threshold rule on `cpu > 90` with both channels attached. After three data points the latest value, 95.0, exceeds 90.0, so `evaluate` sends alerts through both channels: `r1:Slack` and `r1:Email`.
Example 2:
```text
Input:
operations = ["AlertingEngine","add_rule","add_rule","add_channel","attach_channel","ingest","ingest","evaluate","ingest","ingest","evaluate","get_alerts","get_rule_count"]
arguments =
[
[],
["r1","Change","mem","{\"change\": 10.0}"],
["r2","Threshold","mem","{\"threshold\": 80.0}"],
["ch1","PagerDuty"],
["r1","ch1"],
["mem",1,50],
["mem",2,55],
[],
["mem",3,70],
["mem",4,85],
[],
[],
[]
]
Output: [null,true,true,true,true,null,null,[],null,null,["r1:PagerDuty","r2:NO_CHANNEL"],["r1:PagerDuty","r2:NO_CHANNEL"],2]
```
Two rules watch `mem`: Change (more than 10) and Threshold (more than 80). At the first `evaluate` the difference is 5 and the latest value is 55, so nothing triggers. After 70 and 85 are ingested, the difference is 15 (Change triggers) and the latest value is 85 (Threshold triggers). `r2` has no channel attached, so its alert is `r2:NO_CHANNEL`.
```hint Where the type-specific logic lives
Ask what the engine loop needs to know about a rule to decide whether it fired. If the rule object can answer that itself, the loop does not change when a new type appears.
```
```hint Guard cases
Before computing anything, check what each rule type needs: a metric with no data, a Change rule with a single point, and an Anomaly series whose standard deviation is zero.
```
### Constraints & Assumptions
- `1 <= number of rules <= 1000`
- `1 <= number of channels <= 100`
- Metric names are non-empty strings.
- Metric values are floating-point numbers.
- Timestamps are non-negative integers.
- A Threshold config is `{"threshold": float}`, a Change config is `{"change": float}`, and an Anomaly config is `{"std_devs": float}`.
### Clarifying Questions to Ask
- In what order should `evaluate` return alerts when several rules and channels fire?
- For Anomaly, do the mean and standard deviation include the latest point, and is it the population or the sample standard deviation?
- If points can arrive out of timestamp order, does "latest" mean the most recently ingested point or the one with the largest timestamp?
- Should a rule that stays in breach alert again on every `evaluate` call, or only when it first crosses?
- What should `add_rule` do with an unknown rule type or a malformed config?
### What a Strong Answer Covers
- How a new rule type plugs in (an interface plus a registry or factory keyed by the type string) without edits to `evaluate`.
- The exact firing condition of each rule type, with strict comparisons and the guards for missing data, a single point and zero standard deviation.
- A deterministic alert order and format, the `NO_CHANNEL` case, and an alert history that survives rule removal.
- The data structures for rules, channels and per-metric series, and the cost of `evaluate`, where the Anomaly scan is the expensive part.
- Input validation: duplicate ids, unknown rules or channels, repeated attachment, and bad config rejected before any state changes.
### Follow-up Questions
- How would you make the Anomaly check constant time per evaluation instead of a scan over the whole series?
- With the latest point included in the mean, how far from the mean can a single point ever sit, and how would you change the rule so a spike is easier to catch?
- How would you stop a rule that stays above its threshold from paging on every `evaluate` call?
- If Slack, PagerDuty and Email were real network calls that can fail or stall, how would the delivery path change?
Overview: Implement an alerting engine that ingests metric data points and evaluates Threshold, Change and Anomaly rules, sending alerts through Slack, PagerDuty and Email channels. It tests object-oriented design for adding rule types without touching the engine, exact firing conditions, and deterministic alert output.