Expand OM to OpenMetrics

Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
This commit is contained in:
Brian Brazil 2018-10-09 14:09:44 +01:00
parent 9c03e11c2c
commit 3fdb92010e
8 changed files with 40 additions and 39 deletions

View file

@ -18,7 +18,7 @@ STATICCHECK_IGNORE = \
github.com/prometheus/prometheus/discovery/kubernetes/node.go:SA1019 \ github.com/prometheus/prometheus/discovery/kubernetes/node.go:SA1019 \
github.com/prometheus/prometheus/documentation/examples/remote_storage/remote_storage_adapter/main.go:SA1019 \ github.com/prometheus/prometheus/documentation/examples/remote_storage/remote_storage_adapter/main.go:SA1019 \
github.com/prometheus/prometheus/pkg/textparse/promlex.l.go:SA4006 \ github.com/prometheus/prometheus/pkg/textparse/promlex.l.go:SA4006 \
github.com/prometheus/prometheus/pkg/textparse/omlex.l.go:SA4006 \ github.com/prometheus/prometheus/pkg/textparse/openmetricslex.l.go:SA4006 \
github.com/prometheus/prometheus/pkg/pool/pool.go:SA6002 \ github.com/prometheus/prometheus/pkg/pool/pool.go:SA6002 \
github.com/prometheus/prometheus/promql/engine.go:SA6002 \ github.com/prometheus/prometheus/promql/engine.go:SA6002 \
github.com/prometheus/prometheus/prompb/rpc.pb.gw.go:SA1019 github.com/prometheus/prometheus/prompb/rpc.pb.gw.go:SA1019

View file

