Merge pull request #13669 from aiwhj/optimization

[ENHANCEMENT] Relabeling: small speed-up of hashmod

Code optimization: The relabel operation is used very frequently, and strconv.FormatInt() with better performance should be used.
This commit is contained in:
Bryan Boreham 2024-05-14 12:54:00 +01:00 committed by GitHub
commit dc926527ae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -17,6 +17,7 @@ import (
"crypto/md5" "crypto/md5"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"strconv"
"strings" "strings"
"github.com/grafana/regexp" "github.com/grafana/regexp"
@ -290,7 +291,7 @@ func relabel(cfg *Config, lb *labels.Builder) (keep bool) {
hash := md5.Sum([]byte(val)) hash := md5.Sum([]byte(val))
// Use only the last 8 bytes of the hash to give the same result as earlier versions of this code. // Use only the last 8 bytes of the hash to give the same result as earlier versions of this code.
mod := binary.BigEndian.Uint64(hash[8:]) % cfg.Modulus mod := binary.BigEndian.Uint64(hash[8:]) % cfg.Modulus
lb.Set(cfg.TargetLabel, fmt.Sprintf("%d", mod)) lb.Set(cfg.TargetLabel, strconv.FormatUint(mod, 10))
case LabelMap: case LabelMap:
lb.Range(func(l labels.Label) { lb.Range(func(l labels.Label) {
if cfg.Regex.MatchString(l.Name) { if cfg.Regex.MatchString(l.Name) {