grafana/k6 · error

metric %q stored not as counter

Error message

metric %q stored not as counter

What it means

getOrCreateCounterForRate keeps a sync.Map of <name>.total counters. If a stored entry under that name fails the type assertion to otelMetric.Int64Counter, the registry state is inconsistent (the same metric name was previously registered as a different instrument type). This is an internal invariant violation, not user input error.

Source

Thrown at internal/output/opentelemetry/registry.go:103

	totalName := name + ".total"

	var err error
	var totalCounter otelMetric.Int64Counter

	storedTotalCounter, ok := r.rateCounters.Load(totalName)
	if !ok {
		totalCounter, err = r.meter.Int64Counter(totalName)
		if err != nil {
			return nil, fmt.Errorf("failed to create counter for %q: %w", totalName, err)
		}

		r.rateCounters.Store(totalName, totalCounter)
		r.logger.Debugf("registered counter metric %q", totalName)
	} else {
		totalCounter, ok = storedTotalCounter.(otelMetric.Int64Counter)
		if !ok {
			return nil, fmt.Errorf("metric %q stored not as counter", totalName)
		}
	}

	return totalCounter, nil
}

func (r *registry) getOrCreateGauge(name, unit string) (otelMetric.Float64Gauge, error) {
	if gauge, ok := r.gauges.Load(name); ok {
		if v, ok := gauge.(otelMetric.Float64Gauge); ok {
			return v, nil
		}

		return nil, fmt.Errorf("metric %q is not a gauge", name)
	}

	opts := []otelMetric.Float64GaugeOption{}
	if unit != "" {
		opts = append(opts, otelMetric.WithUnit(unit))

View on GitHub (pinned to 01ffac6f24)

Solutions

  1. Rename the rate metric so '<name>.total' doesn't collide with an existing differently-typed instrument
  2. Avoid mixing metric types under the same base name
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at internal/output/opentelemetry/registry.go:103 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/8a3905d11fe02ad0. Report an issue: GitHub.