From ddb519d11aece5266a803e870587cd57a8d128a1 Mon Sep 17 00:00:00 2001 From: Alexander Bartolomey Date: Wed, 6 Mar 2024 11:54:29 +0100 Subject: [PATCH] fix: missing Message initialization and passthru of caches to Sets Signed-off-by: Alexander Bartolomey --- decode.go | 20 ++++++++++++-------- go.mod | 14 ++++++-------- go.sum | 36 ++++++++++++++++++++---------------- sets.go | 10 ++++++++-- 4 files changed, 46 insertions(+), 34 deletions(-) diff --git a/decode.go b/decode.go index e6e40e8..21a4e49 100644 --- a/decode.go +++ b/decode.go @@ -92,9 +92,11 @@ func (d *Decoder) WithCompletionHook(hook func(*decoderMetrics)) *Decoder { // Decode takes payload as a buffer and consumes it to construct an IPFIX packet // containing records containing decoded fields. -func (d *Decoder) Decode(ctx context.Context, payload *bytes.Buffer) (msg *Message, err error) { +func (d *Decoder) Decode(ctx context.Context, payload *bytes.Buffer) (*Message, error) { decoderStart := time.Now() + var err error + // update metrics at the end of decoding depending on the outcome defer func() { DurationMicroseconds.Observe(float64(time.Since(decoderStart).Nanoseconds()) / 1000) // use nanoseconds for higher precision and then convert it back to microseconds @@ -115,6 +117,8 @@ func (d *Decoder) Decode(ctx context.Context, payload *bytes.Buffer) (msg *Messa return nil, errors.New("used decoder before template cache was initialized") } + var msg Message + n, err := msg.Decode(payload) if err != nil { return nil, fmt.Errorf("failed to read IPFIX packet header, %w", err) @@ -154,7 +158,7 @@ func (d *Decoder) Decode(ctx context.Context, payload *bytes.Buffer) (msg *Messa } _, err = ts.Decode(tr) if err != nil { - return msg, fmt.Errorf("failed to decode template set at index %d, %w", i, err) + return nil, fmt.Errorf("failed to decode template set at index %d, %w", i, err) } d.metrics.DecodedRecords += int64(len(ts.Records)) @@ -184,10 +188,10 @@ func (d *Decoder) Decode(ctx context.Context, payload *bytes.Buffer) (msg *Messa fieldCache: d.fieldCache, } - // ipfix options template set + // IPFIX options template set _, err := ots.Decode(tr) if err != nil { - return msg, fmt.Errorf("failed to decode options template set %d, %w", i, err) + return nil, fmt.Errorf("failed to decode options template set %d, %w", i, err) } d.metrics.DecodedRecords += int64(len(ots.Records)) @@ -223,12 +227,12 @@ func (d *Decoder) Decode(ctx context.Context, payload *bytes.Buffer) (msg *Messa TemplateId: h.Id, }) if err != nil { - return msg, err + return nil, err } _, err = ds.With(template).Decode(tr) if err != nil { - return msg, err + return nil, err } set = Set{ @@ -237,7 +241,7 @@ func (d *Decoder) Decode(ctx context.Context, payload *bytes.Buffer) (msg *Messa Set: ds, } } else { - return msg, ErrUnknownFlowId + return nil, ErrUnknownFlowId } d.metrics.DecodedSets++ @@ -249,7 +253,7 @@ func (d *Decoder) Decode(ctx context.Context, payload *bytes.Buffer) (msg *Messa msg.Sets = append(msg.Sets, set) } - return + return &msg, nil } func (d *Decoder) initMetrics() { diff --git a/go.mod b/go.mod index d624a3f..45e6585 100644 --- a/go.mod +++ b/go.mod @@ -4,20 +4,18 @@ go 1.21.3 require gopkg.in/yaml.v3 v3.0.1 -require github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect - require ( - github.com/go-logr/logr v1.3.0 + github.com/go-logr/logr v1.4.1 github.com/kr/text v0.2.0 // indirect ) require ( github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect - github.com/prometheus/client_golang v1.17.0 - github.com/prometheus/client_model v0.5.0 // indirect - github.com/prometheus/common v0.45.0 // indirect + github.com/prometheus/client_golang v1.19.0 + github.com/prometheus/client_model v0.6.0 // indirect + github.com/prometheus/common v0.49.0 // indirect github.com/prometheus/procfs v0.12.0 // indirect - golang.org/x/sys v0.13.0 - google.golang.org/protobuf v1.31.0 // indirect + golang.org/x/sys v0.18.0 + google.golang.org/protobuf v1.33.0 // indirect ) diff --git a/go.sum b/go.sum index 9f851ba..068ded1 100644 --- a/go.sum +++ b/go.sum @@ -3,34 +3,38 @@ github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6r github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= -github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY= -github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= -github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= +github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg= -github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k= -github.com/prometheus/client_golang v1.17.0 h1:rl2sfwZMtSthVU752MqfjQozy7blglC+1SOtjMAMh+Q= -github.com/prometheus/client_golang v1.17.0/go.mod h1:VeL+gMmOAxkS2IqfCq0ZmHSL+LjWfWDUmp1mBz9JgUY= +github.com/prometheus/client_golang v1.18.0 h1:HzFfmkOzH5Q8L8G+kSJKUx5dtG87sewO+FoDDqP5Tbk= +github.com/prometheus/client_golang v1.18.0/go.mod h1:T+GXkCk5wSJyOqMIzVgvvjFDlkOQntgjkJWKrN5txjA= +github.com/prometheus/client_golang v1.19.0 h1:ygXvpU1AoN1MhdzckN+PyD9QJOSD4x7kmXYlnfbA6JU= +github.com/prometheus/client_golang v1.19.0/go.mod h1:ZRM9uEAypZakd+q/x7+gmsvXdURP+DABIEIjnmDdp+k= github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw= github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI= -github.com/prometheus/common v0.45.0 h1:2BGz0eBc2hdMDLnO/8n0jeB3oPrt2D08CekT0lneoxM= -github.com/prometheus/common v0.45.0/go.mod h1:YJmSTw9BoKxJplESWWxlbyttQR4uaEcGyv9MZjVOJsY= +github.com/prometheus/client_model v0.6.0 h1:k1v3CzpSRUTrKMppY35TLwPvxHqBu0bYgxZzqGIgaos= +github.com/prometheus/client_model v0.6.0/go.mod h1:NTQHnmxFpouOD0DpvP4XujX3CdOAGQPoaGhyTchlyt8= +github.com/prometheus/common v0.46.0 h1:doXzt5ybi1HBKpsZOL0sSkaNHJJqkyfEWZGGqqScV0Y= +github.com/prometheus/common v0.46.0/go.mod h1:Tp0qkxpb9Jsg54QMe+EAmqXkSV7Evdy1BTn+g2pa/hQ= +github.com/prometheus/common v0.49.0 h1:ToNTdK4zSnPVJmh698mGFkDor9wBI/iGaJy5dbH1EgI= +github.com/prometheus/common v0.49.0/go.mod h1:Kxm+EULxRbUkjGU6WFsQqo3ORzB4tyKvlWFOE9mB2sE= github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo= github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= -golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= -golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= -google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= +golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= +golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= +google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= +google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= diff --git a/sets.go b/sets.go index 6bc4648..f622dc8 100644 --- a/sets.go +++ b/sets.go @@ -256,7 +256,10 @@ func (d *TemplateSet) Decode(r io.Reader) (n int, err error) { d.Records = make([]TemplateRecord, 0) // "as long as there's set header data (Set ID, Length)" for { - templateRecord := TemplateRecord{} + templateRecord := TemplateRecord{ + fieldCache: d.fieldCache, + templateCache: d.templateCache, + } m, err := templateRecord.Decode(r) n += m @@ -306,7 +309,10 @@ func (d *OptionsTemplateSet) Decode(r io.Reader) (n int, err error) { // TODO(zoomoid): maybe we need this for bound checks... // for r.Len() >= 4 { for { - record := OptionsTemplateRecord{} + record := OptionsTemplateRecord{ + fieldCache: d.fieldCache, + templateCache: d.templateCache, + } m, err := record.Decode(r) n += m