Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add openmetrics format parser #669

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions expfmt/decode.go
Original file line number Diff line number Diff line change
@@ -76,6 +76,8 @@ func NewDecoder(r io.Reader, format Format) Decoder {
switch format.FormatType() {
case TypeProtoDelim:
return &protoDecoder{r: bufio.NewReader(r)}
case TypeOpenMetrics:
return &openMetricsDecoder{r: r}
}
return &textDecoder{r: r}
}
@@ -115,6 +117,36 @@ func (d *protoDecoder) Decode(v *dto.MetricFamily) error {
return nil
}

type openMetricsDecoder struct {
r io.Reader
fams map[string]*dto.MetricFamily
err error
}

// Decode implements Decoder.
func (d *openMetricsDecoder) Decode(mf *dto.MetricFamily) error {
if d.err == nil {
// Read all metrics in one shot.
var p OpenMetricsParser
d.fams, d.err = p.OpenMetricsToMetricFamilies(d.r)
// If we don't get an error, store io.EOF for the end.
if d.err == nil {
d.err = io.EOF
}
}
// Pick off one MetricFamily per Decode until there's nothing left.
for key, fam := range d.fams {
mf.Name = fam.Name
mf.Help = fam.Help
mf.Type = fam.Type
mf.Unit = fam.Unit
mf.Metric = fam.Metric
delete(d.fams, key)
return nil
}
return d.err
}

// textDecoder implements the Decoder interface for the text protocol.
type textDecoder struct {
r io.Reader
72 changes: 72 additions & 0 deletions expfmt/decode_test.go
Original file line number Diff line number Diff line change
@@ -103,6 +103,78 @@ mf2 4
}
}

func TestOpenMetricsDecoder(t *testing.T) {
var (
ts = model.Now()
in = `
# Only a quite simple scenario with two metric families.
# More complicated tests of the parser itself can be found in the openmetrics package.
# TYPE metric1 counter
metric1_total 3
mf1{label="value1"} -3.14 123456
mf1{label="value2"} 42
metric1_total 4
# EOF
`
out = model.Vector{
&model.Sample{
Metric: model.Metric{
model.MetricNameLabel: "mf1",
"label": "value1",
},
Value: -3.14,
Timestamp: 123456,
},
&model.Sample{
Metric: model.Metric{
model.MetricNameLabel: "mf1",
"label": "value2",
},
Value: 42,
Timestamp: ts,
},
&model.Sample{
Metric: model.Metric{
model.MetricNameLabel: "metric1",
},
Value: 3,
Timestamp: ts,
},
&model.Sample{
Metric: model.Metric{
model.MetricNameLabel: "metric1",
},
Value: 4,
Timestamp: ts,
},
}
)

dec := &SampleDecoder{
Dec: NewDecoder(strings.NewReader(in), FmtOpenMetrics_1_0_0),
Opts: &DecodeOptions{
Timestamp: ts,
},
}
var all model.Vector
for {
var smpls model.Vector
err := dec.Decode(&smpls)
if err != nil && errors.Is(err, io.EOF) {
break
}
if err != nil {
t.Fatal(err)
}
all = append(all, smpls...)
}
sort.Sort(all)
sort.Sort(out)
if !reflect.DeepEqual(all, out) {
t.Fatalf("output does not match")
}
}

func TestProtoDecoder(t *testing.T) {
testTime := model.Now()

932 changes: 932 additions & 0 deletions expfmt/openmetrics_parse.go

Large diffs are not rendered by default.

1,664 changes: 1,664 additions & 0 deletions expfmt/openmetrics_parse_test.go

Large diffs are not rendered by default.

18 changes: 13 additions & 5 deletions expfmt/text_parse.go
Original file line number Diff line number Diff line change
@@ -36,16 +36,23 @@ import (
// by nil.
type stateFn func() stateFn

var (
// The format values for the parse error.
FormatText = "text"
FormatOpenMetrics = "openmetrics"
)

// ParseError signals errors while parsing the simple and flat text-based
// exchange format.
type ParseError struct {
Line int
Msg string
Line int
Msg string
Format string
}

// Error implements the error interface.
func (e ParseError) Error() string {
return fmt.Sprintf("text format parsing error in line %d: %s", e.Line, e.Msg)
return fmt.Sprintf("%s format parsing error in line %d: %s", e.Format, e.Line, e.Msg)
}

// TextParser is used to parse the simple and flat text-based exchange format. Its
@@ -534,8 +541,9 @@ func (p *TextParser) readingType() stateFn {
// message.
func (p *TextParser) parseError(msg string) {
p.err = ParseError{
Line: p.lineCount,
Msg: msg,
Line: p.lineCount,
Msg: msg,
Format: FormatText,
}
}