chore: comments and readability updates

Signed-off-by: Manik Rana <manikrana54@gmail.com>
This commit is contained in:
Manik Rana 2024-08-05 21:38:05 +05:30
parent 49bf90c897
commit debe023afa
2 changed files with 23 additions and 18 deletions

View file

@ -95,21 +95,26 @@ type OpenMetricsParser struct {
exemplarTs int64 exemplarTs int64
hasExemplarTs bool hasExemplarTs bool
// lines ending with _created are skipped by default as they are
// parsed by the CreatedTimestamp method. The skipCT flag is set to
// false when the CreatedTimestamp method is called.
skipCTSeries bool skipCTSeries bool
} }
type openMetricsParserOptions struct { type openMetricsParserOptions struct {
// SkipCT skips the parsing of _created lines.
SkipCTSeries bool SkipCTSeries bool
} }
// SkipCTSeries skips the parsing of _created lines.
type OpenMetricsOption func(*openMetricsParserOptions) type OpenMetricsOption func(*openMetricsParserOptions)
func WithOMParserCTSeriesSkipped(skipCTSeries bool) OpenMetricsOption { // WithOMParserCTSeriesSkipped turns off exposing _created lines
// as series, which makes those only used for parsing created timestamp
// for `CreatedTimestamp` method purposes.
//
// It's recommended to use this option to avoid using _created lines for other
// purposes than created timestamp, but leave false by default for the
// best-effort compatibility.
func WithOMParserCTSeriesSkipped() OpenMetricsOption {
return func(o *openMetricsParserOptions) { return func(o *openMetricsParserOptions) {
o.SkipCTSeries = skipCTSeries o.SkipCTSeries = true
} }
} }
@ -124,17 +129,17 @@ func NewOpenMetricsParser(b []byte, st *labels.SymbolTable) Parser {
// NewOpenMetricsParserWithOpts returns a new parser of the byte slice with options. // NewOpenMetricsParserWithOpts returns a new parser of the byte slice with options.
func NewOpenMetricsParserWithOpts(b []byte, st *labels.SymbolTable, opts ...OpenMetricsOption) Parser { func NewOpenMetricsParserWithOpts(b []byte, st *labels.SymbolTable, opts ...OpenMetricsOption) Parser {
options := &openMetricsParserOptions{}
for _, opt := range opts {
opt(options)
}
parser := &OpenMetricsParser{ parser := &OpenMetricsParser{
l: &openMetricsLexer{b: b}, l: &openMetricsLexer{b: b},
builder: labels.NewScratchBuilderWithSymbolTable(st, 16), builder: labels.NewScratchBuilderWithSymbolTable(st, 16),
skipCTSeries: options.SkipCTSeries,
} }
DefaultOpenMetricsParserOptions := openMetricsParserOptions{
SkipCTSeries: true,
}
for _, opt := range opts {
opt(&DefaultOpenMetricsParserOptions)
}
parser.skipCTSeries = DefaultOpenMetricsParserOptions.SkipCTSeries
return parser return parser
} }

View file

@ -330,7 +330,7 @@ fizz_created 17.0`
}, },
} }
p := NewOpenMetricsParserWithOpts([]byte(input), labels.NewSymbolTable(), WithOMParserCTSeriesSkipped(true)) p := NewOpenMetricsParserWithOpts([]byte(input), labels.NewSymbolTable(), WithOMParserCTSeriesSkipped())
checkParseResults(t, p, exp) checkParseResults(t, p, exp)
} }
@ -918,7 +918,7 @@ foobar{quantile="0.99"} 150.0`
}, },
} }
p := NewOpenMetricsParserWithOpts([]byte(input), labels.NewSymbolTable(), WithOMParserCTSeriesSkipped(true)) p := NewOpenMetricsParserWithOpts([]byte(input), labels.NewSymbolTable(), WithOMParserCTSeriesSkipped())
i := 0 i := 0
var res labels.Labels var res labels.Labels
@ -955,7 +955,7 @@ go_gc_duration_seconds
go_gc_duration_seconds_created`) go_gc_duration_seconds_created`)
st := labels.NewSymbolTable() st := labels.NewSymbolTable()
parser := NewOpenMetricsParserWithOpts(input, st, WithOMParserCTSeriesSkipped(true)).(*OpenMetricsParser) parser := NewOpenMetricsParserWithOpts(input, st, WithOMParserCTSeriesSkipped()).(*OpenMetricsParser)
// Modify the original parser state // Modify the original parser state
_, err := parser.Next() _, err := parser.Next()