@ -1,4 +1,4 @@
// Copyright 2017 The Prometheus Authors // Copyright 2018 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
// You may obtain a copy of the License at // You may obtain a copy of the License at
@ -59,7 +59,7 @@ type Parser interface {
func New(b []byte, contentType string) Parser { func New(b []byte, contentType string) Parser {
mediaType, _, err := mime.ParseMediaType(contentType) mediaType, _, err := mime.ParseMediaType(contentType)
if err == nil && mediaType == "application/openmetrics-text" { if err == nil && mediaType == "application/openmetrics-text" {
return NewOMParser(b) return NewOpenMetricsParser(b)
} }
return NewPromParser(b) return NewPromParser(b)
} }

View file

@ -1,5 +1,5 @@
%{ %{
// Copyright 2017 The Prometheus Authors // Copyright 2018 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
// You may obtain a copy of the License at // You may obtain a copy of the License at
@ -21,7 +21,7 @@ import (
// Lex is called by the parser generated by "go tool yacc" to obtain each // Lex is called by the parser generated by "go tool yacc" to obtain each
// token. The method is opened before the matching rules block and closed at // token. The method is opened before the matching rules block and closed at
// the end of the file. // the end of the file.
func (l *omlexer) Lex() token { func (l *openMetricsLexer) Lex() token {
if l.i >= len(l.b) { if l.i >= len(l.b) {
return tEOF return tEOF
} }

View file

@ -1,6 +1,6 @@
// Code generated by golex. DO NOT EDIT. // CAUTION: Generated file - DO NOT EDIT.
// Copyright 2017 The Prometheus Authors // Copyright 2018 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
// You may obtain a copy of the License at // You may obtain a copy of the License at
@ -22,7 +22,7 @@ import (
// Lex is called by the parser generated by "go tool yacc" to obtain each // Lex is called by the parser generated by "go tool yacc" to obtain each
// token. The method is opened before the matching rules block and closed at // token. The method is opened before the matching rules block and closed at
// the end of the file. // the end of the file.
func (l *omlexer) Lex() token { func (l *openMetricsLexer) Lex() token {
if l.i >= len(l.b) { if l.i >= len(l.b) {
return tEOF return tEOF
} }

View file

@ -1,4 +1,4 @@
// Copyright 2017 The Prometheus Authors // Copyright 2018 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
// You may obtain a copy of the License at // You may obtain a copy of the License at
@ -12,7 +12,7 @@
// limitations under the License. // limitations under the License.
//go:generate go get github.com/cznic/golex //go:generate go get github.com/cznic/golex
//go:generate golex -o=omlex.l.go omlex.l //go:generate golex -o=openmetricslex.l.go openmetricslex.l
package textparse package textparse
@ -30,7 +30,7 @@ import (
"github.com/prometheus/prometheus/pkg/value" "github.com/prometheus/prometheus/pkg/value"
) )
type omlexer struct { type openMetricsLexer struct {
b []byte b []byte
i int i int
start int start int
@ -39,16 +39,16 @@ type omlexer struct {
} }
// buf returns the buffer of the current token. // buf returns the buffer of the current token.
func (l *omlexer) buf() []byte { func (l *openMetricsLexer) buf() []byte {
return l.b[l.start:l.i] return l.b[l.start:l.i]
} }
func (l *omlexer) cur() byte { func (l *openMetricsLexer) cur() byte {
return l.b[l.i] return l.b[l.i]
} }
// next advances the omlexer to the next character. // next advances the openMetricsLexer to the next character.
func (l *omlexer) next() byte { func (l *openMetricsLexer) next() byte {
l.i++ l.i++
if l.i >= len(l.b) { if l.i >= len(l.b) {
l.err = io.EOF l.err = io.EOF
@ -66,14 +66,15 @@ func (l *omlexer) next() byte {
return l.b[l.i] return l.b[l.i]
} }
func (l *omlexer) Error(es string) { func (l *openMetricsLexer) Error(es string) {
l.err = errors.New(es) l.err = errors.New(es)
} }
// OMParser parses samples from a byte slice of samples in the official // OpenMetricsParser parses samples from a byte slice of samples in the official
// OpenMetrics text exposition format. // OpenMetrics text exposition format.
type OMParser struct { // This is based on the working draft https://docs.google.com/document/u/1/d/1KwV0mAXwwbvvifBvDKH_LU1YjyXE_wxCkHNoCGq1GX0/edit
l *omlexer type OpenMetricsParser struct {
l *openMetricsLexer
series []byte series []byte
text []byte text []byte
mtype MetricType mtype MetricType
@ -85,13 +86,13 @@ type OMParser struct {
} }
// New returns a new parser of the byte slice. // New returns a new parser of the byte slice.
func NewOMParser(b []byte) Parser { func NewOpenMetricsParser(b []byte) Parser {
return &OMParser{l: &omlexer{b: b}} return &OpenMetricsParser{l: &openMetricsLexer{b: b}}
} }
// Series returns the bytes of the series, the timestamp if set, and the value // Series returns the bytes of the series, the timestamp if set, and the value
// of the current sample. // of the current sample.
func (p *OMParser) Series() ([]byte, *int64, float64) { func (p *OpenMetricsParser) Series() ([]byte, *int64, float64) {
if p.hasTS { if p.hasTS {
return p.series, &p.ts, p.val return p.series, &p.ts, p.val
} }
@ -101,7 +102,7 @@ func (p *OMParser) Series() ([]byte, *int64, float64) {
// Help returns the metric name and help text in the current entry. // Help returns the metric name and help text in the current entry.
// Must only be called after Next returned a help entry. // Must only be called after Next returned a help entry.
// The returned byte slices become invalid after the next call to Next. // The returned byte slices become invalid after the next call to Next.
func (p *OMParser) Help() ([]byte, []byte) { func (p *OpenMetricsParser) Help() ([]byte, []byte) {
m := p.l.b[p.offsets[0]:p.offsets[1]] m := p.l.b[p.offsets[0]:p.offsets[1]]
// Replacer causes allocations. Replace only when necessary. // Replacer causes allocations. Replace only when necessary.
@ -115,14 +116,14 @@ func (p *OMParser) Help() ([]byte, []byte) {
// Type returns the metric name and type in the current entry. // Type returns the metric name and type in the current entry.
// Must only be called after Next returned a type entry. // Must only be called after Next returned a type entry.
// The returned byte slices become invalid after the next call to Next. // The returned byte slices become invalid after the next call to Next.
func (p *OMParser) Type() ([]byte, MetricType) { func (p *OpenMetricsParser) Type() ([]byte, MetricType) {
return p.l.b[p.offsets[0]:p.offsets[1]], p.mtype return p.l.b[p.offsets[0]:p.offsets[1]], p.mtype
} }
// Unit returns the metric name and unit in the current entry. // Unit returns the metric name and unit in the current entry.
// Must only be called after Next returned a unit entry. // Must only be called after Next returned a unit entry.
// The returned byte slices become invalid after the next call to Next. // The returned byte slices become invalid after the next call to Next.
func (p *OMParser) Unit() ([]byte, []byte) { func (p *OpenMetricsParser) Unit() ([]byte, []byte) {
// The Prometheus format does not have units. // The Prometheus format does not have units.
return p.l.b[p.offsets[0]:p.offsets[1]], p.text return p.l.b[p.offsets[0]:p.offsets[1]], p.text
} }
@ -130,13 +131,13 @@ func (p *OMParser) Unit() ([]byte, []byte) {
// Comment returns the text of the current comment. // Comment returns the text of the current comment.
// Must only be called after Next returned a comment entry. // Must only be called after Next returned a comment entry.
// The returned byte slice becomes invalid after the next call to Next. // The returned byte slice becomes invalid after the next call to Next.
func (p *OMParser) Comment() []byte { func (p *OpenMetricsParser) Comment() []byte {
return p.text return p.text
} }
// Metric writes the labels of the current sample into the passed labels. // Metric writes the labels of the current sample into the passed labels.
// It returns the string from which the metric was parsed. // It returns the string from which the metric was parsed.
func (p *OMParser) Metric(l *labels.Labels) string { func (p *OpenMetricsParser) Metric(l *labels.Labels) string {
// Allocate the full immutable string immediately, so we just // Allocate the full immutable string immediately, so we just
// have to create references on it below. // have to create references on it below.
s := string(p.series) s := string(p.series)
@ -167,15 +168,15 @@ func (p *OMParser) Metric(l *labels.Labels) string {
return s return s
} }
// nextToken returns the next token from the omlexer. // nextToken returns the next token from the openMetricsLexer.
func (p *OMParser) nextToken() token { func (p *OpenMetricsParser) nextToken() token {
tok := p.l.Lex() tok := p.l.Lex()
return tok return tok
} }
// Next advances the parser to the next sample. It returns false if no // Next advances the parser to the next sample. It returns false if no
// more samples were read or an error occurred. // more samples were read or an error occurred.
func (p *OMParser) Next() (Entry, error) { func (p *OpenMetricsParser) Next() (Entry, error) {
var err error var err error
p.start = p.l.i p.start = p.l.i
@ -297,7 +298,7 @@ func (p *OMParser) Next() (Entry, error) {
return EntryInvalid, err return EntryInvalid, err
} }
func (p *OMParser) parseLVals() error { func (p *OpenMetricsParser) parseLVals() error {
first := true first := true
for { for {
t := p.nextToken() t := p.nextToken()
@ -338,7 +339,7 @@ func (p *OMParser) parseLVals() error {
return fmt.Errorf("invalid UTF-8 label value") return fmt.Errorf("invalid UTF-8 label value")
} }
// The omlexer ensures the value string is quoted. Strip first // The openMetricsLexer ensures the value string is quoted. Strip first
// and last character. // and last character.
p.offsets = append(p.offsets, p.l.start+1, p.l.i-1) p.offsets = append(p.offsets, p.l.start+1, p.l.i-1)

View file

@ -21,7 +21,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func TestOMParse(t *testing.T) { func TestOpenMetricsParse(t *testing.T) {
input := `# HELP go_gc_duration_seconds A summary of the GC invocation durations. input := `# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary # TYPE go_gc_duration_seconds summary
# UNIT go_gc_duration_seconds seconds # UNIT go_gc_duration_seconds seconds
@ -177,7 +177,7 @@ testmetric{label="\"bar\""} 1`
}, },
} }
p := NewOMParser([]byte(input)) p := NewOpenMetricsParser([]byte(input))
i := 0 i := 0
var res labels.Labels var res labels.Labels
@ -225,7 +225,7 @@ testmetric{label="\"bar\""} 1`
require.Equal(t, len(exp), i) require.Equal(t, len(exp), i)
} }
func TestOMParseErrors(t *testing.T) { func TestOpenMetricsParseErrors(t *testing.T) {
cases := []struct { cases := []struct {
input string input string
err string err string
@ -373,7 +373,7 @@ func TestOMParseErrors(t *testing.T) {
} }
for i, c := range cases { for i, c := range cases {
p := NewOMParser([]byte(c.input)) p := NewOpenMetricsParser([]byte(c.input))
var err error var err error
for err == nil { for err == nil {
_, err = p.Next() _, err = p.Next()
@ -423,7 +423,7 @@ func TestOMNullByteHandling(t *testing.T) {
} }
for i, c := range cases { for i, c := range cases {
p := NewOMParser([]byte(c.input)) p := NewOpenMetricsParser([]byte(c.input))
var err error var err error
for err == nil { for err == nil {
_, err = p.Next() _, err = p.Next()

View file

@ -1,4 +1,4 @@
// Code generated by golex. DO NOT EDIT. // CAUTION: Generated file - DO NOT EDIT.
// Copyright 2017 The Prometheus Authors // Copyright 2017 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");

View file

@ -320,7 +320,7 @@ const (
func BenchmarkParse(b *testing.B) { func BenchmarkParse(b *testing.B) {
for parserName, parser := range map[string]func([]byte) Parser{ for parserName, parser := range map[string]func([]byte) Parser{
"prometheus": NewPromParser, "prometheus": NewPromParser,
"openmetrics": NewOMParser, "openmetrics": NewOpenMetricsParser,
} { } {
for _, fn := range []string{"promtestdata.txt", "promtestdata.nometa.txt"} { for _, fn := range []string{"promtestdata.txt", "promtestdata.nometa.txt"} {