File tree Expand file tree Collapse file tree 3 files changed +48
-60
lines changed Expand file tree Collapse file tree 3 files changed +48
-60
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1
- package promauto
1
+ package promauto_adapter
2
2
3
3
import (
4
4
"github.com/prometheus/client_golang/prometheus"
5
5
"github.com/prometheus/client_golang/prometheus/promauto"
6
6
"github.com/prometheus/client_golang/prometheus/promsafe"
7
7
)
8
8
9
- // NewCounterVec behaves as promauto.NewCounterVec but with type-safe labels
9
+ // NewCounterVec behaves as adapter- promauto.NewCounterVec but with type-safe labels
10
10
func NewCounterVec [T promsafe.LabelsProviderMarker ](opts prometheus.CounterOpts ) * promsafe.CounterVec [T ] {
11
11
//_ = promauto.NewCounterVec // keeping for reference
12
12
@@ -22,12 +22,12 @@ type Factory[T promsafe.LabelsProviderMarker] struct {
22
22
r prometheus.Registerer
23
23
}
24
24
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
26
26
func With [T promsafe.LabelsProviderMarker ](r prometheus.Registerer ) Factory [T ] {
27
27
return Factory [T ]{r : r }
28
28
}
29
29
30
- // NewCounterVec behaves like promauto.NewCounterVec but with type-safe labels
30
+ // NewCounterVec behaves like adapter- promauto.NewCounterVec but with type-safe labels
31
31
func (f Factory [T ]) NewCounterVec (opts prometheus.CounterOpts ) * promsafe.CounterVec [T ] {
32
32
c := NewCounterVec [T ](opts )
33
33
if f .r != nil {
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments