mirror of
https://github.com/hanzoai/metrics.git
synced 2026-10-30 20:03:39 +00:00
The prompb and prompbmarshal share exactly the same models and provide
marshal and unmarshale capabilities for them. This creates duplication
(changes in one model has to be made in another, case with metadata) and
confusion where for example you compare same looking models but golang
says they are not the same (because of the type).
This commit merge prompbmarshal logic into prompb so the rest of the
code is aligned on prompb models.
Moves samplesPool and labelsPool to WriteRequestUnmarshaller.
Make WriteRequest struct clean from unmarshal logic.
The benchmark shows no significant changes:
$benchstat prompbmarshal.bench prompb2.bench
goos: darwin
goarch: arm64
pkg: github.com/VictoriaMetrics/VictoriaMetrics/lib/prompb
cpu: Apple M1 Pro
│ prompbmarshal.bench │ prompb2.bench │
│ sec/op │ sec/op vs base │
WriteRequestUnmarshalProtobuf-10 189.2µ ± 5% 190.8µ ± 8% ~ (p=0.579 n=10)
WriteRequestMarshalProtobuf-10 145.3µ ± 7% 143.6µ ± 2% ~ (p=0.143 n=10)
geomean 165.8µ 165.5µ -0.14%
│ prompbmarshal.bench │ prompb2.bench │
│ B/s │ B/s vs base │
WriteRequestUnmarshalProtobuf-10 50.42Mi ± 5% 49.99Mi ± 8% ~ (p=0.593 n=10)
WriteRequestMarshalProtobuf-10 65.64Mi ± 7% 66.39Mi ± 2% ~ (p=0.143 n=10)
geomean 57.53Mi 57.61Mi +0.14%
│ prompbmarshal.bench │ prompb2.bench │
│ B/op │ B/op vs base │
WriteRequestUnmarshalProtobuf-10 27.70Ki ± 4% 26.90Ki ± 7% ~ (p=0.190 n=10)
WriteRequestMarshalProtobuf-10 3.267Ki ± 12% 3.273Ki ± 12% ~ (p=0.971 n=10)
geomean 9.514Ki 9.383Ki -1.38%
│ prompbmarshal.bench │ prompb2.bench │
│ allocs/op │ allocs/op vs base │
WriteRequestUnmarshalProtobuf-10 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹
WriteRequestMarshalProtobuf-10 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹
geomean ² +0.00% ²
¹ all samples are equal
² summaries must be >0 to compute geomean
84 lines
2.1 KiB
Go
84 lines
2.1 KiB
Go
package protoparserutil
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompb"
|
|
)
|
|
|
|
// GetExtraLabels extracts name:value labels from `extra_label=name=value` query args from req.
|
|
//
|
|
// It also extracts Pushgateways-compatible extra labels from req.URL.Path
|
|
// according to https://github.com/prometheus/pushgateway#url .
|
|
func GetExtraLabels(req *http.Request) ([]prompb.Label, error) {
|
|
labels, err := getPushgatewayLabels(req.URL.Path)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot parse pushgateway-style labels from %q: %w", req.URL.Path, err)
|
|
}
|
|
q := req.URL.Query()
|
|
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)
|
|
}
|
|
labels = append(labels, prompb.Label{
|
|
Name: tmp[0],
|
|
Value: tmp[1],
|
|
})
|
|
}
|
|
return labels, nil
|
|
}
|
|
|
|
func getPushgatewayLabels(path string) ([]prompb.Label, error) {
|
|
n := strings.Index(path, "/metrics/job")
|
|
if n < 0 {
|
|
return nil, nil
|
|
}
|
|
s := path[n+len("/metrics/"):]
|
|
if !strings.HasPrefix(s, "job/") && !strings.HasPrefix(s, "job@base64/") {
|
|
return nil, nil
|
|
}
|
|
labelsCount := (strings.Count(s, "/") + 1) / 2
|
|
labels := make([]prompb.Label, 0, labelsCount)
|
|
for len(s) > 0 {
|
|
n := strings.IndexByte(s, '/')
|
|
if n < 0 {
|
|
return nil, fmt.Errorf("missing value for label %q", s)
|
|
}
|
|
name := s[:n]
|
|
s = s[n+1:]
|
|
isBase64 := strings.HasSuffix(name, "@base64")
|
|
if isBase64 {
|
|
name = name[:len(name)-len("@base64")]
|
|
}
|
|
var value string
|
|
n = strings.IndexByte(s, '/')
|
|
if n < 0 {
|
|
value = s
|
|
s = ""
|
|
} else {
|
|
value = s[:n]
|
|
s = s[n+1:]
|
|
}
|
|
if isBase64 {
|
|
data, err := base64.RawURLEncoding.DecodeString(strings.TrimRight(value, "="))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot base64-decode value=%q for label=%q: %w", value, name, err)
|
|
}
|
|
value = string(data)
|
|
}
|
|
if len(value) == 0 {
|
|
// Skip labels with empty values
|
|
continue
|
|
}
|
|
labels = append(labels, prompb.Label{
|
|
Name: name,
|
|
Value: value,
|
|
})
|
|
}
|
|
return labels, nil
|
|
}
|