@@ -88,3 +88,65 @@ g.test_metainfo_immutable = function()
8888 metainfo [' my_useful_info' ] = ' there'
8989 t .assert_equals (c .metainfo , {my_useful_info = ' here' })
9090end
91+
92+ g .test_gauge_with_fixed_labels = function ()
93+ local fixed_labels = {' label1' , ' label2' }
94+ local gauge = metrics .gauge (' gauge_with_labels' , nil , {}, fixed_labels )
95+
96+ gauge :set (1 , {label1 = 1 , label2 = ' text' })
97+ utils .assert_observations (gauge :collect (), {
98+ {' gauge_with_labels' , 1 , {label1 = 1 , label2 = ' text' }},
99+ })
100+
101+ gauge :set (42 , {label2 = ' text' , label1 = 100 })
102+ utils .assert_observations (gauge :collect (), {
103+ {' gauge_with_labels' , 1 , {label1 = 1 , label2 = ' text' }},
104+ {' gauge_with_labels' , 42 , {label1 = 100 , label2 = ' text' }},
105+ })
106+
107+ gauge :inc (5 , {label2 = ' text' , label1 = 100 })
108+ utils .assert_observations (gauge :collect (), {
109+ {' gauge_with_labels' , 1 , {label1 = 1 , label2 = ' text' }},
110+ {' gauge_with_labels' , 47 , {label1 = 100 , label2 = ' text' }},
111+ })
112+
113+ gauge :dec (11 , {label1 = 1 , label2 = ' text' })
114+ utils .assert_observations (gauge :collect (), {
115+ {' gauge_with_labels' , - 10 , {label1 = 1 , label2 = ' text' }},
116+ {' gauge_with_labels' , 47 , {label1 = 100 , label2 = ' text' }},
117+ })
118+
119+ gauge :remove ({label2 = ' text' , label1 = 100 })
120+ utils .assert_observations (gauge :collect (), {
121+ {' gauge_with_labels' , - 10 , {label1 = 1 , label2 = ' text' }},
122+ })
123+ end
124+
125+ g .test_gauge_missing_label = function ()
126+ local fixed_labels = {' label1' , ' label2' }
127+ local gauge = metrics .gauge (' gauge_with_labels' , nil , {}, fixed_labels )
128+
129+ gauge :set (42 , {label1 = 1 , label2 = ' text' })
130+ utils .assert_observations (gauge :collect (), {
131+ {' gauge_with_labels' , 42 , {label1 = 1 , label2 = ' text' }},
132+ })
133+
134+ t .assert_error_msg_contains (
135+ " Invalid label_pairs: expected a table when label_keys is provided" ,
136+ gauge .set , gauge , 42 , ' text' )
137+
138+ t .assert_error_msg_contains (
139+ " should match the number of label pairs" ,
140+ gauge .set , gauge , 42 , {label1 = 1 , label2 = ' text' , label3 = 42 })
141+
142+ local function assert_missing_label_error (fun , ...)
143+ t .assert_error_msg_contains (
144+ " is missing" ,
145+ fun , gauge , ... )
146+ end
147+
148+ assert_missing_label_error (gauge .inc , 1 , {label1 = 1 , label3 = 42 })
149+ assert_missing_label_error (gauge .dec , 2 , {label1 = 1 , label3 = 42 })
150+ assert_missing_label_error (gauge .set , 42 , {label2 = ' text' , label3 = 42 })
151+ assert_missing_label_error (gauge .remove , {label2 = ' text' , label3 = 42 })
152+ end
0 commit comments