Merge pull request #13299 from vesari/add-unit-parser

protobuf: add unit parser
This commit is contained in:
Björn Rabenstein 2024-01-09 14:47:50 +01:00 committed by GitHub
commit 3db4596965
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 4 deletions

View file

@ -136,7 +136,6 @@ func (p *OpenMetricsParser) Type() ([]byte, model.MetricType) {
// Must only be called after Next returned a unit entry. // Must only be called after Next returned a unit entry.
// The returned byte slices become invalid after the next call to Next. // The returned byte slices become invalid after the next call to Next.
func (p *OpenMetricsParser) Unit() ([]byte, []byte) { func (p *OpenMetricsParser) Unit() ([]byte, []byte) {
// The Prometheus format does not have units.
return p.l.b[p.offsets[0]:p.offsets[1]], p.text return p.l.b[p.offsets[0]:p.offsets[1]], p.text
} }

View file

@ -269,10 +269,11 @@ func (p *ProtobufParser) Type() ([]byte, model.MetricType) {
return n, model.MetricTypeUnknown return n, model.MetricTypeUnknown
} }
// Unit always returns (nil, nil) because units aren't supported by the protobuf // Unit returns the metric unit in the current entry.
// format. // Must only be called after Next returned a unit entry.
// The returned byte slices become invalid after the next call to Next.
func (p *ProtobufParser) Unit() ([]byte, []byte) { func (p *ProtobufParser) Unit() ([]byte, []byte) {
return nil, nil return p.metricBytes.Bytes(), []byte(p.mf.GetUnit())
} }
// Comment always returns nil because comments aren't supported by the protobuf // Comment always returns nil because comments aren't supported by the protobuf
@ -422,6 +423,16 @@ func (p *ProtobufParser) Next() (Entry, error) {
default: default:
return EntryInvalid, fmt.Errorf("unknown metric type for metric %q: %s", name, p.mf.GetType()) return EntryInvalid, fmt.Errorf("unknown metric type for metric %q: %s", name, p.mf.GetType())
} }
unit := p.mf.GetUnit()
if len(unit) > 0 {
if p.mf.GetType() == dto.MetricType_COUNTER && strings.HasSuffix(name, "_total") {
if !strings.HasSuffix(name[:len(name)-6], unit) || len(name)-6 < len(unit)+1 || name[len(name)-6-len(unit)-1] != '_' {
return EntryInvalid, fmt.Errorf("unit %q not a suffix of counter %q", unit, name)
}
} else if !strings.HasSuffix(name, unit) || len(name) < len(unit)+1 || name[len(name)-len(unit)-1] != '_' {
return EntryInvalid, fmt.Errorf("unit %q not a suffix of metric %q", unit, name)
}
}
p.metricBytes.Reset() p.metricBytes.Reset()
p.metricBytes.WriteString(name) p.metricBytes.WriteString(name)

View file

@ -58,6 +58,7 @@ metric: <
`name: "go_memstats_alloc_bytes_total" `name: "go_memstats_alloc_bytes_total"
help: "Total number of bytes allocated, even if freed." help: "Total number of bytes allocated, even if freed."
type: COUNTER type: COUNTER
unit: "bytes"
metric: < metric: <
counter: < counter: <
value: 1.546544e+06 value: 1.546544e+06
@ -665,6 +666,7 @@ func TestProtobufParse(t *testing.T) {
{ {
m: "go_memstats_alloc_bytes_total", m: "go_memstats_alloc_bytes_total",
help: "Total number of bytes allocated, even if freed.", help: "Total number of bytes allocated, even if freed.",
unit: "bytes",
}, },
{ {
m: "go_memstats_alloc_bytes_total", m: "go_memstats_alloc_bytes_total",