Skip to content

Commit

Permalink
Merge pull request prometheus#13299 from vesari/add-unit-parser
Browse files Browse the repository at this point in the history
protobuf: add unit parser
  • Loading branch information
beorn7 authored Jan 9, 2024
2 parents 6cd24d8 + 8f07f9d commit 3db4596
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
1 change: 0 additions & 1 deletion model/textparse/openmetricsparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ func (p *OpenMetricsParser) Type() ([]byte, model.MetricType) {
// Must only be called after Next returned a unit entry.
// The returned byte slices become invalid after the next call to Next.
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
}

Expand Down
17 changes: 14 additions & 3 deletions model/textparse/protobufparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,11 @@ func (p *ProtobufParser) Type() ([]byte, model.MetricType) {
return n, model.MetricTypeUnknown
}

// Unit always returns (nil, nil) because units aren't supported by the protobuf
// format.
// Unit returns the metric 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.
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
Expand Down Expand Up @@ -422,6 +423,16 @@ func (p *ProtobufParser) Next() (Entry, error) {
default:
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.WriteString(name)

Expand Down
2 changes: 2 additions & 0 deletions model/textparse/protobufparse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ metric: <
`name: "go_memstats_alloc_bytes_total"
help: "Total number of bytes allocated, even if freed."
type: COUNTER
unit: "bytes"
metric: <
counter: <
value: 1.546544e+06
Expand Down Expand Up @@ -665,6 +666,7 @@ func TestProtobufParse(t *testing.T) {
{
m: "go_memstats_alloc_bytes_total",
help: "Total number of bytes allocated, even if freed.",
unit: "bytes",
},
{
m: "go_memstats_alloc_bytes_total",
Expand Down

0 comments on commit 3db4596

Please sign in to comment.