grafana/k6 · error

metric '%s' already exists but with type %s, instead of %s

Error message

metric '%s' already exists but with type %s, instead of %s

What it means

Raised by Registry.NewMetric when a metric with the same name was already registered with a different MetricType (counter vs gauge vs trend vs rate), since a name uniquely identifies one metric type in the registry.

Source

Thrown at metrics/registry.go:58

// NewMetric returns new metric registered to this registry
// TODO have multiple versions returning specific metric types when we have such things
func (r *Registry) NewMetric(name string, typ MetricType, t ...ValueType) (*Metric, error) {
	r.l.Lock()
	defer r.l.Unlock()

	if !checkName(name) {
		return nil, fmt.Errorf("Invalid metric name: '%s'. %s", name, badNameWarning) //nolint:staticcheck
	}
	oldMetric, ok := r.metrics[name]

	if !ok {
		m := r.newMetric(name, typ, t...)
		r.metrics[name] = m
		return m, nil
	}
	if oldMetric.Type != typ {
		return nil, fmt.Errorf("metric '%s' already exists but with type %s, instead of %s", name, oldMetric.Type, typ)
	}
	if len(t) > 0 {
		if t[0] != oldMetric.Contains {
			return nil, fmt.Errorf("metric '%s' already exists but with a value type %s, instead of %s",
				name, oldMetric.Contains, t[0])
		}
	}
	return oldMetric, nil
}

// MustNewMetric is like NewMetric, but will panic if there is an error
func (r *Registry) MustNewMetric(name string, typ MetricType, t ...ValueType) *Metric {
	m, err := r.NewMetric(name, typ, t...)
	if err != nil {
		panic(err)
	}
	return m
}

View on GitHub (pinned to 01ffac6f24)

Solutions

  1. Use a different name for the new metric
  2. Make all declarations of the metric use the same type
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at metrics/registry.go:58 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/7dbf4c56af35e5a4. Report an issue: GitHub.