{"record":{"id":"28f48810aa028667","repo":"grafana/k6","slug":"invalid-metric-type","errorCode":null,"errorMessage":"invalid metric type","messagePattern":"invalid metric type","errorType":"validation","errorClass":"ErrInvalidMetricType","httpStatus":null,"severity":"error","filePath":"metrics/metric_type.go","lineNumber":20,"sourceCode":"\nimport (\n\t\"errors\"\n\t\"slices\"\n)\n\n// A MetricType specifies the type of a metric.\ntype MetricType int\n\n// Possible values for MetricType.\nconst (\n\tCounter = MetricType(iota) // A counter that sums its data points\n\tGauge                      // A gauge that displays the latest value\n\tTrend                      // A trend, min/max/avg/med are interesting\n\tRate                       // A rate, displays % of values that aren't 0\n)\n\n// ErrInvalidMetricType indicates the serialized metric type is invalid.\nvar ErrInvalidMetricType = errors.New(\"invalid metric type\")\n\nconst (\n\tcounterString = \"counter\"\n\tgaugeString   = \"gauge\"\n\ttrendString   = \"trend\"\n\trateString    = \"rate\"\n\n\tdefaultString = \"default\"\n\ttimeString    = \"time\"\n\tdataString    = \"data\"\n)\n\n// MarshalJSON serializes a MetricType as a human readable string.\nfunc (t MetricType) MarshalJSON() ([]byte, error) {\n\ttxt, err := t.MarshalText()\n\tif err != nil {\n\t\treturn nil, err\n\t}","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/metrics/metric_type.go#L2-L38","documentation":"ErrInvalidMetricType (metrics/metric_type.go:20) is returned by MetricType.UnmarshalText (line 70) when the string being deserialized is not one of 'counter', 'gauge', 'trend', 'rate' — and symmetrically by MarshalText (line 54) for an out-of-range MetricType integer. It surfaces through UnmarshalJSON/MarshalJSON whenever a metric's serialized type round-trips: registry JSON, k6 archive inspection, or summary/output serialization.","triggerScenarios":"json.Unmarshal into a struct with a metrics.MetricType field given '\"histogram\"', '\"COUNTER\"' (case-sensitive), or '\"\"'; marshaling a MetricType constructed from an invalid int like MetricType(7).","commonSituations":"Consuming metric payloads from old k6 versions or third-party tools that emit legacy type names (e.g. pre-0.4 'histogram' types); hand-built JSON metric definitions with typos or wrong casing; corrupted exported archives.","solutions":["Use exactly one of: counter, gauge, trend, rate (lowercase)","If interoperating with an older format, map legacy names to the four supported types before unmarshaling","When the value comes from user input, validate it against the accepted set before decoding"],"exampleFix":"// before\nvar mt metrics.MetricType\nerr := json.Unmarshal([]byte(`\"Histogram\"`), &mt) // ErrInvalidMetricType\n\n// after\nvar mt metrics.MetricType\nerr := json.Unmarshal([]byte(`\"trend\"`), &mt)","handlingStrategy":"type-guard","validationCode":"// Go: whitelist check before unmarshaling external strings\nvar validMetricTypes = map[string]bool{\"counter\": true, \"gauge\": true, \"trend\": true, \"rate\": true}\nif !validMetricTypes[input] {\n    return fmt.Errorf(\"unsupported metric type %q; want counter|gauge|trend|rate\", input)\n}","typeGuard":"// Go\nfunc isValidMetricType(s string) bool {\n    switch s {\n    case \"counter\", \"gauge\", \"trend\", \"rate\":\n        return true\n    }\n    return false\n}","tryCatchPattern":"// Go\nif err := json.Unmarshal(data, &mt); err != nil {\n    if errors.Is(err, metrics.ErrInvalidMetricType) {\n        // map legacy names (e.g. \"histogram\" -> \"trend\") and retry, or reject input\n    }\n}","preventionTips":["k6 metric types are exactly counter, gauge, trend, rate — lowercase and case-sensitive","When ingesting third-party metric JSON, normalize type names before decoding"],"tags":["metrics","serialization","json","validation"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}