{"record":{"id":"9f406fb3a29c21f3","repo":"thanos-io/thanos","slug":"failed-to-execute-template","errorCode":null,"errorMessage":"failed to execute template","messagePattern":"failed to execute template","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/thanos/rule.go","lineNumber":1135,"sourceCode":"\tfor _, group := range ruleMgr.RuleGroups() {\n\t\tmetrics.rulesLoaded.WithLabelValues(group.PartialResponseStrategy.String(), group.OriginalFile, group.Name()).Set(float64(len(group.Rules())))\n\t}\n\treturn errs.Err()\n}\n\nfunc tableLinkForExpression(tmpl string, expr string) (string, error) {\n\t// template example: \"/graph?g0.expr={{.Expr}}&g0.tab=1\"\n\tescapedExpression := url.QueryEscape(expr)\n\n\tescapedExpr := Expression{Expr: escapedExpression}\n\tt, err := texttemplate.New(\"url\").Parse(tmpl)\n\tif err != nil {\n\t\treturn \"\", errors.Wrap(err, \"failed to parse template\")\n\t}\n\n\tvar buf bytes.Buffer\n\tif err := t.Execute(&buf, escapedExpr); err != nil {\n\t\treturn \"\", errors.Wrap(err, \"failed to execute template\")\n\t}\n\treturn buf.String(), nil\n}\n\nfunc validateTemplate(tmplStr string) error {\n\ttmpl, err := template.New(\"test\").Parse(tmplStr)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to parse the template: %w\", err)\n\t}\n\tvar buf bytes.Buffer\n\terr = tmpl.Execute(&buf, Expression{Expr: \"test_expr\"})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to execute the template: %w\", err)\n\t}\n\treturn nil\n}\n\n// Filter out PromQL related warnings from warning response and keep store related warnings only.","sourceCodeStart":1117,"sourceCodeEnd":1153,"githubUrl":"https://github.com/thanos-io/thanos/blob/35b8b991177def87ed52dcf10f9b6d87f07282c8/cmd/thanos/rule.go#L1117-L1153","documentation":"After the URL template parses successfully, Thanos executes it with text/template's Execute into a bytes.Buffer, passing an Expression{Expr: url.QueryEscape(expr)} as data. If the template's actions reference fields that don't exist on Expression, use invalid pipeline functions, or a writer error occurs on the buffer, the wrapped \"failed to execute template\" error is returned and no URL is produced.","triggerScenarios":"Calling the URL builder with a syntactically valid template that references a non-existent field (e.g. {{.PromQL}} or {{.Query}} instead of {{.Expr}}), calls a method on a nil/wrong type, or uses an unknown function, so t.Execute(&buf, escapedExpr) fails.","commonSituations":"Users guess field names from other templating systems (Grafana uses $expr, Prometheus templating uses $value); copy-pasting templates built for text/template with different data models; templates that index or call methods unsupported by the Expression struct.","solutions":["Use only {{.Expr}} as the field: the data passed to Execute is Expression{Expr: escapedExpression} and nothing else.","Run the template through Thanos's validateTemplate (cmd/thanos/rule.go) which executes it with Expression{Expr: \"test_expr\"} to catch field errors before startup.","Replace unsupported constructs (sprig functions, custom pipelines) with plain Go text/template builtins like urlquery/printf.","Check the underlying template.ExecError in the wrapped error message: it names the exact field or function that failed."],"exampleFix":"// before\ntmpl := \"/graph?g0.expr={{.PromQL}}\"\n// after\ntmpl := \"/graph?g0.expr={{.Expr}}\"","handlingStrategy":"validation","validationCode":"func executesAgainstExpression(s string) bool {\n    t, err := texttemplate.New(\"check\").Parse(s)\n    if err != nil {\n        return false\n    }\n    var buf bytes.Buffer\n    return t.Execute(&buf, Expression{Expr: \"test_expr\"}) == nil\n}","typeGuard":"null","tryCatchPattern":"var buf bytes.Buffer\nif err := t.Execute(&buf, escapedExpr); err != nil {\n    return \"\", fmt.Errorf(\"template execution failed (only .Expr is available): %w\", err)\n}","preventionTips":["Reference only {{.Expr}} — the sole field on the Expression data model","Remember Go templates are case-sensitive and need exported fields","Test templates with a dry-run Execute using Expression{Expr: \"test_expr\"} before deployment"],"tags":["go","template","config","thanos"],"backgroundTag":"template-execution-failed","analyzedSha":"35b8b991177def87ed52dcf10f9b6d87f07282c8","analyzedAt":"2026-09-07T01:49:59.689Z","contentChangedAt":"2026-09-07T01:49:59.689Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}