From 8ae82b5de5d4ec4b5b8c7991262e529f2c67b250 Mon Sep 17 00:00:00 2001 From: Pawel Zak Date: Tue, 8 Oct 2024 19:58:05 +0200 Subject: [PATCH] addressing comments --- internal/snmp/table.go | 2 +- plugins/common/shim/goshim.go | 6 +++--- plugins/inputs/influxdb_listener/influxdb_listener.go | 3 ++- .../inputs/influxdb_v2_listener/influxdb_v2_listener.go | 8 +++----- plugins/inputs/kafka_consumer/kafka_consumer.go | 2 +- plugins/inputs/opcua/read_client.go | 2 +- plugins/inputs/zipkin/codec/jsonV1/jsonV1.go | 2 +- plugins/inputs/zipkin/zipkin_test.go | 2 +- plugins/outputs/timestream/timestream.go | 4 ++-- plugins/outputs/timestream/timestream_test.go | 4 ++-- plugins/parsers/influx/influx_upstream/parser.go | 3 +-- plugins/parsers/influx/influx_upstream/parser_test.go | 6 +++--- plugins/parsers/wavefront/element.go | 5 ++--- 13 files changed, 23 insertions(+), 26 deletions(-) diff --git a/internal/snmp/table.go b/internal/snmp/table.go index feb730ff51930..495ef16ad5a05 100644 --- a/internal/snmp/table.go +++ b/internal/snmp/table.go @@ -56,7 +56,7 @@ func (t *Table) Init(tr Translator) error { // makes sure oid or name is set in config file // otherwise snmp will produce metrics with an empty name if t.Oid == "" && t.Name == "" { - return errors.New("snmp table in config file is not named: one or both of the oid and name settings must be set") + return errors.New("unnamed SNMP table in config file: one or both of the oid and name settings must be set") } if t.initialized { diff --git a/plugins/common/shim/goshim.go b/plugins/common/shim/goshim.go index 89518ac37650f..3d329cf04babb 100644 --- a/plugins/common/shim/goshim.go +++ b/plugins/common/shim/goshim.go @@ -79,17 +79,17 @@ func (s *Shim) Run(pollInterval time.Duration) error { if s.Input != nil { err := s.RunInput(pollInterval) if err != nil { - return fmt.Errorf("error during RunInput: %w", err) + return fmt.Errorf("running input failed: %w", err) } } else if s.Processor != nil { err := s.RunProcessor() if err != nil { - return fmt.Errorf("error during RunProcessor: %w", err) + return fmt.Errorf("running processor failed: %w", err) } } else if s.Output != nil { err := s.RunOutput() if err != nil { - return fmt.Errorf("error during RunOutput: %w", err) + return fmt.Errorf("running output failed: %w", err) } } else { return errors.New("nothing to run") diff --git a/plugins/inputs/influxdb_listener/influxdb_listener.go b/plugins/inputs/influxdb_listener/influxdb_listener.go index 2c54088b34fce..fd4b1d1cae882 100644 --- a/plugins/inputs/influxdb_listener/influxdb_listener.go +++ b/plugins/inputs/influxdb_listener/influxdb_listener.go @@ -9,6 +9,7 @@ import ( "encoding/json" "errors" "fmt" + "io" "net" "net/http" "time" @@ -449,7 +450,7 @@ func (h *InfluxDBListener) handleWriteUpstreamParser(res http.ResponseWriter, re h.acc.AddMetric(m) } - if !errors.Is(err, influx_upstream.ErrEOF) { + if !errors.Is(err, io.EOF) { h.Log.Debugf("Error parsing the request body: %v", err.Error()) if err := badRequest(res, err.Error()); err != nil { h.Log.Debugf("error in bad-request: %v", err) diff --git a/plugins/inputs/influxdb_v2_listener/influxdb_v2_listener.go b/plugins/inputs/influxdb_v2_listener/influxdb_v2_listener.go index 072492c1dae28..44263966918b0 100644 --- a/plugins/inputs/influxdb_v2_listener/influxdb_v2_listener.go +++ b/plugins/inputs/influxdb_v2_listener/influxdb_v2_listener.go @@ -38,8 +38,6 @@ const ( defaultWriteTimeout = 10 * time.Second ) -var ErrEOF = io.EOF - // The BadRequestCode constants keep standard error messages // see: https://v2.docs.influxdata.com/v2.0/api/#operation/PostWrite type BadRequestCode string @@ -309,7 +307,7 @@ func (h *InfluxDBV2Listener) handleWrite() http.HandlerFunc { if h.ParserType == "upstream" { parser := influx_upstream.Parser{} err = parser.Init() - if !errors.Is(err, ErrEOF) && err != nil { + if !errors.Is(err, io.EOF) && err != nil { h.Log.Debugf("Error initializing parser: %v", err.Error()) return } @@ -327,7 +325,7 @@ func (h *InfluxDBV2Listener) handleWrite() http.HandlerFunc { } else { parser := influx.Parser{} err = parser.Init() - if !errors.Is(err, ErrEOF) && err != nil { + if !errors.Is(err, io.EOF) && err != nil { h.Log.Debugf("Error initializing parser: %v", err.Error()) return } @@ -341,7 +339,7 @@ func (h *InfluxDBV2Listener) handleWrite() http.HandlerFunc { metrics, err = parser.Parse(bytes) } - if !errors.Is(err, ErrEOF) && err != nil { + if !errors.Is(err, io.EOF) && err != nil { h.Log.Debugf("Error parsing the request body: %v", err.Error()) if err := badRequest(res, Invalid, err.Error()); err != nil { h.Log.Debugf("error in bad-request: %v", err) diff --git a/plugins/inputs/kafka_consumer/kafka_consumer.go b/plugins/inputs/kafka_consumer/kafka_consumer.go index e61c25a431c55..9d262b7346931 100644 --- a/plugins/inputs/kafka_consumer/kafka_consumer.go +++ b/plugins/inputs/kafka_consumer/kafka_consumer.go @@ -132,7 +132,7 @@ func (k *KafkaConsumer) Init() error { } if err := k.SetConfig(cfg, k.Log); err != nil { - return fmt.Errorf("failed to SetConfig: %w", err) + return fmt.Errorf("setting config failed: %w", err) } switch strings.ToLower(k.Offset) { diff --git a/plugins/inputs/opcua/read_client.go b/plugins/inputs/opcua/read_client.go index a5ce01bbb7366..9aaf0570c1420 100644 --- a/plugins/inputs/opcua/read_client.go +++ b/plugins/inputs/opcua/read_client.go @@ -139,7 +139,7 @@ func (o *ReadClient) read() error { resp, err := o.Client.Read(o.ctx, req) if err != nil { o.ReadError.Incr(1) - return fmt.Errorf("failed to read RegisterNodes: %w", err) + return fmt.Errorf("reading registered nodes failed: %w", err) } o.ReadSuccess.Incr(1) for i, d := range resp.Results { diff --git a/plugins/inputs/zipkin/codec/jsonV1/jsonV1.go b/plugins/inputs/zipkin/codec/jsonV1/jsonV1.go index ae27d1cb4e825..e265540ff5cc7 100644 --- a/plugins/inputs/zipkin/codec/jsonV1/jsonV1.go +++ b/plugins/inputs/zipkin/codec/jsonV1/jsonV1.go @@ -220,7 +220,7 @@ func TraceIDFromString(s string) (string, error) { var hi, lo uint64 var err error if len(s) > 32 { - return "", fmt.Errorf("length of TraceID cannot be greater than 16 hex characters: %s", s) + return "", fmt.Errorf("length of TraceID cannot be greater than 32 hex characters: %s", s) } else if len(s) > 16 { hiLen := len(s) - 16 if hi, err = strconv.ParseUint(s[0:hiLen], 16, 64); err != nil { diff --git a/plugins/inputs/zipkin/zipkin_test.go b/plugins/inputs/zipkin/zipkin_test.go index 36b2e6288533f..9eb5f34ccf59e 100644 --- a/plugins/inputs/zipkin/zipkin_test.go +++ b/plugins/inputs/zipkin/zipkin_test.go @@ -648,7 +648,7 @@ func postThriftData(datafile, address, contentType string) error { endpoint := fmt.Sprintf("http://%s/api/v1/spans", address) req, err := http.NewRequest("POST", endpoint, bytes.NewReader(dat)) if err != nil { - return fmt.Errorf("unable to create new POST request for: %q", endpoint) + return fmt.Errorf("unable to create new POST request for %q: %w", endpoint, err) } req.Header.Set("Content-Type", contentType) diff --git a/plugins/outputs/timestream/timestream.go b/plugins/outputs/timestream/timestream.go index bd442a0cd9b2c..2daa05cb2e51d 100644 --- a/plugins/outputs/timestream/timestream.go +++ b/plugins/outputs/timestream/timestream.go @@ -114,11 +114,11 @@ func (*Timestream) SampleConfig() string { func (t *Timestream) Connect() error { if t.DatabaseName == "" { - return errors.New("`database_name` key is required") + return errors.New("'database_name' key is required") } if t.MappingMode == "" { - return errors.New("`mapping_mode` key is required") + return errors.New("'mapping_mode' key is required") } if t.MappingMode != MappingModeSingleTable && t.MappingMode != MappingModeMultiTable { diff --git a/plugins/outputs/timestream/timestream_test.go b/plugins/outputs/timestream/timestream_test.go index 8d14b497d52a4..4d0f11d942fb8 100644 --- a/plugins/outputs/timestream/timestream_test.go +++ b/plugins/outputs/timestream/timestream_test.go @@ -74,13 +74,13 @@ func TestConnectValidatesConfigParameters(t *testing.T) { } // checking base arguments noDatabaseName := Timestream{Log: testutil.Logger{}} - require.ErrorContains(t, noDatabaseName.Connect(), "`database_name` key is required") + require.ErrorContains(t, noDatabaseName.Connect(), "'database_name' key is required") noMappingMode := Timestream{ DatabaseName: tsDbName, Log: testutil.Logger{}, } - require.ErrorContains(t, noMappingMode.Connect(), "`mapping_mode` key is required") + require.ErrorContains(t, noMappingMode.Connect(), "'mapping_mode' key is required") incorrectMappingMode := Timestream{ DatabaseName: tsDbName, diff --git a/plugins/parsers/influx/influx_upstream/parser.go b/plugins/parsers/influx/influx_upstream/parser.go index f83ebd4b71679..9485e00db9988 100644 --- a/plugins/parsers/influx/influx_upstream/parser.go +++ b/plugins/parsers/influx/influx_upstream/parser.go @@ -21,7 +21,6 @@ const ( var ( ErrNoMetric = errors.New("no metric in line") - ErrEOF = io.EOF ) type TimeFunc func() time.Time @@ -259,7 +258,7 @@ func (sp *StreamParser) Next() (telegraf.Metric, error) { return nil, err } - return nil, ErrEOF + return nil, io.EOF } m, err := nextMetric(sp.decoder, sp.precision, sp.defaultTime, false) diff --git a/plugins/parsers/influx/influx_upstream/parser_test.go b/plugins/parsers/influx/influx_upstream/parser_test.go index d9ad173bd415b..b96bf0b47eece 100644 --- a/plugins/parsers/influx/influx_upstream/parser_test.go +++ b/plugins/parsers/influx/influx_upstream/parser_test.go @@ -660,7 +660,7 @@ func TestStreamParser(t *testing.T) { for { m, err := parser.Next() if err != nil { - if errors.Is(err, ErrEOF) { + if errors.Is(err, io.EOF) { break } require.Equal(t, tt.err.Error(), err.Error()) @@ -958,7 +958,7 @@ func TestStreamParserErrorString(t *testing.T) { var errs []error for i := 0; i < 20; i++ { _, err := parser.Next() - if errors.Is(err, ErrEOF) { + if errors.Is(err, io.EOF) { break } @@ -997,7 +997,7 @@ func TestStreamParserReaderError(t *testing.T) { require.Equal(t, err, readerErr) _, err = parser.Next() - require.Equal(t, err, ErrEOF) + require.Equal(t, err, io.EOF) } func TestStreamParserProducesAllAvailableMetrics(t *testing.T) { diff --git a/plugins/parsers/wavefront/element.go b/plugins/parsers/wavefront/element.go index e6080cd448d6a..1b78b44495225 100644 --- a/plugins/parsers/wavefront/element.go +++ b/plugins/parsers/wavefront/element.go @@ -9,7 +9,6 @@ import ( ) var ( - errEOF = io.EOF errInvalidTimestamp = errors.New("invalid timestamp") ) @@ -131,7 +130,7 @@ func (ep *loopedParser) parse(p *PointParser, pt *Point) error { return err } err = ep.wsParser.parse(p, pt) - if errors.Is(err, errEOF) { + if errors.Is(err, io.EOF) { break } } @@ -171,7 +170,7 @@ func (ep *whiteSpaceParser) parse(p *PointParser, _ *Point) error { if tok == EOF { if !ep.nextOptional { - return errEOF + return io.EOF } return nil }