thanos-io/thanos · error

preparing command failed

Error message

preparing %s command failed

What it means

This is a top-level wrapper in Thanos's main(): when the per-subcommand setup function (flag validation, config parsing, component wiring) returns an error, main wraps it with errors.Wrapf(err, "preparing %s command failed", cmd) and logs it with a full stack trace before exiting with status 1. It means the chosen Thanos subcommand (e.g. query, sidecar) failed during initialization, before any run group started.

Solutions

  1. Read the wrapped inner error in the logged stack trace (%+v prints the full chain); it names the exact setup step that failed.
  2. Re-run the command with --log.level=debug for more context.
  3. Validate all flag values and config files (labels, relabel configs, request-log config) against Thanos documentation.
  4. Fix the underlying misconfiguration and restart the command.

Example fix

// before
thanos query --selector-label='}'   // malformed label flag
// after
thanos query --selector-label='cluster="eu1"'
Defensive patterns

Strategy: validation

Validate before calling

// Pre-validate flags/config before launching thanos
if cfg, err := yaml.Marshal(myConfig); err != nil || len(cfg) == 0 {
    return fmt.Errorf("invalid thanos config: %w", err)
}
// smoke-test the command
if out, err := exec.Command("thanos", cmd, "--help").CombinedOutput(); err != nil {
    return fmt.Errorf("thanos %s unavailable: %s", cmd, out)
}

Try / catch

// In a wrapper script
if ! thanos query --log.level=debug ...; then
  echo "setup failed; see wrapped inner error above" >&2
  exit 1
fi

Prevention

When it happens

Trigger: Running any `thanos <subcommand>` where the cmd.Setup callback invoked by setup() returns a non-nil error: invalid flags, unparseable label/relabel config, failed request-logging config parse, bad TLS paths, etc.

Common situations: Typo in --store or --endpoint flags, malformed YAML in --request-log-config, invalid relabel expressions, missing TLS cert files, or incompatible flag combinations when launching Thanos in Kubernetes/Docker.

Understand the failure class

Background: "Unknown argument", "Invalid value", and "must be one of": invalid CLI argument errors explained — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at cmd/thanos/main.go:143

		ctx, cancel := context.WithCancel(ctx)
		g.Add(func() error {
			<-ctx.Done()
			return ctx.Err()
		}, func(error) {
			if closer != nil {
				if err := closer.Close(); err != nil {
					level.Warn(logger).Log("msg", "closing tracer failed", "err", err)
				}
			}
			cancel()
		})
	}
	// Create a signal channel to dispatch reload events to sub-commands.
	reloadCh := make(chan struct{}, 1)

	if err := setup(&g, logger, metrics, tracer, reloadCh, *logLevel == "debug"); err != nil {
		// Use %+v for github.com/pkg/errors error to print with stack.
		level.Error(logger).Log("err", fmt.Sprintf("%+v", errors.Wrapf(err, "preparing %s command failed", cmd)))
		os.Exit(1)
	}

	// Listen for termination signals.
	{
		cancel := make(chan struct{})
		g.Add(func() error {
			return interrupt(logger, cancel)
		}, func(error) {
			close(cancel)
		})
	}

	// Listen for reload signals.
	{
		cancel := make(chan struct{})
		g.Add(func() error {
			return reload(logger, cancel, reloadCh)

View on GitHub (pinned to 35b8b99117)