diff --git a/app/vmagent/remotewrite/obfuscation.go b/app/vmagent/remotewrite/obfuscation.go index 57f51eff64..0a99ec48e0 100644 --- a/app/vmagent/remotewrite/obfuscation.go +++ b/app/vmagent/remotewrite/obfuscation.go @@ -5,29 +5,23 @@ import ( "encoding/hex" "strings" - "github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil" "github.com/VictoriaMetrics/VictoriaMetrics/lib/prompb" ) func (rwctx *remoteWriteCtx) initObfuscationConfig() { idx := rwctx.idx - if len(*obfuscatedLabels) != 0 { - rwctx.obfuscatedLabels = make(map[string]struct{}) - rwObfuscatedLabels := obfuscatedLabels.GetOptionalArg(idx) - rwObfuscatedLabelsList := strings.Split(rwObfuscatedLabels, "^^") - for _, label := range rwObfuscatedLabelsList { - rwctx.obfuscatedLabels[label] = struct{}{} + if len(*obfuscationLabels) != 0 { + rwctx.obfuscationLabels = make(map[string]struct{}) + rwObfuscationLabels := obfuscationLabels.GetOptionalArg(idx) + rwObfuscationLabelsList := strings.Split(rwObfuscationLabels, "^^") + for _, label := range rwObfuscationLabelsList { + rwctx.obfuscationLabels[label] = struct{}{} } } } -var obfuscationBufPool bytesutil.ByteBufferPool - func (rwctx *remoteWriteCtx) applyObfuscation(tss []prompb.TimeSeries) []prompb.TimeSeries { - if len(rwctx.obfuscatedLabels) == 0 { - return tss - } - if len(rwctx.obfuscatedLabels) == 0 { + if len(rwctx.obfuscationLabels) == 0 || len(tss) == 0 { return tss } cacheObfuscatedResult := make(map[string]string) @@ -36,18 +30,15 @@ func (rwctx *remoteWriteCtx) applyObfuscation(tss []prompb.TimeSeries) []prompb. labels := ts.Labels for j := range labels { label := &labels[j] - if _, ok := rwctx.obfuscatedLabels[label.Name]; !ok { + if _, ok := rwctx.obfuscationLabels[label.Name]; !ok { continue } - if obfuscatedValue, ok := cacheObfuscatedResult[label.Name]; ok { + if obfuscatedValue, ok := cacheObfuscatedResult[label.Value]; ok { label.Value = obfuscatedValue } else { - bb := obfuscationBufPool.Get() - bb.Write([]byte(label.Value)) - obfuscatedResult := sha256.Sum256(bb.B) - cacheObfuscatedResult[label.Name] = hex.EncodeToString(obfuscatedResult[:]) - label.Value = cacheObfuscatedResult[label.Name] - obfuscationBufPool.Put(bb) + obfuscatedResult := sha256.Sum256([]byte(label.Value)) + cacheObfuscatedResult[label.Value] = hex.EncodeToString(obfuscatedResult[:]) + label.Value = cacheObfuscatedResult[label.Value] } } } diff --git a/app/vmagent/remotewrite/remotewrite.go b/app/vmagent/remotewrite/remotewrite.go index 918473b10c..8b8b2a52c2 100644 --- a/app/vmagent/remotewrite/remotewrite.go +++ b/app/vmagent/remotewrite/remotewrite.go @@ -103,8 +103,8 @@ var ( disableMetadataPerURL = flagutil.NewArrayBool("remoteWrite.disableMetadata", "Whether to disable sending metadata to the corresponding -remoteWrite.url. "+ "By default, metadata sending is controlled by the global -enableMetadata flag") - obfuscatedLabels = flagutil.NewArrayString("remoteWrite.obfuscatedLabels", "Whether to disable sending metadata to the corresponding -remoteWrite.url. "+ - "By default, metadata sending is controlled by the global -enableMetadata flag") + obfuscationLabels = flagutil.NewArrayString("remoteWrite.obfuscationLabels", "List of label names whose values must be obfuscated before sending to the corresponding -remoteWrite.url."+ + "By default, label obfuscation is disabled") ) var ( @@ -836,7 +836,7 @@ type remoteWriteCtx struct { pss []*pendingSeries pssNextIdx atomic.Uint64 - obfuscatedLabels map[string]struct{} + obfuscationLabels map[string]struct{} rowsPushedAfterRelabel *metrics.Counter rowsDroppedByRelabel *metrics.Counter @@ -1034,10 +1034,12 @@ func (rwctx *remoteWriteCtx) TryPushTimeSeries(tss []prompb.TimeSeries, forceDro matchIdxsPool.Put(matchIdxs) } - if len(rwctx.obfuscatedLabels) != 0 { - // Make a copy of tss - v = tssPool.Get().(*[]prompb.TimeSeries) - tss = append(*v, tss...) + if len(rwctx.obfuscationLabels) != 0 { + if rctx == nil { + rctx = getRelabelCtx() + v = tssPool.Get().(*[]prompb.TimeSeries) + tss = append(*v, tss...) + } tss = rwctx.applyObfuscation(tss) } if rwctx.deduplicator != nil {