{"record":{"id":"eaedacdb01251f60","repo":"grafana/k6","slug":"s-is-an-invalid-value-for-metric-s-a-number","errorCode":null,"errorMessage":"'%s' is an invalid value for metric '%s', a number or a boolean value is expected","messagePattern":"'(.+?)' is an invalid value for metric '(.+?)', a number or a boolean value is expected","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/metrics/metrics.go","lineNumber":92,"sourceCode":"\t}, string(omitMsg))\n}\n\nfunc (m Metric) add(v sobek.Value, addTags sobek.Value) (bool, error) {\n\tstate := m.vu.State()\n\tif state == nil {\n\t\treturn false, ErrMetricsAddInInitContext\n\t}\n\n\t// return/throw exception if throw enabled, otherwise just log\n\traiseErr := func(err error) (bool, error) { //nolint:unparam // we want to just do `return raiseErr(...)`\n\t\tif state.Options.Throw.Bool {\n\t\t\treturn false, err\n\t\t}\n\t\tstate.Logger.Warn(err)\n\t\treturn false, nil\n\t}\n\traiseNan := func() (bool, error) {\n\t\treturn raiseErr(fmt.Errorf(\"'%s' is an invalid value for metric '%s', a number or a boolean value is expected\",\n\t\t\tlimitValue(v.String()), m.metric.Name))\n\t}\n\n\tif v == nil {\n\t\treturn raiseErr(fmt.Errorf(\"no value was provided for metric '%s', a number or a boolean value is expected\",\n\t\t\tm.metric.Name))\n\t}\n\tif sobek.IsNull(v) {\n\t\treturn raiseNan()\n\t}\n\n\tvfloat := v.ToFloat()\n\tif vfloat == 0 && v.ToBoolean() {\n\t\tvfloat = 1.0\n\t}\n\n\tif math.IsNaN(vfloat) {\n\t\treturn raiseNan()","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/modules/k6/metrics/metrics.go#L74-L110","documentation":"Produced by the raiseNan helper inside Metric.add when the value passed to Counter.add/Value.add etc. cannot be coerced to a number. After sobek coercion the value's float is NaN (e.g. null, a non-numeric string) while its boolean coercion is true; the invalid value (length-limited to 100 chars) and metric name are reported. Depending on the throw option this is either returned as an error or only logged as a warning while add() returns false.","triggerScenarios":"Thrown at internal/js/modules/k6/metrics/metrics.go:92 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Pass a number or boolean to the metric's add() method, e.g. myCounter.add(1)","For boolean semantics use true (recorded as 1) rather than null or strings","Check state.Options.Throw: with throw disabled the error is only a warning and the sample is silently dropped"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"01ffac6f245854c1b8adc6a69857c76a15f90022","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}