tsenart/vegeta · error
error registering prometheus metrics: %s
Error message
error registering prometheus metrics: %s
What it means
When -prometheus-addr is set, attack() creates a prometheus registry and registers the metrics collector; if pm.Register(r) fails, the error is wrapped. Registration fails when the same metric name or duplicate collectors are registered, or malformed metric descriptors are used.
Source
Thrown at attack.go:194
out, err := file(opts.outputf, true)
if err != nil {
return fmt.Errorf("error opening %s: %s", opts.outputf, err)
}
defer out.Close()
tlsc, err := tlsConfig(opts.insecure, opts.certf, opts.keyf, opts.rootCerts)
if err != nil {
return err
}
var pm *prom.Metrics
if opts.promAddr != "" {
pm = prom.NewMetrics()
r := prometheus.NewRegistry()
if err := pm.Register(r); err != nil {
return fmt.Errorf("error registering prometheus metrics: %s", err)
}
srv := http.Server{
Addr: opts.promAddr,
Handler: prom.NewHandler(r, time.Now().UTC()),
}
defer srv.Close()
go srv.ListenAndServe()
}
atk := vegeta.NewAttacker(
vegeta.Redirects(opts.redirects),
vegeta.Timeout(opts.timeout),
vegeta.LocalAddr(*opts.laddr.IPAddr),
vegeta.TLSConfig(tlsc),
vegeta.Workers(opts.workers),
vegeta.MaxWorkers(opts.maxWorkers),View on GitHub (pinned to cf58112690)
Solutions
- Use a fresh prometheus.NewRegistry() per attack run (as upstream does) rather than a shared global registry.
- Check the wrapped error for 'already registered' and remove the duplicate collector registration.
- Verify the prometheus client_golang version matches what the metric definitions expect (metric/label name rules changed across versions).
Defensive patterns
Strategy: try-catch
Validate before calling
reg := prometheus.NewRegistry()
if err := prom.NewMetrics().Register(reg); err != nil {
return fmt.Errorf("prometheus registration pre-check failed: %w", err)
} Try / catch
if err := run(); err != nil {
if strings.Contains(err.Error(), "already registered") {
log.Fatalf("duplicate metric registration: use a fresh registry per run: %v", err)
}
log.Fatal(err)
} Prevention
- Create a new prometheus.Registry per attack run; never share registries globally.
- Keep client_golang versions aligned with vegeta's go.mod.
- Test the -prometheus-addr path in staging before production load tests.
When it happens
Trigger: `-prometheus-addr` supplied and prom.Metrics.Register returns an error from prometheus.Register — typically duplicate metric registration or an invalid metric name/label configuration in the vendored prom version.
Common situations: Custom builds where Metrics is instantiated twice against one registry; dependency upgrades changing prometheus client metric naming rules; embedding attack() into custom code that shares a registry.
Related errors
- failed to register metric %v: %w
- rate frequency and time unit must be bigger than zero
- bad certificate
- -rate=0 requires setting -max-workers
- format %q isn't one of [%s]
AI-assisted analysis of tsenart/vegeta@cf58112690 (2026-08-31).
Data as JSON: /api/errors/924cfab6f83526df.
Report an issue: GitHub.