grafana/k6 · error

could not serialize cloud configuration

Error message

could not serialize cloud configuration: %w

What it means

Generic wrap in applyExternalProvisioningCreds around serializing the final cloud configuration (after injecting externally provisioned credentials such as K6_CLOUD_METRICS_PUSH_URL/K6_CLOUD_TEST_RUN_TOKEN). The %w names the underlying marshal failure; internal invariant rather than user error.

Solutions

  1. Inspect the wrapped serialization error
  2. Verify cloud env vars are not set to values with unexpected characters or types
  3. Report as a bug if it reproduces with a normal provisioning setup
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at internal/cmd/outputs_cloud.go:241 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18). Data as JSON: /api/errors/25542cf17958baaf. Report an issue: GitHub.

Appendix: source

Thrown at internal/cmd/outputs_cloud.go:241

	token := gs.Env["K6_CLOUD_TEST_RUN_TOKEN"]
	if (pushURL == "") != (token == "") {
		return errors.New("both K6_CLOUD_METRICS_PUSH_URL and " +
			"K6_CLOUD_TEST_RUN_TOKEN must be set together")
	}
	// A logs push URL is authenticated with the same scoped token, so reject a
	// logs URL supplied without it rather than silently streaming nothing.
	if token == "" && conf.LogsPushURL.Valid && conf.LogsPushURL.String != "" {
		return errors.New("K6_CLOUD_LOGS_PUSH_URL requires K6_CLOUD_TEST_RUN_TOKEN")
	}
	if pushURL == "" {
		return nil
	}

	conf.MetricsPushURL = null.StringFrom(pushURL)
	conf.TestRunToken = null.StringFrom(token)
	raw, err := cloudConfToRawMessage(*conf)
	if err != nil {
		return fmt.Errorf("could not serialize cloud configuration: %w", err)
	}
	if test.derivedConfig.Collectors == nil {
		test.derivedConfig.Collectors = make(map[string]json.RawMessage)
	}
	test.derivedConfig.Collectors[builtinOutputCloud.String()] = raw
	return nil
}

// externalLogsConfig mirrors the K6_CLOUD_LOGS_* env vars an external
// orchestrator uses to hand the log-push settings to a local k6. These are
// read explicitly (the Logs* fields are not env-bound on cloudapi.Config) so a
// stray env value can't override the run-scoped logs config in the
// self-provisioned flow.
type externalLogsConfig struct {
	PushURL       null.String        `envconfig:"K6_CLOUD_LOGS_PUSH_URL"`
	Level         null.String        `envconfig:"K6_CLOUD_LOGS_LEVEL"`
	Limit         null.Int           `envconfig:"K6_CLOUD_LOGS_LIMIT"`
	PushPeriod    types.NullDuration `envconfig:"K6_CLOUD_LOGS_PUSH_PERIOD"`

View on GitHub (pinned to 01ffac6f24)