Merge pull request #11814 from prometheus/beorn7/protobuf

textparse: Add gauge histogram support
This commit is contained in:
Björn Rabenstein 2023-01-10 15:18:44 +01:00 committed by GitHub
commit f07adbd45f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 7 deletions

View File

@ -105,7 +105,7 @@ func (p *ProtobufParser) Series() ([]byte, *int64, float64) {
default: default:
v = s.GetQuantile()[p.fieldPos].GetValue() v = s.GetQuantile()[p.fieldPos].GetValue()
} }
case dto.MetricType_HISTOGRAM: case dto.MetricType_HISTOGRAM, dto.MetricType_GAUGE_HISTOGRAM:
// This should only happen for a legacy histogram. // This should only happen for a legacy histogram.
h := m.GetHistogram() h := m.GetHistogram()
switch p.fieldPos { switch p.fieldPos {
@ -225,6 +225,8 @@ func (p *ProtobufParser) Type() ([]byte, MetricType) {
return n, MetricTypeGauge return n, MetricTypeGauge
case dto.MetricType_HISTOGRAM: case dto.MetricType_HISTOGRAM:
return n, MetricTypeHistogram return n, MetricTypeHistogram
case dto.MetricType_GAUGE_HISTOGRAM:
return n, MetricTypeGaugeHistogram
case dto.MetricType_SUMMARY: case dto.MetricType_SUMMARY:
return n, MetricTypeSummary return n, MetricTypeSummary
} }
@ -273,7 +275,7 @@ func (p *ProtobufParser) Exemplar(ex *exemplar.Exemplar) bool {
switch p.mf.GetType() { switch p.mf.GetType() {
case dto.MetricType_COUNTER: case dto.MetricType_COUNTER:
exProto = m.GetCounter().GetExemplar() exProto = m.GetCounter().GetExemplar()
case dto.MetricType_HISTOGRAM: case dto.MetricType_HISTOGRAM, dto.MetricType_GAUGE_HISTOGRAM:
bb := m.GetHistogram().GetBucket() bb := m.GetHistogram().GetBucket()
if p.fieldPos < 0 { if p.fieldPos < 0 {
if p.state == EntrySeries { if p.state == EntrySeries {
@ -331,7 +333,7 @@ func (p *ProtobufParser) Next() (Entry, error) {
} }
// We are at the beginning of a metric family. Put only the name // We are at the beginning of a metric family. Put only the name
// into metricBytes and validate only name and help for now. // into metricBytes and validate only name, help, and type for now.
name := p.mf.GetName() name := p.mf.GetName()
if !model.IsValidMetricName(model.LabelValue(name)) { if !model.IsValidMetricName(model.LabelValue(name)) {
return EntryInvalid, errors.Errorf("invalid metric name: %s", name) return EntryInvalid, errors.Errorf("invalid metric name: %s", name)
@ -339,6 +341,17 @@ func (p *ProtobufParser) Next() (Entry, error) {
if help := p.mf.GetHelp(); !utf8.ValidString(help) { if help := p.mf.GetHelp(); !utf8.ValidString(help) {
return EntryInvalid, errors.Errorf("invalid help for metric %q: %s", name, help) return EntryInvalid, errors.Errorf("invalid help for metric %q: %s", name, help)
} }
switch p.mf.GetType() {
case dto.MetricType_COUNTER,
dto.MetricType_GAUGE,
dto.MetricType_HISTOGRAM,
dto.MetricType_GAUGE_HISTOGRAM,
dto.MetricType_SUMMARY,
dto.MetricType_UNTYPED:
// All good.
default:
return EntryInvalid, errors.Errorf("unknown metric type for metric %q: %s", name, p.mf.GetType())
}
p.metricBytes.Reset() p.metricBytes.Reset()
p.metricBytes.WriteString(name) p.metricBytes.WriteString(name)
@ -346,7 +359,8 @@ func (p *ProtobufParser) Next() (Entry, error) {
case EntryHelp: case EntryHelp:
p.state = EntryType p.state = EntryType
case EntryType: case EntryType:
if p.mf.GetType() == dto.MetricType_HISTOGRAM && t := p.mf.GetType()
if (t == dto.MetricType_HISTOGRAM || t == dto.MetricType_GAUGE_HISTOGRAM) &&
isNativeHistogram(p.mf.GetMetric()[0].GetHistogram()) { isNativeHistogram(p.mf.GetMetric()[0].GetHistogram()) {
p.state = EntryHistogram p.state = EntryHistogram
} else { } else {
@ -356,8 +370,11 @@ func (p *ProtobufParser) Next() (Entry, error) {
return EntryInvalid, err return EntryInvalid, err
} }
case EntryHistogram, EntrySeries: case EntryHistogram, EntrySeries:
t := p.mf.GetType()
if p.state == EntrySeries && !p.fieldsDone && if p.state == EntrySeries && !p.fieldsDone &&
(p.mf.GetType() == dto.MetricType_SUMMARY || p.mf.GetType() == dto.MetricType_HISTOGRAM) { (t == dto.MetricType_SUMMARY ||
t == dto.MetricType_HISTOGRAM ||
t == dto.MetricType_GAUGE_HISTOGRAM) {
p.fieldPos++ p.fieldPos++
} else { } else {
p.metricPos++ p.metricPos++
@ -418,7 +435,7 @@ func (p *ProtobufParser) getMagicName() string {
if p.fieldPos == -1 { if p.fieldPos == -1 {
return p.mf.GetName() + "_sum" return p.mf.GetName() + "_sum"
} }
if t == dto.MetricType_HISTOGRAM { if t == dto.MetricType_HISTOGRAM || t == dto.MetricType_GAUGE_HISTOGRAM {
return p.mf.GetName() + "_bucket" return p.mf.GetName() + "_bucket"
} }
return p.mf.GetName() return p.mf.GetName()
@ -436,7 +453,7 @@ func (p *ProtobufParser) getMagicLabel() (bool, string, string) {
q := qq[p.fieldPos] q := qq[p.fieldPos]
p.fieldsDone = p.fieldPos == len(qq)-1 p.fieldsDone = p.fieldPos == len(qq)-1
return true, model.QuantileLabel, formatOpenMetricsFloat(q.GetQuantile()) return true, model.QuantileLabel, formatOpenMetricsFloat(q.GetQuantile())
case dto.MetricType_HISTOGRAM: case dto.MetricType_HISTOGRAM, dto.MetricType_GAUGE_HISTOGRAM:
bb := p.mf.GetMetric()[p.metricPos].GetHistogram().GetBucket() bb := p.mf.GetMetric()[p.metricPos].GetHistogram().GetBucket()
if p.fieldPos >= len(bb) { if p.fieldPos >= len(bb) {
p.fieldsDone = true p.fieldsDone = true