2017-04-19 05:43:09 -07:00
|
|
|
// Copyright 2017 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-01-16 11:34:49 -08:00
|
|
|
//go:generate go get github.com/cznic/golex
|
2017-01-14 07:39:04 -08:00
|
|
|
//go:generate golex -o=lex.l.go lex.l
|
2017-01-16 11:34:49 -08:00
|
|
|
|
|
|
|
// Package textparse contains an efficient parser for the Prometheus text format.
|
2017-01-14 07:39:04 -08:00
|
|
|
package textparse
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
2017-01-14 10:30:19 -08:00
|
|
|
"sort"
|
2017-07-27 06:15:41 -07:00
|
|
|
"strings"
|
2017-01-14 07:39:04 -08:00
|
|
|
"unsafe"
|
|
|
|
|
2017-01-14 10:30:19 -08:00
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
2017-01-14 07:39:04 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
type lexer struct {
|
|
|
|
b []byte
|
|
|
|
i int
|
|
|
|
vstart int
|
2017-04-27 08:02:07 -07:00
|
|
|
tstart int
|
2017-01-14 07:39:04 -08:00
|
|
|
|
|
|
|
err error
|
|
|
|
val float64
|
2017-04-27 08:02:07 -07:00
|
|
|
ts *int64
|
2017-01-14 10:30:19 -08:00
|
|
|
offsets []int
|
|
|
|
mstart, mend int
|
2017-04-27 08:02:07 -07:00
|
|
|
nextMstart int
|
2017-07-07 01:29:38 -07:00
|
|
|
|
|
|
|
state int
|
2017-01-14 07:39:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
const eof = 0
|
|
|
|
|
|
|
|
func (l *lexer) next() byte {
|
|
|
|
l.i++
|
|
|
|
if l.i >= len(l.b) {
|
|
|
|
l.err = io.EOF
|
|
|
|
return eof
|
|
|
|
}
|
|
|
|
c := l.b[l.i]
|
2017-07-07 01:29:38 -07:00
|
|
|
|
|
|
|
// Consume null byte when encountered in label-value.
|
|
|
|
if c == eof && (l.state == lstateLValueIn || l.state == lstateLValue) {
|
|
|
|
return l.next()
|
|
|
|
}
|
2017-01-14 07:39:04 -08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *lexer) Error(es string) {
|
|
|
|
l.err = errors.New(es)
|
|
|
|
}
|
|
|
|
|
2017-01-16 11:34:49 -08:00
|
|
|
// Parser parses samples from a byte slice of samples in the official
|
|
|
|
// Prometheus text exposition format.
|
2017-01-14 07:39:04 -08:00
|
|
|
type Parser struct {
|
|
|
|
l *lexer
|
|
|
|
err error
|
|
|
|
val float64
|
|
|
|
}
|
|
|
|
|
2017-01-16 11:34:49 -08:00
|
|
|
// New returns a new parser of the byte slice.
|
2017-01-14 07:39:04 -08:00
|
|
|
func New(b []byte) *Parser {
|
|
|
|
return &Parser{l: &lexer{b: b}}
|
|
|
|
}
|
|
|
|
|
2017-01-16 11:34:49 -08:00
|
|
|
// Next advances the parser to the next sample. It returns false if no
|
|
|
|
// more samples were read or an error occurred.
|
2017-01-14 07:39:04 -08:00
|
|
|
func (p *Parser) Next() bool {
|
|
|
|
switch p.l.Lex() {
|
2017-04-27 08:02:07 -07:00
|
|
|
case -1, eof:
|
2017-01-14 07:39:04 -08:00
|
|
|
return false
|
|
|
|
case 1:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
panic("unexpected")
|
|
|
|
}
|
|
|
|
|
2017-01-16 11:34:49 -08:00
|
|
|
// At returns the bytes of the metric, the timestamp if set, and the value
|
|
|
|
// of the current sample.
|
2017-01-14 07:39:04 -08:00
|
|
|
func (p *Parser) At() ([]byte, *int64, float64) {
|
2017-04-27 08:02:07 -07:00
|
|
|
return p.l.b[p.l.mstart:p.l.mend], p.l.ts, p.l.val
|
2017-01-14 07:39:04 -08:00
|
|
|
}
|
|
|
|
|
2017-01-16 11:34:49 -08:00
|
|
|
// Err returns the current error.
|
2017-01-14 07:39:04 -08:00
|
|
|
func (p *Parser) Err() error {
|
|
|
|
if p.err != nil {
|
|
|
|
return p.err
|
|
|
|
}
|
|
|
|
if p.l.err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return p.l.err
|
|
|
|
}
|
|
|
|
|
2017-01-16 11:34:49 -08:00
|
|
|
// Metric writes the labels of the current sample into the passed labels.
|
2017-05-26 01:44:48 -07:00
|
|
|
// It returns the string from which the metric was parsed.
|
2017-05-25 23:44:24 -07:00
|
|
|
func (p *Parser) Metric(l *labels.Labels) string {
|
2017-01-16 08:24:00 -08:00
|
|
|
// Allocate the full immutable string immediately, so we just
|
|
|
|
// have to create references on it below.
|
|
|
|
s := string(p.l.b[p.l.mstart:p.l.mend])
|
|
|
|
|
2017-01-14 10:30:19 -08:00
|
|
|
*l = append(*l, labels.Label{
|
|
|
|
Name: labels.MetricName,
|
2017-01-16 08:24:00 -08:00
|
|
|
Value: s[:p.l.offsets[0]-p.l.mstart],
|
2017-01-14 10:30:19 -08:00
|
|
|
})
|
|
|
|
|
2017-06-22 00:38:55 -07:00
|
|
|
for i := 1; i < len(p.l.offsets); i += 4 {
|
2017-01-16 08:24:00 -08:00
|
|
|
a := p.l.offsets[i] - p.l.mstart
|
|
|
|
b := p.l.offsets[i+1] - p.l.mstart
|
|
|
|
c := p.l.offsets[i+2] - p.l.mstart
|
2017-06-22 00:38:55 -07:00
|
|
|
d := p.l.offsets[i+3] - p.l.mstart
|
2017-01-14 10:30:19 -08:00
|
|
|
|
2017-07-27 06:15:41 -07:00
|
|
|
// Replacer causes allocations. Replace only when necessary.
|
|
|
|
if strings.IndexByte(s[c:d], byte('\\')) >= 0 {
|
|
|
|
*l = append(*l, labels.Label{Name: s[a:b], Value: replacer.Replace(s[c:d])})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-06-22 00:38:55 -07:00
|
|
|
*l = append(*l, labels.Label{Name: s[a:b], Value: s[c:d]})
|
2017-01-14 10:30:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort((*l)[1:])
|
2017-05-25 23:44:24 -07:00
|
|
|
|
|
|
|
return s
|
2017-01-14 07:39:04 -08:00
|
|
|
}
|
|
|
|
|
2017-07-27 06:15:41 -07:00
|
|
|
var replacer = strings.NewReplacer(
|
|
|
|
`\"`, `"`,
|
|
|
|
`\\`, `\`,
|
|
|
|
`\n`, `
|
|
|
|
`,
|
|
|
|
`\t`, ` `,
|
|
|
|
)
|
|
|
|
|
2017-01-14 07:39:04 -08:00
|
|
|
func yoloString(b []byte) string {
|
2017-03-07 02:41:11 -08:00
|
|
|
return *((*string)(unsafe.Pointer(&b)))
|
2017-01-14 07:39:04 -08:00
|
|
|
}
|