VictoriaMetrics/lib/protoparser/common/extra_labels.go
Aliaksandr Valialkin 4fa97430d7 app/{vminsert,vmagent}: allow adding extra labels when importing data via Prometheus, CSV and JSON line formats
Extra labels may be added to the imported data by passing `extra_label=name=value` query args.
Multiple query args may be passed in order to add multiple extra labels.

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/719
2020-09-02 19:47:02 +03:00

26 lines
668 B
Go

package common
import (
"fmt"
"net/http"
"strings"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
)
// GetExtraLabels extracts name:value labels from `extra_label=name=value` query args from req.
func GetExtraLabels(req *http.Request) ([]prompbmarshal.Label, error) {
q := req.URL.Query()
var result []prompbmarshal.Label
for _, label := range q["extra_label"] {
tmp := strings.SplitN(label, "=", 2)
if len(tmp) != 2 {
return nil, fmt.Errorf("`extra_label` query arg must have the format `name=value`; got %q", label)
}
result = append(result, prompbmarshal.Label{
Name: tmp[0],
Value: tmp[1],
})
}
return result, nil
}