diff --git a/expfmt/openmetrics_create_test.go b/expfmt/openmetrics_create_test.go index be8e77fb..718f623a 100644 --- a/expfmt/openmetrics_create_test.go +++ b/expfmt/openmetrics_create_test.go @@ -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. @@ -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{ @@ -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 @@ -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{ @@ -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. @@ -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"), @@ -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