prometheus / common

Go libraries shared across Prometheus components and libraries.
Apache License 2.0
259 stars 306 forks source link

LabelSet: Fix alphabetical sorting for prometheus LabelSet #575

Closed wasim-nihal closed 4 months ago

wasim-nihal commented 4 months ago

Fixes: https://github.com/prometheus/common/issues/543

Below are the benchmark results (tested locally) comparing the performance of the current LabelSet.String() and the proposed solution (included in this PR)

// Benchmark Results for LabelSet's String() method
// ---------------------------------------------------------------------------------------------------------
// Existing LabelSet's String() -> (sorting of `<labelName>="<labelValue>"`)
// goos: linux
// goarch: amd64
// pkg: github.com/prometheus/common/model
// cpu: 11th Gen Intel(R) Core(TM) i5-1145G7 @ 2.60GHz
// BenchmarkLabelSetStringMethod-8                                717866              1647 ns/op
// ---------------------------------------------------------------------------------------------------------
// This PR -> (Alphabetical sorting of LabelSet by labelName)
// goos: linux
// goarch: amd64
// pkg: github.com/prometheus/common/model
// cpu: 11th Gen Intel(R) Core(TM) i5-1145G7 @ 2.60GHz
// BenchmarkLabelSetStringMethod-8                               732376              1532 ns/op

Reviewers, please share your input on these changes or alternative approaches. I'll address them in the next commits.