Recount
Python

Alerts

Flag any step in a Python trace as a warning or error, with the values that triggered it, using recount.add_alert.

Call recount.add_alert() to flag the most recently added span with a warn or error badge in the dashboard. There's no built-in threshold comparison - you decide when it fires with a plain if.


Basic usage

recount.add_span("json", "Query time", {"output": {"durationMs": 1240}})

if duration_ms > 1000:
    recount.add_alert("Query duration (ms)", "warn", {"checks": [{"value": 1240}]})

add_alert attaches to whichever span was most recently added on the active trace - call it right after the add_span (or add_message) it applies to.

You can call it more than once to flag several conditions on the same span - for example, separate warn and error alerts:

recount.add_span("json", "Query time", {"output": {"durationMs": 5240}})

if duration_ms > 1000:
    recount.add_alert("Query duration (ms)", "warn", {"checks": [{"value": duration_ms}]})
if duration_ms > 5000:
    recount.add_alert("Query duration (ms)", "error", {"checks": [{"value": duration_ms}]})

Multiple checks

checks shows which field(s) the alert is about. Pass one entry per value, with target naming the field it came from - the SDK doesn't evaluate these against anything, they're just what gets displayed:

recount.add_span("json", "Transaction review", {
    "output": {"amount": 4200, "riskScore": 82},
})

recount.add_alert("Suspicious transaction", "error", {
    "checks": [
        {"target": "Amount", "value": 4200},
        {"target": "Risk score", "value": 82},
    ],
})

An alert with no checks at all is valid too - useful when the label alone says everything:

recount.add_alert("Manual review required", "warn")

Inside groups

SpanGroup (the g passed to add_group's builder) has the same alert() method - it attaches to the most recently added span within that group:

def build_group(g):
    g.span("json", "Credit check", {"output": {"score": 610}})
    if credit_score < 650:
        g.alert("Below minimum credit score", "error", {"checks": [{"value": 610}]})


recount.add_group("Risk checks", build_group)

recount.add_alert(label, level, options=None)

ParameterTypeDescription
labelstrDisplay label shown in the dashboard (max 128 chars)
level"warn" | "error"Severity shown in the dashboard
optionsSpanAlertOptionsOptional - see below

SpanAlertOptions

OptionTypeRequiredDescription
checkslist[SpanAlertCheck]NoContext values to show alongside the alert

SpanAlertCheck

OptionTypeRequiredDescription
valuefloatYesThe value to display
targetstrNoName of the field value came from, e.g. 'Amount'

Max 10 alerts per span, max 200 checks per alert. See Limits.


Dashboard behaviour

  • Collapsed span - a warn or error badge appears on the right of the span header if any alert was attached.
  • Expanded span - a coloured banner appears for each alert, showing the label and any check values (e.g. Suspicious transaction: Amount: 4200, Risk score: 82).

On this page