Skip to content

Commit

Permalink
replace remaining usage of hashNew() with xxhash
Browse files Browse the repository at this point in the history
  • Loading branch information
costela committed Oct 17, 2019
1 parent b292466 commit dfafbc6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 70 deletions.
42 changes: 0 additions & 42 deletions prometheus/fnv.go

This file was deleted.

50 changes: 30 additions & 20 deletions prometheus/vec.go
Expand Up @@ -17,9 +17,22 @@ import (
"fmt"
"sync"

"github.com/prometheus/common/model"
"github.com/cespare/xxhash/v2"
)

// hasher represents the partial API from xxhash, to enable replacing for
// collision tests
type hasher interface {
WriteString(string) (int, error)
Write([]byte) (int, error)
Sum64() uint64
}

// newHasher wraps xxhash.New to satisfy the hasher interface
func newHasher() hasher {
return xxhash.New()
}

// metricVec is a Collector to bundle metrics of the same name that differ in
// their label values. metricVec is not used directly (and therefore
// unexported). It is used as a building block for implementations of vectors of
Expand All @@ -30,9 +43,8 @@ type metricVec struct {

curry []curriedLabelValue

// hashAdd and hashAddByte can be replaced for testing collision handling.
hashAdd func(h uint64, s string) uint64
hashAddByte func(h uint64, b byte) uint64
// hasher can be replaced for testing collision handling.
newHasher func() hasher
}

// newMetricVec returns an initialized metricVec.
Expand All @@ -43,8 +55,7 @@ func newMetricVec(desc *Desc, newMetric func(lvs ...string) Metric) *metricVec {
desc: desc,
newMetric: newMetric,
},
hashAdd: hashAdd,
hashAddByte: hashAddByte,
newHasher: newHasher,
}
}

Expand Down Expand Up @@ -117,10 +128,9 @@ func (m *metricVec) curryWith(labels Labels) (*metricVec, error) {
}

return &metricVec{
metricMap: m.metricMap,
curry: newCurry,
hashAdd: m.hashAdd,
hashAddByte: m.hashAddByte,
metricMap: m.metricMap,
curry: newCurry,
newHasher: m.newHasher,
}, nil
}

Expand Down Expand Up @@ -148,21 +158,21 @@ func (m *metricVec) hashLabelValues(vals []string) (uint64, error) {
}

var (
h = hashNew()
h = m.newHasher()
curry = m.curry
iVals, iCurry int
)
for i := 0; i < len(m.desc.variableLabels); i++ {
if iCurry < len(curry) && curry[iCurry].index == i {
h = m.hashAdd(h, curry[iCurry].value)
h.WriteString(curry[iCurry].value)
iCurry++
} else {
h = m.hashAdd(h, vals[iVals])
h.WriteString(vals[iVals])
iVals++
}
h = m.hashAddByte(h, model.SeparatorByte)
h.Write(separatorByteSlice)
}
return h, nil
return h.Sum64(), nil
}

func (m *metricVec) hashLabels(labels Labels) (uint64, error) {
Expand All @@ -171,7 +181,7 @@ func (m *metricVec) hashLabels(labels Labels) (uint64, error) {
}

var (
h = hashNew()
h = m.newHasher()
curry = m.curry
iCurry int
)
Expand All @@ -181,17 +191,17 @@ func (m *metricVec) hashLabels(labels Labels) (uint64, error) {
if ok {
return 0, fmt.Errorf("label name %q is already curried", label)
}
h = m.hashAdd(h, curry[iCurry].value)
h.WriteString(curry[iCurry].value)
iCurry++
} else {
if !ok {
return 0, fmt.Errorf("label name %q missing in label map", label)
}
h = m.hashAdd(h, val)
h.WriteString(val)
}
h = m.hashAddByte(h, model.SeparatorByte)
h.Write(separatorByteSlice)
}
return h, nil
return h.Sum64(), nil
}

// metricWithLabelValues provides the metric and its label values for
Expand Down
18 changes: 10 additions & 8 deletions prometheus/vec_test.go
Expand Up @@ -20,6 +20,12 @@ import (
dto "github.com/prometheus/client_model/go"
)

type testHasher uint64

func (th testHasher) Write(b []byte) (int, error) { return len(b), nil }
func (th testHasher) WriteString(s string) (int, error) { return len(s), nil }
func (th testHasher) Sum64() uint64 { return uint64(th) }

func TestDelete(t *testing.T) {
vec := NewGaugeVec(
GaugeOpts{
Expand All @@ -39,8 +45,7 @@ func TestDeleteWithCollisions(t *testing.T) {
},
[]string{"l1", "l2"},
)
vec.hashAdd = func(h uint64, s string) uint64 { return 1 }
vec.hashAddByte = func(h uint64, b byte) uint64 { return 1 }
vec.newHasher = func() hasher { return testHasher(1) }
testDelete(t, vec)
}

Expand Down Expand Up @@ -93,8 +98,7 @@ func TestDeleteLabelValuesWithCollisions(t *testing.T) {
},
[]string{"l1", "l2"},
)
vec.hashAdd = func(h uint64, s string) uint64 { return 1 }
vec.hashAddByte = func(h uint64, b byte) uint64 { return 1 }
vec.newHasher = func() hasher { return testHasher(1) }
testDeleteLabelValues(t, vec)
}

Expand Down Expand Up @@ -144,8 +148,7 @@ func TestMetricVecWithCollisions(t *testing.T) {
},
[]string{"l1", "l2"},
)
vec.hashAdd = func(h uint64, s string) uint64 { return 1 }
vec.hashAddByte = func(h uint64, b byte) uint64 { return 1 }
vec.newHasher = func() hasher { return testHasher(1) }
testMetricVec(t, vec)
}

Expand Down Expand Up @@ -258,8 +261,7 @@ func TestCurryVecWithCollisions(t *testing.T) {
},
[]string{"one", "two", "three"},
)
vec.hashAdd = func(h uint64, s string) uint64 { return 1 }
vec.hashAddByte = func(h uint64, b byte) uint64 { return 1 }
vec.newHasher = func() hasher { return testHasher(1) }
testCurryVec(t, vec)
}

Expand Down

0 comments on commit dfafbc6

Please sign in to comment.