Files
loki/pkg/logql/log/labels_stringlabels.go
Charles Korn 545fb1597c refactor(stringlabels): Support stringlabels in logql/log package (#17838)
This is another step towards supporting Prometheus' stringlabels implementation in Loki. It adds support in the `logql/log` package.

Part of https://github.com/grafana/loki/issues/17122

The tests should now compile and pass with `-tags stringlabels`.

---
Co-authored-by: Karsten Jeschkies <karsten.jeschkies@grafana.com>
2025-06-04 06:39:25 +00:00

44 lines
1.3 KiB
Go

//go:build stringlabels
package log
import "github.com/prometheus/prometheus/model/labels"
type hasher struct{}
// newHasher returns a hasher that computes hashes for labels.
func newHasher() *hasher {
return &hasher{}
}
// Hash computes a hash of lbs.
// It is not guaranteed to be stable across different Loki processes or versions.
func (h *hasher) Hash(lbs labels.Labels) uint64 {
// We use Hash() here because there's no performance advantage to using HashWithoutLabels() with stringlabels.
// The results from Hash(l) and HashWithoutLabels(l, []string{}) are different with stringlabels, so using Hash
// here also simplifies our tests.
return lbs.Hash()
}
// BufferedLabelsBuilder is a simple builder that uses a label buffer passed in.
// It is used to avoid allocations when building labels.
type BufferedLabelsBuilder struct {
builder *labels.Builder
}
func NewBufferedLabelsBuilder(l labels.Labels) *BufferedLabelsBuilder {
return &BufferedLabelsBuilder{builder: labels.NewBuilder(l)}
}
func (b *BufferedLabelsBuilder) Reset() {
b.builder.Reset(labels.EmptyLabels())
}
func (b *BufferedLabelsBuilder) Add(label labels.Label) {
b.builder.Set(label.Name, label.Value)
}
func (b *BufferedLabelsBuilder) Labels() labels.Labels {
return b.builder.Labels()
}