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 (
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"
"strings"
"unicode/utf8"
2023-11-22 06:39:21 -08:00
"github.com/prometheus/common/model"
2021-11-08 06:23:17 -08:00
"github.com/prometheus/prometheus/model/exemplar"
Style cleanup of all the changes in sparsehistogram so far
A lot of this code was hacked together, literally during a
hackathon. This commit intends not to change the code substantially,
but just make the code obey the usual style practices.
A (possibly incomplete) list of areas:
* Generally address linter warnings.
* The `pgk` directory is deprecated as per dev-summit. No new packages should
be added to it. I moved the new `pkg/histogram` package to `model`
anticipating what's proposed in #9478.
* Make the naming of the Sparse Histogram more consistent. Including
abbreviations, there were just too many names for it: SparseHistogram,
Histogram, Histo, hist, his, shs, h. The idea is to call it "Histogram" in
general. Only add "Sparse" if it is needed to avoid confusion with
conventional Histograms (which is rare because the TSDB really has no notion
of conventional Histograms). Use abbreviations only in local scope, and then
really abbreviate (not just removing three out of seven letters like in
"Histo"). This is in the spirit of
https://github.com/golang/go/wiki/CodeReviewComments#variable-names
* Several other minor name changes.
* A lot of formatting of doc comments. For one, following
https://github.com/golang/go/wiki/CodeReviewComments#comment-sentences
, but also layout question, anticipating how things will look like
when rendered by `godoc` (even where `godoc` doesn't render them
right now because they are for unexported types or not a doc comment
at all but just a normal code comment - consistency is queen!).
* Re-enabled `TestQueryLog` and `TestEndopints` (they pass now,
leaving them disabled was presumably an oversight).
* Bucket iterator for histogram.Histogram is now created with a
method.
* HistogramChunk.iterator now allows iterator recycling. (I think
@dieterbe only commented it out because he was confused by the
question in the comment.)
* HistogramAppender.Append panics now because we decided to treat
staleness marker differently.
Signed-off-by: beorn7 <beorn@grafana.com>
2021-10-09 06:57:07 -07:00
"github.com/prometheus/prometheus/model/histogram"
2021-11-08 06:23:17 -08:00
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/value"
2018-10-04 09:57:47 -07:00
)
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
// 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
2022-03-09 14:13:50 -08:00
builder labels . ScratchBuilder
2018-10-04 09:57:47 -07:00
series [ ] byte
text [ ] byte
2023-11-22 06:39:21 -08:00
mtype model . MetricType
2024-07-04 08:40:24 -07:00
mName string
2018-10-04 09:57:47 -07:00
val float64
ts int64
hasTS bool
start int
2024-02-15 11:25:12 -08:00
// offsets is a list of offsets into series that describe the positions
// of the metric name and label names and values for this series.
// p.offsets[0] is the start character of the metric name.
// p.offsets[1] is the end of the metric name.
// Subsequently, p.offsets is a pair of pair of offsets for the positions
// of the label name and value start and end characters.
2018-10-04 09:57:47 -07:00
offsets [ ] int
2019-11-19 01:33:30 -08:00
eOffsets [ ] int
exemplar [ ] byte
exemplarVal float64
exemplarTs int64
hasExemplarTs bool
2024-07-09 08:56:34 -07:00
skipCT 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.
2023-03-25 06:31:24 -07:00
func NewOpenMetricsParser ( b [ ] byte , st * labels . SymbolTable ) Parser {
return & OpenMetricsParser {
l : & openMetricsLexer { b : b } ,
builder : labels . NewScratchBuilderWithSymbolTable ( st , 16 ) ,
2024-07-09 07:46:20 -07:00
skipCT : true ,
2023-03-25 06:31:24 -07:00
}
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
}
2023-01-10 15:30:55 -08:00
// Histogram returns (nil, nil, nil, nil) for now because OpenMetrics does not
// support sparse histograms yet.
2022-08-25 08:07:41 -07:00
func ( p * OpenMetricsParser ) Histogram ( ) ( [ ] byte , * int64 , * histogram . Histogram , * histogram . FloatHistogram ) {
return nil , nil , nil , nil
2021-06-29 14:45:23 -07:00
}
2018-10-04 09:57:47 -07:00
// 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.
2023-12-12 04:14:36 -08:00
func ( p * OpenMetricsParser ) Type ( ) ( [ ] byte , model . 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
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 {
2022-03-09 14:13:50 -08:00
// Copy the buffer to a string: this is only necessary for the return value.
2018-10-04 09:57:47 -07:00
s := string ( p . series )
2022-03-09 14:13:50 -08:00
p . builder . Reset ( )
2024-02-15 11:25:12 -08:00
metricName := unreplace ( s [ p . offsets [ 0 ] - p . start : p . offsets [ 1 ] - p . start ] )
p . builder . Add ( labels . MetricName , metricName )
2018-10-04 09:57:47 -07:00
2024-02-15 11:25:12 -08:00
for i := 2 ; i < len ( p . offsets ) ; i += 4 {
2018-10-04 09:57:47 -07:00
a := p . offsets [ i ] - p . start
b := p . offsets [ i + 1 ] - p . start
2024-02-15 11:25:12 -08:00
label := unreplace ( s [ a : b ] )
2018-10-04 09:57:47 -07:00
c := p . offsets [ i + 2 ] - p . start
d := p . offsets [ i + 3 ] - p . start
2024-02-15 11:25:12 -08:00
value := unreplace ( s [ c : d ] )
2018-10-04 09:57:47 -07:00
2024-02-15 11:25:12 -08:00
p . builder . Add ( label , value )
2018-10-04 09:57:47 -07:00
}
2022-03-09 14:13:50 -08:00
p . builder . Sort ( )
* l = p . builder . Labels ( )
2018-10-04 09:57:47 -07:00
return s
}
2023-07-13 05:16:10 -07:00
// Exemplar writes the exemplar of the current sample into the passed exemplar.
// It returns whether an exemplar exists. As OpenMetrics only ever has one
// exemplar per sample, every call after the first (for the same sample) will
// always return false.
2019-11-19 01:33:30 -08:00
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
}
2022-03-09 14:13:50 -08:00
p . builder . Reset ( )
2019-11-19 01:33:30 -08:00
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
2022-03-09 14:13:50 -08:00
p . builder . Add ( s [ a : b ] , s [ c : d ] )
2019-11-19 01:33:30 -08:00
}
2022-03-09 14:13:50 -08:00
p . builder . Sort ( )
e . Labels = p . builder . Labels ( )
2019-11-19 01:33:30 -08:00
2023-07-13 05:16:10 -07:00
// Wipe exemplar so that future calls return false.
p . exemplar = p . exemplar [ : 0 ]
2019-11-19 01:33:30 -08:00
return true
}
2023-12-11 00:43:42 -08:00
// CreatedTimestamp returns nil as it's not implemented yet.
// TODO(bwplotka): https://github.com/prometheus/prometheus/issues/12980
func ( p * OpenMetricsParser ) CreatedTimestamp ( ) * int64 {
2024-07-04 08:40:24 -07:00
var lbs labels . Labels
p . Metric ( & lbs )
lbs = lbs . DropMetricName ( )
2024-06-27 06:54:47 -07:00
newParser := deepCopyParser ( p )
2024-07-04 08:40:24 -07:00
loop :
for {
switch t , _ := newParser . Next ( ) ; t {
2024-06-27 06:54:47 -07:00
case EntrySeries :
2024-07-09 07:46:20 -07:00
// TODO: potentially broken? Missing type?
if newParser . mName != p . mName {
return nil
}
2024-07-04 08:40:24 -07:00
var newLbs labels . Labels
newParser . Metric ( & newLbs )
name := newLbs . Get ( model . MetricNameLabel )
if ! strings . HasSuffix ( name , "_created" ) {
continue
}
2024-07-04 08:50:19 -07:00
// edge case: if gauge_created of unknown type -> skip parsing
2024-07-04 08:40:24 -07:00
newLbs = newLbs . DropMetricName ( )
2024-07-09 07:46:20 -07:00
switch p . mtype {
case model . MetricTypeCounter :
if ! labels . Equal ( lbs , newLbs ) {
return nil
}
case model . MetricTypeSummary :
labelDiffs := lbs . MatchLabels ( false , newLbs . ExtractNames ( ) ... )
if labelDiffs . Len ( ) != 0 {
if ! labelDiffs . Contains ( "quantile" ) || labelDiffs . Len ( ) != 1 {
return nil
}
}
case model . MetricTypeHistogram :
labelDiffs := lbs . MatchLabels ( false , newLbs . ExtractNames ( ) ... )
if labelDiffs . Len ( ) != 0 {
if ! labelDiffs . Contains ( "le" ) || labelDiffs . Len ( ) != 1 {
return nil
}
}
default :
2024-07-09 08:12:13 -07:00
// if mtype is not counter, summary or histogram, it won't have a valid _created line
return nil
2024-07-04 08:40:24 -07:00
}
ct := int64 ( newParser . val )
return & ct
2024-06-27 06:54:47 -07:00
default :
2024-07-04 08:40:24 -07:00
break loop
2024-06-27 06:54:47 -07:00
}
2024-07-04 08:40:24 -07:00
}
2023-12-11 00:43:42 -08:00
return nil
2024-07-04 08:40:24 -07:00
}
2024-06-27 06:54:47 -07:00
2024-07-09 11:07:46 -07:00
// deepCopyParser creates a copy of a parser without re-using the slices' original memory addresses.
// The function `CreatedTimestamp()` uses a copy of the parser to "peek" at _created lines that might be several lines ahead, without changing the state of the original parser.
//
// Additionally, deepCopyParser switches `skipCT` from false to true, because this new parser needs to return _created lines.
2024-07-04 08:40:24 -07:00
func deepCopyParser ( p * OpenMetricsParser ) OpenMetricsParser {
2024-06-27 06:54:47 -07:00
newB := make ( [ ] byte , len ( p . l . b ) )
copy ( newB , p . l . b )
newLexer := & openMetricsLexer {
2024-07-04 08:40:24 -07:00
b : newB ,
i : p . l . i ,
2024-06-27 06:54:47 -07:00
start : p . l . start ,
2024-07-04 08:40:24 -07:00
err : p . l . err ,
2024-06-27 06:54:47 -07:00
state : p . l . state ,
}
2023-10-18 11:04:02 -07:00
2024-06-27 06:54:47 -07:00
newSeries := make ( [ ] byte , len ( p . series ) )
copy ( newSeries , p . series )
newText := make ( [ ] byte , len ( p . text ) )
copy ( newText , p . text )
newOffsets := make ( [ ] int , len ( p . offsets ) )
copy ( newOffsets , p . offsets )
newEOffsets := p . eOffsets
newExemplar := p . exemplar
newParser := OpenMetricsParser {
2024-07-04 08:40:24 -07:00
l : newLexer ,
builder : p . builder ,
series : newSeries ,
text : newText ,
mtype : p . mtype ,
mName : p . mName ,
val : p . val ,
ts : p . ts ,
hasTS : p . hasTS ,
start : p . start ,
offsets : newOffsets ,
eOffsets : newEOffsets ,
exemplar : newExemplar ,
exemplarVal : p . exemplarVal ,
exemplarTs : p . exemplarTs ,
hasExemplarTs : p . hasExemplarTs ,
2024-07-09 08:56:34 -07:00
skipCT : false ,
2024-06-27 06:54:47 -07:00
}
return newParser
2024-07-04 08:40:24 -07:00
}
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
}
2023-02-03 07:50:15 -08:00
func ( p * OpenMetricsParser ) parseError ( exp string , got token ) error {
e := p . l . i + 1
if len ( p . l . b ) < e {
e = len ( p . l . b )
}
return fmt . Errorf ( "%s, got %q (%q) while parsing: %q" , exp , p . l . b [ p . l . start : e ] , got , p . l . b [ p . start : e ] )
2022-12-07 05:32:03 -08:00
}
2024-02-29 07:40:53 -08:00
// Next advances the parser to the next sample.
// It returns (EntryInvalid, io.EOF) if no samples were read.
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 :
2024-02-15 11:25:12 -08:00
mStart := p . l . start
mEnd := p . l . i
if p . l . b [ mStart ] == '"' && p . l . b [ mEnd - 1 ] == '"' {
mStart ++
mEnd --
}
p . offsets = append ( p . offsets , mStart , mEnd )
2024-07-02 13:28:02 -07:00
p . mName = string ( p . l . b [ mStart : mEnd ] )
2018-10-04 09:57:47 -07:00
default :
2023-02-03 07:50:15 -08:00
return EntryInvalid , p . 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" :
2023-11-22 06:39:21 -08:00
p . mtype = model . MetricTypeCounter
2018-10-04 09:57:47 -07:00
case "gauge" :
2023-11-22 06:39:21 -08:00
p . mtype = model . MetricTypeGauge
2018-10-04 09:57:47 -07:00
case "histogram" :
2023-11-22 06:39:21 -08:00
p . mtype = model . MetricTypeHistogram
2018-10-04 09:57:47 -07:00
case "gaugehistogram" :
2023-11-22 06:39:21 -08:00
p . mtype = model . MetricTypeGaugeHistogram
2018-10-04 09:57:47 -07:00
case "summary" :
2023-11-22 06:39:21 -08:00
p . mtype = model . MetricTypeSummary
2018-10-04 09:57:47 -07:00
case "info" :
2023-11-22 06:39:21 -08:00
p . mtype = model . MetricTypeInfo
2018-10-04 09:57:47 -07:00
case "stateset" :
2023-11-22 06:39:21 -08:00
p . mtype = model . MetricTypeStateset
2018-10-04 09:57:47 -07:00
case "unknown" :
2023-11-22 06:39:21 -08:00
p . mtype = model . MetricTypeUnknown
2018-10-04 09:57:47 -07:00
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 ) {
2023-02-03 07:50:15 -08:00
return EntryInvalid , fmt . Errorf ( "help text %q is not a valid utf8 string" , p . text )
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 ] != '_' {
2023-02-03 07:50:15 -08:00
return EntryInvalid , fmt . Errorf ( "unit %q not a suffix of metric %q" , u , m )
2018-10-04 09:57:47 -07:00
}
}
return EntryUnit , nil
}
2024-02-15 11:25:12 -08:00
case tBraceOpen :
// We found a brace, so make room for the eventual metric name. If these
// values aren't updated, then the metric name was not set inside the
// braces and we can return an error.
if len ( p . offsets ) == 0 {
p . offsets = [ ] int { - 1 , - 1 }
}
if p . offsets , err = p . parseLVals ( p . offsets , false ) ; err != nil {
return EntryInvalid , err
}
p . series = p . l . b [ p . start : p . l . i ]
return p . parseMetricSuffix ( p . nextToken ( ) )
2018-10-04 09:57:47 -07:00
case tMName :
2024-02-15 11:25:12 -08:00
p . offsets = append ( p . offsets , p . start , p . l . i )
2018-10-04 09:57:47 -07:00
p . series = p . l . b [ p . start : p . l . i ]
t2 := p . nextToken ( )
if t2 == tBraceOpen {
2024-02-15 11:25:12 -08:00
p . offsets , err = p . parseLVals ( p . offsets , false )
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 ( )
}
2024-02-15 11:25:12 -08:00
return p . parseMetricSuffix ( t2 )
2018-10-04 09:57:47 -07:00
default :
2023-02-03 07:50:15 -08:00
err = p . parseError ( "expected a valid start token" , t )
2018-10-04 09:57:47 -07:00
}
return EntryInvalid , err
}
2019-11-19 01:33:30 -08:00
func ( p * OpenMetricsParser ) parseComment ( ) error {
2021-09-08 01:09:21 -07:00
var err error
2019-11-19 01:33:30 -08:00
// Parse the labels.
2024-02-15 11:25:12 -08:00
p . eOffsets , err = p . parseLVals ( p . eOffsets , true )
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 {
2023-09-29 13:50:30 -07:00
return fmt . Errorf ( "%w while parsing: %q" , err , p . l . b [ p . start : p . l . i ] )
2019-11-19 01:33:30 -08:00
}
2021-06-28 17:54:50 -07:00
if math . IsNaN ( ts ) || math . IsInf ( ts , 0 ) {
2023-02-03 07:50:15 -08:00
return fmt . Errorf ( "invalid exemplar timestamp %f" , ts )
2021-06-28 17:54:50 -07:00
}
2019-11-19 01:33:30 -08:00
p . exemplarTs = int64 ( ts * 1000 )
switch t3 := p . nextToken ( ) ; t3 {
case tLinebreak :
default :
2023-02-03 07:50:15 -08:00
return p . parseError ( "expected next entry after exemplar timestamp" , t3 )
2019-11-19 01:33:30 -08:00
}
default :
2023-02-03 07:50:15 -08:00
return p . parseError ( "expected timestamp or comment" , t2 )
2019-11-19 01:33:30 -08:00
}
return nil
}
2024-02-15 11:25:12 -08:00
func ( p * OpenMetricsParser ) parseLVals ( offsets [ ] int , isExemplar bool ) ( [ ] int , error ) {
t := p . nextToken ( )
2018-10-04 09:57:47 -07:00
for {
2024-02-15 11:25:12 -08:00
curTStart := p . l . start
curTI := p . l . i
2018-10-04 09:57:47 -07:00
switch t {
case tBraceClose :
2019-11-19 01:33:30 -08:00
return offsets , nil
2024-02-15 11:25:12 -08:00
case tLName :
case tQString :
default :
return nil , p . parseError ( "expected label name" , t )
}
t = p . nextToken ( )
// A quoted string followed by a comma or brace is a metric name. Set the
// offsets and continue processing. If this is an exemplar, this format
// is not allowed.
if t == tComma || t == tBraceClose {
if isExemplar {
2023-02-03 07:50:15 -08:00
return nil , p . parseError ( "expected label name" , t )
2018-10-04 09:57:47 -07:00
}
2024-02-15 11:25:12 -08:00
if offsets [ 0 ] != - 1 || offsets [ 1 ] != - 1 {
return nil , fmt . Errorf ( "metric name already set while parsing: %q" , p . l . b [ p . start : p . l . i ] )
2018-10-04 09:57:47 -07:00
}
2024-02-15 11:25:12 -08:00
offsets [ 0 ] = curTStart + 1
offsets [ 1 ] = curTI - 1
if t == tBraceClose {
return offsets , nil
2018-10-04 09:57:47 -07:00
}
2024-02-15 11:25:12 -08:00
t = p . nextToken ( )
continue
2018-10-04 09:57:47 -07:00
}
2024-02-15 11:25:12 -08:00
// We have a label name, and it might be quoted.
if p . l . b [ curTStart ] == '"' {
curTStart ++
curTI --
}
offsets = append ( offsets , curTStart , curTI )
2018-10-04 09:57:47 -07:00
2024-02-15 11:25:12 -08:00
if t != tEqual {
2023-02-03 07:50:15 -08:00
return nil , p . parseError ( "expected equal" , t )
2018-10-04 09:57:47 -07:00
}
if t := p . nextToken ( ) ; t != tLValue {
2023-02-03 07:50:15 -08:00
return nil , p . parseError ( "expected label value" , t )
2018-10-04 09:57:47 -07:00
}
if ! utf8 . Valid ( p . l . buf ( ) ) {
2023-02-03 07:50:15 -08:00
return nil , fmt . Errorf ( "invalid UTF-8 label value: %q" , p . l . buf ( ) )
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 )
2024-02-15 11:25:12 -08:00
// Free trailing commas are allowed.
t = p . nextToken ( )
if t == tComma {
t = p . nextToken ( )
} else if t != tBraceClose {
return nil , p . parseError ( "expected comma or brace close" , t )
}
}
}
// parseMetricSuffix parses the end of the line after the metric name and
// labels. It starts parsing with the provided token.
func ( p * OpenMetricsParser ) parseMetricSuffix ( t token ) ( Entry , error ) {
if p . offsets [ 0 ] == - 1 {
return EntryInvalid , fmt . Errorf ( "metric name not set while parsing: %q" , p . l . b [ p . start : p . l . i ] )
}
var err error
p . val , err = p . getFloatValue ( t , "metric" )
if err != nil {
return EntryInvalid , err
}
p . hasTS = false
switch t2 := p . nextToken ( ) ; t2 {
case tEOF :
return EntryInvalid , errors . New ( "data does not end with # EOF" )
case tLinebreak :
break
case tComment :
if err := p . parseComment ( ) ; err != nil {
return EntryInvalid , err
}
case tTimestamp :
p . hasTS = 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 EntryInvalid , fmt . Errorf ( "%w while parsing: %q" , err , p . l . b [ p . start : p . l . i ] )
}
if math . IsNaN ( ts ) || math . IsInf ( ts , 0 ) {
return EntryInvalid , fmt . Errorf ( "invalid timestamp %f" , ts )
}
p . ts = int64 ( ts * 1000 )
switch t3 := p . nextToken ( ) ; t3 {
case tLinebreak :
case tComment :
if err := p . parseComment ( ) ; err != nil {
return EntryInvalid , err
}
default :
return EntryInvalid , p . parseError ( "expected next entry after timestamp" , t3 )
}
2019-11-19 01:33:30 -08:00
}
2024-07-09 07:46:20 -07:00
var newLbs labels . Labels
p . Metric ( & newLbs )
name := newLbs . Get ( model . MetricNameLabel )
2024-07-09 08:56:34 -07:00
if strings . HasSuffix ( name , "_created" ) && p . skipCT {
2024-07-09 07:46:20 -07:00
return p . Next ( )
}
2024-02-15 11:25:12 -08:00
return EntrySeries , nil
2019-11-19 01:33:30 -08:00
}
func ( p * OpenMetricsParser ) getFloatValue ( t token , after string ) ( float64 , error ) {
if t != tValue {
2023-02-03 07:50:15 -08:00
return 0 , p . parseError ( fmt . Sprintf ( "expected value after %v" , after ) , t )
2019-11-19 01:33:30 -08:00
}
val , err := parseFloat ( yoloString ( p . l . buf ( ) [ 1 : ] ) )
if err != nil {
2023-09-29 13:50:30 -07:00
return 0 , fmt . Errorf ( "%w while parsing: %q" , err , p . l . b [ p . start : p . l . i ] )
2019-11-19 01:33:30 -08:00
}
// Ensure canonical NaN value.
if math . IsNaN ( p . exemplarVal ) {
val = math . Float64frombits ( value . NormalNaN )
}
return val , nil
}