Skip to content

Commit 2a56112

Browse files
committed
Move changes behind build tag
Signed-off-by: Arve Knudsen <[email protected]>
1 parent 07b6764 commit 2a56112

File tree

72 files changed

+1707
-358
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+1707
-358
lines changed

examples/exemplars/main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
"strconv"
2424
"time"
2525

26+
"github.com/prometheus/common/model"
27+
2628
"github.com/prometheus/client_golang/prometheus"
2729
"github.com/prometheus/client_golang/prometheus/collectors"
2830
"github.com/prometheus/client_golang/prometheus/promhttp"
@@ -49,8 +51,11 @@ func main() {
4951
for {
5052
// Record fictional latency.
5153
now := time.Now()
52-
requestDurations.(prometheus.ExemplarObserver).ObserveWithExemplar(
53-
time.Since(now).Seconds(), prometheus.Labels{"dummyID": strconv.Itoa(rand.Intn(100000))},
54+
observeWithExemplar(
55+
requestDurations.(prometheus.ExemplarObserver),
56+
time.Since(now).Seconds(),
57+
prometheus.Labels{"dummyID": strconv.Itoa(rand.Intn(100000))},
58+
model.UTF8Validation,
5459
)
5560
time.Sleep(600 * time.Millisecond)
5661
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
13+
//go:build !localvalidationscheme
14+
15+
package main
16+
17+
import (
18+
"github.com/prometheus/common/model"
19+
20+
"github.com/prometheus/client_golang/prometheus"
21+
)
22+
23+
func observeWithExemplar(obs prometheus.ExemplarObserver, val float64, labels prometheus.Labels, _ model.ValidationScheme) {
24+
obs.ObserveWithExemplar(val, labels)
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
13+
//go:build localvalidationscheme
14+
15+
package main
16+
17+
import (
18+
"github.com/prometheus/common/model"
19+
20+
"github.com/prometheus/client_golang/prometheus"
21+
)
22+
23+
func observeWithExemplar(obs prometheus.ExemplarObserver, val float64, labels prometheus.Labels, scheme model.ValidationScheme) {
24+
obs.ObserveWithExemplar(val, labels, scheme)
25+
}

examples/random/main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
"strconv"
2626
"time"
2727

28+
"github.com/prometheus/common/model"
29+
2830
"github.com/prometheus/client_golang/prometheus"
2931
"github.com/prometheus/client_golang/prometheus/collectors"
3032
"github.com/prometheus/client_golang/prometheus/promhttp"
@@ -115,8 +117,11 @@ func main() {
115117
// already know that rpcDurationsHistogram implements
116118
// the ExemplarObserver interface and thus don't need to
117119
// check the outcome of the type assertion.
118-
m.rpcDurationsHistogram.(prometheus.ExemplarObserver).ObserveWithExemplar(
119-
v, prometheus.Labels{"dummyID": strconv.Itoa(rand.Intn(100000))},
120+
observeWithExemplar(
121+
m.rpcDurationsHistogram.(prometheus.ExemplarObserver),
122+
v,
123+
prometheus.Labels{"dummyID": strconv.Itoa(rand.Intn(100000))},
124+
model.UTF8Validation,
120125
)
121126
time.Sleep(time.Duration(75*oscillationFactor()) * time.Millisecond)
122127
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
13+
//go:build !localvalidationscheme
14+
15+
package main
16+
17+
import (
18+
"github.com/prometheus/common/model"
19+
20+
"github.com/prometheus/client_golang/prometheus"
21+
)
22+
23+
func observeWithExemplar(obs prometheus.ExemplarObserver, val float64, labels prometheus.Labels, _ model.ValidationScheme) {
24+
obs.ObserveWithExemplar(val, labels)
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
13+
//go:build localvalidationscheme
14+
15+
package main
16+
17+
import (
18+
"github.com/prometheus/common/model"
19+
20+
"github.com/prometheus/client_golang/prometheus"
21+
)
22+
23+
func observeWithExemplar(obs prometheus.ExemplarObserver, val float64, labels prometheus.Labels, scheme model.ValidationScheme) {
24+
obs.ObserveWithExemplar(val, labels, scheme)
25+
}

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ go 1.23.0
44

55
toolchain go1.24.4
66

7-
toolchain go1.24.4
8-
97
require (
108
github.com/beorn7/perks v1.0.1
119
github.com/cespare/xxhash/v2 v2.3.0

prometheus/counter.go

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"time"
2121

2222
dto "github.com/prometheus/client_model/go"
23+
"github.com/prometheus/common/model"
2324
"google.golang.org/protobuf/types/known/timestamppb"
2425
)
2526

@@ -44,19 +45,6 @@ type Counter interface {
4445
Add(float64)
4546
}
4647

47-
// ExemplarAdder is implemented by Counters that offer the option of adding a
48-
// value to the Counter together with an exemplar. Its AddWithExemplar method
49-
// works like the Add method of the Counter interface but also replaces the
50-
// currently saved exemplar (if any) with a new one, created from the provided
51-
// value, the current time as timestamp, and the provided labels. Empty Labels
52-
// will lead to a valid (label-less) exemplar. But if Labels is nil, the current
53-
// exemplar is left in place. AddWithExemplar panics if the value is < 0, if any
54-
// of the provided labels are invalid, or if the provided labels contain more
55-
// than 128 runes in total.
56-
type ExemplarAdder interface {
57-
AddWithExemplar(value float64, exemplar Labels)
58-
}
59-
6048
// CounterOpts is an alias for Opts. See there for doc comments.
6149
type CounterOpts Opts
6250

@@ -143,11 +131,6 @@ func (c *counter) Add(v float64) {
143131
}
144132
}
145133

146-
func (c *counter) AddWithExemplar(v float64, e Labels) {
147-
c.Add(v)
148-
c.updateExemplar(v, e)
149-
}
150-
151134
func (c *counter) Inc() {
152135
atomic.AddUint64(&c.valInt, 1)
153136
}
@@ -169,11 +152,11 @@ func (c *counter) Write(out *dto.Metric) error {
169152
return populateMetric(CounterValue, val, c.labelPairs, exemplar, out, c.createdTs)
170153
}
171154

172-
func (c *counter) updateExemplar(v float64, l Labels) {
155+
func (c *counter) updateExemplar(v float64, l Labels, scheme model.ValidationScheme) {
173156
if l == nil {
174157
return
175158
}
176-
e, err := newExemplar(v, c.now(), l)
159+
e, err := newExemplar(v, c.now(), l, scheme)
177160
if err != nil {
178161
panic(err)
179162
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2025 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
//go:build !localvalidationscheme
15+
16+
package prometheus
17+
18+
import "github.com/prometheus/common/model"
19+
20+
// ExemplarAdder is implemented by Counters that offer the option of adding a
21+
// value to the Counter together with an exemplar. Its AddWithExemplar method
22+
// works like the Add method of the Counter interface but also replaces the
23+
// currently saved exemplar (if any) with a new one, created from the provided
24+
// value, the current time as timestamp, and the provided labels. Empty Labels
25+
// will lead to a valid (label-less) exemplar. But if Labels is nil, the current
26+
// exemplar is left in place. AddWithExemplar panics if the value is < 0, if any
27+
// of the provided labels are invalid, or if the provided labels contain more
28+
// than 128 runes in total.
29+
type ExemplarAdder interface {
30+
AddWithExemplar(value float64, exemplar Labels)
31+
}
32+
33+
func (c *counter) AddWithExemplar(v float64, e Labels) {
34+
c.Add(v)
35+
//nolint:staticcheck // model.NameValidationScheme is being phased out.
36+
c.updateExemplar(v, e, model.NameValidationScheme)
37+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2025 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
//go:build !localvalidationscheme
15+
16+
package prometheus
17+
18+
import "github.com/prometheus/common/model"
19+
20+
func addToCounterWithExemplar(counter *counter, v float64, e Labels, _ model.ValidationScheme) {
21+
counter.AddWithExemplar(v, e)
22+
}

0 commit comments

Comments
 (0)