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
115 lines
2.3 KiB
Go
115 lines
2.3 KiB
Go
package rule
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/datasource"
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompb"
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promrelabel"
|
|
)
|
|
|
|
// newTimeSeries first sorts given labels, then returns new time series.
|
|
func newTimeSeries(values []float64, timestamps []int64, labels []prompb.Label) prompb.TimeSeries {
|
|
promrelabel.SortLabels(labels)
|
|
ts := prompb.TimeSeries{
|
|
Labels: labels,
|
|
Samples: make([]prompb.Sample, len(values)),
|
|
}
|
|
for i := range values {
|
|
ts.Samples[i] = prompb.Sample{
|
|
Value: values[i],
|
|
Timestamp: time.Unix(timestamps[i], 0).UnixNano() / 1e6,
|
|
}
|
|
}
|
|
return ts
|
|
}
|
|
|
|
type curlWriter struct {
|
|
b strings.Builder
|
|
}
|
|
|
|
func (cw *curlWriter) string() string {
|
|
res := "curl " + cw.b.String()
|
|
cw.b.Reset()
|
|
return strings.TrimSpace(res)
|
|
}
|
|
|
|
func (cw *curlWriter) addWithEsc(str string) {
|
|
escStr := `'` + strings.ReplaceAll(str, `'`, `'\''`) + `'`
|
|
cw.add(escStr)
|
|
}
|
|
|
|
func (cw *curlWriter) add(str string) {
|
|
cw.b.WriteString(str)
|
|
cw.b.WriteString(" ")
|
|
}
|
|
|
|
func requestToCurl(req *http.Request) string {
|
|
if req == nil || req.URL == nil {
|
|
return ""
|
|
}
|
|
|
|
cw := &curlWriter{}
|
|
|
|
schema := req.URL.Scheme
|
|
requestURL := req.URL.String()
|
|
if !datasource.ShowDatasourceURL() {
|
|
requestURL = req.URL.Redacted()
|
|
}
|
|
if schema == "" {
|
|
schema = "http"
|
|
if req.TLS != nil {
|
|
schema = "https"
|
|
}
|
|
requestURL = schema + "://" + req.Host + requestURL
|
|
}
|
|
|
|
if schema == "https" {
|
|
cw.add("-k")
|
|
}
|
|
|
|
cw.add("-X")
|
|
cw.add(req.Method)
|
|
|
|
var keys []string
|
|
for k := range req.Header {
|
|
keys = append(keys, k)
|
|
}
|
|
sort.Strings(keys)
|
|
|
|
for _, k := range keys {
|
|
cw.add("-H")
|
|
if !datasource.ShowDatasourceURL() && isSecreteHeader(k) {
|
|
cw.addWithEsc(fmt.Sprintf("%s: <secret>", k))
|
|
continue
|
|
}
|
|
cw.addWithEsc(fmt.Sprintf("%s: %s", k, strings.Join(req.Header[k], " ")))
|
|
}
|
|
|
|
cw.addWithEsc(requestURL)
|
|
return cw.string()
|
|
}
|
|
|
|
var secretWords = []string{"auth", "pass", "key", "secret", "token"}
|
|
|
|
func isSecreteHeader(str string) bool {
|
|
s := strings.ToLower(str)
|
|
for _, secret := range secretWords {
|
|
if strings.Contains(s, secret) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func isPartialResponse(res datasource.Result) bool {
|
|
if res.IsPartial != nil && *res.IsPartial {
|
|
return true
|
|
}
|
|
return false
|
|
}
|