2018-10-09 06:09:44 -07:00
|
|
|
// Copyright 2018 The Prometheus Authors
|
2018-10-04 09:57:47 -07:00
|
|
|
// 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.
|
|
|
|
|
2018-12-24 14:59:32 -08:00
|
|
|
//go:generate go get -u modernc.org/golex
|
2018-10-09 06:09:44 -07:00
|
|
|
//go:generate golex -o=openmetricslex.l.go openmetricslex.l
|
2018-10-04 09:57:47 -07:00
|
|
|
|
|
|
|
package textparse
|
|
|
|
|
|
|
|
import (
|
2019-11-19 01:33:30 -08:00
|
|
|
"bytes"
|
2022-06-27 12:29:19 -07:00
|
|
|
"errors"
|
2019-11-19 01:33:30 -08:00
|
|
|
"fmt"
|
2018-10-04 09:57:47 -07:00
|
|
|
"io"
|
|
|
|
"math"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"unicode/utf8"
|
|
|
|
|
2021-11-08 06:23:17 -08:00
|
|
|
"github.com/prometheus/prometheus/model/exemplar"
|
|
|
|
"github.com/prometheus/prometheus/model/labels"
|
|
|
|
"github.com/prometheus/prometheus/model/value"
|
2018-10-04 09:57:47 -07:00
|
|
|
)
|
|
|
|
|
2019-11-19 01:33:30 -08:00
|
|
|
var allowedSuffixes = [][]byte{[]byte("_total"), []byte("_bucket")}
|
|
|
|
|
2018-10-09 06:09:44 -07:00
|
|
|
type openMetricsLexer struct {
|
2018-10-04 09:57:47 -07:00
|
|
|
b []byte
|
|
|
|
i int
|
|
|
|
start int
|
|
|
|
err error
|
|
|
|
state int
|
|
|
|
}
|
|
|
|
|
|
|
|
// buf returns the buffer of the current token.
|
2018-10-09 06:09:44 -07:00
|
|
|
func (l *openMetricsLexer) buf() []byte {
|
2018-10-04 09:57:47 -07:00
|
|
|
return l.b[l.start:l.i]
|
|
|
|
}
|
|
|
|
|
2018-10-09 06:09:44 -07:00
|
|
|
func (l *openMetricsLexer) cur() byte {
|
2019-08-23 07:44:16 -07:00
|
|
|
if l.i < len(l.b) {
|
|
|
|
return l.b[l.i]
|
|
|
|
}
|
|
|
|
return byte(' ')
|
2018-10-04 09:57:47 -07:00
|
|
|
}
|
|
|
|
|
2018-10-09 06:09:44 -07:00
|
|
|
// next advances the openMetricsLexer to the next character.
|
|
|
|
func (l *openMetricsLexer) next() byte {
|
2018-10-04 09:57:47 -07:00
|
|
|
l.i++
|
|
|
|
if l.i >= len(l.b) {
|
|
|
|
l.err = io.EOF
|
|
|
|
return byte(tEOF)
|
|
|
|
}
|
|
|
|
// Lex struggles with null bytes. If we are in a label value or help string, where
|
|
|
|
// they are allowed, consume them here immediately.
|
|
|
|
for l.b[l.i] == 0 && (l.state == sLValue || l.state == sMeta2 || l.state == sComment) {
|
|
|
|
l.i++
|
|
|
|
if l.i >= len(l.b) {
|
|
|
|
l.err = io.EOF
|
|
|
|
return byte(tEOF)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return l.b[l.i]
|
|
|
|
}
|
|
|
|
|
2018-10-09 06:09:44 -07:00
|
|
|
func (l *openMetricsLexer) Error(es string) {
|
2018-10-04 09:57:47 -07:00
|
|
|
l.err = errors.New(es)
|
|
|
|
}
|
|
|
|
|
2018-10-09 06:09:44 -07:00
|
|
|
// OpenMetricsParser parses samples from a byte slice of samples in the official
|
2018-10-04 09:57:47 -07:00
|
|
|
// OpenMetrics text exposition format.
|
2018-10-09 06:09:44 -07:00
|
|
|
// This is based on the working draft https://docs.google.com/document/u/1/d/1KwV0mAXwwbvvifBvDKH_LU1YjyXE_wxCkHNoCGq1GX0/edit
|
|
|
|
type OpenMetricsParser struct {
|
|
|
|
l *openMetricsLexer
|
2018-10-04 09:57:47 -07:00
|
|
|
series []byte
|
|
|
|
text []byte
|
|
|
|
mtype MetricType
|
|
|
|
val float64
|
|
|
|
ts int64
|
|
|
|
hasTS bool
|
|
|
|
start int
|
|
|
|
offsets []int
|
2019-11-19 01:33:30 -08:00
|
|
|
|
|
|
|
eOffsets []int
|
|
|
|
exemplar []byte
|
|
|
|
exemplarVal float64
|
|
|
|
exemplarTs int64
|
|
|
|
hasExemplarTs bool
|
2018-10-04 09:57:47 -07:00
|
|
|
}
|
|
|
|
|
2019-09-10 06:45:09 -07:00
|
|
|
// NewOpenMetricsParser returns a new parser of the byte slice.
|
2018-10-09 06:09:44 -07:00
|
|
|
func NewOpenMetricsParser(b []byte) Parser {
|
|
|
|
return &OpenMetricsParser{l: &openMetricsLexer{b: b}}
|
2018-10-04 09:57:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Series returns the bytes of the series, the timestamp if set, and the value
|
|
|
|
// of the current sample.
|
2018-10-09 06:09:44 -07:00
|
|
|
func (p *OpenMetricsParser) Series() ([]byte, *int64, float64) {
|
2018-10-04 09:57:47 -07:00
|
|
|
if p.hasTS {
|
2019-11-19 01:33:30 -08:00
|
|
|
ts := p.ts
|
|
|
|
return p.series, &ts, p.val
|
2018-10-04 09:57:47 -07:00
|
|
|
}
|
|
|
|
return p.series, nil, p.val
|
|
|
|
}
|
|
|
|
|
|
|
|
// Help returns the metric name and help text in the current entry.
|
|
|
|
// Must only be called after Next returned a help entry.
|
|
|
|
// The returned byte slices become invalid after the next call to Next.
|
2018-10-09 06:09:44 -07:00
|
|
|
func (p *OpenMetricsParser) Help() ([]byte, []byte) {
|
2018-10-04 09:57:47 -07:00
|
|
|
m := p.l.b[p.offsets[0]:p.offsets[1]]
|
|
|
|
|
|
|
|
// Replacer causes allocations. Replace only when necessary.
|
|
|
|
if strings.IndexByte(yoloString(p.text), byte('\\')) >= 0 {
|
|
|
|
// OpenMetrics always uses the Prometheus format label value escaping.
|
|
|
|
return m, []byte(lvalReplacer.Replace(string(p.text)))
|
|
|
|
}
|
|
|
|
return m, p.text
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the metric name and type in the current entry.
|
|
|
|
// Must only be called after Next returned a type entry.
|
|
|
|
// The returned byte slices become invalid after the next call to Next.
|
2018-10-09 06:09:44 -07:00
|
|
|
func (p *OpenMetricsParser) Type() ([]byte, MetricType) {
|
2018-10-04 09:57:47 -07:00
|
|
|
return p.l.b[p.offsets[0]:p.offsets[1]], p.mtype
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unit returns the metric name and unit in the current entry.
|
|
|
|
// Must only be called after Next returned a unit entry.
|
|
|
|
// The returned byte slices become invalid after the next call to Next.
|
2018-10-09 06:09:44 -07:00
|
|
|
func (p *OpenMetricsParser) Unit() ([]byte, []byte) {
|
2018-10-04 09:57:47 -07:00
|
|
|
// The Prometheus format does not have units.
|
|
|
|
return p.l.b[p.offsets[0]:p.offsets[1]], p.text
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comment returns the text of the current comment.
|
|
|
|
// Must only be called after Next returned a comment entry.
|
|
|
|
// The returned byte slice becomes invalid after the next call to Next.
|
2018-10-09 06:09:44 -07:00
|
|
|
func (p *OpenMetricsParser) Comment() []byte {
|
2018-10-04 09:57:47 -07:00
|
|
|
return p.text
|
|
|
|
}
|
|
|
|
|
|
|
|
// Metric writes the labels of the current sample into the passed labels.
|
|
|
|
// It returns the string from which the metric was parsed.
|
2018-10-09 06:09:44 -07:00
|
|
|
func (p *OpenMetricsParser) Metric(l *labels.Labels) string {
|
2018-10-04 09:57:47 -07:00
|
|
|
// Allocate the full immutable string immediately, so we just
|
|
|
|
// have to create references on it below.
|
|
|
|
s := string(p.series)
|
|
|
|
|
|
|
|
*l = append(*l, labels.Label{
|
|
|
|
Name: labels.MetricName,
|
|
|
|
Value: s[:p.offsets[0]-p.start],
|
|
|
|
})
|
|
|
|
|
|
|
|
for i := 1; i < len(p.offsets); i += 4 {
|
|
|
|
a := p.offsets[i] - p.start
|
|
|
|
b := p.offsets[i+1] - p.start
|
|
|
|
c := p.offsets[i+2] - p.start
|
|
|
|
d := p.offsets[i+3] - p.start
|
|
|
|
|
|
|
|
// 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: lvalReplacer.Replace(s[c:d])})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
*l = append(*l, labels.Label{Name: s[a:b], Value: s[c:d]})
|
|
|
|
}
|
|
|
|
|
2022-03-31 11:18:53 -07:00
|
|
|
// Sort labels.
|
|
|
|
sort.Sort(*l)
|
2018-10-04 09:57:47 -07:00
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2019-11-19 01:33:30 -08:00
|
|
|
// Exemplar writes the exemplar of the current sample into the passed
|
|
|
|
// exemplar. It returns the whether an exemplar exists.
|
|
|
|
func (p *OpenMetricsParser) Exemplar(e *exemplar.Exemplar) bool {
|
|
|
|
if len(p.exemplar) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate the full immutable string immediately, so we just
|
|
|
|
// have to create references on it below.
|
|
|
|
s := string(p.exemplar)
|
|
|
|
|
|
|
|
e.Value = p.exemplarVal
|
|
|
|
if p.hasExemplarTs {
|
|
|
|
e.HasTs = true
|
|
|
|
e.Ts = p.exemplarTs
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(p.eOffsets); i += 4 {
|
|
|
|
a := p.eOffsets[i] - p.start
|
|
|
|
b := p.eOffsets[i+1] - p.start
|
|
|
|
c := p.eOffsets[i+2] - p.start
|
|
|
|
d := p.eOffsets[i+3] - p.start
|
|
|
|
|
|
|
|
e.Labels = append(e.Labels, labels.Label{Name: s[a:b], Value: s[c:d]})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the labels.
|
|
|
|
sort.Sort(e.Labels)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-10-09 06:09:44 -07:00
|
|
|
// nextToken returns the next token from the openMetricsLexer.
|
|
|
|
func (p *OpenMetricsParser) nextToken() token {
|
2018-10-04 09:57:47 -07:00
|
|
|
tok := p.l.Lex()
|
|
|
|
return tok
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next advances the parser to the next sample. It returns false if no
|
|
|
|
// more samples were read or an error occurred.
|
2018-10-09 06:09:44 -07:00
|
|
|
func (p *OpenMetricsParser) Next() (Entry, error) {
|
2018-10-04 09:57:47 -07:00
|
|
|
var err error
|
|
|
|
|
|
|
|
p.start = p.l.i
|
|
|
|
p.offsets = p.offsets[:0]
|
2019-11-19 01:33:30 -08:00
|
|
|
p.eOffsets = p.eOffsets[:0]
|
|
|
|
p.exemplar = p.exemplar[:0]
|
|
|
|
p.exemplarVal = 0
|
|
|
|
p.hasExemplarTs = false
|
2018-10-04 09:57:47 -07:00
|
|
|
|
2020-02-06 23:33:26 -08:00
|
|
|
switch t := p.nextToken(); t {
|
2020-03-23 07:47:11 -07:00
|
|
|
case tEOFWord:
|
2018-10-04 09:57:47 -07:00
|
|
|
if t := p.nextToken(); t != tEOF {
|
2019-03-25 16:01:12 -07:00
|
|
|
return EntryInvalid, errors.New("unexpected data after # EOF")
|
2018-10-04 09:57:47 -07:00
|
|
|
}
|
|
|
|
return EntryInvalid, io.EOF
|
|
|
|
case tEOF:
|
2020-02-06 23:33:26 -08:00
|
|
|
return EntryInvalid, errors.New("data does not end with # EOF")
|
2018-10-04 09:57:47 -07:00
|
|
|
case tHelp, tType, tUnit:
|
2022-03-08 05:04:11 -08:00
|
|
|
switch t2 := p.nextToken(); t2 {
|
2018-10-04 09:57:47 -07:00
|
|
|
case tMName:
|
|
|
|
p.offsets = append(p.offsets, p.l.start, p.l.i)
|
|
|
|
default:
|
2022-03-08 05:04:11 -08:00
|
|
|
return EntryInvalid, parseError("expected metric name after "+t.String(), t2)
|
2018-10-04 09:57:47 -07:00
|
|
|
}
|
2022-03-08 05:04:11 -08:00
|
|
|
switch t2 := p.nextToken(); t2 {
|
2018-10-04 09:57:47 -07:00
|
|
|
case tText:
|
|
|
|
if len(p.l.buf()) > 1 {
|
|
|
|
p.text = p.l.buf()[1 : len(p.l.buf())-1]
|
|
|
|
} else {
|
|
|
|
p.text = []byte{}
|
|
|
|
}
|
|
|
|
default:
|
2022-03-08 05:04:11 -08:00
|
|
|
return EntryInvalid, fmt.Errorf("expected text in %s", t.String())
|
2018-10-04 09:57:47 -07:00
|
|
|
}
|
|
|
|
switch t {
|
|
|
|
case tType:
|
|
|
|
switch s := yoloString(p.text); s {
|
|
|
|
case "counter":
|
|
|
|
p.mtype = MetricTypeCounter
|
|
|
|
case "gauge":
|
|
|
|
p.mtype = MetricTypeGauge
|
|
|
|
case "histogram":
|
|
|
|
p.mtype = MetricTypeHistogram
|
|
|
|
case "gaugehistogram":
|
|
|
|
p.mtype = MetricTypeGaugeHistogram
|
|
|
|
case "summary":
|
|
|
|
p.mtype = MetricTypeSummary
|
|
|
|
case "info":
|
|
|
|
p.mtype = MetricTypeInfo
|
|
|
|
case "stateset":
|
|
|
|
p.mtype = MetricTypeStateset
|
|
|
|
case "unknown":
|
|
|
|
p.mtype = MetricTypeUnknown
|
|
|
|
default:
|
2022-06-27 12:29:19 -07:00
|
|
|
return EntryInvalid, fmt.Errorf("invalid metric type %q", s)
|
2018-10-04 09:57:47 -07:00
|
|
|
}
|
|
|
|
case tHelp:
|
|
|
|
if !utf8.Valid(p.text) {
|
2019-03-25 16:01:12 -07:00
|
|
|
return EntryInvalid, errors.New("help text is not a valid utf8 string")
|
2018-10-04 09:57:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
switch t {
|
|
|
|
case tHelp:
|
|
|
|
return EntryHelp, nil
|
|
|
|
case tType:
|
|
|
|
return EntryType, nil
|
|
|
|
case tUnit:
|
|
|
|
m := yoloString(p.l.b[p.offsets[0]:p.offsets[1]])
|
|
|
|
u := yoloString(p.text)
|
|
|
|
if len(u) > 0 {
|
|
|
|
if !strings.HasSuffix(m, u) || len(m) < len(u)+1 || p.l.b[p.offsets[1]-len(u)-1] != '_' {
|
2022-06-27 12:29:19 -07:00
|
|
|
return EntryInvalid, fmt.Errorf("unit not a suffix of metric %q", m)
|
2018-10-04 09:57:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return EntryUnit, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
case tMName:
|
|
|
|
p.offsets = append(p.offsets, p.l.i)
|
|
|
|
p.series = p.l.b[p.start:p.l.i]
|
|
|
|
|
|
|
|
t2 := p.nextToken()
|
|
|
|
if t2 == tBraceOpen {
|
2021-09-08 01:09:21 -07:00
|
|
|
p.offsets, err = p.parseLVals(p.offsets)
|
2019-11-19 01:33:30 -08:00
|
|
|
if err != nil {
|
2018-10-04 09:57:47 -07:00
|
|
|
return EntryInvalid, err
|
|
|
|
}
|
|
|
|
p.series = p.l.b[p.start:p.l.i]
|
|
|
|
t2 = p.nextToken()
|
|
|
|
}
|
2019-11-19 01:33:30 -08:00
|
|
|
p.val, err = p.getFloatValue(t2, "metric")
|
|
|
|
if err != nil {
|
2018-10-04 09:57:47 -07:00
|
|
|
return EntryInvalid, err
|
|
|
|
}
|
2019-11-19 01:33:30 -08:00
|
|
|
|
2018-10-04 09:57:47 -07:00
|
|
|
p.hasTS = false
|
2019-11-19 01:33:30 -08:00
|
|
|
switch t2 := p.nextToken(); t2 {
|
|
|
|
case tEOF:
|
2019-12-24 00:48:28 -08:00
|
|
|
return EntryInvalid, errors.New("data does not end with # EOF")
|
2018-10-04 09:57:47 -07:00
|
|
|
case tLinebreak:
|
|
|
|
break
|
2019-11-19 01:33:30 -08:00
|
|
|
case tComment:
|
|
|
|
if err := p.parseComment(); err != nil {
|
|
|
|
return EntryInvalid, err
|
|
|
|
}
|
2018-10-04 09:57:47 -07:00
|
|
|
case tTimestamp:
|
|
|
|
p.hasTS = true
|
|
|
|
var ts float64
|
|
|
|
// A float is enough to hold what we need for millisecond resolution.
|
2019-09-05 07:18:18 -07:00
|
|
|
if ts, err = parseFloat(yoloString(p.l.buf()[1:])); err != nil {
|
2018-10-04 09:57:47 -07:00
|
|
|
return EntryInvalid, err
|
|
|
|
}
|
2021-06-28 17:54:50 -07:00
|
|
|
if math.IsNaN(ts) || math.IsInf(ts, 0) {
|
|
|
|
return EntryInvalid, errors.New("invalid timestamp")
|
|
|
|
}
|
2018-10-04 09:57:47 -07:00
|
|
|
p.ts = int64(ts * 1000)
|
2019-11-19 01:33:30 -08:00
|
|
|
switch t3 := p.nextToken(); t3 {
|
|
|
|
case tLinebreak:
|
|
|
|
case tComment:
|
|
|
|
if err := p.parseComment(); err != nil {
|
|
|
|
return EntryInvalid, err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return EntryInvalid, parseError("expected next entry after timestamp", t3)
|
2018-10-04 09:57:47 -07:00
|
|
|
}
|
|
|
|
default:
|
2019-11-19 01:33:30 -08:00
|
|
|
return EntryInvalid, parseError("expected timestamp or # symbol", t2)
|
2018-10-04 09:57:47 -07:00
|
|
|
}
|
|
|
|
return EntrySeries, nil
|
|
|
|
|
|
|
|
default:
|
2022-06-27 12:29:19 -07:00
|
|
|
err = fmt.Errorf("%q %q is not a valid start token", t, string(p.l.cur()))
|
2018-10-04 09:57:47 -07:00
|
|
|
}
|
|
|
|
return EntryInvalid, err
|
|
|
|
}
|
|
|
|
|
2019-11-19 01:33:30 -08:00
|
|
|
func (p *OpenMetricsParser) parseComment() error {
|
|
|
|
// Validate the name of the metric. It must have _total or _bucket as
|
|
|
|
// suffix for exemplars to be supported.
|
|
|
|
if err := p.validateNameForExemplar(p.series[:p.offsets[0]-p.start]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-08 01:09:21 -07:00
|
|
|
var err error
|
2019-11-19 01:33:30 -08:00
|
|
|
// Parse the labels.
|
2021-09-08 01:09:21 -07:00
|
|
|
p.eOffsets, err = p.parseLVals(p.eOffsets)
|
2019-11-19 01:33:30 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.exemplar = p.l.b[p.start:p.l.i]
|
|
|
|
|
|
|
|
// Get the value.
|
|
|
|
p.exemplarVal, err = p.getFloatValue(p.nextToken(), "exemplar labels")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the optional timestamp.
|
|
|
|
p.hasExemplarTs = false
|
|
|
|
switch t2 := p.nextToken(); t2 {
|
|
|
|
case tEOF:
|
2019-12-24 00:48:28 -08:00
|
|
|
return errors.New("data does not end with # EOF")
|
2019-11-19 01:33:30 -08:00
|
|
|
case tLinebreak:
|
|
|
|
break
|
|
|
|
case tTimestamp:
|
|
|
|
p.hasExemplarTs = true
|
|
|
|
var ts float64
|
|
|
|
// A float is enough to hold what we need for millisecond resolution.
|
|
|
|
if ts, err = parseFloat(yoloString(p.l.buf()[1:])); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-28 17:54:50 -07:00
|
|
|
if math.IsNaN(ts) || math.IsInf(ts, 0) {
|
|
|
|
return errors.New("invalid exemplar timestamp")
|
|
|
|
}
|
2019-11-19 01:33:30 -08:00
|
|
|
p.exemplarTs = int64(ts * 1000)
|
|
|
|
switch t3 := p.nextToken(); t3 {
|
|
|
|
case tLinebreak:
|
|
|
|
default:
|
|
|
|
return parseError("expected next entry after exemplar timestamp", t3)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return parseError("expected timestamp or comment", t2)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-08 01:09:21 -07:00
|
|
|
func (p *OpenMetricsParser) parseLVals(offsets []int) ([]int, error) {
|
2018-10-04 09:57:47 -07:00
|
|
|
first := true
|
|
|
|
for {
|
|
|
|
t := p.nextToken()
|
|
|
|
switch t {
|
|
|
|
case tBraceClose:
|
2019-11-19 01:33:30 -08:00
|
|
|
return offsets, nil
|
2018-10-04 09:57:47 -07:00
|
|
|
case tComma:
|
|
|
|
if first {
|
2019-11-19 01:33:30 -08:00
|
|
|
return nil, parseError("expected label name or left brace", t)
|
2018-10-04 09:57:47 -07:00
|
|
|
}
|
|
|
|
t = p.nextToken()
|
|
|
|
if t != tLName {
|
2019-11-19 01:33:30 -08:00
|
|
|
return nil, parseError("expected label name", t)
|
2018-10-04 09:57:47 -07:00
|
|
|
}
|
|
|
|
case tLName:
|
|
|
|
if !first {
|
2019-11-19 01:33:30 -08:00
|
|
|
return nil, parseError("expected comma", t)
|
2018-10-04 09:57:47 -07:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
if first {
|
2019-11-19 01:33:30 -08:00
|
|
|
return nil, parseError("expected label name or left brace", t)
|
2018-10-04 09:57:47 -07:00
|
|
|
}
|
2019-11-19 01:33:30 -08:00
|
|
|
return nil, parseError("expected comma or left brace", t)
|
2018-10-04 09:57:47 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
first = false
|
|
|
|
// t is now a label name.
|
|
|
|
|
2019-11-19 01:33:30 -08:00
|
|
|
offsets = append(offsets, p.l.start, p.l.i)
|
2018-10-04 09:57:47 -07:00
|
|
|
|
|
|
|
if t := p.nextToken(); t != tEqual {
|
2019-11-19 01:33:30 -08:00
|
|
|
return nil, parseError("expected equal", t)
|
2018-10-04 09:57:47 -07:00
|
|
|
}
|
|
|
|
if t := p.nextToken(); t != tLValue {
|
2019-11-19 01:33:30 -08:00
|
|
|
return nil, parseError("expected label value", t)
|
2018-10-04 09:57:47 -07:00
|
|
|
}
|
|
|
|
if !utf8.Valid(p.l.buf()) {
|
2019-11-19 01:33:30 -08:00
|
|
|
return nil, errors.New("invalid UTF-8 label value")
|
2018-10-04 09:57:47 -07:00
|
|
|
}
|
|
|
|
|
2018-10-09 06:09:44 -07:00
|
|
|
// The openMetricsLexer ensures the value string is quoted. Strip first
|
2018-10-04 09:57:47 -07:00
|
|
|
// and last character.
|
2019-11-19 01:33:30 -08:00
|
|
|
offsets = append(offsets, p.l.start+1, p.l.i-1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *OpenMetricsParser) getFloatValue(t token, after string) (float64, error) {
|
|
|
|
if t != tValue {
|
|
|
|
return 0, parseError(fmt.Sprintf("expected value after %v", after), t)
|
|
|
|
}
|
|
|
|
val, err := parseFloat(yoloString(p.l.buf()[1:]))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
// Ensure canonical NaN value.
|
|
|
|
if math.IsNaN(p.exemplarVal) {
|
|
|
|
val = math.Float64frombits(value.NormalNaN)
|
|
|
|
}
|
|
|
|
return val, nil
|
|
|
|
}
|
2018-10-04 09:57:47 -07:00
|
|
|
|
2019-11-19 01:33:30 -08:00
|
|
|
func (p *OpenMetricsParser) validateNameForExemplar(name []byte) error {
|
|
|
|
for _, suffix := range allowedSuffixes {
|
|
|
|
if bytes.HasSuffix(name, suffix) {
|
|
|
|
return nil
|
|
|
|
}
|
2018-10-04 09:57:47 -07:00
|
|
|
}
|
2019-11-19 01:33:30 -08:00
|
|
|
return fmt.Errorf("metric name %v does not support exemplars", string(name))
|
2018-10-04 09:57:47 -07:00
|
|
|
}
|