diff --git a/expfmt/openmetrics_create_test.go b/expfmt/openmetrics_create_test.go index 5aa27f88..89571b11 100644 --- a/expfmt/openmetrics_create_test.go +++ b/expfmt/openmetrics_create_test.go @@ -422,14 +422,14 @@ foos_total 42.0 // 9: No metric plus unit. { in: &dto.MetricFamily{ - Name: proto.String("name_seconds"), + 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 unknown +# TYPE name_seconds counter # UNIT name_seconds seconds `, }, diff --git a/expfmt/text_create.go b/expfmt/text_create.go index 10f0e522..2946b8f1 100644 --- a/expfmt/text_create.go +++ b/expfmt/text_create.go @@ -148,33 +148,6 @@ func MetricFamilyToText(out io.Writer, in *dto.MetricFamily) (written int, err e if err != nil { return } - if in.Unit != nil { - n, err = w.WriteString("# UNIT ") - written += n - if err != nil { - return - } - n, err = w.WriteString(name) - written += n - if err != nil { - return - } - err = w.WriteByte(' ') - written++ - if err != nil { - return - } - n, err = writeEscapedString(w, *in.Unit, false) - written += n - if err != nil { - return - } - err = w.WriteByte('\n') - written++ - if err != nil { - return - } - } // Finally the samples, one line for each. for _, metric := range in.Metric { diff --git a/expfmt/text_create_test.go b/expfmt/text_create_test.go index eeb7d985..e6071666 100644 --- a/expfmt/text_create_test.go +++ b/expfmt/text_create_test.go @@ -330,56 +330,6 @@ request_duration_microseconds_count 2693 out: `# HELP name doc string # TYPE name counter name -Inf -`, - }, - // 7: Histogram with unit - { - in: &dto.MetricFamily{ - 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{ - SampleCount: proto.Uint64(2693), - SampleSum: proto.Float64(1756047.3), - Bucket: []*dto.Bucket{ - &dto.Bucket{ - UpperBound: proto.Float64(100), - CumulativeCount: proto.Uint64(123), - }, - &dto.Bucket{ - UpperBound: proto.Float64(120), - CumulativeCount: proto.Uint64(412), - }, - &dto.Bucket{ - UpperBound: proto.Float64(144), - CumulativeCount: proto.Uint64(592), - }, - &dto.Bucket{ - UpperBound: proto.Float64(172.8), - CumulativeCount: proto.Uint64(1524), - }, - &dto.Bucket{ - UpperBound: proto.Float64(math.Inf(+1)), - CumulativeCount: proto.Uint64(2693), - }, - }, - }, - }, - }, - }, - out: `# HELP request_duration_microseconds The response latency. -# TYPE request_duration_microseconds histogram -# UNIT request_duration_microseconds microseconds -request_duration_microseconds_bucket{le="100"} 123 -request_duration_microseconds_bucket{le="120"} 412 -request_duration_microseconds_bucket{le="144"} 592 -request_duration_microseconds_bucket{le="172.8"} 1524 -request_duration_microseconds_bucket{le="+Inf"} 2693 -request_duration_microseconds_sum 1.7560473e+06 -request_duration_microseconds_count 2693 `, }, } diff --git a/expfmt/text_parse.go b/expfmt/text_parse.go index 47fa4480..35db1cc9 100644 --- a/expfmt/text_parse.go +++ b/expfmt/text_parse.go @@ -178,7 +178,7 @@ func (p *TextParser) startComment() stateFn { return p.startOfLine } keyword := p.currentToken.String() - if keyword != "HELP" && keyword != "TYPE" && keyword != "UNIT" { + if keyword != "HELP" && keyword != "TYPE" { // Generic comment, ignore by fast forwarding to end of line. for p.currentByte != '\n' { if p.currentByte, p.err = p.buf.ReadByte(); p.err != nil { @@ -217,8 +217,6 @@ func (p *TextParser) startComment() stateFn { return p.readingHelp case "TYPE": return p.readingType - case "UNIT": - return p.readingUnit } panic(fmt.Sprintf("code error: unexpected keyword %q", keyword)) } @@ -530,21 +528,6 @@ func (p *TextParser) readingType() stateFn { return p.startOfLine } -// readingUnit represents the state where the last byte read (now in -// p.currentByte) is the first byte of the docstring after 'UNIT'. -func (p *TextParser) readingUnit() stateFn { - if p.currentMF.Unit != nil { - p.parseError(fmt.Sprintf("second UNIT line for metric name %q", p.currentMF.GetName())) - return nil - } - // Rest of line is the docstring. - if p.readTokenUntilNewline(true); p.err != nil { - return nil // Unexpected end of input. - } - p.currentMF.Unit = proto.String(p.currentToken.String()) - return p.startOfLine -} - // parseError sets p.err to a ParseError at the current line with the given // message. func (p *TextParser) parseError(msg string) { diff --git a/expfmt/text_parse_test.go b/expfmt/text_parse_test.go index 6899779c..893a8574 100644 --- a/expfmt/text_parse_test.go +++ b/expfmt/text_parse_test.go @@ -385,110 +385,6 @@ request_duration_microseconds_count 2693 }, }, }, - // 5: The histogram with unit. - { - in: ` -# HELP request_duration_microseconds The response latency. -# TYPE request_duration_microseconds histogram -# UNIT request_duration_microseconds microseconds -request_duration_microseconds_bucket{le="100"} 123 -request_duration_microseconds_bucket{le="120"} 412 -request_duration_microseconds_bucket{le="144"} 592 -request_duration_microseconds_bucket{le="172.8"} 1524 -request_duration_microseconds_bucket{le="+Inf"} 2693 -request_duration_microseconds_sum 1.7560473e+06 -request_duration_microseconds_count 2693 -`, - out: []*dto.MetricFamily{ - { - 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{ - SampleCount: proto.Uint64(2693), - SampleSum: proto.Float64(1756047.3), - Bucket: []*dto.Bucket{ - &dto.Bucket{ - UpperBound: proto.Float64(100), - CumulativeCount: proto.Uint64(123), - }, - &dto.Bucket{ - UpperBound: proto.Float64(120), - CumulativeCount: proto.Uint64(412), - }, - &dto.Bucket{ - UpperBound: proto.Float64(144), - CumulativeCount: proto.Uint64(592), - }, - &dto.Bucket{ - UpperBound: proto.Float64(172.8), - CumulativeCount: proto.Uint64(1524), - }, - &dto.Bucket{ - UpperBound: proto.Float64(math.Inf(+1)), - CumulativeCount: proto.Uint64(2693), - }, - }, - }, - }, - }, - }, - }, - }, - // 6: A counter with unit - { - in: ` -# HELP name something to do with time -# TYPE name counter -# UNIT name seconds -name{labelname="val1",basename="basevalue"} 50.0 -name{labelname="val2",basename="basevalue"} 0.24 2 -`, - out: []*dto.MetricFamily{ - { - Name: proto.String("name"), - Help: proto.String("something to do with time"), - Type: dto.MetricType_COUNTER.Enum(), - Unit: proto.String("seconds"), - Metric: []*dto.Metric{ - &dto.Metric{ - Label: []*dto.LabelPair{ - &dto.LabelPair{ - Name: proto.String("labelname"), - Value: proto.String("val1"), - }, - &dto.LabelPair{ - Name: proto.String("basename"), - Value: proto.String("basevalue"), - }, - }, - Counter: &dto.Counter{ - Value: proto.Float64(50), - }, - }, - &dto.Metric{ - Label: []*dto.LabelPair{ - &dto.LabelPair{ - Name: proto.String("labelname"), - Value: proto.String("val2"), - }, - &dto.LabelPair{ - Name: proto.String("basename"), - Value: proto.String("basevalue"), - }, - }, - Counter: &dto.Counter{ - Value: proto.Float64(.24), - }, - TimestampMs: proto.Int64(2), - }, - }, - }, - }, - }, } for i, scenario := range scenarios {