fix(grafana): always set optional alert fields to avoid render_context errors#5658
Conversation
|
@nflx is attempting to deploy a commit to the KeepHQ Team on Vercel. A member of the Team first needs to authorize it. |
|
Target branch is not in the allowed branches list. |
|
Blocked by #5676 — the |
|
@hakatt can you fix "test docs" so it will pass? |
0b2c7c9 to
76181bd
Compare
|
The root cause is a capitalisation mismatch in Fixed in #5709. Once that merges and this branch is rebased onto |
|
…t errors
Fields like panelUrl, dashboardUrl, silenceURL, valueString, value and
datasource are only present for certain Grafana alert types (webhook vs
datasource). When absent they are missing entirely from the AlertDto
context, causing render_context() to raise a RenderException with
"Could not find key" because it renders all provider with: parameters
using safe=True.
Schema fields defined on AlertDto default to None (Chevron renders None
as "") so they are fine. Extra fields that are never set are genuinely
absent from the context dict, which is what triggers the error.
Fix: always set these optional extra fields (as "" when absent) in both
the webhook alert path and the datasource alert path of the Grafana
provider. Workflow templates can then safely reference them and use
Python or-chains for fallbacks: '{{ alert.panelUrl }}' or 'N/A'.
Fixes keephq#5070
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
76181bd to
af7556a
Compare
Summary
Fixes #5070 and the general class of
"Could not find key 'alert.X'"errors that occur when a workflow template references an alert field that is absent from the context.Root cause
render_context()iniohandler.pyrenders all providerwith:parameters using Chevron/Mustache withsafe=True. When Chevron encounters{{ alert.someField }}and that field doesn't exist in the alert context dict, it writes"Could not find key alert.someField"to stderr — which_render()detects and raises aRenderException.This affects schema fields and extra fields differently:
AlertDto(e.g.url,description) always exist in the context because they haveNonedefaults — Chevron rendersNoneas"", no error.extra = Extra.allow) that are never set are genuinely absent from the context dict, which triggers the error.For the Grafana provider, several useful fields (
panelUrl,dashboardUrl,silenceURL,valueString,value,datasource) fall into this second category — they're only populated for certain alert types (webhook vs. datasource alerts) and were previously set toNoneor not set at all when absent.Fix
Always set these optional extra fields as
""when absent, in both the webhook alert path (_format_alert) and the datasource alert path (_get_alerts_datasource). Workflow templates can then safely reference them and useor-chains for fallbacks:The same pattern should be applied to other providers where fields are conditionally present. The underlying issue is that
render_context()usessafe=Trueuniformly — a more general fix could be to allow providers or individual parameters to opt out of safe rendering, but that is a larger change.Test plan
panelUrl,dashboardUrl,silenceURL,valueString) — optional fields populated correctlydatasource,value) — webhook-only fields default to""RenderException🤖 Generated with Claude Code