Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
fix(prometheus): remove newlines in HELP (#1196)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyam8 authored Jun 5, 2023
1 parent ea0ab29 commit 78d8779
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/prometheus/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package prometheus
import (
"errors"
"io"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -80,6 +81,8 @@ func (p *promTextParser) parseToSeries(text []byte) (Series, error) {
return p.series, nil
}

var reSpace = regexp.MustCompile(`\s+`)

func (p *promTextParser) parseToMetricFamilies(text []byte) (MetricFamilies, error) {
p.reset()

Expand All @@ -101,6 +104,10 @@ func (p *promTextParser) parseToMetricFamilies(text []byte) (MetricFamilies, err
name, help := parser.Help()
p.setMetricFamilyByName(string(name))
p.currMF.help = string(help)
if strings.IndexByte(p.currMF.help, '\n') != -1 {
// convert multiline to one line because HELP is used as the chart title.
p.currMF.help = reSpace.ReplaceAllString(strings.TrimSpace(p.currMF.help), " ")
}
case textparse.EntryType:
name, typ := parser.Type()
p.setMetricFamilyByName(string(name))
Expand Down
19 changes: 19 additions & 0 deletions pkg/prometheus/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
)

var (
dataMultilineHelp, _ = os.ReadFile("testdata/multiline-help.txt")

dataGaugeMeta, _ = os.ReadFile("testdata/gauge-meta.txt")
dataGaugeNoMeta, _ = os.ReadFile("testdata/gauge-no-meta.txt")
dataCounterMeta, _ = os.ReadFile("testdata/counter-meta.txt")
Expand All @@ -32,6 +34,7 @@ var (

func Test_testParseDataIsValid(t *testing.T) {
for name, data := range map[string][]byte{
"dataMultilineHelp": dataMultilineHelp,
"dataGaugeMeta": dataGaugeMeta,
"dataGaugeNoMeta": dataGaugeNoMeta,
"dataCounterMeta": dataCounterMeta,
Expand All @@ -51,6 +54,22 @@ func TestPromTextParser_parseToMetricFamilies(t *testing.T) {
input []byte
want MetricFamilies
}{
"Gauge with multiline HELP": {
input: dataMultilineHelp,
want: MetricFamilies{
"test_gauge_metric_1": {
name: "test_gauge_metric_1",
help: "First line. Second line.",
typ: textparse.MetricTypeGauge,
metrics: []Metric{
{
labels: labels.Labels{{Name: "label1", Value: "value1"}},
gauge: &Gauge{value: 11},
},
},
},
},
},
"Gauge with meta parsed as Gauge": {
input: dataGaugeMeta,
want: MetricFamilies{
Expand Down
3 changes: 3 additions & 0 deletions pkg/prometheus/testdata/multiline-help.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# HELP test_gauge_metric_1 \n First line.\n Second line.\n
# TYPE test_gauge_metric_1 gauge
test_gauge_metric_1{label1="value1"} 11

0 comments on commit 78d8779

Please sign in to comment.