Skip to content

Commit 5efce43

Browse files
committed
update examples on promauto adapter
1 parent 30f9c92 commit 5efce43

File tree

3 files changed

+48
-60
lines changed

3 files changed

+48
-60
lines changed

prometheus/promsafe/promauto/auto_test.go

Lines changed: 0 additions & 56 deletions
This file was deleted.

prometheus/promsafe/promauto/auto.go renamed to prometheus/promsafe/promauto_adapter/promauto_adapter.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package promauto
1+
package promauto_adapter
22

33
import (
44
"github.com/prometheus/client_golang/prometheus"
55
"github.com/prometheus/client_golang/prometheus/promauto"
66
"github.com/prometheus/client_golang/prometheus/promsafe"
77
)
88

9-
// NewCounterVec behaves as promauto.NewCounterVec but with type-safe labels
9+
// NewCounterVec behaves as adapter-promauto.NewCounterVec but with type-safe labels
1010
func NewCounterVec[T promsafe.LabelsProviderMarker](opts prometheus.CounterOpts) *promsafe.CounterVec[T] {
1111
//_ = promauto.NewCounterVec // keeping for reference
1212

@@ -22,12 +22,12 @@ type Factory[T promsafe.LabelsProviderMarker] struct {
2222
r prometheus.Registerer
2323
}
2424

25-
// With behaves same as promauto.With but with type-safe labels
25+
// With behaves same as adapter-promauto.With but with type-safe labels
2626
func With[T promsafe.LabelsProviderMarker](r prometheus.Registerer) Factory[T] {
2727
return Factory[T]{r: r}
2828
}
2929

30-
// NewCounterVec behaves like promauto.NewCounterVec but with type-safe labels
30+
// NewCounterVec behaves like adapter-promauto.NewCounterVec but with type-safe labels
3131
func (f Factory[T]) NewCounterVec(opts prometheus.CounterOpts) *promsafe.CounterVec[T] {
3232
c := NewCounterVec[T](opts)
3333
if f.r != nil {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package promauto_adapter_test
2+
3+
import (
4+
"github.com/prometheus/client_golang/prometheus"
5+
"github.com/prometheus/client_golang/prometheus/promsafe"
6+
promauto "github.com/prometheus/client_golang/prometheus/promsafe/promauto_adapter"
7+
)
8+
9+
func ExampleNewCounterVec_promauto_adapted() {
10+
// Examples on how to migrate from promauto to promsafe gently
11+
//
12+
// Before:
13+
// import "github.com/prometheus/client_golang/prometheus/promauto"
14+
// func main() {
15+
// myReg := prometheus.NewRegistry()
16+
// counterOpts := prometheus.CounterOpts{Name:"..."}
17+
// promauto.With(myReg).NewCounterVec(counterOpts, []string{"event_type", "source"})
18+
// }
19+
//
20+
// After:
21+
//
22+
// import (
23+
// promauto "github.com/prometheus/client_golang/prometheus/promsafe/promauto_adapter"
24+
// )
25+
// ...
26+
27+
myReg := prometheus.NewRegistry()
28+
counterOpts := prometheus.CounterOpts{
29+
Name: "items_counted_detailed_auto",
30+
}
31+
32+
type MyLabels struct {
33+
promsafe.StructLabelProvider
34+
EventType string
35+
Source string
36+
}
37+
c := promauto.With[MyLabels](myReg).NewCounterVec(counterOpts)
38+
39+
c.With(MyLabels{
40+
EventType: "reservation", Source: "source1",
41+
}).Inc()
42+
43+
// Output:
44+
}

0 commit comments

Comments
 (0)