Consumer lag monitor
Implement a consumer lag monitor.
```python
compute_lag(produced_offsets, committed_offsets, *, warn_lag=1000, critical_lag=10000)
```
`produced_offsets` is a dict `{partition: next offset}`: how many messages the
producer has written to the partition (the high watermark). `committed_offsets`
is a dict `{partition: committed offset}`; a partition may be missing.
The partition lag is `produced - committed`. If there is no commit, the consumer
has not read the partition yet: the lag equals `produced`, and `committed` in the
report is `None`.
Partition status:
- `critical` if `lag >= critical_lag`;
- otherwise `warn` if `lag >= warn_lag`;
- otherwise `ok`.
Return a dict:
```python
{
"partitions": {
0: {"lag": 5, "committed": 95, "status": "ok"},
1: {"lag": 1000, "committed": 0, "status": "warn"},
2: {"lag": 300, "committed": None, "status": "ok"},
},
"total_lag": 1305,
"uncommitted": [2],
"status": "warn",
}
```
- `partitions` contains every partition from `produced_offsets`, with integer
keys;
- `total_lag` is the sum of all partition lags;
- `uncommitted` is a sorted list of partitions without a commit;
- `status` is the worst partition status; an empty input gives `ok`.
Boundaries and errors — `ValueError`:
- `produced_offsets` or `committed_offsets` is not a dict;
- a partition key is not an integer (`bool` does not count) or is negative;
- an offset is not an integer (`bool` does not count) or is negative;
- `committed_offsets` contains a partition missing from `produced_offsets`;
- `committed > produced` — the commit is ahead of the producer, the data is
inconsistent;
- `warn_lag` is negative, `critical_lag <= warn_lag`, or a threshold is not an
integer or is `bool`.
Hidden tests check equal offsets, lag per partition and the total, a partition
without a commit, a commit ahead of the producer, an empty input and threshold
boundaries.
The AI mentor is available after you log in: it sees your attempt history and does not spend resources on anonymous requests.
Log in and askHint 1
Which two offsets do you subtract to get the lag, and what does a missing commit for a partition mean?