feat: multiple changes

- implement changes from pair programming session
- use newParse.val()
- advance parser p if ct is found

Signed-off-by: Manik Rana <manikrana54@gmail.com>
This commit is contained in:
Manik Rana 2024-07-04 21:10:24 +05:30
parent 188d4cc927
commit e841e00eed

View file

@ -21,7 +21,6 @@ import (
"fmt" "fmt"
"io" "io"
"math" "math"
"strconv"
"strings" "strings"
"unicode/utf8" "unicode/utf8"
@ -224,44 +223,48 @@ func (p *OpenMetricsParser) Exemplar(e *exemplar.Exemplar) bool {
// CreatedTimestamp returns nil as it's not implemented yet. // CreatedTimestamp returns nil as it's not implemented yet.
// TODO(bwplotka): https://github.com/prometheus/prometheus/issues/12980 // TODO(bwplotka): https://github.com/prometheus/prometheus/issues/12980
func (p *OpenMetricsParser) CreatedTimestamp() *int64 { func (p *OpenMetricsParser) CreatedTimestamp() *int64 {
var lbs labels.Labels
p.Metric(&lbs)
lbs = lbs.DropMetricName()
newParser := deepCopyParser(p) newParser := deepCopyParser(p)
loop:
for {
switch t, _ := newParser.Next(); t { switch t, _ := newParser.Next(); t {
case EntrySeries: case EntrySeries:
// continue instead of return nil until we get new series/labels. can happen for histograms, summaries // continue instead of return nil until we get new series/labels. can happen for histograms, summaries
for {
// Check _created suffix // Check _created suffix
var lbs labels.Labels var newLbs labels.Labels
newParser.Metric(&lbs) newParser.Metric(&newLbs)
name := lbs.Get(model.MetricNameLabel) name := newLbs.Get(model.MetricNameLabel)
if name[len(name)-8:] != "_created" { if !strings.HasSuffix(name, "_created") {
return nil continue
} }
// TODO: potentially broken? Missing type?
if newParser.mName != p.mName { if newParser.mName != p.mName {
return nil return nil
} }
// edge case: if guage_created of unknown type -> skip parsing // edge case: if guage_created of unknown type -> skip parsing
newLbs = newLbs.DropMetricName()
// Check if labelsets are the same if !labels.Equal(lbs, newLbs) {
return nil
}
// TODO: for histograms // TODO: for histograms
// if t, _ := newParser.Next(); t != EntrySeries { // if t, _ := newParser.Next(); t != EntrySeries {
// return nil // return nil
// } // }
// return CT value
ctBytes := newParser.l.b[newParser.offsets[len(newParser.offsets)-1]+1:newParser.l.start]
ct, err := strconv.ParseInt(yoloString(ctBytes), 10, 64)
if err != nil {
return nil
}
// guage_created is a metric // guage_created is a metric
ct := int64(newParser.val)
p.Next()
return &ct return &ct
}
default: default:
// If not series, we don't care. break loop
}
} }
return nil return nil
} }