grafana/k6 · error

cannot add tags for the

Error message

cannot add tags for the '%s' custom metric: %w

What it means

Returned by Metric.add when applying the optional tags argument fails. The helper common.ApplyCustomUserTags could not interpret the second argument of add(value, tags) as a valid tags object (e.g. not an object or containing invalid values). Note this error is returned directly, not routed through raiseErr, so it propagates regardless of the throw option; the metric sample is never emitted.

Solutions

  1. Pass tags as a plain object: myCounter.add(1, { env: 'staging' })
  2. Omit the second argument if no custom tags are needed
  3. Inspect the wrapped error for the exact tag that failed validation
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at internal/js/modules/k6/metrics/metrics.go:115 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/ace6d626207adc97. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/metrics/metrics.go:115

		return raiseErr(fmt.Errorf("no value was provided for metric '%s', a number or a boolean value is expected",
			m.metric.Name))
	}
	if sobek.IsNull(v) {
		return raiseNan()
	}

	vfloat := v.ToFloat()
	if vfloat == 0 && v.ToBoolean() {
		vfloat = 1.0
	}

	if math.IsNaN(vfloat) {
		return raiseNan()
	}

	ctm := state.Tags.GetCurrentValues()
	if err := common.ApplyCustomUserTags(m.vu.Runtime(), &ctm, addTags); err != nil {
		return false, fmt.Errorf("cannot add tags for the '%s' custom metric: %w", m.metric.Name, err)
	}

	sample := metrics.Sample{
		TimeSeries: metrics.TimeSeries{
			Metric: m.metric,
			Tags:   ctm.Tags,
		},
		Time:     time.Now(),
		Metadata: ctm.Metadata,
		Value:    vfloat,
	}
	metrics.PushIfNotDone(m.vu.Context(), state.Samples, sample)
	return true, nil
}

type (
	// RootModule is the root metrics module
	RootModule struct{}

View on GitHub (pinned to 01ffac6f24)