Skip to content

Commit

Permalink
Correct the tests
Browse files Browse the repository at this point in the history
Signed-off-by: Arianna Vespri <[email protected]>
  • Loading branch information
vesari committed Dec 15, 2023
1 parent 7de7f54 commit 2511f67
Showing 1 changed file with 15 additions and 83 deletions.
98 changes: 15 additions & 83 deletions expfmt/openmetrics_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestCreateOpenMetrics(t *testing.T) {

var scenarios = []struct {
in *dto.MetricFamily
withUnit ToOpenMetricsOption
withUnit bool
out string
}{
// 0: Counter, timestamp given, no _total suffix.
Expand Down Expand Up @@ -236,6 +236,7 @@ summary_name_count{name_1="value 1",name_2="value 2"} 4711
Name: proto.String("request_duration_microseconds"),
Help: proto.String("The response latency."),
Type: dto.MetricType_HISTOGRAM.Enum(),
Unit: proto.String("microseconds"),
Metric: []*dto.Metric{
&dto.Metric{
Histogram: &dto.Histogram{
Expand Down Expand Up @@ -267,8 +268,10 @@ summary_name_count{name_1="value 1",name_2="value 2"} 4711
},
},
},
withUnit: true,
out: `# HELP request_duration_microseconds The response latency.
# TYPE request_duration_microseconds histogram
# UNIT request_duration_microseconds microseconds
request_duration_microseconds_bucket{le="100.0"} 123
request_duration_microseconds_bucket{le="120.0"} 412
request_duration_microseconds_bucket{le="144.0"} 592
Expand All @@ -284,6 +287,7 @@ request_duration_microseconds_count 2693
Name: proto.String("request_duration_microseconds"),
Help: proto.String("The response latency."),
Type: dto.MetricType_HISTOGRAM.Enum(),
Unit: proto.String("microseconds"),
Metric: []*dto.Metric{
&dto.Metric{
Histogram: &dto.Histogram{
Expand Down Expand Up @@ -429,8 +433,10 @@ foos_total 42.0
Unit: proto.String("seconds"),
Metric: []*dto.Metric{},
},
withUnit: true,
out: `# HELP name_seconds doc string
# TYPE name_seconds counter
# UNIT name_seconds seconds
`,
},
// 10: Histogram plus unit.
Expand Down Expand Up @@ -482,58 +488,7 @@ request_duration_microseconds_sum 1.7560473e+06
request_duration_microseconds_count 2693
`,
},
}

for i, scenario := range scenarios {
out := bytes.NewBuffer(make([]byte, 0, len(scenario.out)))

n, err := MetricFamilyToOpenMetrics(out, scenario.in)
if err != nil {
t.Errorf("%d. error: %s", i, err)
continue
}
if expected, got := len(scenario.out), n; expected != got {
t.Errorf(
"%d. expected %d bytes written, got %d",
i, expected, got,
)
}
if expected, got := scenario.out, out.String(); expected != got {
t.Errorf(
"%d. expected out=%q, got %q",
i, expected, got,
)
}
}

}

func TestCreateOpenMatricsWithUnit(t *testing.T) {
openMetricsTimestamp := timestamppb.New(time.Unix(12345, 600000000))
if err := openMetricsTimestamp.CheckValid(); err != nil {
t.Error(err)
}

var scenarios = []struct {
in *dto.MetricFamily
withUnit ToOpenMetricsOption
out string
}{
// 1: No metric plus unit.
{
in: &dto.MetricFamily{
Name: proto.String("name_seconds_total"),
Help: proto.String("doc string"),
Type: dto.MetricType_COUNTER.Enum(),
Unit: proto.String("seconds"),
Metric: []*dto.Metric{},
},
out: `# HELP name_seconds doc string
# TYPE name_seconds counter
# UNIT name_seconds seconds
`,
},
// 2: No metric plus unit no unit in name.
// 11: No metric unit opted in no unit in name
{
in: &dto.MetricFamily{
Name: proto.String("name_total"),
Expand All @@ -542,44 +497,21 @@ func TestCreateOpenMatricsWithUnit(t *testing.T) {
Unit: proto.String("seconds"),
Metric: []*dto.Metric{},
},
out: `# HELP name_seconds doc string
# TYPE name_seconds counter
# UNIT name_seconds seconds
`,
},
// 3: No metric plus unit wrong unit in name. // Can this happen at all?
{
in: &dto.MetricFamily{
Name: proto.String("name_milliseconds_total"),
Help: proto.String("doc string"),
Type: dto.MetricType_COUNTER.Enum(),
Unit: proto.String("seconds"),
Metric: []*dto.Metric{},
},
out: `# HELP name_milliseconds_seconds doc string
# TYPE name_milliseconds_seconds counter
# UNIT name_milliseconds_seconds seconds
`,
},
// 3: No metric plus unit already in name.
{
in: &dto.MetricFamily{
Name: proto.String("name_seconds_total"),
Help: proto.String("doc string"),
Type: dto.MetricType_COUNTER.Enum(),
Unit: proto.String("seconds"),
Metric: []*dto.Metric{},
},
withUnit: true,
out: `# HELP name_seconds doc string
# TYPE name_seconds counter
# UNIT name_seconds seconds
`,
},
}

for i, scenario := range scenarios {
out := bytes.NewBuffer(make([]byte, 0, len(scenario.out)))

n, err := MetricFamilyToOpenMetrics(out, scenario.in, ToOpenMetricsWithUnit())
opts := []ToOpenMetricsOption{}
if scenario.withUnit {
opts = append(opts, ToOpenMetricsWithUnit())
}
n, err := MetricFamilyToOpenMetrics(out, scenario.in, opts...)
if err != nil {
t.Errorf("%d. error: %s", i, err)
continue
Expand Down

0 comments on commit 2511f67

Please sign in to comment.