lib/promscrape: reduce CPU usage an memory allocations when constructing scrapeWorkKey

This commit is contained in:
Aliaksandr Valialkin 2021-02-28 22:29:34 +02:00
parent 17eb29206d
commit 109bfaadad

View file

@ -7,7 +7,6 @@ import (
"net/url" "net/url"
"path/filepath" "path/filepath"
"sort" "sort"
"strconv"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -720,25 +719,29 @@ func (stc *StaticConfig) appendScrapeWork(dst []*ScrapeWork, swc *scrapeWorkConf
} }
func (swc *scrapeWorkConfig) getScrapeWork(target string, extraLabels, metaLabels map[string]string) (*ScrapeWork, error) { func (swc *scrapeWorkConfig) getScrapeWork(target string, extraLabels, metaLabels map[string]string) (*ScrapeWork, error) {
key := getScrapeWorkKey(extraLabels, metaLabels) bb := scrapeWorkKeyBufPool.Get()
if needSkipScrapeWork(key) { defer scrapeWorkKeyBufPool.Put(bb)
bb.B = appendScrapeWorkKey(bb.B[:0], extraLabels, metaLabels)
keyStrUnsafe := bytesutil.ToUnsafeString(bb.B)
if needSkipScrapeWork(keyStrUnsafe) {
return nil, nil return nil, nil
} }
if sw := swc.cache.Get(key); sw != nil { if sw := swc.cache.Get(keyStrUnsafe); sw != nil {
return sw, nil return sw, nil
} }
sw, err := swc.getScrapeWorkReal(target, extraLabels, metaLabels) sw, err := swc.getScrapeWorkReal(target, extraLabels, metaLabels)
if err != nil { if err != nil {
swc.cache.Set(key, sw) swc.cache.Set(string(bb.B), sw)
} }
return sw, err return sw, err
} }
func getScrapeWorkKey(extraLabels, metaLabels map[string]string) string { var scrapeWorkKeyBufPool bytesutil.ByteBufferPool
var b []byte
b = appendSortedKeyValuePairs(b, extraLabels) func appendScrapeWorkKey(dst []byte, extraLabels, metaLabels map[string]string) []byte {
b = appendSortedKeyValuePairs(b, metaLabels) dst = appendSortedKeyValuePairs(dst, extraLabels)
return string(b) dst = appendSortedKeyValuePairs(dst, metaLabels)
return dst
} }
func needSkipScrapeWork(key string) bool { func needSkipScrapeWork(key string) bool {
@ -756,9 +759,10 @@ func appendSortedKeyValuePairs(dst []byte, m map[string]string) []byte {
} }
sort.Strings(keys) sort.Strings(keys)
for _, k := range keys { for _, k := range keys {
dst = strconv.AppendQuote(dst, k) // Do not use strconv.AppendQuote, since it is slow according to CPU profile.
dst = append(dst, ':') dst = append(dst, k...)
dst = strconv.AppendQuote(dst, m[k]) dst = append(dst, '=')
dst = append(dst, m[k]...)
dst = append(dst, ',') dst = append(dst, ',')
} }
dst = append(dst, '\n') dst = append(dst, '\n')