{"record":{"id":"7dbf4c56af35e5a4","repo":"grafana/k6","slug":"metric-s-already-exists-but-with-type-s-inste","errorCode":null,"errorMessage":"metric '%s' already exists but with type %s, instead of %s","messagePattern":"metric '(.+?)' already exists but with type (.+?), instead of (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"metrics/registry.go","lineNumber":58,"sourceCode":"\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\n}\n\n// MustNewMetric is like NewMetric, but will panic if there is an error\nfunc (r *Registry) MustNewMetric(name string, typ MetricType, t ...ValueType) *Metric {\n\tm, err := r.NewMetric(name, typ, t...)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn m\n}","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/metrics/registry.go#L40-L76","documentation":"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.","triggerScenarios":"Thrown at metrics/registry.go:58 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Use a different name for the new metric","Make all declarations of the metric use the same type"],"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"}