Skip to content

[Experiment] fastdto for MakePairLabels (on top of #1734) #1746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion prometheus/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"sync/atomic"
"time"

"github.com/prometheus/client_golang/prometheus/internal/fastdto"

dto "github.com/prometheus/client_model/go"
"google.golang.org/protobuf/types/known/timestamppb"
)
Expand Down Expand Up @@ -94,7 +96,7 @@ func NewCounter(opts CounterOpts) Counter {
if opts.now == nil {
opts.now = time.Now
}
result := &counter{desc: desc, labelPairs: desc.constLabelPairs, now: opts.now}
result := &counter{desc: desc, labelPairs: fastdto.ToDTOLabelPair(desc.labelPairs), now: opts.now}
result.init(result) // Init self-collection.
result.createdTs = timestamppb.New(opts.now())
return result
Expand Down
52 changes: 37 additions & 15 deletions prometheus/desc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ import (
"sort"
"strings"

"github.com/prometheus/client_golang/prometheus/internal/fastdto"

"github.com/cespare/xxhash/v2"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/model"
"google.golang.org/protobuf/proto"

"github.com/prometheus/client_golang/prometheus/internal"
)

// Desc is the descriptor used by every Prometheus Metric. It is essentially
Expand All @@ -47,12 +45,16 @@ type Desc struct {
fqName string
// help provides some helpful information about this metric.
help string
// constLabelPairs contains precalculated DTO label pairs based on
// the constant labels.
constLabelPairs []*dto.LabelPair
// variableLabels contains names of labels and normalization function for
// which the metric maintains variable values.
variableLabels *compiledLabels
// variableLabelOrder maps variableLabels indexes to the position in the
// pre-computed labelPairs slice. This allows fast MakeLabelPair function
// that have to place ordered variable label values into pre-sorted labelPairs.
variableLabelOrder []int
// labelPairs contains the sorted DTO label pairs based on the constant labels
// and variable labels
labelPairs []fastdto.LabelPair
// id is a hash of the values of the ConstLabels and fqName. This
// must be unique among all registered descriptors and can therefore be
// used as an identifier of the descriptor.
Expand Down Expand Up @@ -160,14 +162,31 @@ func (v2) NewDesc(fqName, help string, variableLabels ConstrainableLabels, const
}
d.dimHash = xxh.Sum64()

d.constLabelPairs = make([]*dto.LabelPair, 0, len(constLabels))
d.labelPairs = make([]fastdto.LabelPair, len(constLabels)+len(d.variableLabels.names))
i := 0
for n, v := range constLabels {
d.constLabelPairs = append(d.constLabelPairs, &dto.LabelPair{
Name: proto.String(n),
Value: proto.String(v),
})
d.labelPairs[i].Name = n
d.labelPairs[i].Value = v
i++
}
for _, labelName := range d.variableLabels.names {
d.labelPairs[i].Name = labelName
i++
}
sort.Sort(fastdto.LabelPairSorter(d.labelPairs))

d.variableLabelOrder = make([]int, len(d.variableLabels.names))
for outputIndex, pair := range d.labelPairs {
// Constant labels have values variable labels do not.
if pair.Value != "" {
continue
}
for sourceIndex, variableLabel := range d.variableLabels.names {
if variableLabel == pair.GetName() {
d.variableLabelOrder[sourceIndex] = outputIndex
}
}
}
sort.Sort(internal.LabelPairSorter(d.constLabelPairs))
return d
}

Expand All @@ -182,8 +201,11 @@ func NewInvalidDesc(err error) *Desc {
}

func (d *Desc) String() string {
lpStrings := make([]string, 0, len(d.constLabelPairs))
for _, lp := range d.constLabelPairs {
lpStrings := make([]string, 0, len(d.labelPairs))
for _, lp := range d.labelPairs {
if lp.Value == "" {
continue
}
lpStrings = append(
lpStrings,
fmt.Sprintf("%s=%q", lp.GetName(), lp.GetValue()),
Expand Down
41 changes: 41 additions & 0 deletions prometheus/desc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package prometheus

import (
"fmt"
"testing"
)

Expand Down Expand Up @@ -61,3 +62,43 @@ func TestNewInvalidDesc_String(t *testing.T) {
t.Errorf("String: unexpected output: %s", desc.String())
}
}

/*
export bench=newDesc && go test ./prometheus \
-run '^$' -bench '^BenchmarkNewDesc/labels=10' \
-benchtime 5s -benchmem -cpu 2 -timeout 999m \
-memprofile=${bench}.mem.pprof \
| tee ${bench}.txt

export bench=newDesc-v2 && go test ./prometheus \
-run '^$' -bench '^BenchmarkNewDesc' \
-benchtime 5s -benchmem -count=6 -cpu 2 -timeout 999m \
| tee ${bench}.txt
*/
func BenchmarkNewDesc(b *testing.B) {
for _, bm := range []struct {
labelCount int
descFunc func() *Desc
}{
{
labelCount: 1,
descFunc: new1LabelDescFunc,
},
{
labelCount: 3,
descFunc: new3LabelsDescFunc,
},
{
labelCount: 10,
descFunc: new10LabelsDescFunc,
},
} {
b.Run(fmt.Sprintf("labels=%v", bm.labelCount), func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
bm.descFunc()
}
})
}
}
4 changes: 3 additions & 1 deletion prometheus/gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"sync/atomic"
"time"

"github.com/prometheus/client_golang/prometheus/internal/fastdto"

dto "github.com/prometheus/client_model/go"
)

Expand Down Expand Up @@ -82,7 +84,7 @@ func NewGauge(opts GaugeOpts) Gauge {
nil,
opts.ConstLabels,
)
result := &gauge{desc: desc, labelPairs: desc.constLabelPairs}
result := &gauge{desc: desc, labelPairs: fastdto.ToDTOLabelPair(desc.labelPairs)}
result.init(result) // Init self-collection.
return result
}
Expand Down
7 changes: 1 addition & 6 deletions prometheus/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,12 +537,7 @@ func newHistogram(desc *Desc, opts HistogramOpts, labelValues ...string) Histogr
panic(makeInconsistentCardinalityError(desc.fqName, desc.variableLabels.names, labelValues))
}

