@@ -104,9 +104,7 @@ func TestNegotiateOpenMetrics(t *testing.T) {
104
104
}
105
105
106
106
func TestEncode (t * testing.T ) {
107
- var buff bytes.Buffer
108
- delimEncoder := NewEncoder (& buff , FmtProtoDelim )
109
- metric := & dto.MetricFamily {
107
+ metric1 := & dto.MetricFamily {
110
108
Name : proto .String ("foo_metric" ),
111
109
Type : dto .MetricType_UNTYPED .Enum (),
112
110
Metric : []* dto.Metric {
@@ -118,59 +116,122 @@ func TestEncode(t *testing.T) {
118
116
},
119
117
}
120
118
121
- err := delimEncoder .Encode (metric )
122
- if err != nil {
123
- t .Errorf ("unexpected error during encode: %s" , err .Error ())
124
- }
125
-
126
- out := buff .Bytes ()
127
- if len (out ) == 0 {
128
- t .Errorf ("expected the output bytes buffer to be non-empty" )
129
- }
130
-
131
- buff .Reset ()
132
-
133
- compactEncoder := NewEncoder (& buff , FmtProtoCompact )
134
- err = compactEncoder .Encode (metric )
135
- if err != nil {
136
- t .Errorf ("unexpected error during encode: %s" , err .Error ())
137
- }
138
-
139
- out = buff .Bytes ()
140
- if len (out ) == 0 {
141
- t .Errorf ("expected the output bytes buffer to be non-empty" )
142
- }
143
-
144
- buff .Reset ()
145
-
146
- protoTextEncoder := NewEncoder (& buff , FmtProtoText )
147
- err = protoTextEncoder .Encode (metric )
148
- if err != nil {
149
- t .Errorf ("unexpected error during encode: %s" , err .Error ())
150
- }
151
-
152
- out = buff .Bytes ()
153
- if len (out ) == 0 {
154
- t .Errorf ("expected the output bytes buffer to be non-empty" )
155
- }
156
-
157
- buff .Reset ()
158
-
159
- textEncoder := NewEncoder (& buff , FmtText )
160
- err = textEncoder .Encode (metric )
161
- if err != nil {
162
- t .Errorf ("unexpected error during encode: %s" , err .Error ())
163
- }
164
-
165
- out = buff .Bytes ()
166
- if len (out ) == 0 {
167
- t .Errorf ("expected the output bytes buffer to be non-empty" )
119
+ scenarios := []struct {
120
+ metric * dto.MetricFamily
121
+ format Format
122
+ withUnit bool
123
+ out string
124
+ }{
125
+ // // 1: Untyped ProtoDelim
126
+ // {
127
+ // metric: metric1,
128
+ // format: FmtProtoDelim,
129
+ // out: ,
130
+ // },
131
+ // // 2: Untyped FmtProtoCompact
132
+ // {
133
+ // metric: metric1,
134
+ // format: FmtProtoCompact,
135
+ // out: ,
136
+ // },
137
+ // // 3: Untyped FmtProtoText
138
+ // {
139
+ // metric: metric1,
140
+ // format: FmtProtoText,
141
+ // out: ,
142
+ // },
143
+ // 4: Untyped FmtText
144
+ {
145
+ metric : metric1 ,
146
+ format : FmtText ,
147
+ out : `# TYPE foo_metric untyped
148
+ foo_metric 1.234
149
+ ` ,
150
+ },
151
+ // 5: Untyped FmtOpenMetrics_0_0_1
152
+ {
153
+ metric : metric1 ,
154
+ format : FmtOpenMetrics_0_0_1 ,
155
+ out : `# TYPE foo_metric unknown
156
+ foo_metric 1.234
157
+ ` ,
158
+ },
159
+ // 6: Untyped FmtOpenMetrics_1_0_0
160
+ {
161
+ metric : metric1 ,
162
+ format : FmtOpenMetrics_1_0_0 ,
163
+ out : `# TYPE foo_metric unknown
164
+ foo_metric 1.234
165
+ ` ,
166
+ },
167
+ // 7: Simple Counter FmtOpenMetrics_0_0_1 unit opted in
168
+ {
169
+ metric : & dto.MetricFamily {
170
+ Name : proto .String ("foos_duration_seconds_total" ),
171
+ Help : proto .String ("Duration of foos in seconds." ),
172
+ Type : dto .MetricType_COUNTER .Enum (),
173
+ Unit : proto .String ("seconds" ),
174
+ Metric : []* dto.Metric {
175
+ {
176
+ Counter : & dto.Counter {
177
+ Value : proto .Float64 (420 ),
178
+ },
179
+ },
180
+ },
181
+ },
182
+ format : FmtOpenMetrics_0_0_1 ,
183
+ withUnit : true ,
184
+ out : `# HELP foos_duration_seconds Duration of foos in seconds.
185
+ # TYPE foos_duration_seconds counter
186
+ # UNIT foos_duration_seconds seconds
187
+ foos_duration_seconds_total 420.0
188
+ ` ,
189
+ },
190
+ // 8: Simple Counter FmtOpenMetrics_1_0_0 unit opted out
191
+ {
192
+ metric : & dto.MetricFamily {
193
+ Name : proto .String ("foos_duration_seconds_total" ),
194
+ Help : proto .String ("Duration of foos in seconds." ),
195
+ Type : dto .MetricType_COUNTER .Enum (),
196
+ Metric : []* dto.Metric {
197
+ {
198
+ Counter : & dto.Counter {
199
+ Value : proto .Float64 (420 ),
200
+ },
201
+ },
202
+ },
203
+ },
204
+ format : FmtOpenMetrics_1_0_0 ,
205
+ out : `# HELP foos_duration_seconds Duration of foos in seconds.
206
+ # TYPE foos_duration_seconds counter
207
+ foos_duration_seconds_total 420.0
208
+ ` ,
209
+ },
168
210
}
169
-
170
- expected := "# TYPE foo_metric untyped\n " +
171
- "foo_metric 1.234\n "
172
-
173
- if string (out ) != expected {
174
- t .Errorf ("expected TextEncoder to return %s, but got %s instead" , expected , string (out ))
211
+ for i , scenario := range scenarios {
212
+ opts := []ToOpenMetricsOption {}
213
+ if scenario .withUnit {
214
+ opts = append (opts , ToOpenMetricsWithUnit ())
215
+ }
216
+ out := bytes .NewBuffer (make ([]byte , 0 , len (scenario .out )))
217
+ enc := NewEncoder (out , scenario .format , opts ... )
218
+ err := enc .Encode (scenario .metric )
219
+ if err != nil {
220
+ t .Errorf ("%d. error: %s" , i , err )
221
+ continue
222
+ }
223
+
224
+ if expected , got := len (scenario .out ), len (out .Bytes ()); expected != got {
225
+ t .Errorf (
226
+ "%d. expected %d bytes written, got %d" ,
227
+ i , expected , got ,
228
+ )
229
+ }
230
+ if expected , got := scenario .out , out .String (); expected != got {
231
+ t .Errorf (
232
+ "%d. expected out=%q, got %q" ,
233
+ i , expected , got ,
234
+ )
235
+ }
175
236
}
176
237
}
0 commit comments