{"record":{"id":"472c97a33c71a873","repo":"grafana/k6","slug":"invalid-metric-name-s-s","errorCode":null,"errorMessage":"Invalid metric name: '%s'. %s","messagePattern":"Invalid metric name: '(.+?)'\\. (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"metrics/registry.go","lineNumber":48,"sourceCode":"\tnameRegexString = \"^[a-zA-Z_][a-zA-Z0-9_]{1,128}$\"\n\tbadNameWarning  = \"Metric names must only include up to 128 ASCII letters, numbers, or underscores \" +\n\t\t\"and start with a letter or an underscore.\"\n)\n\nvar compileNameRegex = regexp.MustCompile(nameRegexString)\n\nfunc checkName(name string) bool {\n\treturn compileNameRegex.MatchString(name)\n}\n\n// NewMetric returns new metric registered to this registry\n// TODO have multiple versions returning specific metric types when we have such things\nfunc (r *Registry) NewMetric(name string, typ MetricType, t ...ValueType) (*Metric, error) {\n\tr.l.Lock()\n\tdefer r.l.Unlock()\n\n\tif !checkName(name) {\n\t\treturn nil, fmt.Errorf(\"Invalid metric name: '%s'. %s\", name, badNameWarning) //nolint:staticcheck\n\t}\n\toldMetric, ok := r.metrics[name]\n\n\tif !ok {\n\t\tm := r.newMetric(name, typ, t...)\n\t\tr.metrics[name] = m\n\t\treturn m, nil\n\t}\n\tif oldMetric.Type != typ {\n\t\treturn nil, fmt.Errorf(\"metric '%s' already exists but with type %s, instead of %s\", name, oldMetric.Type, typ)\n\t}\n\tif len(t) > 0 {\n\t\tif t[0] != oldMetric.Contains {\n\t\t\treturn nil, fmt.Errorf(\"metric '%s' already exists but with a value type %s, instead of %s\",\n\t\t\t\tname, oldMetric.Contains, t[0])\n\t\t}\n\t}\n\treturn oldMetric, nil","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/metrics/registry.go#L30-L66","documentation":"Raised by Registry.NewMetric when the metric name fails the name regex: it must start with a letter or underscore, contain only ASCII letters/digits/underscores, and be at most 128 characters. The message appends a static explanation (badNameWarning).","triggerScenarios":"Thrown at metrics/registry.go:48 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Rename the metric to match [a-zA-Z_][a-zA-Z0-9_]{1,128}","Remove spaces, dashes, dots or other special characters","Prefix with a letter or underscore, not a digit"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"01ffac6f245854c1b8adc6a69857c76a15f90022","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}