← All posts
datadog monitoring aws sqs learning

avg, max, sum: What a Datadog Monitor Actually Counts

Two Datadog monitors can watch the same queue with the same threshold and still disagree. I worked out how time and space aggregation reduce a window of data points to one number, and why the choice matters more than the threshold.

· 7 min read

Here are two Datadog monitors on the same dead-letter queue:

avg(last_10m):sum:aws.sqs.approximate_number_of_messages_visible{queuename:orders-dlq} > 1
max(last_10m):max:aws.sqs.approximate_number_of_messages_visible{queuename:orders-dlq} > 1

Same metric, same queue, same threshold. I assumed they were interchangeable. They are not. Three messages can sit in that queue and one of these monitors will stay green.

This post covers what those words in front of the metric actually do, where the data points come from, and how to pick one.

Reading the query

A metric monitor query has more parts than it looks:

max(last_10m):max:aws.sqs.approximate_number_of_messages_visible{queuename:orders-dlq} by {queuename} > 1

There are two aggregation steps, and they work on different axes.

Picture a grid. Rows are series, columns are timestamps. Space aggregation squashes each column into one cell. Time aggregation squashes the resulting row into one number.

When the space aggregator does not matter

If the scope matches exactly one series, there is nothing to merge. sum, max and avg across one series all return the same value. In a query scoped to a single queue, the second aggregator is decoration.

It starts to matter when the scope is wide, for example queuename:*-dlq*:

Where the data points come from

Before you can reason about avg or sum, you need to know how many points are in the window and what each point means.

For AWS metrics this has more layers than you’d expect:

  1. AWS publishes SQS metrics to CloudWatch once a minute.
  2. Each CloudWatch point is already a statistic. CloudWatch summarises the minute as Average, Maximum, Minimum or Sum. Datadog takes one of those as the default for each metric and usually exposes the others as .maximum, .minimum and similar suffixes. So when a monitor does avg over SQS depth, it is averaging per-minute averages.
  3. Datadog pulls from CloudWatch on a schedule, roughly every ten minutes by default. The points are backfilled with their original timestamps, so they show up late.
  4. That delay is why AWS monitors use evaluation_delay. With a delay of 900 seconds, a last_10m window covers the ten minutes that ended fifteen minutes ago. That is old enough for the data to have arrived.

You get about one point per minute, so ten points in a ten-minute window. It is worth checking this rather than assuming it, because some integrations end up with five-minute spacing. Graph the metric for one queue with .rollup(count, 60). If each minute shows a count of 1, the points are one minute apart.

The spacing changes how the aggregators behave. With five-minute points, a last_5m window holds about one point. At that point avg, max and sum all return the same number, and the choice between them stops mattering.

The time aggregators

The monitor editor describes these in plain English, which is the most useful way to remember them.

AggregatorEditor wordingOver the window, it returns
avgon averagethe sum of the points divided by how many points there are
maxat least oncethe single highest point
minat all timesthe single lowest point
sumin totalevery point added together

For an “above” threshold, max means at least once: one point over the line is enough. min means at all times: every point must be over the line. avg sits between those two.

A worked example

Take a ten-minute window, one point per minute. Three messages land in the queue at minute eight and stay there:

0  0  0  0  0  0  0  3  3  3
QueryResult> 1?
avg(last_10m)9 / 10 = 0.9no
max(last_10m)3yes
sum(last_10m)9yes
min(last_10m)0no

Three messages are stuck, and the avg monitor stays green. On a later evaluation the zeros slide out of the window and the average climbs past 1, so it will fire eventually. It is just late. How late depends on how big the spike is compared to the threshold.

Now one message, sitting in a five-minute window:

0  0  1  1  1
QueryResult> 1?
sum(last_5m)3yes
max(last_5m)1no

Here sum fires and max does not. sum counts the same message once for each point it appears in, so the window adds it up to 3.

Things that caught me out

sum on a gauge measures time, not quantity

Queue depth is a gauge: each point says “this many messages are sitting here right now.” Adding gauge readings together counts the same message again on every point. A single message sitting for ten minutes sums to 10. The result grows with how many points fit in the window, so a sum threshold quietly changes meaning whenever the point spacing or the window length changes.

sum is the right tool for metrics where each point counts events in that interval, such as messages received or requests served. For those, adding the points gives a real total. For a depth or a level, use max, min or avg.

> 1 means two

The threshold is a strict comparison. > 1 fires at two messages, not one. If one stuck message is worth an alert, the threshold is > 0.

avg > 0 and max > 0 are the same alert

Message counts are never negative. If any point in the window is above zero, the average is above zero too. For a > 0 threshold on a count, avg and max fire at the same moment. They only part ways once the threshold is above zero.

The window decides how fast you recover

With max(last_10m), one bad point keeps the monitor red until it has aged out of the window. Redrive a dead-letter queue and the alert still lasts ten more minutes, plus the evaluation delay. A longer window means a steadier signal and a slower all-clear.

Missing points are skipped, not zero

avg divides by the points that exist, not by the minutes in the window. A window with two points of value 5 averages to 5, not 1.

AWS also stops publishing SQS metrics for a queue that has sat idle for about six hours. An empty, quiet dead-letter queue often has no data at all. The first message wakes it up, so alerting still works, but expect gaps on the graph and think about what on_missing_data should do.

require_full_window = false lets the monitor evaluate a window that is only partly filled. That is harmless for max. For sum, it means smaller totals right after a gap.

Choosing one

You want to knowUse
Did anything land in the dead-letter queue?max(...) > 0
Has the backlog stayed high, not just spiked?min(...) or avg(...) above the level
Has the oldest message been waiting too long?max(...) on message age
Did enough work arrive in the last hour?sum(...) on a per-interval count, such as messages received
Has the producer gone quiet?sum(...) on a count == 0, with a window long enough to rule out normal lulls

What I want to remember

A monitor reduces a whole window to one number, in two steps. Space aggregation merges series at each timestamp. Time aggregation merges timestamps across the window. The threshold only ever sees the final number.

Before trusting a threshold, know how many points sit in the window and what each point already is. For AWS metrics, each point is a CloudWatch statistic that arrives late.

max asks “at least once?” min asks “the whole time?” avg asks “mostly?” sum asks “how much in total?”, and on a gauge that question has no useful answer.

Two monitors with the same metric and the same threshold can still disagree. The aggregator decides what the threshold means.

← Back to all posts