Go on adding protobuf parsing for unit

Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
This commit is contained in:
Arianna Vespri 2023-12-14 17:00:52 +01:00
parent 5d5acf5744
commit 9fb1e9715c
2 changed files with 14 additions and 2 deletions

View File

@ -269,8 +269,9 @@ func (p *ProtobufParser) Type() ([]byte, MetricType) {
return n, 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 p.metricBytes.Bytes(), []byte(p.mf.GetUnit())
}
@ -418,6 +419,13 @@ 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()
fmt.Println("This is the unit:", unit)
if len(unit) > 0 {
if !strings.HasSuffix(name, fmt.Sprintf("_%s_total", unit)) || !strings.HasSuffix(name, fmt.Sprintf("_%s", unit)) || len(name) < len(unit)+1 {
return EntryInvalid, fmt.Errorf("invalid unit for metric %q: %s", name, unit)
}
}
p.metricBytes.Reset()
p.metricBytes.WriteString(name)
@ -435,6 +443,8 @@ func (p *ProtobufParser) Next() (Entry, error) {
if err := p.updateMetricBytes(); err != nil {
return EntryInvalid, err
}
// case EntryUnit:
// p.state = EntryType // what is the right state and place for this?
case EntryHistogram, EntrySeries:
if p.redoClassic {
p.redoClassic = false

View File

@ -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
@ -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",