mirror of
https://github.com/prometheus/prometheus.git
synced 2025-02-02 08:31:11 -08:00
Merge pull request #503 from grafana/fixup-labels-usage3
labels: add stringlabels version of StableHash
This commit is contained in:
commit
e5eb66f422
24
.github/workflows/test.yml
vendored
24
.github/workflows/test.yml
vendored
|
@ -28,3 +28,27 @@ jobs:
|
|||
|
||||
- name: Run Tests
|
||||
run: GO=/usr/local/go/bin/go make common-test
|
||||
|
||||
test-stringlabels:
|
||||
runs-on: ubuntu-20.04
|
||||
steps:
|
||||
- name: Upgrade golang
|
||||
run: |
|
||||
cd /tmp
|
||||
wget https://dl.google.com/go/go1.20.3.linux-amd64.tar.gz
|
||||
tar -zxvf go1.20.3.linux-amd64.tar.gz
|
||||
sudo rm -fr /usr/local/go
|
||||
sudo mv /tmp/go /usr/local/go
|
||||
cd -
|
||||
ls -l /usr/bin/go
|
||||
|
||||
- name: Checkout Repo
|
||||
uses: actions/checkout@v2
|
||||
|
||||
# This file would normally be created by `make assets`, here we just
|
||||
# mock it because the file is required for the tests to pass.
|
||||
- name: Mock building of necessary react file
|
||||
run: mkdir web/ui/static/react && touch web/ui/static/react/index.html
|
||||
|
||||
- name: Run Tests -tags=stringlabels
|
||||
run: GO=/usr/local/go/bin/go GOOPTS=-tags=stringlabels make common-test
|
|
@ -1,3 +1,5 @@
|
|||
//go:build !stringlabels
|
||||
|
||||
package labels
|
||||
|
||||
import (
|
||||
|
|
41
model/labels/sharding_stringlabels.go
Normal file
41
model/labels/sharding_stringlabels.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
//go:build stringlabels
|
||||
|
||||
package labels
|
||||
|
||||
import (
|
||||
"github.com/cespare/xxhash/v2"
|
||||
)
|
||||
|
||||
// StableHash is a labels hashing implementation which is guaranteed to not change over time.
|
||||
// This function should be used whenever labels hashing backward compatibility must be guaranteed.
|
||||
func StableHash(ls Labels) uint64 {
|
||||
// Use xxhash.Sum64(b) for fast path as it's faster.
|
||||
b := make([]byte, 0, 1024)
|
||||
var h *xxhash.Digest
|
||||
for i := 0; i < len(ls.data); {
|
||||
var v Label
|
||||
v.Name, i = decodeString(ls.data, i)
|
||||
v.Value, i = decodeString(ls.data, i)
|
||||
if h == nil && len(b)+len(v.Name)+len(v.Value)+2 >= cap(b) {
|
||||
// If labels entry is 1KB+, switch to Write API. Copy in the values up to this point.
|
||||
h = xxhash.New()
|
||||
_, _ = h.Write(b)
|
||||
}
|
||||
if h != nil {
|
||||
_, _ = h.WriteString(v.Name)
|
||||
_, _ = h.Write(seps)
|
||||
_, _ = h.WriteString(v.Value)
|
||||
_, _ = h.Write(seps)
|
||||
continue
|
||||
}
|
||||
|
||||
b = append(b, v.Name...)
|
||||
b = append(b, seps[0])
|
||||
b = append(b, v.Value...)
|
||||
b = append(b, seps[0])
|
||||
}
|
||||
if h != nil {
|
||||
return h.Sum64()
|
||||
}
|
||||
return xxhash.Sum64(b)
|
||||
}
|
19
model/labels/sharding_test.go
Normal file
19
model/labels/sharding_test.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
package labels
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// TestStableHash tests that StableHash is stable.
|
||||
// The hashes this test asserts should not be changed.
|
||||
func TestStableHash(t *testing.T) {
|
||||
for expectedHash, lbls := range map[uint64]Labels{
|
||||
0xef46db3751d8e999: EmptyLabels(),
|
||||
0x347c8ee7a9e29708: FromStrings("hello", "world"),
|
||||
0xcbab40540f26097d: FromStrings(MetricName, "metric", "label", "value"),
|
||||
} {
|
||||
require.Equal(t, expectedHash, StableHash(lbls))
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue