File tree Expand file tree Collapse file tree 2 files changed +61
-2
lines changed Expand file tree Collapse file tree 2 files changed +61
-2
lines changed Original file line number Diff line number Diff line change @@ -21,11 +21,43 @@ import (
2121 "github.com/prometheus/client_golang/prometheus"
2222)
2323
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+
2453type TestLabels struct {
2554 StructLabelProvider
2655 Field1 string
2756 Field2 int
2857 Field3 bool
58+
59+ Field4 CustomType
60+ Field5 CustomTypeInt
2961}
3062
3163type TestLabelsWithTags struct {
@@ -74,11 +106,15 @@ func Test_extractLabelsWithValues(t *testing.T) {
74106 Field1 : "value1" ,
75107 Field2 : 123 ,
76108 Field3 : true ,
109+ Field4 : CustomConst1 ,
110+ Field5 : CustomConstInt200 ,
77111 },
78112 expected : prometheus.Labels {
79113 "field1" : "value1" ,
80114 "field2" : "123" ,
81115 "field3" : "true" ,
116+ "field4" : "c1" ,
117+ "field5" : "200" ,
82118 },
83119 },
84120 {
Original file line number Diff line number Diff line change @@ -20,6 +20,26 @@ import (
2020 promauto "github.com/prometheus/client_golang/prometheus/promsafe/promauto_adapter"
2121)
2222
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+
2343func ExampleNewCounterVec_promauto_adapted () {
2444 // Examples on how to migrate from promauto to promsafe gently
2545 //
@@ -45,13 +65,16 @@ func ExampleNewCounterVec_promauto_adapted() {
4565
4666 type MyLabels struct {
4767 promsafe.StructLabelProvider
48- EventType string
68+ EventType EventType
4969 Source string
5070 }
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+
5174 c := promauto.With [MyLabels ](myReg ).NewCounterVec (counterOpts )
5275
5376 c .With (MyLabels {
54- EventType : "reservation" , Source : "source1" ,
77+ EventType : EventTypeUser , Source : "source1" ,
5578 }).Inc ()
5679
5780 // Output:
You can’t perform that action at this time.
0 commit comments