thanos-io/thanos · error

failed to execute the template

Error message

failed to execute the template: %w

What it means

After successfully parsing the template, validateTemplate executes it with data Expression{Expr: "test_expr"} to ensure actions are valid against the actual data model. If Execute fails — typically because the template references fields that don't exist on Expression — it returns fmt.Errorf("failed to execute the template: %w", err).

Solutions

  1. Restrict actions to {{.Expr}} — the only exported field on the Expression data passed in.
  2. Remember Go template field access is case-sensitive and requires exported names: use {{.Expr}}, not {{.expr}} or {{.Expression}}.
  3. Inspect the wrapped template.ExecError in the message to find the exact field/function that failed.
  4. Simplify the template to a minimal {{.Expr}} version, confirm it validates, then add constructs back one at a time.

Example fix

// before
tmpl := "/graph?g0.expr={{ .expr }}&g0.tab=1"
// after
tmpl := "/graph?g0.expr={{ .Expr }}&g0.tab=1"
Defensive patterns

Strategy: validation

Validate before calling

func executesOnTestData(tmplStr string) error {
    t, err := template.New("test").Parse(tmplStr)
    if err != nil {
        return err
    }
    var buf bytes.Buffer
    return t.Execute(&buf, Expression{Expr: "test_expr"})
}

Type guard

null

Try / catch

if err := executesOnTestData(tmplStr); err != nil {
    var ee template.ExecError
    if errors.As(err, &ee) {
        log.Printf("template references invalid field/function: %v", ee)
    }
}

Prevention

When it happens

Trigger: validateTemplate receives a syntactically valid template that at execute time references a missing field (e.g. {{.Expr.Name}}, {{.Query}}), applies an incompatible pipeline, or calls a function with wrong argument types, causing tmpl.Execute(&buf, Expression{Expr: "test_expr"}) to error.

Common situations: Templates copied from Grafana or Prometheus rule templates that use different variables; typos in field names ({{.expr}} is a different, missing field since Go templates are case-sensitive and exported-field only); attempts to call methods Expression doesn't have.

Related errors


AI-assisted analysis of thanos-io/thanos@35b8b99117 (2026-09-07). Data as JSON: /api/errors/2a78c6eecf09f53b. Report an issue: GitHub.

Appendix: source

Thrown at cmd/thanos/rule.go:1148

		return "", errors.Wrap(err, "failed to parse template")
	}

	var buf bytes.Buffer
	if err := t.Execute(&buf, escapedExpr); err != nil {
		return "", errors.Wrap(err, "failed to execute template")
	}
	return buf.String(), nil
}

func validateTemplate(tmplStr string) error {
	tmpl, err := template.New("test").Parse(tmplStr)
	if err != nil {
		return fmt.Errorf("failed to parse the template: %w", err)
	}
	var buf bytes.Buffer
	err = tmpl.Execute(&buf, Expression{Expr: "test_expr"})
	if err != nil {
		return fmt.Errorf("failed to execute the template: %w", err)
	}
	return nil
}

// Filter out PromQL related warnings from warning response and keep store related warnings only.
func filterOutPromQLWarnings(warns []string, logger log.Logger, query string) []string {
	storeWarnings := make([]string, 0, len(warns))
	for _, warn := range warns {
		if extannotations.IsPromQLAnnotation(warn) {
			level.Warn(logger).Log("warning", warn, "query", query)
			continue
		}
		storeWarnings = append(storeWarnings, warn)
	}
	return storeWarnings
}

// ReadyScrapeManager allows a scrape manager to be retrieved. Even if it's set at a later point in time.

View on GitHub (pinned to 35b8b99117)