for _, n := range desc.variableLabels.names {
if n == bucketLabel {
panic(errBucketLabelNotAllowed)
}
}
for _, lp := range desc.constLabelPairs {
for _, lp := range desc.labelPairs {
if lp.GetName() == bucketLabel {
panic(errBucketLabelNotAllowed)
}
Expand Down
58 changes: 58 additions & 0 deletions prometheus/internal/fastdto/labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2025 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package fastdto

import (
dto "github.com/prometheus/client_model/go"
)

type LabelPair struct {
Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
Value string `protobuf:"bytes,2,opt,name=value" json:"value,omitempty"`
}

func (p LabelPair) GetName() string {
return p.Name
}

func (p LabelPair) GetValue() string {
return p.Value
}

// LabelPairSorter implements sort.Interface. It is used to sort a slice of
// LabelPairs
type LabelPairSorter []LabelPair

func (s LabelPairSorter) Len() int {
return len(s)
}

func (s LabelPairSorter) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}

func (s LabelPairSorter) Less(i, j int) bool {
return s[i].Name < s[j].Name
}

func ToDTOLabelPair(in []LabelPair) []*dto.LabelPair {
ret := make([]*dto.LabelPair, len(in))
for i := range in {
ret[i] = &dto.LabelPair{
Name: &(in[i].Name),
Value: &(in[i].Value),
}
}
return ret
}
71 changes: 71 additions & 0 deletions prometheus/internal/fastdto/labels_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2025 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package fastdto

import (
"sort"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
dto "github.com/prometheus/client_model/go"
"google.golang.org/protobuf/proto"
)

func BenchmarkToDTOLabelPairs(b *testing.B) {
test := []LabelPair{
{"foo", "bar"},
{"foo2", "bar2"},
{"foo3", "bar3"},
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = ToDTOLabelPair(test)
}
}

func TestLabelPairSorter(t *testing.T) {
test := []LabelPair{
{"foo3", "bar3"},
{"foo", "bar"},
{"foo2", "bar2"},
}
sort.Sort(LabelPairSorter(test))

expected := []LabelPair{
{"foo", "bar"},
{"foo2", "bar2"},
{"foo3", "bar3"},
}
if diff := cmp.Diff(test, expected); diff != "" {
t.Fatal(diff)
}
}

func TestToDTOLabelPair(t *testing.T) {
test := []LabelPair{
{"foo", "bar"},
{"foo2", "bar2"},
{"foo3", "bar3"},
}
expected := []*dto.LabelPair{
{Name: proto.String("foo"), Value: proto.String("bar")},
{Name: proto.String("foo2"), Value: proto.String("bar2")},
{Name: proto.String("foo3"), Value: proto.String("bar3")},
}
if diff := cmp.Diff(ToDTOLabelPair(test), expected, cmpopts.IgnoreUnexported(dto.LabelPair{})); diff != "" {
t.Fatal(diff)
}
}
14 changes: 3 additions & 11 deletions prometheus/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -962,24 +962,16 @@ func checkDescConsistency(
}

// Is the desc consistent with the content of the metric?
lpsFromDesc := make([]*dto.LabelPair, len(desc.constLabelPairs), len(dtoMetric.Label))
copy(lpsFromDesc, desc.constLabelPairs)
for _, l := range desc.variableLabels.names {
lpsFromDesc = append(lpsFromDesc, &dto.LabelPair{
Name: proto.String(l),
})
}
if len(lpsFromDesc) != len(dtoMetric.Label) {
if len(desc.labelPairs) != len(dtoMetric.Label) {
return fmt.Errorf(
"labels in collected metric %s %s are inconsistent with descriptor %s",
metricFamily.GetName(), dtoMetric, desc,
)
}
sort.Sort(internal.LabelPairSorter(lpsFromDesc))
for i, lpFromDesc := range lpsFromDesc {
for i, lpFromDesc := range desc.labelPairs {
lpFromMetric := dtoMetric.Label[i]
if lpFromDesc.GetName() != lpFromMetric.GetName() ||
lpFromDesc.Value != nil && lpFromDesc.GetValue() != lpFromMetric.GetValue() {
lpFromDesc.Value != "" && lpFromDesc.GetValue() != lpFromMetric.GetValue() {
return fmt.Errorf(
"labels in collected metric %s %s are inconsistent with descriptor %s",
metricFamily.GetName(), dtoMetric, desc,
Expand Down
7 changes: 1 addition & 6 deletions prometheus/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,7 @@ func newSummary(desc *Desc, opts SummaryOpts, labelValues ...string) Summary {
panic(makeInconsistentCardinalityError(desc.fqName, desc.variableLabels.names, labelValues))
}

for _, n := range desc.variableLabels.names {
if n == quantileLabel {
panic(errQuantileLabelNotAllowed)
}
}
for _, lp := range desc.constLabelPairs {
for _, lp := range desc.labelPairs {
if lp.GetName() == quantileLabel {
panic(errQuantileLabelNotAllowed)
}
Expand Down
Loading
Loading