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
Show file tree
Hide file tree
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
Expand Up @@ -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}
}
Expand Down Expand Up @@ -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
Expand Down
72 changes: 72 additions & 0 deletions expfmt/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Loading