Merge pull request #503 from grafana/fixup-labels-usage3

labels: add stringlabels version of StableHash
This commit is contained in:
Oleg Zaytsev 2023-05-05 13:11:00 +02:00 committed by GitHub
commit e5eb66f422
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 86 additions and 0 deletions

View file

@ -28,3 +28,27 @@ jobs:
- name: Run Tests - name: Run Tests
run: GO=/usr/local/go/bin/go make common-test 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

View file

@ -1,3 +1,5 @@
//go:build !stringlabels
package labels package labels
import ( import (

View 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)
}

View 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))
}
}