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 return n, 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 p.metricBytes.Bytes(), []byte(p.mf.GetUnit()) return p.metricBytes.Bytes(), []byte(p.mf.GetUnit())
} }
@ -418,6 +419,13 @@ 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()
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.Reset()
p.metricBytes.WriteString(name) p.metricBytes.WriteString(name)
@ -435,6 +443,8 @@ func (p *ProtobufParser) Next() (Entry, error) {
if err := p.updateMetricBytes(); err != nil { if err := p.updateMetricBytes(); err != nil {
return EntryInvalid, err return EntryInvalid, err
} }
// case EntryUnit:
// p.state = EntryType // what is the right state and place for this?
case EntryHistogram, EntrySeries: case EntryHistogram, EntrySeries:
if p.redoClassic { if p.redoClassic {
p.redoClassic = false p.redoClassic = false

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