Skip to content

Commit 6a86956

Browse files
committed
Add unit
Signed-off-by: Arianna Vespri <[email protected]>
1 parent 36d0bf9 commit 6a86956

File tree

4 files changed

+276
-90
lines changed

4 files changed

+276
-90
lines changed

expfmt/encode_test.go

Lines changed: 93 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,10 @@ func TestNegotiateOpenMetrics(t *testing.T) {
200200
}
201201

202202
func TestEncode(t *testing.T) {
203-
var buff bytes.Buffer
204-
delimEncoder := NewEncoder(&buff, fmtProtoDelim)
205-
metric := &dto.MetricFamily{
203+
metric1 := &dto.MetricFamily{
206204
Name: proto.String("foo_metric"),
207205
Type: dto.MetricType_UNTYPED.Enum(),
206+
Unit: proto.String("seconds"),
208207
Metric: []*dto.Metric{
209208
{
210209
Untyped: &dto.Untyped{
@@ -214,60 +213,98 @@ func TestEncode(t *testing.T) {
214213
},
215214
}
216215

217-
err := delimEncoder.Encode(metric)
218-
if err != nil {
219-
t.Errorf("unexpected error during encode: %s", err.Error())
220-
}
221-
222-
out := buff.Bytes()
223-
if len(out) == 0 {
224-
t.Errorf("expected the output bytes buffer to be non-empty")
225-
}
226-
227-
buff.Reset()
228-
229-
compactEncoder := NewEncoder(&buff, fmtProtoCompact)
230-
err = compactEncoder.Encode(metric)
231-
if err != nil {
232-
t.Errorf("unexpected error during encode: %s", err.Error())
233-
}
234-
235-
out = buff.Bytes()
236-
if len(out) == 0 {
237-
t.Errorf("expected the output bytes buffer to be non-empty")
238-
}
239-
240-
buff.Reset()
241-
242-
protoTextEncoder := NewEncoder(&buff, fmtProtoText)
243-
err = protoTextEncoder.Encode(metric)
244-
if err != nil {
245-
t.Errorf("unexpected error during encode: %s", err.Error())
246-
}
247-
248-
out = buff.Bytes()
249-
if len(out) == 0 {
250-
t.Errorf("expected the output bytes buffer to be non-empty")
251-
}
252-
253-
buff.Reset()
254-
255-
textEncoder := NewEncoder(&buff, fmtText)
256-
err = textEncoder.Encode(metric)
257-
if err != nil {
258-
t.Errorf("unexpected error during encode: %s", err.Error())
259-
}
260-
261-
out = buff.Bytes()
262-
if len(out) == 0 {
263-
t.Errorf("expected the output bytes buffer to be non-empty")
216+
scenarios := []struct {
217+
metric *dto.MetricFamily
218+
format Format
219+
options []EncoderOption
220+
expOut string
221+
}{
222+
// 1: Untyped ProtoDelim
223+
{
224+
metric: metric1,
225+
format: fmtProtoDelim,
226+
},
227+
// 2: Untyped fmtProtoCompact
228+
{
229+
metric: metric1,
230+
format: fmtProtoCompact,
231+
},
232+
// 3: Untyped fmtProtoText
233+
{
234+
metric: metric1,
235+
format: fmtProtoText,
236+
},
237+
// 4: Untyped fmtText
238+
{
239+
metric: metric1,
240+
format: fmtText,
241+
expOut: `# TYPE foo_metric untyped
242+
foo_metric 1.234
243+
`,
244+
},
245+
// 5: Untyped fmtOpenMetrics_0_0_1
246+
{
247+
metric: metric1,
248+
format: fmtOpenMetrics_0_0_1,
249+
expOut: `# TYPE foo_metric unknown
250+
foo_metric 1.234
251+
`,
252+
},
253+
// 6: Untyped fmtOpenMetrics_1_0_0
254+
{
255+
metric: metric1,
256+
format: fmtOpenMetrics_1_0_0,
257+
expOut: `# TYPE foo_metric unknown
258+
foo_metric 1.234
259+
`,
260+
},
261+
// 7: Simple Counter fmtOpenMetrics_0_0_1 unit opted in
262+
{
263+
metric: metric1,
264+
format: fmtOpenMetrics_0_0_1,
265+
options: []EncoderOption{WithUnit()},
266+
expOut: `# TYPE foo_metric_seconds unknown
267+
# UNIT foo_metric_seconds seconds
268+
foo_metric_seconds 1.234
269+
`,
270+
},
271+
// 8: Simple Counter fmtOpenMetrics_1_0_0 unit opted out
272+
{
273+
metric: metric1,
274+
format: fmtOpenMetrics_1_0_0,
275+
expOut: `# TYPE foo_metric unknown
276+
foo_metric 1.234
277+
`,
278+
},
264279
}
265-
266-
expected := "# TYPE foo_metric untyped\n" +
267-
"foo_metric 1.234\n"
268-
269-
if string(out) != expected {
270-
t.Errorf("expected TextEncoder to return %s, but got %s instead", expected, string(out))
280+
for i, scenario := range scenarios {
281+
out := bytes.NewBuffer(make([]byte, 0, len(scenario.expOut)))
282+
enc := NewEncoder(out, scenario.format, scenario.options...)
283+
err := enc.Encode(scenario.metric)
284+
if err != nil {
285+
t.Errorf("%d. error: %s", i, err)
286+
continue
287+
}
288+
289+
if expected, got := len(scenario.expOut), len(out.Bytes()); expected != 0 && expected != got {
290+
t.Errorf(
291+
"%d. expected %d bytes written, got %d",
292+
i, expected, got,
293+
)
294+
}
295+
if expected, got := scenario.expOut, out.String(); expected != "" && expected != got {
296+
t.Errorf(
297+
"%d. expected out=%q, got %q",
298+
i, expected, got,
299+
)
300+
}
301+
302+
if len(out.Bytes()) == 0 {
303+
t.Errorf(
304+
"%d. expected output not to be empty",
305+
i,
306+
)
307+
}
271308
}
272309
}
273310

0 commit comments

Comments
 (0)