File tree 2 files changed +61
-2
lines changed
2 files changed +61
-2
lines changed Original file line number Diff line number Diff line change @@ -21,11 +21,43 @@ import (
21
21
"github.com/prometheus/client_golang/prometheus"
22
22
)
23
23
24
+ // CustomType that will be stringified via String() method
25
+ type CustomType int
26
+
27
+ const (
28
+ CustomConst0 CustomType = iota
29
+ CustomConst1
30
+ CustomConst2
31
+ )
32
+
33
+ func (ct CustomType ) String () string {
34
+ switch ct {
35
+ case CustomConst1 :
36
+ return "c1"
37
+ case CustomConst2 :
38
+ return "c2"
39
+
40
+ default :
41
+ return "c0"
42
+ }
43
+ }
44
+
45
+ // CustomTypeInt will remain as simple int
46
+ type CustomTypeInt int
47
+
48
+ const (
49
+ CustomConstInt100 CustomTypeInt = 100
50
+ CustomConstInt200 CustomTypeInt = 200
51
+ )
52
+
24
53
type TestLabels struct {
25
54
StructLabelProvider
26
55
Field1 string
27
56
Field2 int
28
57
Field3 bool
58
+
59
+ Field4 CustomType
60
+ Field5 CustomTypeInt
29
61
}
30
62
31
63
type TestLabelsWithTags struct {
@@ -74,11 +106,15 @@ func Test_extractLabelsWithValues(t *testing.T) {
74
106
Field1 : "value1" ,
75
107
Field2 : 123 ,
76
108
Field3 : true ,
109
+ Field4 : CustomConst1 ,
110
+ Field5 : CustomConstInt200 ,
77
111
},
78
112
expected : prometheus.Labels {
79
113
"field1" : "value1" ,
80
114
"field2" : "123" ,
81
115
"field3" : "true" ,
116
+ "field4" : "c1" ,
117
+ "field5" : "200" ,
82
118
},
83
119
},
84
120
{
Original file line number Diff line number Diff line change @@ -20,6 +20,26 @@ import (
20
20
promauto "github.com/prometheus/client_golang/prometheus/promsafe/promauto_adapter"
21
21
)
22
22
23
+ type EventType int64
24
+
25
+ const (
26
+ EventTypeUndefined EventType = iota
27
+ EventTypeUser
28
+ EventTypeSystem
29
+ )
30
+
31
+ // String returns stringified value of EventType enum
32
+ func (et EventType ) String () string {
33
+ switch et {
34
+ case EventTypeUser :
35
+ return "user"
36
+ case EventTypeSystem :
37
+ return "system"
38
+ default :
39
+ return "undefined"
40
+ }
41
+ }
42
+
23
43
func ExampleNewCounterVec_promauto_adapted () {
24
44
// Examples on how to migrate from promauto to promsafe gently
25
45
//
@@ -45,13 +65,16 @@ func ExampleNewCounterVec_promauto_adapted() {
45
65
46
66
type MyLabels struct {
47
67
promsafe.StructLabelProvider
48
- EventType string
68
+ EventType EventType
49
69
Source string
50
70
}
71
+ // if declare Mylabels as global type you can add .ToPrometheusLabels() method
72
+ // that will use fast labels convertion instead of automatic reflect-based
73
+
51
74
c := promauto.With [MyLabels ](myReg ).NewCounterVec (counterOpts )
52
75
53
76
c .With (MyLabels {
54
- EventType : "reservation" , Source : "source1" ,
77
+ EventType : EventTypeUser , Source : "source1" ,
55
78
}).Inc ()
56
79
57
80
// Output:
You can’t perform that action at this time.
0 commit comments