From 05435e47d325a8fd4bd2d49655650323fb060973 Mon Sep 17 00:00:00 2001 From: Sven Rebhan <36194019+srebhan@users.noreply.github.com> Date: Wed, 11 Dec 2024 21:25:03 +0100 Subject: [PATCH 01/21] chore(inputs.kinesis_consumer): Cleanup code (#16267) Co-authored-by: Dane Strandboge <136023093+DStrand1@users.noreply.github.com> --- plugins/inputs/kinesis_consumer/encoding.go | 45 +++ .../kinesis_consumer/kinesis_consumer.go | 250 ++++++---------- .../kinesis_consumer/kinesis_consumer_test.go | 277 +++++++----------- plugins/inputs/kinesis_consumer/logging.go | 27 ++ plugins/inputs/kinesis_consumer/noop_store.go | 7 + 5 files changed, 274 insertions(+), 332 deletions(-) create mode 100644 plugins/inputs/kinesis_consumer/encoding.go create mode 100644 plugins/inputs/kinesis_consumer/logging.go create mode 100644 plugins/inputs/kinesis_consumer/noop_store.go diff --git a/plugins/inputs/kinesis_consumer/encoding.go b/plugins/inputs/kinesis_consumer/encoding.go new file mode 100644 index 0000000000000..d2bad6fd8301d --- /dev/null +++ b/plugins/inputs/kinesis_consumer/encoding.go @@ -0,0 +1,45 @@ +package kinesis_consumer + +import ( + "bytes" + "compress/gzip" + "compress/zlib" + "fmt" + "io" +) + +type decodingFunc func([]byte) ([]byte, error) + +func processGzip(data []byte) ([]byte, error) { + zipData, err := gzip.NewReader(bytes.NewReader(data)) + if err != nil { + return nil, err + } + defer zipData.Close() + return io.ReadAll(zipData) +} + +func processZlib(data []byte) ([]byte, error) { + zlibData, err := zlib.NewReader(bytes.NewReader(data)) + if err != nil { + return nil, err + } + defer zlibData.Close() + return io.ReadAll(zlibData) +} + +func processNoOp(data []byte) ([]byte, error) { + return data, nil +} + +func getDecodingFunc(encoding string) (decodingFunc, error) { + switch encoding { + case "gzip": + return processGzip, nil + case "zlib": + return processZlib, nil + case "none", "identity", "": + return processNoOp, nil + } + return nil, fmt.Errorf("unknown content encoding %q", encoding) +} diff --git a/plugins/inputs/kinesis_consumer/kinesis_consumer.go b/plugins/inputs/kinesis_consumer/kinesis_consumer.go index 819a36b0a33da..4c65aadef41fa 100644 --- a/plugins/inputs/kinesis_consumer/kinesis_consumer.go +++ b/plugins/inputs/kinesis_consumer/kinesis_consumer.go @@ -2,23 +2,15 @@ package kinesis_consumer import ( - "bytes" - "compress/gzip" - "compress/zlib" "context" _ "embed" "errors" - "fmt" - "io" - "math/big" - "strings" "sync" "time" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/dynamodb" "github.com/aws/aws-sdk-go-v2/service/kinesis" - "github.com/aws/smithy-go/logging" consumer "github.com/harlow/kinesis-consumer" "github.com/harlow/kinesis-consumer/store/ddb" @@ -31,86 +23,85 @@ import ( //go:embed sample.conf var sampleConfig string -var ( - once sync.Once - // this is the largest sequence number allowed - https://docs.aws.amazon.com/kinesis/latest/APIReference/API_SequenceNumberRange.html - maxSeq = strToBint(strings.Repeat("9", 129)) - negOne *big.Int -) - -const ( - defaultMaxUndeliveredMessages = 1000 -) - -type ( - KinesisConsumer struct { - StreamName string `toml:"streamname"` - ShardIteratorType string `toml:"shard_iterator_type"` - DynamoDB *dynamoDB `toml:"checkpoint_dynamodb"` - MaxUndeliveredMessages int `toml:"max_undelivered_messages"` - ContentEncoding string `toml:"content_encoding"` - - Log telegraf.Logger `toml:"-"` - - cons *consumer.Consumer - parser telegraf.Parser - cancel context.CancelFunc - acc telegraf.TrackingAccumulator - sem chan struct{} +var once sync.Once + +type KinesisConsumer struct { + StreamName string `toml:"streamname"` + ShardIteratorType string `toml:"shard_iterator_type"` + DynamoDB *dynamoDB `toml:"checkpoint_dynamodb"` + MaxUndeliveredMessages int `toml:"max_undelivered_messages"` + ContentEncoding string `toml:"content_encoding"` + Log telegraf.Logger `toml:"-"` + common_aws.CredentialConfig + + cons *consumer.Consumer + parser telegraf.Parser + cancel context.CancelFunc + acc telegraf.TrackingAccumulator + sem chan struct{} + + checkpoint consumer.Store + checkpoints map[string]checkpoint + records map[telegraf.TrackingID]string + checkpointTex sync.Mutex + recordsTex sync.Mutex + wg sync.WaitGroup + + contentDecodingFunc decodingFunc + + lastSeqNum string +} - checkpoint consumer.Store - checkpoints map[string]checkpoint - records map[telegraf.TrackingID]string - checkpointTex sync.Mutex - recordsTex sync.Mutex - wg sync.WaitGroup +type dynamoDB struct { + AppName string `toml:"app_name"` + TableName string `toml:"table_name"` +} - processContentEncodingFunc processContent +type checkpoint struct { + streamName string + shardID string +} - lastSeqNum *big.Int +func (*KinesisConsumer) SampleConfig() string { + return sampleConfig +} - common_aws.CredentialConfig +func (k *KinesisConsumer) Init() error { + // Set defaults + if k.MaxUndeliveredMessages < 1 { + k.MaxUndeliveredMessages = 1000 } - dynamoDB struct { - AppName string `toml:"app_name"` - TableName string `toml:"table_name"` + if k.ShardIteratorType == "" { + k.ShardIteratorType = "TRIM_HORIZON" } - - checkpoint struct { - streamName string - shardID string + if k.ContentEncoding == "" { + k.ContentEncoding = "identity" } -) -type processContent func([]byte) ([]byte, error) - -func (*KinesisConsumer) SampleConfig() string { - return sampleConfig -} + f, err := getDecodingFunc(k.ContentEncoding) + if err != nil { + return err + } + k.contentDecodingFunc = f -func (k *KinesisConsumer) Init() error { - return k.configureProcessContentEncodingFunc() + return nil } func (k *KinesisConsumer) SetParser(parser telegraf.Parser) { k.parser = parser } -func (k *KinesisConsumer) Start(ac telegraf.Accumulator) error { - err := k.connect(ac) - if err != nil { - return err - } - - return nil +func (k *KinesisConsumer) Start(acc telegraf.Accumulator) error { + return k.connect(acc) } func (k *KinesisConsumer) Gather(acc telegraf.Accumulator) error { if k.cons == nil { return k.connect(acc) } - k.lastSeqNum = maxSeq + // Enforce writing of last received sequence number + k.lastSeqNum = "" return nil } @@ -138,7 +129,7 @@ func (k *KinesisConsumer) SetCheckpoint(streamName, shardID, sequenceNumber stri return nil } -func (k *KinesisConsumer) connect(ac telegraf.Accumulator) error { +func (k *KinesisConsumer) connect(acc telegraf.Accumulator) error { cfg, err := k.CredentialConfig.Credentials() if err != nil { return err @@ -180,7 +171,7 @@ func (k *KinesisConsumer) connect(ac telegraf.Accumulator) error { k.cons = cons - k.acc = ac.WithTracking(k.MaxUndeliveredMessages) + k.acc = acc.WithTracking(k.MaxUndeliveredMessages) k.records = make(map[telegraf.TrackingID]string, k.MaxUndeliveredMessages) k.checkpoints = make(map[string]checkpoint, k.MaxUndeliveredMessages) k.sem = make(chan struct{}, k.MaxUndeliveredMessages) @@ -204,8 +195,7 @@ func (k *KinesisConsumer) connect(ac telegraf.Accumulator) error { case k.sem <- struct{}{}: break } - err := k.onMessage(k.acc, r) - if err != nil { + if err := k.onMessage(k.acc, r); err != nil { <-k.sem k.Log.Errorf("Scan parser error: %v", err) } @@ -223,7 +213,7 @@ func (k *KinesisConsumer) connect(ac telegraf.Accumulator) error { } func (k *KinesisConsumer) onMessage(acc telegraf.TrackingAccumulator, r *consumer.Record) error { - data, err := k.processContentEncodingFunc(r.Data) + data, err := k.contentDecodingFunc(r.Data) if err != nil { return err } @@ -262,111 +252,37 @@ func (k *KinesisConsumer) onDelivery(ctx context.Context) { delete(k.records, info.ID()) k.recordsTex.Unlock() - if info.Delivered() { - k.checkpointTex.Lock() - chk, ok := k.checkpoints[sequenceNum] - if !ok { - k.checkpointTex.Unlock() - continue - } - delete(k.checkpoints, sequenceNum) - k.checkpointTex.Unlock() - - // at least once - if strToBint(sequenceNum).Cmp(k.lastSeqNum) > 0 { - continue - } - - k.lastSeqNum = strToBint(sequenceNum) - if err := k.checkpoint.SetCheckpoint(chk.streamName, chk.shardID, sequenceNum); err != nil { - k.Log.Debugf("Setting checkpoint failed: %v", err) - } - } else { + if !info.Delivered() { k.Log.Debug("Metric group failed to process") + continue } - } - } -} -func processGzip(data []byte) ([]byte, error) { - zipData, err := gzip.NewReader(bytes.NewReader(data)) - if err != nil { - return nil, err - } - defer zipData.Close() - return io.ReadAll(zipData) -} - -func processZlib(data []byte) ([]byte, error) { - zlibData, err := zlib.NewReader(bytes.NewReader(data)) - if err != nil { - return nil, err - } - defer zlibData.Close() - return io.ReadAll(zlibData) -} - -func processNoOp(data []byte) ([]byte, error) { - return data, nil -} - -func strToBint(s string) *big.Int { - n, ok := new(big.Int).SetString(s, 10) - if !ok { - return negOne - } - return n -} - -func (k *KinesisConsumer) configureProcessContentEncodingFunc() error { - switch k.ContentEncoding { - case "gzip": - k.processContentEncodingFunc = processGzip - case "zlib": - k.processContentEncodingFunc = processZlib - case "none", "identity", "": - k.processContentEncodingFunc = processNoOp - default: - return fmt.Errorf("unknown content encoding %q", k.ContentEncoding) - } - return nil -} - -type telegrafLoggerWrapper struct { - telegraf.Logger -} + if k.lastSeqNum != "" { + continue + } -func (t *telegrafLoggerWrapper) Log(args ...interface{}) { - t.Trace(args...) -} + // Store the sequence number at least once per gather cycle using the checkpoint + // storage (usually DynamoDB). + k.checkpointTex.Lock() + chk, ok := k.checkpoints[sequenceNum] + if !ok { + k.checkpointTex.Unlock() + continue + } + delete(k.checkpoints, sequenceNum) + k.checkpointTex.Unlock() -func (t *telegrafLoggerWrapper) Logf(classification logging.Classification, format string, v ...interface{}) { - switch classification { - case logging.Debug: - format = "DEBUG " + format - case logging.Warn: - format = "WARN" + format - default: - format = "INFO " + format + k.Log.Tracef("persisting sequence number %q for stream %q and shard %q", sequenceNum) + k.lastSeqNum = sequenceNum + if err := k.checkpoint.SetCheckpoint(chk.streamName, chk.shardID, sequenceNum); err != nil { + k.Log.Errorf("Setting checkpoint failed: %v", err) + } + } } - t.Logger.Tracef(format, v...) } -// noopStore implements the storage interface with discard -type noopStore struct{} - -func (n noopStore) SetCheckpoint(_, _, _ string) error { return nil } -func (n noopStore) GetCheckpoint(_, _ string) (string, error) { return "", nil } - func init() { - negOne, _ = new(big.Int).SetString("-1", 10) - inputs.Add("kinesis_consumer", func() telegraf.Input { - return &KinesisConsumer{ - ShardIteratorType: "TRIM_HORIZON", - MaxUndeliveredMessages: defaultMaxUndeliveredMessages, - lastSeqNum: maxSeq, - ContentEncoding: "identity", - } + return &KinesisConsumer{} }) } diff --git a/plugins/inputs/kinesis_consumer/kinesis_consumer_test.go b/plugins/inputs/kinesis_consumer/kinesis_consumer_test.go index e09e0df3717a6..b48372571b879 100644 --- a/plugins/inputs/kinesis_consumer/kinesis_consumer_test.go +++ b/plugins/inputs/kinesis_consumer/kinesis_consumer_test.go @@ -14,220 +14,167 @@ import ( "github.com/influxdata/telegraf/testutil" ) -func TestKinesisConsumer_onMessage(t *testing.T) { +func TestInvalidCoding(t *testing.T) { + plugin := &KinesisConsumer{ + ContentEncoding: "notsupported", + } + require.ErrorContains(t, plugin.Init(), "unknown content encoding") +} + +func TestOnMessage(t *testing.T) { + // Prepare messages zlibBytpes, err := base64.StdEncoding.DecodeString( "eF5FjlFrgzAUhf9KuM+2aNB2zdsQ2xe3whQGW8qIeqdhaiSJK0P874u1Y4+Hc/jON0GHxoga858BgUF8fs5fzunHU5Jlj6cEPFDXHvXStGqsrsKWTapq44pW1SetxsF1a8qsRtGt0Yy" + "FKbUcrFT9UbYWtQH2frntkm/s7RInkNU6t9JpWNE5WBAFPo3CcHeg+9D703OziUOhCg6MQ/yakrspuZsyEjdYfsm+Jg2K1jZEfZLKQWUvFglylBobZXDLwSP8//EGpD4NNj7dUJpT6" + "hQY3W33h/AhCt84zDBf5l/MDl08", ) require.NoError(t, err) + gzippedBytes, err := base64.StdEncoding.DecodeString( "H4sIAAFXNGAAA0WOUWuDMBSF/0q4z7Zo0HbN2xDbF7fCFAZbyoh6p2FqJIkrQ/zvi7Vjj4dz+M43QYfGiBrznwGBQXx+zl/O6cdTkmWPpwQ8UNce9dK0aqyuwpZNqmrjilbVJ63GwXVr" + "yqxG0a3RjIUptRysVP1Rtha1AfZ+ue2Sb+ztEieQ1Tq30mlY0TlYEAU+jcJwd6D70PvTc7OJQ6EKDoxD/JqSuym5mzISN1h+yb4mDYrWNkR9kspBZS8WCXKUGhtlcMvBI/z/8QakPg02" + "Pt1QmlPqFBjdbfeH8CEK3zjMMF/mX0TaxZUpAQAA", ) require.NoError(t, err) - notZippedBytes := []byte(`{ - "messageType": "CONTROL_MESSAGE", - "owner": "CloudwatchLogs", - "logGroup": "", - "logStream": "", - "subscriptionFilters": [], - "logEvents": [ - { - "id": "", - "timestamp": 1510254469274, - "message": "{\"bob\":\"CWL CONTROL MESSAGE: Checking health of destination Firehose.\", \"timestamp\":\"2021-02-22T22:15:26.794854Z\"}," - }, - { - "id": "", - "timestamp": 1510254469274, - "message": "{\"bob\":\"CWL CONTROL MESSAGE: Checking health of destination Firehose.\", \"timestamp\":\"2021-02-22T22:15:26.794854Z\"}" - } - ] -}`) - parser := &json.Parser{ - MetricName: "json_test", - Query: "logEvents", - StringFields: []string{"message"}, - } - require.NoError(t, parser.Init()) - type fields struct { - ContentEncoding string - parser telegraf.Parser - records map[telegraf.TrackingID]string - } - type args struct { - r *consumer.Record - } - type expected struct { - numberOfMetrics int - messageContains string + notZippedBytes := []byte(` + { + "messageType": "CONTROL_MESSAGE", + "owner": "CloudwatchLogs", + "logGroup": "", + "logStream": "", + "subscriptionFilters": [], + "logEvents": [ + { + "id": "", + "timestamp": 1510254469274, + "message": "{\"bob\":\"CWL CONTROL MESSAGE: Checking health of destination Firehose.\", \"timestamp\":\"2021-02-22T22:15:26.794854Z\"}," + }, + { + "id": "", + "timestamp": 1510254469274, + "message": "{\"bob\":\"CWL CONTROL MESSAGE: Checking health of destination Firehose.\", \"timestamp\":\"2021-02-22T22:15:26.794854Z\"}" + } + ] } + `) + tests := []struct { - name string - fields fields - args args - wantErr bool - expected expected + name string + encoding string + records map[telegraf.TrackingID]string + args *consumer.Record + expectedNumber int + expectedContent string }{ { - name: "test no compression", - fields: fields{ - ContentEncoding: "none", - parser: parser, - records: make(map[telegraf.TrackingID]string), - }, - args: args{ - r: &consumer.Record{ - Record: types.Record{ - Data: notZippedBytes, - SequenceNumber: aws.String("anything"), - }, + name: "test no compression", + encoding: "none", + records: make(map[telegraf.TrackingID]string), + args: &consumer.Record{ + Record: types.Record{ + Data: notZippedBytes, + SequenceNumber: aws.String("anything"), }, }, - wantErr: false, - expected: expected{ - messageContains: "bob", - numberOfMetrics: 2, - }, + expectedNumber: 2, + expectedContent: "bob", }, { - name: "test no compression via empty string for ContentEncoding", - fields: fields{ - ContentEncoding: "", - parser: parser, - records: make(map[telegraf.TrackingID]string), - }, - args: args{ - r: &consumer.Record{ - Record: types.Record{ - Data: notZippedBytes, - SequenceNumber: aws.String("anything"), - }, + name: "test no compression via empty string for ContentEncoding", + records: make(map[telegraf.TrackingID]string), + args: &consumer.Record{ + Record: types.Record{ + Data: notZippedBytes, + SequenceNumber: aws.String("anything"), }, }, - wantErr: false, - expected: expected{ - messageContains: "bob", - numberOfMetrics: 2, - }, + expectedNumber: 2, + expectedContent: "bob", }, { - name: "test no compression via identity ContentEncoding", - fields: fields{ - ContentEncoding: "identity", - parser: parser, - records: make(map[telegraf.TrackingID]string), - }, - args: args{ - r: &consumer.Record{ - Record: types.Record{ - Data: notZippedBytes, - SequenceNumber: aws.String("anything"), - }, + name: "test no compression via identity ContentEncoding", + encoding: "identity", + records: make(map[telegraf.TrackingID]string), + args: &consumer.Record{ + Record: types.Record{ + Data: notZippedBytes, + SequenceNumber: aws.String("anything"), }, }, - wantErr: false, - expected: expected{ - messageContains: "bob", - numberOfMetrics: 2, - }, + expectedNumber: 2, + expectedContent: "bob", }, { - name: "test no compression via no ContentEncoding", - fields: fields{ - parser: parser, - records: make(map[telegraf.TrackingID]string), - }, - args: args{ - r: &consumer.Record{ - Record: types.Record{ - Data: notZippedBytes, - SequenceNumber: aws.String("anything"), - }, + name: "test no compression via no ContentEncoding", + records: make(map[telegraf.TrackingID]string), + args: &consumer.Record{ + Record: types.Record{ + Data: notZippedBytes, + SequenceNumber: aws.String("anything"), }, }, - wantErr: false, - expected: expected{ - messageContains: "bob", - numberOfMetrics: 2, - }, + expectedNumber: 2, + expectedContent: "bob", }, { - name: "test gzip compression", - fields: fields{ - ContentEncoding: "gzip", - parser: parser, - records: make(map[telegraf.TrackingID]string), - }, - args: args{ - r: &consumer.Record{ - Record: types.Record{ - Data: gzippedBytes, - SequenceNumber: aws.String("anything"), - }, + name: "test gzip compression", + encoding: "gzip", + records: make(map[telegraf.TrackingID]string), + args: &consumer.Record{ + Record: types.Record{ + Data: gzippedBytes, + SequenceNumber: aws.String("anything"), }, }, - wantErr: false, - expected: expected{ - messageContains: "bob", - numberOfMetrics: 1, - }, + expectedNumber: 1, + expectedContent: "bob", }, { - name: "test zlib compression", - fields: fields{ - ContentEncoding: "zlib", - parser: parser, - records: make(map[telegraf.TrackingID]string), - }, - args: args{ - r: &consumer.Record{ - Record: types.Record{ - Data: zlibBytpes, - SequenceNumber: aws.String("anything"), - }, + name: "test zlib compression", + encoding: "zlib", + records: make(map[telegraf.TrackingID]string), + args: &consumer.Record{ + Record: types.Record{ + Data: zlibBytpes, + SequenceNumber: aws.String("anything"), }, }, - wantErr: false, - expected: expected{ - messageContains: "bob", - numberOfMetrics: 1, - }, + expectedNumber: 1, + expectedContent: "bob", }, } - k := &KinesisConsumer{ - ContentEncoding: "notsupported", - } - err = k.Init() - require.Error(t, err) - for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - k := &KinesisConsumer{ - ContentEncoding: tt.fields.ContentEncoding, - parser: tt.fields.parser, - records: tt.fields.records, + // Prepare JSON parser + parser := &json.Parser{ + MetricName: "json_test", + Query: "logEvents", + StringFields: []string{"message"}, } - err := k.Init() - require.NoError(t, err) + require.NoError(t, parser.Init()) - acc := testutil.Accumulator{} - if err := k.onMessage(acc.WithTracking(tt.expected.numberOfMetrics), tt.args.r); (err != nil) != tt.wantErr { - t.Errorf("onMessage() error = %v, wantErr %v", err, tt.wantErr) + // Setup plugin + plugin := &KinesisConsumer{ + ContentEncoding: tt.encoding, + parser: parser, + records: tt.records, } + require.NoError(t, plugin.Init()) + + var acc testutil.Accumulator + require.NoError(t, plugin.onMessage(acc.WithTracking(tt.expectedNumber), tt.args)) - require.Len(t, acc.Metrics, tt.expected.numberOfMetrics) + actual := acc.GetTelegrafMetrics() + require.Len(t, actual, tt.expectedNumber) - for _, metric := range acc.Metrics { - if logEventMessage, ok := metric.Fields["message"]; ok { - require.Contains(t, logEventMessage.(string), tt.expected.messageContains) - } else { - t.Errorf("Expect logEvents to be present") - } + for _, metric := range actual { + raw, found := metric.GetField("message") + require.True(t, found, "no message present") + message, ok := raw.(string) + require.Truef(t, ok, "message not a string but %T", raw) + require.Contains(t, message, tt.expectedContent) } }) } diff --git a/plugins/inputs/kinesis_consumer/logging.go b/plugins/inputs/kinesis_consumer/logging.go new file mode 100644 index 0000000000000..82e9458654ea4 --- /dev/null +++ b/plugins/inputs/kinesis_consumer/logging.go @@ -0,0 +1,27 @@ +package kinesis_consumer + +import ( + "github.com/aws/smithy-go/logging" + + "github.com/influxdata/telegraf" +) + +type telegrafLoggerWrapper struct { + telegraf.Logger +} + +func (t *telegrafLoggerWrapper) Log(args ...interface{}) { + t.Trace(args...) +} + +func (t *telegrafLoggerWrapper) Logf(classification logging.Classification, format string, v ...interface{}) { + switch classification { + case logging.Debug: + format = "DEBUG " + format + case logging.Warn: + format = "WARN" + format + default: + format = "INFO " + format + } + t.Logger.Tracef(format, v...) +} diff --git a/plugins/inputs/kinesis_consumer/noop_store.go b/plugins/inputs/kinesis_consumer/noop_store.go new file mode 100644 index 0000000000000..f400fdc718b9f --- /dev/null +++ b/plugins/inputs/kinesis_consumer/noop_store.go @@ -0,0 +1,7 @@ +package kinesis_consumer + +// noopStore implements the storage interface with discard +type noopStore struct{} + +func (noopStore) SetCheckpoint(_, _, _ string) error { return nil } +func (noopStore) GetCheckpoint(_, _ string) (string, error) { return "", nil } From 795b8a9f963c6cb40f48cb5b66c99ea9c81a9759 Mon Sep 17 00:00:00 2001 From: Landon Clipp <11232769+LandonTClipp@users.noreply.github.com> Date: Wed, 11 Dec 2024 14:31:28 -0600 Subject: [PATCH 02/21] docs(specs): Add `probe` as value to `startup_error_behavior` (#16052) --- docs/specs/tsd-006-startup-error-behavior.md | 13 ++++ docs/specs/tsd-009-probe-on-startup.md | 68 ++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 docs/specs/tsd-009-probe-on-startup.md diff --git a/docs/specs/tsd-006-startup-error-behavior.md b/docs/specs/tsd-006-startup-error-behavior.md index 33fd39d8b16c1..4ae8549546828 100644 --- a/docs/specs/tsd-006-startup-error-behavior.md +++ b/docs/specs/tsd-006-startup-error-behavior.md @@ -75,6 +75,19 @@ must *not* fail on startup errors and should continue running. On startup error, Telegraf must ignore the plugin as-if it was not configured at all, i.e. the plugin must be completely removed from processing. +### `probe` behavior + +When using the `probe` setting for the `startup_error_behavior` option Telegraf +must *not* fail on startup errors and should continue running. On startup error, +Telegraf must ignore the plugin as-if it was not configured at all, i.e. the +plugin must be completely removed from processing, similar to the `ignore` +behavior. Additionally, Telegraf must probe the plugin (as defined in +[TSD-009][tsd_009]) after startup, if it implements the `ProbePlugin` interface. +If probing is available *and* returns an error Telegraf must *ignore* the +plugin as-if it was not configured at all. + +[tsd_009]: /docs/specs/tsd-009-probe-on-startup.md + ## Plugin Requirements Plugins participating in handling startup errors must implement the `Start()` diff --git a/docs/specs/tsd-009-probe-on-startup.md b/docs/specs/tsd-009-probe-on-startup.md new file mode 100644 index 0000000000000..99eec04178b43 --- /dev/null +++ b/docs/specs/tsd-009-probe-on-startup.md @@ -0,0 +1,68 @@ +# Probing plugins after startup + +## Objective + +Allow Telegraf to probe plugins during startup to enable enhanced plugin error +detection like availability of hardware or services + +## Keywords + +inputs, outputs, startup, probe, error, ignore, behavior + +## Overview + +When plugins are first instantiated, Telegraf will call the plugin's `Start()` +method (for inputs) or `Connect()` (for outputs) which will initialize its +configuration based off of config options and the running environment. It is +sometimes the case that while the initialization step succeeds, the upstream +service in which the plugin relies on is not actually running, or is not capable +of being communicated with due to incorrect configuration or environmental +problems. In situations like this, Telegraf does not detect that the plugin's +upstream service is not functioning properly, and thus it will continually call +the plugin during each `Gather()` iteration. This often has the effect of +polluting journald and system logs with voluminous error messages, which creates +issues for system administrators who rely on such logs to identify other +unrelated system problems. + +More background discussion on this option, including other possible avenues, can +be viewed [here](https://github.com/influxdata/telegraf/issues/16028). + +## Probing + +Probing is an action whereby the plugin should ensure that the plugin will be +fully functional on a best effort basis. This may comprise communicating with +its external service, trying to access required devices, entities or executables +etc to ensure that the plugin will not produce errors during e.g. data collection +or data output. Probing must *not* produce, process or output any metrics. + +Plugins that support probing must implement the `ProbePlugin` interface. Such +plugins must behave in the following manner: + +1. Return an error if the external dependencies (hardware, services, +executables, etc.) of the plugin are not available. +2. Return an error if information cannot be gathered (in the case of inputs) or +sent (in the case of outputs) due to unrecoverable issues. For example, invalid +authentication, missing permissions, or non-existent endpoints. +3. Otherwise, return `nil` indicating the plugin will be fully functional. + +## Plugin Requirements + +Plugins that allow probing must implement the `ProbePlugin` interface. The +exact implementation depends on the plugin's functionality and requirements, +but generally it should take the same actions as it would during normal operation +e.g. calling `Gather()` or `Write()` and check if errors occur. If probing fails, +it must be safe to call the plugin's `Close()` method. + +Input plugins must *not* produce metrics, output plugins must *not* send any +metrics to the service. Plugins must *not* influence the later data processing or +collection by modifying the internal state of the plugin or the external state of the +service or hardware. For example, file-offsets or other service states must be +reset to not lose data during the first gather or write cycle. + +Plugins must return `nil` upon successful probing or an error otherwise. + +## Related Issues + +- [#16028](https://github.com/influxdata/telegraf/issues/16028) +- [#15916](https://github.com/influxdata/telegraf/pull/15916) +- [#16001](https://github.com/influxdata/telegraf/pull/16001) From 2ccc79ce2781e22efed29ce33a2ebe3e82c23c8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20=C5=BBak?= Date: Wed, 11 Dec 2024 21:32:16 +0100 Subject: [PATCH 03/21] test(linters): Enable `testifylint`: `contains`, `encoded-compare` and `regexp` (#16262) --- .golangci.yml | 3 ++ plugins/serializers/json/json_test.go | 10 +++---- .../serializers/nowmetric/nowmetric_test.go | 30 ++++++++----------- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index a7eab4390f758..6c70d0cbc176a 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -335,7 +335,9 @@ linters-settings: - blank-import - bool-compare - compares + - contains - empty + - encoded-compare - error-is-as - error-nil - expected-actual @@ -345,6 +347,7 @@ linters-settings: - len - negative-positive - nil-compare + - regexp - require-error - suite-broken-parallel - suite-dont-use-pkg diff --git a/plugins/serializers/json/json_test.go b/plugins/serializers/json/json_test.go index d562ca79f59d4..8c1b94aa655d7 100644 --- a/plugins/serializers/json/json_test.go +++ b/plugins/serializers/json/json_test.go @@ -196,10 +196,10 @@ func TestSerializeBatch(t *testing.T) { require.NoError(t, s.Init()) buf, err := s.SerializeBatch(metrics) require.NoError(t, err) - require.Equal( + require.JSONEq( t, - []byte(`{"metrics":[{"fields":{"value":42},"name":"cpu","tags":{},"timestamp":0},{"fields":{"value":42},"name":"cpu","tags":{},"timestamp":0}]}`+"\n"), - buf, + `{"metrics":[{"fields":{"value":42},"name":"cpu","tags":{},"timestamp":0},{"fields":{"value":42},"name":"cpu","tags":{},"timestamp":0}]}`, + string(buf), ) } @@ -220,7 +220,7 @@ func TestSerializeBatchSkipInf(t *testing.T) { require.NoError(t, s.Init()) buf, err := s.SerializeBatch(metrics) require.NoError(t, err) - require.Equal(t, []byte(`{"metrics":[{"fields":{"time_idle":42},"name":"cpu","tags":{},"timestamp":0}]}`+"\n"), buf) + require.JSONEq(t, `{"metrics":[{"fields":{"time_idle":42},"name":"cpu","tags":{},"timestamp":0}]}`, string(buf)) } func TestSerializeBatchSkipInfAllFields(t *testing.T) { @@ -239,7 +239,7 @@ func TestSerializeBatchSkipInfAllFields(t *testing.T) { require.NoError(t, s.Init()) buf, err := s.SerializeBatch(metrics) require.NoError(t, err) - require.Equal(t, []byte(`{"metrics":[{"fields":{},"name":"cpu","tags":{},"timestamp":0}]}`+"\n"), buf) + require.JSONEq(t, `{"metrics":[{"fields":{},"name":"cpu","tags":{},"timestamp":0}]}`, string(buf)) } func TestSerializeTransformationNonBatch(t *testing.T) { diff --git a/plugins/serializers/nowmetric/nowmetric_test.go b/plugins/serializers/nowmetric/nowmetric_test.go index 167963d461466..e0b2de3cce3a1 100644 --- a/plugins/serializers/nowmetric/nowmetric_test.go +++ b/plugins/serializers/nowmetric/nowmetric_test.go @@ -191,13 +191,11 @@ func TestSerializeBatch(t *testing.T) { s := &Serializer{} buf, err := s.SerializeBatch(metrics) require.NoError(t, err) - require.Equal( + require.JSONEq( t, - []byte( - `[{"metric_type":"value","resource":"","node":"","value":42,"timestamp":0,"ci2metric_id":null,"source":"Telegraf"},`+ - `{"metric_type":"value","resource":"","node":"","value":42,"timestamp":0,"ci2metric_id":null,"source":"Telegraf"}]`, - ), - buf, + `[{"metric_type":"value","resource":"","node":"","value":42,"timestamp":0,"ci2metric_id":null,"source":"Telegraf"},`+ + `{"metric_type":"value","resource":"","node":"","value":42,"timestamp":0,"ci2metric_id":null,"source":"Telegraf"}]`, + string(buf), ) } @@ -213,10 +211,10 @@ func TestSerializeJSONv2Format(t *testing.T) { s := &Serializer{Format: "jsonv2"} buf, err := s.Serialize(m) require.NoError(t, err) - require.Equal( + require.JSONEq( t, - []byte(`{"records":[{"metric_type":"value","resource":"","node":"","value":42,"timestamp":0,"ci2metric_id":null,"source":"Telegraf"}]}`), - buf, + `{"records":[{"metric_type":"value","resource":"","node":"","value":42,"timestamp":0,"ci2metric_id":null,"source":"Telegraf"}]}`, + string(buf), ) } @@ -233,15 +231,13 @@ func TestSerializeJSONv2FormatBatch(t *testing.T) { metrics := []telegraf.Metric{m, m} buf, err := s.SerializeBatch(metrics) require.NoError(t, err) - require.Equal( + require.JSONEq( t, - []byte( - `{"records":[`+ - `{"metric_type":"value","resource":"","node":"","value":42,"timestamp":0,"ci2metric_id":null,"source":"Telegraf"},`+ - `{"metric_type":"value","resource":"","node":"","value":42,"timestamp":0,"ci2metric_id":null,"source":"Telegraf"}`+ - `]}`, - ), - buf, + `{"records":[`+ + `{"metric_type":"value","resource":"","node":"","value":42,"timestamp":0,"ci2metric_id":null,"source":"Telegraf"},`+ + `{"metric_type":"value","resource":"","node":"","value":42,"timestamp":0,"ci2metric_id":null,"source":"Telegraf"}`+ + `]}`, + string(buf), ) } From e3ce01abf029c23fd16152d7290c92b1aa301ac8 Mon Sep 17 00:00:00 2001 From: Sven Rebhan <36194019+srebhan@users.noreply.github.com> Date: Wed, 11 Dec 2024 22:32:52 +0100 Subject: [PATCH 04/21] feat(inputs.systemd_units): Add active_enter_timestamp_us field (#16287) --- go.mod | 2 +- plugins/inputs/systemd_units/README.md | 1 + .../systemd_units/systemd_units_linux.go | 44 +++-- .../systemd_units/systemd_units_test.go | 158 +++++++++--------- 4 files changed, 113 insertions(+), 92 deletions(-) diff --git a/go.mod b/go.mod index 9f8ae22fefcc5..fa91210beccf1 100644 --- a/go.mod +++ b/go.mod @@ -89,7 +89,6 @@ require ( github.com/go-sql-driver/mysql v1.8.1 github.com/go-stomp/stomp v2.1.4+incompatible github.com/gobwas/glob v0.2.3 - github.com/godbus/dbus/v5 v5.1.0 github.com/gofrs/uuid/v5 v5.3.0 github.com/golang-jwt/jwt/v5 v5.2.1 github.com/golang/geo v0.0.0-20190916061304-5b978397cfec @@ -348,6 +347,7 @@ require ( github.com/goburrow/serial v0.1.1-0.20211022031912-bfb69110f8dd // indirect github.com/goccy/go-json v0.10.3 // indirect github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect + github.com/godbus/dbus/v5 v5.1.0 // indirect github.com/gofrs/uuid v4.4.0+incompatible // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang-jwt/jwt/v4 v4.5.1 // indirect diff --git a/plugins/inputs/systemd_units/README.md b/plugins/inputs/systemd_units/README.md index 5364f43ef574c..679d5819e9691 100644 --- a/plugins/inputs/systemd_units/README.md +++ b/plugins/inputs/systemd_units/README.md @@ -94,6 +94,7 @@ The following *additional* metrics are available with `details = true`: - swap_current (uint, current swap usage) - swap_peak (uint, peak swap usage) - mem_avail (uint, available memory for this unit) + - active_enter_timestamp_us (uint, timestamp in us when entered the state) ### Load diff --git a/plugins/inputs/systemd_units/systemd_units_linux.go b/plugins/inputs/systemd_units/systemd_units_linux.go index 443095ee203d8..2500b6cce62be 100644 --- a/plugins/inputs/systemd_units/systemd_units_linux.go +++ b/plugins/inputs/systemd_units/systemd_units_linux.go @@ -123,17 +123,18 @@ type client interface { ListUnitFilesByPatternsContext(ctx context.Context, states, pattern []string) ([]dbus.UnitFile, error) ListUnitsByNamesContext(ctx context.Context, units []string) ([]dbus.UnitStatus, error) GetUnitTypePropertiesContext(ctx context.Context, unit, unitType string) (map[string]interface{}, error) - GetUnitPropertyContext(ctx context.Context, unit, propertyName string) (*dbus.Property, error) + GetUnitPropertiesContext(ctx context.Context, unit string) (map[string]interface{}, error) ListUnitsContext(ctx context.Context) ([]dbus.UnitStatus, error) } type archParams struct { - client client - pattern []string - filter filter.Filter - unitTypeDBus string - scope string - user string + client client + pattern []string + filter filter.Filter + unitTypeDBus string + scope string + user string + warnUnitProps map[string]bool } func (s *SystemdUnits) Init() error { @@ -176,6 +177,8 @@ func (s *SystemdUnits) Init() error { return fmt.Errorf("invalid 'scope' %q", s.Scope) } + s.warnUnitProps = make(map[string]bool) + return nil } @@ -374,26 +377,35 @@ func (s *SystemdUnits) Gather(acc telegraf.Accumulator) error { } // Get required unit file properties - var unitFileState string - if v, err := s.client.GetUnitPropertyContext(ctx, state.Name, "UnitFileState"); err == nil { - unitFileState = strings.Trim(v.Value.String(), `'"`) + unitProperties, err := s.client.GetUnitPropertiesContext(ctx, state.Name) + if err != nil && !s.warnUnitProps[state.Name] { + s.Log.Warnf("Cannot read unit properties for %q: %v", state.Name, err) + s.warnUnitProps[state.Name] = true + } + + // Set tags + if v, found := unitProperties["UnitFileState"]; found { + tags["state"] = v.(string) } - var unitFilePreset string - if v, err := s.client.GetUnitPropertyContext(ctx, state.Name, "UnitFilePreset"); err == nil { - unitFilePreset = strings.Trim(v.Value.String(), `'"`) + if v, found := unitProperties["UnitFilePreset"]; found { + tags["preset"] = v.(string) } - tags["state"] = unitFileState - tags["preset"] = unitFilePreset + // Set fields + if v, found := unitProperties["ActiveEnterTimestamp"]; found { + fields["active_enter_timestamp_us"] = v + } fields["status_errno"] = properties["StatusErrno"] fields["restarts"] = properties["NRestarts"] fields["pid"] = properties["MainPID"] + fields["mem_current"] = properties["MemoryCurrent"] fields["mem_peak"] = properties["MemoryPeak"] + fields["mem_avail"] = properties["MemoryAvailable"] + fields["swap_current"] = properties["MemorySwapCurrent"] fields["swap_peak"] = properties["MemorySwapPeak"] - fields["mem_avail"] = properties["MemoryAvailable"] // Sanitize unset memory fields for k, value := range fields { diff --git a/plugins/inputs/systemd_units/systemd_units_test.go b/plugins/inputs/systemd_units/systemd_units_test.go index 7add99775d661..3c2c711110ac0 100644 --- a/plugins/inputs/systemd_units/systemd_units_test.go +++ b/plugins/inputs/systemd_units/systemd_units_test.go @@ -4,7 +4,6 @@ package systemd_units import ( "context" - "errors" "fmt" "math" "os/user" @@ -13,7 +12,6 @@ import ( "time" sdbus "github.com/coreos/go-systemd/v22/dbus" - "github.com/godbus/dbus/v5" "github.com/stretchr/testify/require" "github.com/influxdata/telegraf" @@ -25,12 +23,13 @@ import ( ) type properties struct { - uf *sdbus.UnitFile - utype string - state *sdbus.UnitStatus - ufPreset string - ufState string - properties map[string]interface{} + uf *sdbus.UnitFile + utype string + state *sdbus.UnitStatus + ufPreset string + ufState string + ufActiveEnter uint64 + properties map[string]interface{} } func TestDefaultPattern(t *testing.T) { @@ -284,6 +283,7 @@ func TestListFiles(t *testing.T) { } func TestShow(t *testing.T) { + enter := time.Now().UnixMicro() tests := []struct { name string properties map[string]properties @@ -301,8 +301,9 @@ func TestShow(t *testing.T) { ActiveState: "active", SubState: "running", }, - ufPreset: "disabled", - ufState: "enabled", + ufPreset: "disabled", + ufState: "enabled", + ufActiveEnter: uint64(enter), properties: map[string]interface{}{ "Id": "example.service", "StatusErrno": 0, @@ -328,17 +329,18 @@ func TestShow(t *testing.T) { "preset": "disabled", }, map[string]interface{}{ - "load_code": 0, - "active_code": 0, - "sub_code": 0, - "status_errno": 0, - "restarts": 1, - "mem_current": uint64(1000), - "mem_peak": uint64(2000), - "swap_current": uint64(3000), - "swap_peak": uint64(4000), - "mem_avail": uint64(5000), - "pid": 9999, + "load_code": 0, + "active_code": 0, + "sub_code": 0, + "status_errno": 0, + "restarts": 1, + "mem_current": uint64(1000), + "mem_peak": uint64(2000), + "swap_current": uint64(3000), + "swap_peak": uint64(4000), + "mem_avail": uint64(5000), + "pid": 9999, + "active_enter_timestamp_us": uint64(enter), }, time.Unix(0, 0), ), @@ -355,8 +357,9 @@ func TestShow(t *testing.T) { ActiveState: "active", SubState: "exited", }, - ufPreset: "disabled", - ufState: "enabled", + ufPreset: "disabled", + ufState: "enabled", + ufActiveEnter: 0, properties: map[string]interface{}{ "Id": "example.service", "StatusErrno": 0, @@ -376,16 +379,17 @@ func TestShow(t *testing.T) { "preset": "disabled", }, map[string]interface{}{ - "load_code": 0, - "active_code": 0, - "sub_code": 4, - "status_errno": 0, - "restarts": 0, - "mem_current": uint64(0), - "mem_peak": uint64(0), - "swap_current": uint64(0), - "swap_peak": uint64(0), - "mem_avail": uint64(0), + "load_code": 0, + "active_code": 0, + "sub_code": 4, + "status_errno": 0, + "restarts": 0, + "mem_current": uint64(0), + "mem_peak": uint64(0), + "swap_current": uint64(0), + "swap_peak": uint64(0), + "mem_avail": uint64(0), + "active_enter_timestamp_us": uint64(0), }, time.Unix(0, 0), ), @@ -402,8 +406,9 @@ func TestShow(t *testing.T) { ActiveState: "failed", SubState: "failed", }, - ufPreset: "disabled", - ufState: "enabled", + ufPreset: "disabled", + ufState: "enabled", + ufActiveEnter: uint64(enter), properties: map[string]interface{}{ "Id": "example.service", "StatusErrno": 10, @@ -428,16 +433,17 @@ func TestShow(t *testing.T) { "preset": "disabled", }, map[string]interface{}{ - "load_code": 0, - "active_code": 3, - "sub_code": 12, - "status_errno": 10, - "restarts": 1, - "mem_current": uint64(1000), - "mem_peak": uint64(2000), - "swap_current": uint64(3000), - "swap_peak": uint64(4000), - "mem_avail": uint64(5000), + "load_code": 0, + "active_code": 3, + "sub_code": 12, + "status_errno": 10, + "restarts": 1, + "mem_current": uint64(1000), + "mem_peak": uint64(2000), + "swap_current": uint64(3000), + "swap_peak": uint64(4000), + "mem_avail": uint64(5000), + "active_enter_timestamp_us": uint64(enter), }, time.Unix(0, 0), ), @@ -454,8 +460,9 @@ func TestShow(t *testing.T) { ActiveState: "inactive", SubState: "dead", }, - ufPreset: "disabled", - ufState: "enabled", + ufPreset: "disabled", + ufState: "enabled", + ufActiveEnter: uint64(0), properties: map[string]interface{}{ "Id": "example.service", }, @@ -473,14 +480,15 @@ func TestShow(t *testing.T) { "preset": "disabled", }, map[string]interface{}{ - "load_code": 2, - "active_code": 2, - "sub_code": 1, - "mem_current": uint64(0), - "mem_peak": uint64(0), - "swap_current": uint64(0), - "swap_peak": uint64(0), - "mem_avail": uint64(0), + "load_code": 2, + "active_code": 2, + "sub_code": 1, + "mem_current": uint64(0), + "mem_peak": uint64(0), + "swap_current": uint64(0), + "swap_peak": uint64(0), + "mem_avail": uint64(0), + "active_enter_timestamp_us": uint64(0), }, time.Unix(0, 0), ), @@ -517,8 +525,9 @@ func TestShow(t *testing.T) { ActiveState: "inactive", SubState: "dead", }, - ufPreset: "disabled", - ufState: "disabled", + ufPreset: "disabled", + ufState: "disabled", + ufActiveEnter: uint64(0), properties: map[string]interface{}{ "Id": "example.service", "StatusErrno": 0, @@ -543,16 +552,17 @@ func TestShow(t *testing.T) { "preset": "disabled", }, map[string]interface{}{ - "load_code": 0, - "active_code": int64(2), - "sub_code": 1, - "status_errno": 0, - "restarts": 0, - "mem_current": uint64(0), - "mem_peak": uint64(0), - "swap_current": uint64(0), - "swap_peak": uint64(0), - "mem_avail": uint64(0), + "load_code": 0, + "active_code": int64(2), + "sub_code": 1, + "status_errno": 0, + "restarts": 0, + "mem_current": uint64(0), + "mem_peak": uint64(0), + "swap_current": uint64(0), + "swap_peak": uint64(0), + "mem_avail": uint64(0), + "active_enter_timestamp_us": uint64(0), }, time.Unix(0, 0), ), @@ -974,19 +984,17 @@ func (c *fakeClient) GetUnitTypePropertiesContext(_ context.Context, unit, unitT return u.properties, nil } -func (c *fakeClient) GetUnitPropertyContext(_ context.Context, unit, propertyName string) (*sdbus.Property, error) { +func (c *fakeClient) GetUnitPropertiesContext(_ context.Context, unit string) (map[string]interface{}, error) { u, found := c.units[unit] if !found { return nil, nil } - switch propertyName { - case "UnitFileState": - return &sdbus.Property{Name: propertyName, Value: dbus.MakeVariant(u.ufState)}, nil - case "UnitFilePreset": - return &sdbus.Property{Name: propertyName, Value: dbus.MakeVariant(u.ufPreset)}, nil - } - return nil, errors.New("unknown property") + return map[string]interface{}{ + "UnitFileState": u.ufState, + "UnitFilePreset": u.ufPreset, + "ActiveEnterTimestamp": u.ufActiveEnter, + }, nil } func (c *fakeClient) ListUnitsContext(_ context.Context) ([]sdbus.UnitStatus, error) { From d2e032eb4a8519cc556b0d887ce8e1395b7591e1 Mon Sep 17 00:00:00 2001 From: Sven Rebhan <36194019+srebhan@users.noreply.github.com> Date: Wed, 11 Dec 2024 22:33:31 +0100 Subject: [PATCH 05/21] fix(agent): Skip initialization of second processor state if requested (#16290) --- agent/agent.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index 7f00fc6ca9ff5..ed19d3f764c38 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -231,10 +231,12 @@ func (a *Agent) InitPlugins() error { return fmt.Errorf("could not initialize aggregator %s: %w", aggregator.LogName(), err) } } - for _, processor := range a.Config.AggProcessors { - err := processor.Init() - if err != nil { - return fmt.Errorf("could not initialize processor %s: %w", processor.LogName(), err) + if !a.Config.Agent.SkipProcessorsAfterAggregators { + for _, processor := range a.Config.AggProcessors { + err := processor.Init() + if err != nil { + return fmt.Errorf("could not initialize processor %s: %w", processor.LogName(), err) + } } } for _, output := range a.Config.Outputs { From 36553aec1e7f5ae14feb9d24d3d6a666a7714156 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 13 Dec 2024 10:45:53 -0600 Subject: [PATCH 06/21] chore(deps): Bump golang.org/x/crypto from 0.29.0 to 0.31.0 (#16297) --- go.mod | 10 +++++----- go.sum | 20 ++++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/go.mod b/go.mod index fa91210beccf1..bd7f76df4317e 100644 --- a/go.mod +++ b/go.mod @@ -211,14 +211,14 @@ require ( go.opentelemetry.io/proto/otlp v1.3.1 go.starlark.net v0.0.0-20240925182052-1207426daebd go.step.sm/crypto v0.54.0 - golang.org/x/crypto v0.29.0 + golang.org/x/crypto v0.31.0 golang.org/x/mod v0.21.0 golang.org/x/net v0.31.0 golang.org/x/oauth2 v0.23.0 - golang.org/x/sync v0.9.0 - golang.org/x/sys v0.27.0 - golang.org/x/term v0.26.0 - golang.org/x/text v0.20.0 + golang.org/x/sync v0.10.0 + golang.org/x/sys v0.28.0 + golang.org/x/term v0.27.0 + golang.org/x/text v0.21.0 golang.zx2c4.com/wireguard/wgctrl v0.0.0-20211230205640-daad0b7ba671 gonum.org/v1/gonum v0.15.1 google.golang.org/api v0.203.0 diff --git a/go.sum b/go.sum index 58b901287ccef..628bb6e28e1ee 100644 --- a/go.sum +++ b/go.sum @@ -2550,8 +2550,8 @@ golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1m golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= golang.org/x/crypto v0.20.0/go.mod h1:Xwo95rrVNIoSMx9wa1JroENMToLWn3RNVrTBpLHgZPQ= golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= -golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ= -golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg= +golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U= +golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk= golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= @@ -2753,8 +2753,8 @@ golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20220819030929-7fc1605a5dde/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ= -golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= +golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -2891,8 +2891,8 @@ golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s= -golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= +golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= @@ -2910,8 +2910,8 @@ golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0= golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY= golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58= -golang.org/x/term v0.26.0 h1:WEQa6V3Gja/BhNxg540hBip/kkaYtRg3cxg4oXSw4AU= -golang.org/x/term v0.26.0/go.mod h1:Si5m1o57C5nBNQo5z1iq+XDijt21BDBDp2bK0QI8e3E= +golang.org/x/term v0.27.0 h1:WP60Sv1nlK1T6SupCHbXzSaN0b9wUmsPoRS9b61A23Q= +golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -2931,8 +2931,8 @@ golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= -golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug= -golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4= +golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= +golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= From e15a3c8dc6ba0ae3de621429b24b8687b529ffeb Mon Sep 17 00:00:00 2001 From: Sven Rebhan <36194019+srebhan@users.noreply.github.com> Date: Fri, 13 Dec 2024 17:46:37 +0100 Subject: [PATCH 07/21] chore(agent): Add warning about changing default for 'skip_processors_after_aggregators' (#16302) --- CHANGELOG.md | 10 ++++++++++ agent/agent.go | 36 ++++++++++++++++++++++++++++++++---- config/config.go | 2 +- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a022cbf4d0dac..0d73b17fcc318 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,16 @@ # Changelog +## Unreleased + +### Important Changes + +- The default value of `skip_processors_after_aggregators` will change to `true` + with Telegraf `v1.40.0`, skip running the processors again after aggregators! + If you need the current default behavior, please explicitly set the option to + `false`! To silence the warning and use the future default behavior, please + explicitly set the option to `true`. + ## v1.33.0 [2024-12-09] ### New Plugins diff --git a/agent/agent.go b/agent/agent.go index ed19d3f764c38..d4c284ac905a6 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -10,6 +10,7 @@ import ( "sync" "time" + "github.com/fatih/color" "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/config" "github.com/influxdata/telegraf/internal" @@ -106,6 +107,15 @@ func (a *Agent) Run(ctx context.Context) error { time.Duration(a.Config.Agent.Interval), a.Config.Agent.Quiet, a.Config.Agent.Hostname, time.Duration(a.Config.Agent.FlushInterval)) + // Set the default for processor skipping + if a.Config.Agent.SkipProcessorsAfterAggregators == nil { + msg := `The default value of 'skip_processors_after_aggregators' will change to 'true' with Telegraf v1.40.0! ` + msg += `If you need the current default behavior, please explicitly set the option to 'false'!` + log.Print("W! [agent] ", color.YellowString(msg)) + skipProcessorsAfterAggregators := false + a.Config.Agent.SkipProcessorsAfterAggregators = &skipProcessorsAfterAggregators + } + log.Printf("D! [agent] Initializing plugins") if err := a.InitPlugins(); err != nil { return err @@ -136,7 +146,7 @@ func (a *Agent) Run(ctx context.Context) error { var au *aggregatorUnit if len(a.Config.Aggregators) != 0 { aggC := next - if len(a.Config.AggProcessors) != 0 && !a.Config.Agent.SkipProcessorsAfterAggregators { + if len(a.Config.AggProcessors) != 0 && !*a.Config.Agent.SkipProcessorsAfterAggregators { aggC, apu, err = a.startProcessors(next, a.Config.AggProcessors) if err != nil { return err @@ -231,7 +241,7 @@ func (a *Agent) InitPlugins() error { return fmt.Errorf("could not initialize aggregator %s: %w", aggregator.LogName(), err) } } - if !a.Config.Agent.SkipProcessorsAfterAggregators { + if !*a.Config.Agent.SkipProcessorsAfterAggregators { for _, processor := range a.Config.AggProcessors { err := processor.Init() if err != nil { @@ -1000,6 +1010,15 @@ func (a *Agent) Test(ctx context.Context, wait time.Duration) error { // outputC. After gathering pauses for the wait duration to allow service // inputs to run. func (a *Agent) runTest(ctx context.Context, wait time.Duration, outputC chan<- telegraf.Metric) error { + // Set the default for processor skipping + if a.Config.Agent.SkipProcessorsAfterAggregators == nil { + msg := `The default value of 'skip_processors_after_aggregators' will change to 'true' with Telegraf v1.40.0! ` + msg += `If you need the current default behavior, please explicitly set the option to 'false'!` + log.Print("W! [agent] ", color.YellowString(msg)) + skipProcessorsAfterAggregators := false + a.Config.Agent.SkipProcessorsAfterAggregators = &skipProcessorsAfterAggregators + } + log.Printf("D! [agent] Initializing plugins") if err := a.InitPlugins(); err != nil { return err @@ -1013,7 +1032,7 @@ func (a *Agent) runTest(ctx context.Context, wait time.Duration, outputC chan<- var au *aggregatorUnit if len(a.Config.Aggregators) != 0 { procC := next - if len(a.Config.AggProcessors) != 0 && !a.Config.Agent.SkipProcessorsAfterAggregators { + if len(a.Config.AggProcessors) != 0 && !*a.Config.Agent.SkipProcessorsAfterAggregators { var err error procC, apu, err = a.startProcessors(next, a.Config.AggProcessors) if err != nil { @@ -1096,6 +1115,15 @@ func (a *Agent) Once(ctx context.Context, wait time.Duration) error { // outputC. After gathering pauses for the wait duration to allow service // inputs to run. func (a *Agent) runOnce(ctx context.Context, wait time.Duration) error { + // Set the default for processor skipping + if a.Config.Agent.SkipProcessorsAfterAggregators == nil { + msg := `The default value of 'skip_processors_after_aggregators' will change to 'true' with Telegraf v1.40.0! ` + msg += `If you need the current default behavior, please explicitly set the option to 'false'!` + log.Print("W! [agent] ", color.YellowString(msg)) + skipProcessorsAfterAggregators := false + a.Config.Agent.SkipProcessorsAfterAggregators = &skipProcessorsAfterAggregators + } + log.Printf("D! [agent] Initializing plugins") if err := a.InitPlugins(); err != nil { return err @@ -1113,7 +1141,7 @@ func (a *Agent) runOnce(ctx context.Context, wait time.Duration) error { var au *aggregatorUnit if len(a.Config.Aggregators) != 0 { procC := next - if len(a.Config.AggProcessors) != 0 && !a.Config.Agent.SkipProcessorsAfterAggregators { + if len(a.Config.AggProcessors) != 0 && !*a.Config.Agent.SkipProcessorsAfterAggregators { procC, apu, err = a.startProcessors(next, a.Config.AggProcessors) if err != nil { return err diff --git a/config/config.go b/config/config.go index 6a71646b095da..4790f03e82e18 100644 --- a/config/config.go +++ b/config/config.go @@ -279,7 +279,7 @@ type AgentConfig struct { // Flag to skip running processors after aggregators // By default, processors are run a second time after aggregators. Changing // this setting to true will skip the second run of processors. - SkipProcessorsAfterAggregators bool `toml:"skip_processors_after_aggregators"` + SkipProcessorsAfterAggregators *bool `toml:"skip_processors_after_aggregators"` // Number of attempts to obtain a remote configuration via a URL during // startup. Set to -1 for unlimited attempts. From 2bd4559bc142ef36a1c3dfd4999e066443a836d1 Mon Sep 17 00:00:00 2001 From: Sven Rebhan <36194019+srebhan@users.noreply.github.com> Date: Fri, 13 Dec 2024 18:23:50 +0100 Subject: [PATCH 08/21] chore(processors.unpivot): Cleanup code and improve performance (#16299) --- plugins/processors/unpivot/unpivot.go | 45 +++---- plugins/processors/unpivot/unpivot_test.go | 147 +++++++++++++++------ 2 files changed, 124 insertions(+), 68 deletions(-) diff --git a/plugins/processors/unpivot/unpivot.go b/plugins/processors/unpivot/unpivot.go index 3f41d6bb7b9ba..53bfb25aebe27 100644 --- a/plugins/processors/unpivot/unpivot.go +++ b/plugins/processors/unpivot/unpivot.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/metric" "github.com/influxdata/telegraf/plugins/processors" ) @@ -18,30 +19,15 @@ type Unpivot struct { ValueKey string `toml:"value_key"` } -func copyWithoutFields(metric telegraf.Metric) telegraf.Metric { - m := metric.Copy() - - fieldKeys := make([]string, 0, len(m.FieldList())) - for _, field := range m.FieldList() { - fieldKeys = append(fieldKeys, field.Key) - } - - for _, fk := range fieldKeys { - m.RemoveField(fk) - } - - return m -} - func (*Unpivot) SampleConfig() string { return sampleConfig } func (p *Unpivot) Init() error { switch p.FieldNameAs { - case "metric": case "", "tag": p.FieldNameAs = "tag" + case "metric": default: return fmt.Errorf("unrecognized metric mode: %q", p.FieldNameAs) } @@ -63,27 +49,28 @@ func (p *Unpivot) Apply(metrics ...telegraf.Metric) []telegraf.Metric { } results := make([]telegraf.Metric, 0, fieldCount) - for _, m := range metrics { - base := m - if wm, ok := m.(telegraf.UnwrappableMetric); ok { - base = wm.Unwrap() + for _, src := range metrics { + // Create a copy without fields and tracking information + base := metric.New(src.Name(), make(map[string]string), make(map[string]interface{}), src.Time()) + for _, t := range src.TagList() { + base.AddTag(t.Key, t.Value) } - base = copyWithoutFields(base) - for _, field := range m.FieldList() { - newMetric := base.Copy() - newMetric.AddField(p.ValueKey, field.Value) + // Create a new metric per field and add it to the output + for _, field := range src.FieldList() { + m := base.Copy() + m.AddField(p.ValueKey, field.Value) switch p.FieldNameAs { case "metric": - newMetric.SetName(field.Key) - case "", "tag": - newMetric.AddTag(p.TagKey, field.Key) + m.SetName(field.Key) + case "tag": + m.AddTag(p.TagKey, field.Key) } - results = append(results, newMetric) + results = append(results, m) } - m.Accept() + src.Accept() } return results } diff --git a/plugins/processors/unpivot/unpivot_test.go b/plugins/processors/unpivot/unpivot_test.go index 0152513159ad8..b44632db3ec1b 100644 --- a/plugins/processors/unpivot/unpivot_test.go +++ b/plugins/processors/unpivot/unpivot_test.go @@ -12,7 +12,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestUnpivot_defaults(t *testing.T) { +func TestDefaults(t *testing.T) { unpivot := &Unpivot{} require.NoError(t, unpivot.Init()) require.Equal(t, "tag", unpivot.FieldNameAs) @@ -20,25 +20,25 @@ func TestUnpivot_defaults(t *testing.T) { require.Equal(t, "value", unpivot.ValueKey) } -func TestUnpivot_invalidMetricMode(t *testing.T) { +func TestInvalidMetricMode(t *testing.T) { unpivot := &Unpivot{FieldNameAs: "unknown"} require.Error(t, unpivot.Init()) } -func TestUnpivot_originalMode(t *testing.T) { +func TestOriginalMode(t *testing.T) { now := time.Now() tests := []struct { name string - unpivot *Unpivot + tagKey string + valueKey string + metrics []telegraf.Metric expected []telegraf.Metric }{ { - name: "simple", - unpivot: &Unpivot{ - TagKey: "name", - ValueKey: "value", - }, + name: "simple", + tagKey: "name", + valueKey: "value", metrics: []telegraf.Metric{ testutil.MustMetric("cpu", map[string]string{}, @@ -61,11 +61,9 @@ func TestUnpivot_originalMode(t *testing.T) { }, }, { - name: "multi fields", - unpivot: &Unpivot{ - TagKey: "name", - ValueKey: "value", - }, + name: "multi fields", + tagKey: "name", + valueKey: "value", metrics: []telegraf.Metric{ testutil.MustMetric("cpu", map[string]string{}, @@ -100,27 +98,33 @@ func TestUnpivot_originalMode(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - actual := tt.unpivot.Apply(tt.metrics...) + plugin := &Unpivot{ + TagKey: tt.tagKey, + ValueKey: tt.valueKey, + } + require.NoError(t, plugin.Init()) + + actual := plugin.Apply(tt.metrics...) testutil.RequireMetricsEqual(t, tt.expected, actual, testutil.SortMetrics()) }) } } -func TestUnpivot_fieldMode(t *testing.T) { +func TestFieldMode(t *testing.T) { now := time.Now() tests := []struct { - name string - unpivot *Unpivot - metrics []telegraf.Metric - expected []telegraf.Metric + name string + fieldNameAs string + tagKey string + valueKey string + metrics []telegraf.Metric + expected []telegraf.Metric }{ { - name: "simple", - unpivot: &Unpivot{ - FieldNameAs: "metric", - TagKey: "name", - ValueKey: "value", - }, + name: "simple", + fieldNameAs: "metric", + tagKey: "name", + valueKey: "value", metrics: []telegraf.Metric{ testutil.MustMetric("cpu", map[string]string{}, @@ -141,12 +145,10 @@ func TestUnpivot_fieldMode(t *testing.T) { }, }, { - name: "multi fields", - unpivot: &Unpivot{ - FieldNameAs: "metric", - TagKey: "name", - ValueKey: "value", - }, + name: "multi fields", + fieldNameAs: "metric", + tagKey: "name", + valueKey: "value", metrics: []telegraf.Metric{ testutil.MustMetric("cpu", map[string]string{}, @@ -175,12 +177,10 @@ func TestUnpivot_fieldMode(t *testing.T) { }, }, { - name: "multi fields and tags", - unpivot: &Unpivot{ - FieldNameAs: "metric", - TagKey: "name", - ValueKey: "value", - }, + name: "multi fields and tags", + fieldNameAs: "metric", + tagKey: "name", + valueKey: "value", metrics: []telegraf.Metric{ testutil.MustMetric("cpu", map[string]string{ @@ -217,7 +217,14 @@ func TestUnpivot_fieldMode(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - actual := tt.unpivot.Apply(tt.metrics...) + plugin := &Unpivot{ + FieldNameAs: tt.fieldNameAs, + TagKey: tt.tagKey, + ValueKey: tt.valueKey, + } + require.NoError(t, plugin.Init()) + + actual := plugin.Apply(tt.metrics...) testutil.RequireMetricsEqual(t, tt.expected, actual, testutil.SortMetrics()) }) } @@ -247,6 +254,8 @@ func TestTrackedMetricNotLost(t *testing.T) { // Process expected metrics and compare with resulting metrics plugin := &Unpivot{TagKey: "name", ValueKey: "value"} + require.NoError(t, plugin.Init()) + actual := plugin.Apply(input...) testutil.RequireMetricsEqual(t, expected, actual, testutil.SortMetrics()) @@ -262,3 +271,63 @@ func TestTrackedMetricNotLost(t *testing.T) { return len(input) == len(delivered) }, time.Second, 100*time.Millisecond, "%d delivered but %d expected", len(delivered), len(input)) } + +func BenchmarkAsTag(b *testing.B) { + input := metric.New( + "test", + map[string]string{ + "source": "device A", + "location": "main building", + }, + map[string]interface{}{ + "field0": 0.1, + "field1": 1.2, + "field2": 2.3, + "field3": 3.4, + "field4": 4.5, + "field5": 5.6, + "field6": 6.7, + "field7": 7.8, + "field8": 8.9, + "field9": 9.0, + }, + time.Now(), + ) + + plugin := &Unpivot{} + require.NoError(b, plugin.Init()) + + for n := 0; n < b.N; n++ { + plugin.Apply(input) + } +} + +func BenchmarkAsMetric(b *testing.B) { + input := metric.New( + "test", + map[string]string{ + "source": "device A", + "location": "main building", + }, + map[string]interface{}{ + "field0": 0.1, + "field1": 1.2, + "field2": 2.3, + "field3": 3.4, + "field4": 4.5, + "field5": 5.6, + "field6": 6.7, + "field7": 7.8, + "field8": 8.9, + "field9": 9.0, + }, + time.Now(), + ) + + plugin := &Unpivot{FieldNameAs: "metric"} + require.NoError(b, plugin.Init()) + + for n := 0; n < b.N; n++ { + plugin.Apply(input) + } +} From 516b8cfbd16dc1eade0034fe873bb23a242bee04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20=C5=BBak?= Date: Fri, 13 Dec 2024 18:26:34 +0100 Subject: [PATCH 09/21] chore: Fix linter findings for `revive:unused-receiver` in `plugins/inputs/[a-e]` (#16263) --- plugins/inputs/aerospike/aerospike.go | 31 ++++----- plugins/inputs/aerospike/aerospike_test.go | 20 ++---- plugins/inputs/aliyuncms/aliyuncms_test.go | 2 +- plugins/inputs/amd_rocm_smi/amd_rocm_smi.go | 2 +- plugins/inputs/amqp_consumer/amqp_consumer.go | 6 +- plugins/inputs/apache/apache.go | 4 +- plugins/inputs/azure_monitor/azure_monitor.go | 4 +- .../azure_monitor/azure_monitor_test.go | 10 +-- plugins/inputs/bcache/bcache.go | 4 +- plugins/inputs/bond/bond.go | 4 +- plugins/inputs/bond/bond_test.go | 4 +- plugins/inputs/burrow/burrow.go | 8 +-- .../cisco_telemetry_mdt.go | 10 +-- plugins/inputs/clickhouse/clickhouse.go | 22 +++---- plugins/inputs/cloud_pubsub/cloud_pubsub.go | 2 +- .../cloud_pubsub_push/cloud_pubsub_push.go | 2 +- .../cloud_pubsub_push_test.go | 6 +- plugins/inputs/cloudwatch/cloudwatch_test.go | 20 +++--- .../cloudwatch_metric_streams.go | 2 +- plugins/inputs/couchdb/couchdb.go | 64 +++++++++---------- plugins/inputs/csgo/csgo.go | 4 +- .../inputs/ctrlx_datalayer/ctrlx_datalayer.go | 2 +- plugins/inputs/dcos/client.go | 4 +- plugins/inputs/dcos/creds.go | 6 +- plugins/inputs/dcos/dcos.go | 24 +++---- plugins/inputs/dcos/dcos_test.go | 9 +-- plugins/inputs/docker_log/docker_log.go | 2 +- plugins/inputs/docker_log/docker_log_test.go | 2 +- plugins/inputs/dovecot/dovecot.go | 4 +- plugins/inputs/ecs/ecs.go | 4 +- plugins/inputs/elasticsearch/elasticsearch.go | 2 +- .../elasticsearch_query.go | 2 +- plugins/inputs/ethtool/ethtool_linux.go | 6 +- plugins/inputs/ethtool/ethtool_test.go | 10 +-- plugins/inputs/exec/exec.go | 4 +- plugins/inputs/exec/exec_test.go | 3 +- .../{run_notwinodws.go => run_notwindows.go} | 2 +- plugins/inputs/exec/run_windows.go | 2 +- plugins/inputs/execd/execd_test.go | 6 +- plugins/inputs/execd/shim/input.go | 6 +- plugins/inputs/execd/shim/shim_test.go | 22 ++----- 41 files changed, 168 insertions(+), 185 deletions(-) rename plugins/inputs/exec/{run_notwinodws.go => run_notwindows.go} (96%) diff --git a/plugins/inputs/aerospike/aerospike.go b/plugins/inputs/aerospike/aerospike.go index 26a5677075d63..52732cff85cdc 100644 --- a/plugins/inputs/aerospike/aerospike.go +++ b/plugins/inputs/aerospike/aerospike.go @@ -121,11 +121,11 @@ func (a *Aerospike) gatherServer(acc telegraf.Accumulator, hostPort string) erro nodes := c.GetNodes() for _, n := range nodes { nodeHost := n.GetHost().String() - stats, err := a.getNodeInfo(n, asInfoPolicy) + stats, err := getNodeInfo(n, asInfoPolicy) if err != nil { return err } - a.parseNodeInfo(acc, stats, nodeHost, n.GetName()) + parseNodeInfo(acc, stats, nodeHost, n.GetName()) namespaces, err := a.getNamespaces(n, asInfoPolicy) if err != nil { @@ -135,12 +135,12 @@ func (a *Aerospike) gatherServer(acc telegraf.Accumulator, hostPort string) erro if !a.DisableQueryNamespaces { // Query Namespaces for _, namespace := range namespaces { - stats, err = a.getNamespaceInfo(namespace, n, asInfoPolicy) + stats, err = getNamespaceInfo(namespace, n, asInfoPolicy) if err != nil { continue } - a.parseNamespaceInfo(acc, stats, nodeHost, namespace, n.GetName()) + parseNamespaceInfo(acc, stats, nodeHost, namespace, n.GetName()) if a.EnableTTLHistogram { err = a.getTTLHistogram(acc, nodeHost, namespace, "", n, asInfoPolicy) @@ -162,12 +162,12 @@ func (a *Aerospike) gatherServer(acc telegraf.Accumulator, hostPort string) erro if err == nil { for _, namespaceSet := range namespaceSets { namespace, set := splitNamespaceSet(namespaceSet) - stats, err := a.getSetInfo(namespaceSet, n, asInfoPolicy) + stats, err := getSetInfo(namespaceSet, n, asInfoPolicy) if err != nil { continue } - a.parseSetInfo(acc, stats, nodeHost, namespaceSet, n.GetName()) + parseSetInfo(acc, stats, nodeHost, namespaceSet, n.GetName()) if a.EnableTTLHistogram { err = a.getTTLHistogram(acc, nodeHost, namespace, set, n, asInfoPolicy) @@ -189,7 +189,7 @@ func (a *Aerospike) gatherServer(acc telegraf.Accumulator, hostPort string) erro return nil } -func (a *Aerospike) getNodeInfo(n *as.Node, infoPolicy *as.InfoPolicy) (map[string]string, error) { +func getNodeInfo(n *as.Node, infoPolicy *as.InfoPolicy) (map[string]string, error) { stats, err := n.RequestInfo(infoPolicy, "statistics") if err != nil { return nil, err @@ -198,7 +198,7 @@ func (a *Aerospike) getNodeInfo(n *as.Node, infoPolicy *as.InfoPolicy) (map[stri return stats, nil } -func (a *Aerospike) parseNodeInfo(acc telegraf.Accumulator, stats map[string]string, hostPort, nodeName string) { +func parseNodeInfo(acc telegraf.Accumulator, stats map[string]string, hostPort, nodeName string) { nTags := map[string]string{ "aerospike_host": hostPort, "node_name": nodeName, @@ -231,7 +231,7 @@ func (a *Aerospike) getNamespaces(n *as.Node, infoPolicy *as.InfoPolicy) ([]stri return namespaces, nil } -func (a *Aerospike) getNamespaceInfo(namespace string, n *as.Node, infoPolicy *as.InfoPolicy) (map[string]string, error) { +func getNamespaceInfo(namespace string, n *as.Node, infoPolicy *as.InfoPolicy) (map[string]string, error) { stats, err := n.RequestInfo(infoPolicy, "namespace/"+namespace) if err != nil { return nil, err @@ -239,7 +239,8 @@ func (a *Aerospike) getNamespaceInfo(namespace string, n *as.Node, infoPolicy *a return stats, err } -func (a *Aerospike) parseNamespaceInfo(acc telegraf.Accumulator, stats map[string]string, hostPort, namespace, nodeName string) { + +func parseNamespaceInfo(acc telegraf.Accumulator, stats map[string]string, hostPort, namespace, nodeName string) { nTags := map[string]string{ "aerospike_host": hostPort, "node_name": nodeName, @@ -296,7 +297,7 @@ func (a *Aerospike) getSets(n *as.Node, infoPolicy *as.InfoPolicy) ([]string, er return namespaceSets, nil } -func (a *Aerospike) getSetInfo(namespaceSet string, n *as.Node, infoPolicy *as.InfoPolicy) (map[string]string, error) { +func getSetInfo(namespaceSet string, n *as.Node, infoPolicy *as.InfoPolicy) (map[string]string, error) { stats, err := n.RequestInfo(infoPolicy, "sets/"+namespaceSet) if err != nil { return nil, err @@ -304,7 +305,7 @@ func (a *Aerospike) getSetInfo(namespaceSet string, n *as.Node, infoPolicy *as.I return stats, nil } -func (a *Aerospike) parseSetInfo(acc telegraf.Accumulator, stats map[string]string, hostPort, namespaceSet, nodeName string) { +func parseSetInfo(acc telegraf.Accumulator, stats map[string]string, hostPort, namespaceSet, nodeName string) { stat := strings.Split( strings.TrimSuffix( stats["sets/"+namespaceSet], ";"), ":") @@ -327,7 +328,7 @@ func (a *Aerospike) parseSetInfo(acc telegraf.Accumulator, stats map[string]stri } func (a *Aerospike) getTTLHistogram(acc telegraf.Accumulator, hostPort, namespace, set string, n *as.Node, infoPolicy *as.InfoPolicy) error { - stats, err := a.getHistogram(namespace, set, "ttl", n, infoPolicy) + stats, err := getHistogram(namespace, set, "ttl", n, infoPolicy) if err != nil { return err } @@ -339,7 +340,7 @@ func (a *Aerospike) getTTLHistogram(acc telegraf.Accumulator, hostPort, namespac } func (a *Aerospike) getObjectSizeLinearHistogram(acc telegraf.Accumulator, hostPort, namespace, set string, n *as.Node, infoPolicy *as.InfoPolicy) error { - stats, err := a.getHistogram(namespace, set, "object-size-linear", n, infoPolicy) + stats, err := getHistogram(namespace, set, "object-size-linear", n, infoPolicy) if err != nil { return err } @@ -350,7 +351,7 @@ func (a *Aerospike) getObjectSizeLinearHistogram(acc telegraf.Accumulator, hostP return nil } -func (a *Aerospike) getHistogram(namespace, set, histogramType string, n *as.Node, infoPolicy *as.InfoPolicy) (map[string]string, error) { +func getHistogram(namespace, set, histogramType string, n *as.Node, infoPolicy *as.InfoPolicy) (map[string]string, error) { var queryArg string if len(set) > 0 { queryArg = fmt.Sprintf("histogram:type=%s;namespace=%v;set=%v", histogramType, namespace, set) diff --git a/plugins/inputs/aerospike/aerospike_test.go b/plugins/inputs/aerospike/aerospike_test.go index 1f3090f7a23b9..73d7b977dab72 100644 --- a/plugins/inputs/aerospike/aerospike_test.go +++ b/plugins/inputs/aerospike/aerospike_test.go @@ -309,9 +309,6 @@ func TestDisableObjectSizeLinearHistogramIntegration(t *testing.T) { } func TestParseNodeInfo(t *testing.T) { - a := &Aerospike{} - var acc testutil.Accumulator - stats := map[string]string{ "statistics": "early_tsvc_from_proxy_error=0;cluster_principal=BB9020012AC4202;cluster_is_member=true", } @@ -327,14 +324,12 @@ func TestParseNodeInfo(t *testing.T) { "node_name": "TestNodeName", } - a.parseNodeInfo(&acc, stats, "127.0.0.1:3000", "TestNodeName") + var acc testutil.Accumulator + parseNodeInfo(&acc, stats, "127.0.0.1:3000", "TestNodeName") acc.AssertContainsTaggedFields(t, "aerospike_node", expectedFields, expectedTags) } func TestParseNamespaceInfo(t *testing.T) { - a := &Aerospike{} - var acc testutil.Accumulator - stats := map[string]string{ "namespace/test": "ns_cluster_size=1;effective_replication_factor=1;objects=2;tombstones=0;master_objects=2", } @@ -353,15 +348,12 @@ func TestParseNamespaceInfo(t *testing.T) { "namespace": "test", } - a.parseNamespaceInfo(&acc, stats, "127.0.0.1:3000", "test", "TestNodeName") + var acc testutil.Accumulator + parseNamespaceInfo(&acc, stats, "127.0.0.1:3000", "test", "TestNodeName") acc.AssertContainsTaggedFields(t, "aerospike_namespace", expectedFields, expectedTags) } func TestParseSetInfo(t *testing.T) { - a := &Aerospike{} - - var acc testutil.Accumulator - stats := map[string]string{ "sets/test/foo": "objects=1:tombstones=0:memory_data_bytes=26;", } @@ -377,7 +369,9 @@ func TestParseSetInfo(t *testing.T) { "node_name": "TestNodeName", "set": "test/foo", } - a.parseSetInfo(&acc, stats, "127.0.0.1:3000", "test/foo", "TestNodeName") + + var acc testutil.Accumulator + parseSetInfo(&acc, stats, "127.0.0.1:3000", "test/foo", "TestNodeName") acc.AssertContainsTaggedFields(t, "aerospike_set", expectedFields, expectedTags) } diff --git a/plugins/inputs/aliyuncms/aliyuncms_test.go b/plugins/inputs/aliyuncms/aliyuncms_test.go index 4042baf1b0237..83dfafad02e3e 100644 --- a/plugins/inputs/aliyuncms/aliyuncms_test.go +++ b/plugins/inputs/aliyuncms/aliyuncms_test.go @@ -26,7 +26,7 @@ const inputTitle = "inputs.aliyuncms" type mockGatherAliyunCMSClient struct{} -func (m *mockGatherAliyunCMSClient) DescribeMetricList(request *cms.DescribeMetricListRequest) (*cms.DescribeMetricListResponse, error) { +func (*mockGatherAliyunCMSClient) DescribeMetricList(request *cms.DescribeMetricListRequest) (*cms.DescribeMetricListResponse, error) { resp := new(cms.DescribeMetricListResponse) // switch request.Metric { diff --git a/plugins/inputs/amd_rocm_smi/amd_rocm_smi.go b/plugins/inputs/amd_rocm_smi/amd_rocm_smi.go index b30a8b1b80871..a6fe8b7780d3e 100644 --- a/plugins/inputs/amd_rocm_smi/amd_rocm_smi.go +++ b/plugins/inputs/amd_rocm_smi/amd_rocm_smi.go @@ -131,7 +131,7 @@ func (rsmi *ROCmSMI) Gather(acc telegraf.Accumulator) error { return gatherROCmSMI(data, acc) } -func (rsmi *ROCmSMI) Stop() {} +func (*ROCmSMI) Stop() {} func (rsmi *ROCmSMI) pollROCmSMI() ([]byte, error) { // Construct and execute metrics query, there currently exist (ROCm v4.3.x) a "-a" option diff --git a/plugins/inputs/amqp_consumer/amqp_consumer.go b/plugins/inputs/amqp_consumer/amqp_consumer.go index 448efad87d987..e5a32eab5b264 100644 --- a/plugins/inputs/amqp_consumer/amqp_consumer.go +++ b/plugins/inputs/amqp_consumer/amqp_consumer.go @@ -64,11 +64,11 @@ type AMQPConsumer struct { decoder internal.ContentDecoder } -func (a *externalAuth) Mechanism() string { +func (*externalAuth) Mechanism() string { return "EXTERNAL" } -func (a *externalAuth) Response() string { +func (*externalAuth) Response() string { return "\000" } @@ -175,7 +175,7 @@ func (a *AMQPConsumer) Start(acc telegraf.Accumulator) error { return nil } -func (a *AMQPConsumer) Gather(_ telegraf.Accumulator) error { +func (*AMQPConsumer) Gather(_ telegraf.Accumulator) error { return nil } diff --git a/plugins/inputs/apache/apache.go b/plugins/inputs/apache/apache.go index 6263404faa549..cb53f2b668ad5 100644 --- a/plugins/inputs/apache/apache.go +++ b/plugins/inputs/apache/apache.go @@ -120,7 +120,7 @@ func (n *Apache) gatherURL(addr *url.URL, acc telegraf.Accumulator) error { switch key { case "Scoreboard": - for field, value := range n.gatherScores(part) { + for field, value := range gatherScores(part) { fields[field] = value } default: @@ -137,7 +137,7 @@ func (n *Apache) gatherURL(addr *url.URL, acc telegraf.Accumulator) error { return nil } -func (n *Apache) gatherScores(data string) map[string]interface{} { +func gatherScores(data string) map[string]interface{} { var waiting, open = 0, 0 var s, r, w, k, d, c, l, g, i = 0, 0, 0, 0, 0, 0, 0, 0, 0 diff --git a/plugins/inputs/azure_monitor/azure_monitor.go b/plugins/inputs/azure_monitor/azure_monitor.go index 93b3627bf47d4..a53c8710e40d4 100644 --- a/plugins/inputs/azure_monitor/azure_monitor.go +++ b/plugins/inputs/azure_monitor/azure_monitor.go @@ -58,7 +58,7 @@ type azureClientsCreator interface { //go:embed sample.conf var sampleConfig string -func (am *AzureMonitor) SampleConfig() string { +func (*AzureMonitor) SampleConfig() string { return sampleConfig } @@ -170,7 +170,7 @@ func (am *AzureMonitor) setReceiver() error { return err } -func (acm *azureClientsManager) createAzureClients( +func (*azureClientsManager) createAzureClients( subscriptionID, clientID, clientSecret, tenantID string, clientOptions azcore.ClientOptions, ) (*receiver.AzureClients, error) { diff --git a/plugins/inputs/azure_monitor/azure_monitor_test.go b/plugins/inputs/azure_monitor/azure_monitor_test.go index e51b616baad0b..421b9282493b9 100644 --- a/plugins/inputs/azure_monitor/azure_monitor_test.go +++ b/plugins/inputs/azure_monitor/azure_monitor_test.go @@ -27,7 +27,7 @@ type mockAzureMetricDefinitionsClient struct{} type mockAzureMetricsClient struct{} -func (mam *mockAzureClientsManager) createAzureClients(_, _, _, _ string, _ azcore.ClientOptions) (*receiver.AzureClients, error) { +func (*mockAzureClientsManager) createAzureClients(_, _, _, _ string, _ azcore.ClientOptions) (*receiver.AzureClients, error) { return &receiver.AzureClients{ Ctx: context.Background(), ResourcesClient: &mockAzureResourcesClient{}, @@ -36,7 +36,7 @@ func (mam *mockAzureClientsManager) createAzureClients(_, _, _, _ string, _ azco }, nil } -func (marc *mockAzureResourcesClient) List(_ context.Context, _ *armresources.ClientListOptions) ([]*armresources.ClientListResponse, error) { +func (*mockAzureResourcesClient) List(_ context.Context, _ *armresources.ClientListOptions) ([]*armresources.ClientListResponse, error) { var responses []*armresources.ClientListResponse file, err := os.ReadFile("testdata/json/azure_resources_response.json") @@ -59,7 +59,7 @@ func (marc *mockAzureResourcesClient) List(_ context.Context, _ *armresources.Cl return responses, nil } -func (marc *mockAzureResourcesClient) ListByResourceGroup( +func (*mockAzureResourcesClient) ListByResourceGroup( _ context.Context, resourceGroup string, _ *armresources.ClientListByResourceGroupOptions) ([]*armresources.ClientListByResourceGroupResponse, error) { @@ -105,7 +105,7 @@ func (marc *mockAzureResourcesClient) ListByResourceGroup( return nil, errors.New("resource group was not found") } -func (mamdc *mockAzureMetricDefinitionsClient) List( +func (*mockAzureMetricDefinitionsClient) List( _ context.Context, resourceID string, _ *armmonitor.MetricDefinitionsClientListOptions) (armmonitor.MetricDefinitionsClientListResponse, error) { @@ -146,7 +146,7 @@ func (mamdc *mockAzureMetricDefinitionsClient) List( return armmonitor.MetricDefinitionsClientListResponse{}, errors.New("resource ID was not found") } -func (mamc *mockAzureMetricsClient) List( +func (*mockAzureMetricsClient) List( _ context.Context, resourceID string, _ *armmonitor.MetricsClientListOptions) (armmonitor.MetricsClientListResponse, error) { diff --git a/plugins/inputs/bcache/bcache.go b/plugins/inputs/bcache/bcache.go index 37114a2d921a1..8a9c79fc19c22 100644 --- a/plugins/inputs/bcache/bcache.go +++ b/plugins/inputs/bcache/bcache.go @@ -53,7 +53,7 @@ func (b *Bcache) Gather(acc telegraf.Accumulator) error { continue } } - if err := b.gatherBcache(bdev, acc); err != nil { + if err := gatherBcache(bdev, acc); err != nil { return fmt.Errorf("gathering bcache failed: %w", err) } } @@ -97,7 +97,7 @@ func prettyToBytes(v string) uint64 { return uint64(result) } -func (b *Bcache) gatherBcache(bdev string, acc telegraf.Accumulator) error { +func gatherBcache(bdev string, acc telegraf.Accumulator) error { tags := getTags(bdev) metrics, err := filepath.Glob(bdev + "/stats_total/*") if err != nil { diff --git a/plugins/inputs/bond/bond.go b/plugins/inputs/bond/bond.go index a5c244ad3ce3e..6fdb33ba6ab8b 100644 --- a/plugins/inputs/bond/bond.go +++ b/plugins/inputs/bond/bond.go @@ -66,7 +66,7 @@ func (bond *Bond) Gather(acc telegraf.Accumulator) error { if err != nil { acc.AddError(err) } - bond.gatherSysDetails(bondName, files, acc) + gatherSysDetails(bondName, files, acc) } } return nil @@ -164,7 +164,7 @@ func (bond *Bond) readSysFiles(bondDir string) (sysFiles, error) { return output, nil } -func (bond *Bond) gatherSysDetails(bondName string, files sysFiles, acc telegraf.Accumulator) { +func gatherSysDetails(bondName string, files sysFiles, acc telegraf.Accumulator) { var slaves []string var adPortCount int diff --git a/plugins/inputs/bond/bond_test.go b/plugins/inputs/bond/bond_test.go index 18d5c71ace644..17d91c640d498 100644 --- a/plugins/inputs/bond/bond_test.go +++ b/plugins/inputs/bond/bond_test.go @@ -145,7 +145,7 @@ func TestGatherBondInterface(t *testing.T) { acc = testutil.Accumulator{} require.NoError(t, bond.gatherBondInterface("bondLACP", sampleTestLACP, &acc)) - bond.gatherSysDetails("bondLACP", sysFiles{ModeFile: sampleSysMode, SlaveFile: sampleSysSlaves, ADPortsFile: sampleSysAdPorts}, &acc) + gatherSysDetails("bondLACP", sysFiles{ModeFile: sampleSysMode, SlaveFile: sampleSysSlaves, ADPortsFile: sampleSysAdPorts}, &acc) acc.AssertContainsTaggedFields(t, "bond", map[string]interface{}{"status": 1}, map[string]string{"bond": "bondLACP"}) acc.AssertContainsTaggedFields( t, @@ -169,7 +169,7 @@ func TestGatherBondInterface(t *testing.T) { acc = testutil.Accumulator{} require.NoError(t, bond.gatherBondInterface("bondLACPUpDown", sampleTestLACPFirstUpSecondDown, &acc)) - bond.gatherSysDetails("bondLACPUpDown", sysFiles{ModeFile: sampleSysMode, SlaveFile: sampleSysSlaves, ADPortsFile: sampleSysAdPorts}, &acc) + gatherSysDetails("bondLACPUpDown", sysFiles{ModeFile: sampleSysMode, SlaveFile: sampleSysSlaves, ADPortsFile: sampleSysAdPorts}, &acc) acc.AssertContainsTaggedFields(t, "bond", map[string]interface{}{"status": 1}, map[string]string{"bond": "bondLACPUpDown"}) acc.AssertContainsTaggedFields( t, diff --git a/plugins/inputs/burrow/burrow.go b/plugins/inputs/burrow/burrow.go index 0cdf8a00bf8b9..05c2f84ed9f3b 100644 --- a/plugins/inputs/burrow/burrow.go +++ b/plugins/inputs/burrow/burrow.go @@ -289,14 +289,14 @@ func (b *Burrow) gatherTopics(guard chan struct{}, src *url.URL, cluster string, return } - b.genTopicMetrics(tr, cluster, topic, acc) + genTopicMetrics(tr, cluster, topic, acc) }(topic) } wg.Wait() } -func (b *Burrow) genTopicMetrics(r *apiResponse, cluster, topic string, acc telegraf.Accumulator) { +func genTopicMetrics(r *apiResponse, cluster, topic string, acc telegraf.Accumulator) { for i, offset := range r.Offsets { tags := map[string]string{ "cluster": cluster, @@ -346,7 +346,7 @@ func (b *Burrow) gatherGroups(guard chan struct{}, src *url.URL, cluster string, return } - b.genGroupStatusMetrics(gr, cluster, group, acc) + genGroupStatusMetrics(gr, cluster, group, acc) b.genGroupLagMetrics(gr, cluster, group, acc) }(group) } @@ -354,7 +354,7 @@ func (b *Burrow) gatherGroups(guard chan struct{}, src *url.URL, cluster string, wg.Wait() } -func (b *Burrow) genGroupStatusMetrics(r *apiResponse, cluster, group string, acc telegraf.Accumulator) { +func genGroupStatusMetrics(r *apiResponse, cluster, group string, acc telegraf.Accumulator) { partitionCount := r.Status.PartitionCount if partitionCount == 0 { partitionCount = len(r.Status.Partitions) diff --git a/plugins/inputs/cisco_telemetry_mdt/cisco_telemetry_mdt.go b/plugins/inputs/cisco_telemetry_mdt/cisco_telemetry_mdt.go index b364c6e914f64..481e60a96a9c6 100644 --- a/plugins/inputs/cisco_telemetry_mdt/cisco_telemetry_mdt.go +++ b/plugins/inputs/cisco_telemetry_mdt/cisco_telemetry_mdt.go @@ -218,7 +218,7 @@ func (c *CiscoTelemetryMDT) Start(acc telegraf.Accumulator) error { return nil } -func (c *CiscoTelemetryMDT) Gather(_ telegraf.Accumulator) error { +func (*CiscoTelemetryMDT) Gather(telegraf.Accumulator) error { return nil } @@ -541,7 +541,7 @@ func (c *CiscoTelemetryMDT) parseKeyField(tags map[string]string, field *telemet } } -func (c *CiscoTelemetryMDT) parseRib(grouper *metric.SeriesGrouper, field *telemetry.TelemetryField, +func parseRib(grouper *metric.SeriesGrouper, field *telemetry.TelemetryField, encodingPath string, tags map[string]string, timestamp time.Time) { // RIB measurement := encodingPath @@ -574,7 +574,7 @@ func (c *CiscoTelemetryMDT) parseRib(grouper *metric.SeriesGrouper, field *telem } } -func (c *CiscoTelemetryMDT) parseMicroburst(grouper *metric.SeriesGrouper, field *telemetry.TelemetryField, +func parseMicroburst(grouper *metric.SeriesGrouper, field *telemetry.TelemetryField, encodingPath string, tags map[string]string, timestamp time.Time) { var nxMicro *telemetry.TelemetryField var nxMicro1 *telemetry.TelemetryField @@ -623,12 +623,12 @@ func (c *CiscoTelemetryMDT) parseClassAttributeField(grouper *metric.SeriesGroup isDme := strings.Contains(encodingPath, "sys/") if encodingPath == "rib" { // handle native data path rib - c.parseRib(grouper, field, encodingPath, tags, timestamp) + parseRib(grouper, field, encodingPath, tags, timestamp) return } if encodingPath == "microburst" { // dump microburst - c.parseMicroburst(grouper, field, encodingPath, tags, timestamp) + parseMicroburst(grouper, field, encodingPath, tags, timestamp) return } if field == nil || !isDme || len(field.Fields) == 0 || len(field.Fields[0].Fields) == 0 || len(field.Fields[0].Fields[0].Fields) == 0 { diff --git a/plugins/inputs/clickhouse/clickhouse.go b/plugins/inputs/clickhouse/clickhouse.go index 73a8e39c623bb..11ec30251168c 100644 --- a/plugins/inputs/clickhouse/clickhouse.go +++ b/plugins/inputs/clickhouse/clickhouse.go @@ -210,7 +210,7 @@ func (ch *ClickHouse) commonMetrics(acc telegraf.Accumulator, conn *connect, met Value float64 `json:"value"` } - tags := ch.makeDefaultTags(conn) + tags := makeDefaultTags(conn) fields := make(map[string]interface{}) if commonMetricsIsFloat[metric] { @@ -241,7 +241,7 @@ func (ch *ClickHouse) zookeeper(acc telegraf.Accumulator, conn *connect) error { if err := ch.execQuery(conn.url, systemZookeeperExistsSQL, &zkExists); err != nil { return err } - tags := ch.makeDefaultTags(conn) + tags := makeDefaultTags(conn) if len(zkExists) > 0 && zkExists[0].ZkExists > 0 { var zkRootNodes []struct { @@ -270,7 +270,7 @@ func (ch *ClickHouse) replicationQueue(acc telegraf.Accumulator, conn *connect) return err } - tags := ch.makeDefaultTags(conn) + tags := makeDefaultTags(conn) if len(replicationQueueExists) > 0 && replicationQueueExists[0].ReplicationQueueExists > 0 { var replicationTooManyTries []struct { @@ -301,7 +301,7 @@ func (ch *ClickHouse) detachedParts(acc telegraf.Accumulator, conn *connect) err } if len(detachedParts) > 0 { - tags := ch.makeDefaultTags(conn) + tags := makeDefaultTags(conn) acc.AddFields("clickhouse_detached_parts", map[string]interface{}{ "detached_parts": uint64(detachedParts[0].DetachedParts), @@ -323,7 +323,7 @@ func (ch *ClickHouse) dictionaries(acc telegraf.Accumulator, conn *connect) erro } for _, dict := range brokenDictionaries { - tags := ch.makeDefaultTags(conn) + tags := makeDefaultTags(conn) isLoaded := uint64(1) if dict.Status != "LOADED" { @@ -356,7 +356,7 @@ func (ch *ClickHouse) mutations(acc telegraf.Accumulator, conn *connect) error { } if len(mutationsStatus) > 0 { - tags := ch.makeDefaultTags(conn) + tags := makeDefaultTags(conn) acc.AddFields("clickhouse_mutations", map[string]interface{}{ @@ -384,7 +384,7 @@ func (ch *ClickHouse) disks(acc telegraf.Accumulator, conn *connect) error { } for _, disk := range disksStatus { - tags := ch.makeDefaultTags(conn) + tags := makeDefaultTags(conn) tags["name"] = disk.Name tags["path"] = disk.Path @@ -413,7 +413,7 @@ func (ch *ClickHouse) processes(acc telegraf.Accumulator, conn *connect) error { } for _, process := range processesStats { - tags := ch.makeDefaultTags(conn) + tags := makeDefaultTags(conn) tags["query_type"] = process.QueryType acc.AddFields("clickhouse_processes", @@ -448,7 +448,7 @@ func (ch *ClickHouse) textLog(acc telegraf.Accumulator, conn *connect) error { } for _, textLogItem := range textLogLast10MinMessages { - tags := ch.makeDefaultTags(conn) + tags := makeDefaultTags(conn) tags["level"] = textLogItem.Level acc.AddFields("clickhouse_text_log", map[string]interface{}{ @@ -473,7 +473,7 @@ func (ch *ClickHouse) tables(acc telegraf.Accumulator, conn *connect) error { if err := ch.execQuery(conn.url, systemPartsSQL, &parts); err != nil { return err } - tags := ch.makeDefaultTags(conn) + tags := makeDefaultTags(conn) for _, part := range parts { tags["table"] = part.Table @@ -490,7 +490,7 @@ func (ch *ClickHouse) tables(acc telegraf.Accumulator, conn *connect) error { return nil } -func (ch *ClickHouse) makeDefaultTags(conn *connect) map[string]string { +func makeDefaultTags(conn *connect) map[string]string { tags := map[string]string{ "source": conn.Hostname, } diff --git a/plugins/inputs/cloud_pubsub/cloud_pubsub.go b/plugins/inputs/cloud_pubsub/cloud_pubsub.go index 0f686b3e7f0df..d91c55f6683bf 100644 --- a/plugins/inputs/cloud_pubsub/cloud_pubsub.go +++ b/plugins/inputs/cloud_pubsub/cloud_pubsub.go @@ -152,7 +152,7 @@ func (ps *PubSub) Start(ac telegraf.Accumulator) error { } // Gather does nothing for this service input. -func (ps *PubSub) Gather(_ telegraf.Accumulator) error { +func (*PubSub) Gather(telegraf.Accumulator) error { return nil } diff --git a/plugins/inputs/cloud_pubsub_push/cloud_pubsub_push.go b/plugins/inputs/cloud_pubsub_push/cloud_pubsub_push.go index e745ee57eeb2c..d446d04e991bc 100644 --- a/plugins/inputs/cloud_pubsub_push/cloud_pubsub_push.go +++ b/plugins/inputs/cloud_pubsub_push/cloud_pubsub_push.go @@ -133,7 +133,7 @@ func (p *PubSubPush) Start(acc telegraf.Accumulator) error { return nil } -func (p *PubSubPush) Gather(_ telegraf.Accumulator) error { +func (*PubSubPush) Gather(telegraf.Accumulator) error { return nil } diff --git a/plugins/inputs/cloud_pubsub_push/cloud_pubsub_push_test.go b/plugins/inputs/cloud_pubsub_push/cloud_pubsub_push_test.go index 9e8aa07d1f3db..06d91190f97bc 100644 --- a/plugins/inputs/cloud_pubsub_push/cloud_pubsub_push_test.go +++ b/plugins/inputs/cloud_pubsub_push/cloud_pubsub_push_test.go @@ -219,7 +219,7 @@ func TestServeHTTP(t *testing.T) { type testMetricMaker struct{} -func (tm *testMetricMaker) Name() string { +func (*testMetricMaker) Name() string { return "TestPlugin" } @@ -227,11 +227,11 @@ func (tm *testMetricMaker) LogName() string { return tm.Name() } -func (tm *testMetricMaker) MakeMetric(metric telegraf.Metric) telegraf.Metric { +func (*testMetricMaker) MakeMetric(metric telegraf.Metric) telegraf.Metric { return metric } -func (tm *testMetricMaker) Log() telegraf.Logger { +func (*testMetricMaker) Log() telegraf.Logger { return logger.New("test", "test", "") } diff --git a/plugins/inputs/cloudwatch/cloudwatch_test.go b/plugins/inputs/cloudwatch/cloudwatch_test.go index 4cf11fe5955db..602da9b460c5f 100644 --- a/plugins/inputs/cloudwatch/cloudwatch_test.go +++ b/plugins/inputs/cloudwatch/cloudwatch_test.go @@ -21,7 +21,7 @@ import ( type mockGatherCloudWatchClient struct{} -func (m *mockGatherCloudWatchClient) ListMetrics( +func (*mockGatherCloudWatchClient) ListMetrics( _ context.Context, params *cloudwatch.ListMetricsInput, _ ...func(*cloudwatch.Options), @@ -56,7 +56,7 @@ func (m *mockGatherCloudWatchClient) ListMetrics( return response, nil } -func (m *mockGatherCloudWatchClient) GetMetricData( +func (*mockGatherCloudWatchClient) GetMetricData( _ context.Context, params *cloudwatch.GetMetricDataInput, _ ...func(*cloudwatch.Options), @@ -307,10 +307,10 @@ func TestGather_MultipleNamespaces(t *testing.T) { type mockSelectMetricsCloudWatchClient struct{} -func (m *mockSelectMetricsCloudWatchClient) ListMetrics( - _ context.Context, - _ *cloudwatch.ListMetricsInput, - _ ...func(*cloudwatch.Options), +func (*mockSelectMetricsCloudWatchClient) ListMetrics( + context.Context, + *cloudwatch.ListMetricsInput, + ...func(*cloudwatch.Options), ) (*cloudwatch.ListMetricsOutput, error) { metrics := make([]types.Metric, 0) // 4 metrics are available @@ -358,10 +358,10 @@ func (m *mockSelectMetricsCloudWatchClient) ListMetrics( return result, nil } -func (m *mockSelectMetricsCloudWatchClient) GetMetricData( - _ context.Context, - _ *cloudwatch.GetMetricDataInput, - _ ...func(*cloudwatch.Options), +func (*mockSelectMetricsCloudWatchClient) GetMetricData( + context.Context, + *cloudwatch.GetMetricDataInput, + ...func(*cloudwatch.Options), ) (*cloudwatch.GetMetricDataOutput, error) { return nil, nil } diff --git a/plugins/inputs/cloudwatch_metric_streams/cloudwatch_metric_streams.go b/plugins/inputs/cloudwatch_metric_streams/cloudwatch_metric_streams.go index a453932859139..6e824f50634a1 100644 --- a/plugins/inputs/cloudwatch_metric_streams/cloudwatch_metric_streams.go +++ b/plugins/inputs/cloudwatch_metric_streams/cloudwatch_metric_streams.go @@ -149,7 +149,7 @@ func (cms *CloudWatchMetricStreams) Start(acc telegraf.Accumulator) error { return nil } -func (cms *CloudWatchMetricStreams) Gather(_ telegraf.Accumulator) error { +func (*CloudWatchMetricStreams) Gather(telegraf.Accumulator) error { return nil } diff --git a/plugins/inputs/couchdb/couchdb.go b/plugins/inputs/couchdb/couchdb.go index f24bd795621f3..d0917df11c039 100644 --- a/plugins/inputs/couchdb/couchdb.go +++ b/plugins/inputs/couchdb/couchdb.go @@ -207,43 +207,43 @@ func (c *CouchDB) fetchAndInsertData(accumulator telegraf.Accumulator, host stri fields := make(map[string]interface{}, 31) // CouchDB meta stats: - c.generateFields(fields, "couchdb_auth_cache_misses", stats.Couchdb.AuthCacheMisses) - c.generateFields(fields, "couchdb_database_writes", stats.Couchdb.DatabaseWrites) - c.generateFields(fields, "couchdb_open_databases", stats.Couchdb.OpenDatabases) - c.generateFields(fields, "couchdb_auth_cache_hits", stats.Couchdb.AuthCacheHits) - c.generateFields(fields, "couchdb_request_time", requestTime) - c.generateFields(fields, "couchdb_database_reads", stats.Couchdb.DatabaseReads) - c.generateFields(fields, "couchdb_open_os_files", stats.Couchdb.OpenOsFiles) + generateFields(fields, "couchdb_auth_cache_misses", stats.Couchdb.AuthCacheMisses) + generateFields(fields, "couchdb_database_writes", stats.Couchdb.DatabaseWrites) + generateFields(fields, "couchdb_open_databases", stats.Couchdb.OpenDatabases) + generateFields(fields, "couchdb_auth_cache_hits", stats.Couchdb.AuthCacheHits) + generateFields(fields, "couchdb_request_time", requestTime) + generateFields(fields, "couchdb_database_reads", stats.Couchdb.DatabaseReads) + generateFields(fields, "couchdb_open_os_files", stats.Couchdb.OpenOsFiles) // http request methods stats: - c.generateFields(fields, "httpd_request_methods_put", httpdRequestMethodsPut) - c.generateFields(fields, "httpd_request_methods_get", httpdRequestMethodsGet) - c.generateFields(fields, "httpd_request_methods_copy", httpdRequestMethodsCopy) - c.generateFields(fields, "httpd_request_methods_delete", httpdRequestMethodsDelete) - c.generateFields(fields, "httpd_request_methods_post", httpdRequestMethodsPost) - c.generateFields(fields, "httpd_request_methods_head", httpdRequestMethodsHead) + generateFields(fields, "httpd_request_methods_put", httpdRequestMethodsPut) + generateFields(fields, "httpd_request_methods_get", httpdRequestMethodsGet) + generateFields(fields, "httpd_request_methods_copy", httpdRequestMethodsCopy) + generateFields(fields, "httpd_request_methods_delete", httpdRequestMethodsDelete) + generateFields(fields, "httpd_request_methods_post", httpdRequestMethodsPost) + generateFields(fields, "httpd_request_methods_head", httpdRequestMethodsHead) // status code stats: - c.generateFields(fields, "httpd_status_codes_200", httpdStatusCodesStatus200) - c.generateFields(fields, "httpd_status_codes_201", httpdStatusCodesStatus201) - c.generateFields(fields, "httpd_status_codes_202", httpdStatusCodesStatus202) - c.generateFields(fields, "httpd_status_codes_301", httpdStatusCodesStatus301) - c.generateFields(fields, "httpd_status_codes_304", httpdStatusCodesStatus304) - c.generateFields(fields, "httpd_status_codes_400", httpdStatusCodesStatus400) - c.generateFields(fields, "httpd_status_codes_401", httpdStatusCodesStatus401) - c.generateFields(fields, "httpd_status_codes_403", httpdStatusCodesStatus403) - c.generateFields(fields, "httpd_status_codes_404", httpdStatusCodesStatus404) - c.generateFields(fields, "httpd_status_codes_405", httpdStatusCodesStatus405) - c.generateFields(fields, "httpd_status_codes_409", httpdStatusCodesStatus409) - c.generateFields(fields, "httpd_status_codes_412", httpdStatusCodesStatus412) - c.generateFields(fields, "httpd_status_codes_500", httpdStatusCodesStatus500) + generateFields(fields, "httpd_status_codes_200", httpdStatusCodesStatus200) + generateFields(fields, "httpd_status_codes_201", httpdStatusCodesStatus201) + generateFields(fields, "httpd_status_codes_202", httpdStatusCodesStatus202) + generateFields(fields, "httpd_status_codes_301", httpdStatusCodesStatus301) + generateFields(fields, "httpd_status_codes_304", httpdStatusCodesStatus304) + generateFields(fields, "httpd_status_codes_400", httpdStatusCodesStatus400) + generateFields(fields, "httpd_status_codes_401", httpdStatusCodesStatus401) + generateFields(fields, "httpd_status_codes_403", httpdStatusCodesStatus403) + generateFields(fields, "httpd_status_codes_404", httpdStatusCodesStatus404) + generateFields(fields, "httpd_status_codes_405", httpdStatusCodesStatus405) + generateFields(fields, "httpd_status_codes_409", httpdStatusCodesStatus409) + generateFields(fields, "httpd_status_codes_412", httpdStatusCodesStatus412) + generateFields(fields, "httpd_status_codes_500", httpdStatusCodesStatus500) // httpd stats: - c.generateFields(fields, "httpd_clients_requesting_changes", stats.Httpd.ClientsRequestingChanges) - c.generateFields(fields, "httpd_temporary_view_reads", stats.Httpd.TemporaryViewReads) - c.generateFields(fields, "httpd_requests", stats.Httpd.Requests) - c.generateFields(fields, "httpd_bulk_requests", stats.Httpd.BulkRequests) - c.generateFields(fields, "httpd_view_reads", stats.Httpd.ViewReads) + generateFields(fields, "httpd_clients_requesting_changes", stats.Httpd.ClientsRequestingChanges) + generateFields(fields, "httpd_temporary_view_reads", stats.Httpd.TemporaryViewReads) + generateFields(fields, "httpd_requests", stats.Httpd.Requests) + generateFields(fields, "httpd_bulk_requests", stats.Httpd.BulkRequests) + generateFields(fields, "httpd_view_reads", stats.Httpd.ViewReads) tags := map[string]string{ "server": host, @@ -252,7 +252,7 @@ func (c *CouchDB) fetchAndInsertData(accumulator telegraf.Accumulator, host stri return nil } -func (c *CouchDB) generateFields(fields map[string]interface{}, prefix string, obj metaData) { +func generateFields(fields map[string]interface{}, prefix string, obj metaData) { if obj.Value != nil { fields[prefix+"_value"] = *obj.Value } diff --git a/plugins/inputs/csgo/csgo.go b/plugins/inputs/csgo/csgo.go index ed91a39ceeb0a..718139c53822d 100644 --- a/plugins/inputs/csgo/csgo.go +++ b/plugins/inputs/csgo/csgo.go @@ -61,7 +61,7 @@ func (s *CSGO) Gather(acc telegraf.Accumulator) error { } // Generate the metric and add it to the accumulator - m, err := s.parseResponse(addr, response, t) + m, err := parseResponse(addr, response, t) if err != nil { acc.AddError(err) return @@ -74,7 +74,7 @@ func (s *CSGO) Gather(acc telegraf.Accumulator) error { return nil } -func (s *CSGO) parseResponse(addr, response string, t time.Time) (telegraf.Metric, error) { +func parseResponse(addr, response string, t time.Time) (telegraf.Metric, error) { rows := strings.Split(response, "\n") if len(rows) < 2 { return nil, errors.New("bad response") diff --git a/plugins/inputs/ctrlx_datalayer/ctrlx_datalayer.go b/plugins/inputs/ctrlx_datalayer/ctrlx_datalayer.go index c6c4791597e39..b09cb4398b083 100644 --- a/plugins/inputs/ctrlx_datalayer/ctrlx_datalayer.go +++ b/plugins/inputs/ctrlx_datalayer/ctrlx_datalayer.go @@ -131,7 +131,7 @@ func (c *CtrlXDataLayer) Start(acc telegraf.Accumulator) error { return nil } -func (c *CtrlXDataLayer) Gather(_ telegraf.Accumulator) error { +func (*CtrlXDataLayer) Gather(telegraf.Accumulator) error { // Metrics are sent to the accumulator asynchronously in worker thread. So nothing to do here. return nil } diff --git a/plugins/inputs/dcos/client.go b/plugins/inputs/dcos/client.go index 1b1af7d818e69..d89a023d0798f 100644 --- a/plugins/inputs/dcos/client.go +++ b/plugins/inputs/dcos/client.go @@ -133,7 +133,7 @@ func (c *clusterClient) setToken(token string) { } func (c *clusterClient) login(ctx context.Context, sa *serviceAccount) (*authToken, error) { - token, err := c.createLoginToken(sa) + token, err := createLoginToken(sa) if err != nil { return nil, err } @@ -316,7 +316,7 @@ func (c *clusterClient) toURL(path string) string { return clusterURL.String() } -func (c *clusterClient) createLoginToken(sa *serviceAccount) (string, error) { +func createLoginToken(sa *serviceAccount) (string, error) { token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims{ UID: sa.accountID, RegisteredClaims: jwt.RegisteredClaims{ diff --git a/plugins/inputs/dcos/creds.go b/plugins/inputs/dcos/creds.go index 411c3c7329174..8b195bc98ba94 100644 --- a/plugins/inputs/dcos/creds.go +++ b/plugins/inputs/dcos/creds.go @@ -59,14 +59,14 @@ func (c *tokenCreds) token(_ context.Context, _ client) (string, error) { return token, nil } -func (c *tokenCreds) isExpired() bool { +func (*tokenCreds) isExpired() bool { return true } -func (c *nullCreds) token(_ context.Context, _ client) (string, error) { +func (*nullCreds) token(context.Context, client) (string, error) { return "", nil } -func (c *nullCreds) isExpired() bool { +func (*nullCreds) isExpired() bool { return true } diff --git a/plugins/inputs/dcos/dcos.go b/plugins/inputs/dcos/dcos.go index 1b099fb3b1ef4..098954b177fcc 100644 --- a/plugins/inputs/dcos/dcos.go +++ b/plugins/inputs/dcos/dcos.go @@ -131,7 +131,7 @@ func (d *DCOS) gatherNode(ctx context.Context, acc telegraf.Accumulator, cluster acc.AddError(err) return } - d.addNodeMetrics(acc, cluster, m) + addNodeMetrics(acc, cluster, m) }() d.gatherContainers(ctx, acc, cluster, node) @@ -160,7 +160,7 @@ func (d *DCOS) gatherContainers(ctx context.Context, acc telegraf.Accumulator, c acc.AddError(err) return } - d.addContainerMetrics(acc, cluster, m) + addContainerMetrics(acc, cluster, m) }(container.ID) } @@ -177,14 +177,14 @@ func (d *DCOS) gatherContainers(ctx context.Context, acc telegraf.Accumulator, c acc.AddError(err) return } - d.addAppMetrics(acc, cluster, m) + addAppMetrics(acc, cluster, m) }(container.ID) } } wg.Wait() } -func (d *DCOS) createPoints(m *metrics) []*point { +func createPoints(m *metrics) []*point { points := make(map[string]*point) for _, dp := range m.Datapoints { fieldKey := strings.ReplaceAll(dp.Name, ".", "_") @@ -244,10 +244,10 @@ func (d *DCOS) createPoints(m *metrics) []*point { return results } -func (d *DCOS) addMetrics(acc telegraf.Accumulator, cluster, mname string, m *metrics, tagDimensions []string) { +func addMetrics(acc telegraf.Accumulator, cluster, mname string, m *metrics, tagDimensions []string) { tm := time.Now() - points := d.createPoints(m) + points := createPoints(m) for _, p := range points { tags := make(map[string]string) @@ -266,16 +266,16 @@ func (d *DCOS) addMetrics(acc telegraf.Accumulator, cluster, mname string, m *me } } -func (d *DCOS) addNodeMetrics(acc telegraf.Accumulator, cluster string, m *metrics) { - d.addMetrics(acc, cluster, "dcos_node", m, nodeDimensions) +func addNodeMetrics(acc telegraf.Accumulator, cluster string, m *metrics) { + addMetrics(acc, cluster, "dcos_node", m, nodeDimensions) } -func (d *DCOS) addContainerMetrics(acc telegraf.Accumulator, cluster string, m *metrics) { - d.addMetrics(acc, cluster, "dcos_container", m, containerDimensions) +func addContainerMetrics(acc telegraf.Accumulator, cluster string, m *metrics) { + addMetrics(acc, cluster, "dcos_container", m, containerDimensions) } -func (d *DCOS) addAppMetrics(acc telegraf.Accumulator, cluster string, m *metrics) { - d.addMetrics(acc, cluster, "dcos_app", m, appDimensions) +func addAppMetrics(acc telegraf.Accumulator, cluster string, m *metrics) { + addMetrics(acc, cluster, "dcos_app", m, appDimensions) } func (d *DCOS) initialize() error { diff --git a/plugins/inputs/dcos/dcos_test.go b/plugins/inputs/dcos/dcos_test.go index 4915b73c6f8d7..60e32e0f0f6a0 100644 --- a/plugins/inputs/dcos/dcos_test.go +++ b/plugins/inputs/dcos/dcos_test.go @@ -196,8 +196,7 @@ func TestAddNodeMetrics(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var acc testutil.Accumulator - dcos := &DCOS{} - dcos.addNodeMetrics(&acc, "a", tt.metrics) + addNodeMetrics(&acc, "a", tt.metrics) for i, ok := range tt.check(&acc) { require.Truef(t, ok, "Index was not true: %d", i) } @@ -267,8 +266,7 @@ func TestAddContainerMetrics(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var acc testutil.Accumulator - dcos := &DCOS{} - dcos.addContainerMetrics(&acc, "a", tt.metrics) + addContainerMetrics(&acc, "a", tt.metrics) for i, ok := range tt.check(&acc) { require.Truef(t, ok, "Index was not true: %d", i) } @@ -341,8 +339,7 @@ func TestAddAppMetrics(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var acc testutil.Accumulator - dcos := &DCOS{} - dcos.addAppMetrics(&acc, "a", tt.metrics) + addAppMetrics(&acc, "a", tt.metrics) for i, ok := range tt.check(&acc) { require.Truef(t, ok, "Index was not true: %d", i) } diff --git a/plugins/inputs/docker_log/docker_log.go b/plugins/inputs/docker_log/docker_log.go index abc0e489e01d5..8e6eb1ee0c85a 100644 --- a/plugins/inputs/docker_log/docker_log.go +++ b/plugins/inputs/docker_log/docker_log.go @@ -128,7 +128,7 @@ func (d *DockerLogs) Init() error { } // Start is a noop which is required for a *DockerLogs to implement the telegraf.ServiceInput interface -func (d *DockerLogs) Start(telegraf.Accumulator) error { +func (*DockerLogs) Start(telegraf.Accumulator) error { return nil } diff --git a/plugins/inputs/docker_log/docker_log_test.go b/plugins/inputs/docker_log/docker_log_test.go index 95dedd43bf42c..ff3b0b808ebf8 100644 --- a/plugins/inputs/docker_log/docker_log_test.go +++ b/plugins/inputs/docker_log/docker_log_test.go @@ -40,7 +40,7 @@ type response struct { io.Reader } -func (r *response) Close() error { +func (*response) Close() error { return nil } diff --git a/plugins/inputs/dovecot/dovecot.go b/plugins/inputs/dovecot/dovecot.go index f1564327507b8..ea4864f04ca3b 100644 --- a/plugins/inputs/dovecot/dovecot.go +++ b/plugins/inputs/dovecot/dovecot.go @@ -56,7 +56,7 @@ func (d *Dovecot) Gather(acc telegraf.Accumulator) error { wg.Add(1) go func(s string, f string) { defer wg.Done() - acc.AddError(d.gatherServer(s, acc, d.Type, f)) + acc.AddError(gatherServer(s, acc, d.Type, f)) }(server, filter) } } @@ -65,7 +65,7 @@ func (d *Dovecot) Gather(acc telegraf.Accumulator) error { return nil } -func (d *Dovecot) gatherServer(addr string, acc telegraf.Accumulator, qtype, filter string) error { +func gatherServer(addr string, acc telegraf.Accumulator, qtype, filter string) error { var proto string if strings.HasPrefix(addr, "/") { diff --git a/plugins/inputs/ecs/ecs.go b/plugins/inputs/ecs/ecs.go index b537c1fdc09c4..712ca3d439df1 100644 --- a/plugins/inputs/ecs/ecs.go +++ b/plugins/inputs/ecs/ecs.go @@ -68,7 +68,7 @@ func (ecs *Ecs) Gather(acc telegraf.Accumulator) error { } // accumulate metrics - ecs.accTask(task, taskTags, acc) + accTask(task, taskTags, acc) ecs.accContainers(task, taskTags, acc) return nil @@ -137,7 +137,7 @@ func resolveEndpoint(ecs *Ecs) { ecs.metadataVersion = 2 } -func (ecs *Ecs) accTask(task *ecsTask, tags map[string]string, acc telegraf.Accumulator) { +func accTask(task *ecsTask, tags map[string]string, acc telegraf.Accumulator) { taskFields := map[string]interface{}{ "desired_status": task.DesiredStatus, "known_status": task.KnownStatus, diff --git a/plugins/inputs/elasticsearch/elasticsearch.go b/plugins/inputs/elasticsearch/elasticsearch.go index 3cbc7fd2e3b48..b8f51dbc29502 100644 --- a/plugins/inputs/elasticsearch/elasticsearch.go +++ b/plugins/inputs/elasticsearch/elasticsearch.go @@ -159,7 +159,7 @@ func (e *Elasticsearch) Init() error { return nil } -func (e *Elasticsearch) Start(_ telegraf.Accumulator) error { +func (*Elasticsearch) Start(telegraf.Accumulator) error { return nil } diff --git a/plugins/inputs/elasticsearch_query/elasticsearch_query.go b/plugins/inputs/elasticsearch_query/elasticsearch_query.go index 0e06b4b049489..525a9a061d9d7 100644 --- a/plugins/inputs/elasticsearch_query/elasticsearch_query.go +++ b/plugins/inputs/elasticsearch_query/elasticsearch_query.go @@ -89,7 +89,7 @@ func (e *ElasticsearchQuery) Init() error { return nil } -func (e *ElasticsearchQuery) Start(_ telegraf.Accumulator) error { +func (*ElasticsearchQuery) Start(telegraf.Accumulator) error { return nil } diff --git a/plugins/inputs/ethtool/ethtool_linux.go b/plugins/inputs/ethtool/ethtool_linux.go index 4629a8500066b..0116fc3d94e9a 100644 --- a/plugins/inputs/ethtool/ethtool_linux.go +++ b/plugins/inputs/ethtool/ethtool_linux.go @@ -269,15 +269,15 @@ func (c *commandEthtool) init() error { return nil } -func (c *commandEthtool) driverName(intf namespacedInterface) (driver string, err error) { +func (*commandEthtool) driverName(intf namespacedInterface) (driver string, err error) { return intf.namespace.driverName(intf) } -func (c *commandEthtool) stats(intf namespacedInterface) (stats map[string]uint64, err error) { +func (*commandEthtool) stats(intf namespacedInterface) (stats map[string]uint64, err error) { return intf.namespace.stats(intf) } -func (c *commandEthtool) get(intf namespacedInterface) (stats map[string]uint64, err error) { +func (*commandEthtool) get(intf namespacedInterface) (stats map[string]uint64, err error) { return intf.namespace.get(intf) } diff --git a/plugins/inputs/ethtool/ethtool_test.go b/plugins/inputs/ethtool/ethtool_test.go index 0088a3f3c93de..64e0c848bf789 100644 --- a/plugins/inputs/ethtool/ethtool_test.go +++ b/plugins/inputs/ethtool/ethtool_test.go @@ -35,19 +35,19 @@ func (n *namespaceMock) name() string { return n.namespaceName } -func (n *namespaceMock) interfaces() ([]namespacedInterface, error) { +func (*namespaceMock) interfaces() ([]namespacedInterface, error) { return nil, errors.New("it is a test bug to invoke this function") } -func (n *namespaceMock) driverName(_ namespacedInterface) (string, error) { +func (*namespaceMock) driverName(_ namespacedInterface) (string, error) { return "", errors.New("it is a test bug to invoke this function") } -func (n *namespaceMock) stats(_ namespacedInterface) (map[string]uint64, error) { +func (*namespaceMock) stats(_ namespacedInterface) (map[string]uint64, error) { return nil, errors.New("it is a test bug to invoke this function") } -func (n *namespaceMock) get(_ namespacedInterface) (map[string]uint64, error) { +func (*namespaceMock) get(_ namespacedInterface) (map[string]uint64, error) { return nil, errors.New("it is a test bug to invoke this function") } @@ -55,7 +55,7 @@ type commandEthtoolMock struct { interfaceMap map[string]*interfaceMock } -func (c *commandEthtoolMock) init() error { +func (*commandEthtoolMock) init() error { // Not required for test mock return nil } diff --git a/plugins/inputs/exec/exec.go b/plugins/inputs/exec/exec.go index efe8c29687a29..66dc7eea0f87b 100644 --- a/plugins/inputs/exec/exec.go +++ b/plugins/inputs/exec/exec.go @@ -59,7 +59,7 @@ func (*Exec) SampleConfig() string { return sampleConfig } -func (e *Exec) Init() error { +func (*Exec) Init() error { return nil } @@ -121,7 +121,7 @@ func (e *Exec) Gather(acc telegraf.Accumulator) error { return nil } -func (c commandRunner) truncate(buf bytes.Buffer) bytes.Buffer { +func truncate(buf bytes.Buffer) bytes.Buffer { // Limit the number of bytes. didTruncate := false if buf.Len() > maxStderrBytes { diff --git a/plugins/inputs/exec/exec_test.go b/plugins/inputs/exec/exec_test.go index def86cb1c9897..eb605f8fd0cde 100644 --- a/plugins/inputs/exec/exec_test.go +++ b/plugins/inputs/exec/exec_test.go @@ -302,10 +302,9 @@ func TestTruncate(t *testing.T) { }, } - c := commandRunner{} for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - res := c.truncate(*tt.bufF()) + res := truncate(*tt.bufF()) require.Equal(t, tt.expF().Bytes(), res.Bytes()) }) } diff --git a/plugins/inputs/exec/run_notwinodws.go b/plugins/inputs/exec/run_notwindows.go similarity index 96% rename from plugins/inputs/exec/run_notwinodws.go rename to plugins/inputs/exec/run_notwindows.go index 0fdaf2e73eb37..fa346e590bf0e 100644 --- a/plugins/inputs/exec/run_notwinodws.go +++ b/plugins/inputs/exec/run_notwindows.go @@ -44,7 +44,7 @@ func (c commandRunner) run( out = removeWindowsCarriageReturns(out) if stderr.Len() > 0 && !c.debug { stderr = removeWindowsCarriageReturns(stderr) - stderr = c.truncate(stderr) + stderr = truncate(stderr) } return out.Bytes(), stderr.Bytes(), runErr diff --git a/plugins/inputs/exec/run_windows.go b/plugins/inputs/exec/run_windows.go index fad0160b3119a..f7acc7c5fb712 100644 --- a/plugins/inputs/exec/run_windows.go +++ b/plugins/inputs/exec/run_windows.go @@ -46,7 +46,7 @@ func (c commandRunner) run( out = removeWindowsCarriageReturns(out) if stderr.Len() > 0 && !c.debug { stderr = removeWindowsCarriageReturns(stderr) - stderr = c.truncate(stderr) + stderr = truncate(stderr) } return out.Bytes(), stderr.Bytes(), runErr diff --git a/plugins/inputs/execd/execd_test.go b/plugins/inputs/execd/execd_test.go index 6368e2d21746f..d4dfcf00c232f 100644 --- a/plugins/inputs/execd/execd_test.go +++ b/plugins/inputs/execd/execd_test.go @@ -362,7 +362,7 @@ func readChanWithTimeout(t *testing.T, metrics chan telegraf.Metric, timeout tim type TestMetricMaker struct{} -func (tm *TestMetricMaker) Name() string { +func (*TestMetricMaker) Name() string { return "TestPlugin" } @@ -370,11 +370,11 @@ func (tm *TestMetricMaker) LogName() string { return tm.Name() } -func (tm *TestMetricMaker) MakeMetric(aMetric telegraf.Metric) telegraf.Metric { +func (*TestMetricMaker) MakeMetric(aMetric telegraf.Metric) telegraf.Metric { return aMetric } -func (tm *TestMetricMaker) Log() telegraf.Logger { +func (*TestMetricMaker) Log() telegraf.Logger { return logger.New("TestPlugin", "test", "") } diff --git a/plugins/inputs/execd/shim/input.go b/plugins/inputs/execd/shim/input.go index cf100256fe0b3..0b4ddf30891fc 100644 --- a/plugins/inputs/execd/shim/input.go +++ b/plugins/inputs/execd/shim/input.go @@ -8,16 +8,16 @@ type inputShim struct { } // LogName satisfies the MetricMaker interface -func (i inputShim) LogName() string { +func (inputShim) LogName() string { return "" } // MakeMetric satisfies the MetricMaker interface -func (i inputShim) MakeMetric(m telegraf.Metric) telegraf.Metric { +func (inputShim) MakeMetric(m telegraf.Metric) telegraf.Metric { return m // don't need to do anything to it. } // Log satisfies the MetricMaker interface -func (i inputShim) Log() telegraf.Logger { +func (inputShim) Log() telegraf.Logger { return nil } diff --git a/plugins/inputs/execd/shim/shim_test.go b/plugins/inputs/execd/shim/shim_test.go index 63e073e5498fc..e3124ea74b353 100644 --- a/plugins/inputs/execd/shim/shim_test.go +++ b/plugins/inputs/execd/shim/shim_test.go @@ -85,11 +85,7 @@ type testInput struct { metricProcessed chan bool } -func (i *testInput) SampleConfig() string { - return "" -} - -func (i *testInput) Description() string { +func (*testInput) SampleConfig() string { return "" } @@ -105,11 +101,11 @@ func (i *testInput) Gather(acc telegraf.Accumulator) error { return nil } -func (i *testInput) Start(_ telegraf.Accumulator) error { +func (*testInput) Start(telegraf.Accumulator) error { return nil } -func (i *testInput) Stop() { +func (*testInput) Stop() { } func TestLoadConfig(t *testing.T) { @@ -137,15 +133,11 @@ type serviceInput struct { SecretValue string `toml:"secret_value"` } -func (i *serviceInput) SampleConfig() string { - return "" -} - -func (i *serviceInput) Description() string { +func (*serviceInput) SampleConfig() string { return "" } -func (i *serviceInput) Gather(acc telegraf.Accumulator) error { +func (*serviceInput) Gather(acc telegraf.Accumulator) error { acc.AddFields("measurement", map[string]interface{}{ "field": 1, @@ -157,11 +149,11 @@ func (i *serviceInput) Gather(acc telegraf.Accumulator) error { return nil } -func (i *serviceInput) Start(_ telegraf.Accumulator) error { +func (*serviceInput) Start(telegraf.Accumulator) error { return nil } -func (i *serviceInput) Stop() { +func (*serviceInput) Stop() { } // we can get stuck if stdout gets clogged up and nobody's reading from it. From c0b3dd489e370e92aa399c42f0fca3e24b71e49d Mon Sep 17 00:00:00 2001 From: Sergio <239811+zomfg@users.noreply.github.com> Date: Mon, 16 Dec 2024 17:06:22 +0100 Subject: [PATCH 10/21] feat(inputs.docker): Support swarm jobs (#16292) --- plugins/inputs/docker/docker.go | 12 +++++++++++ plugins/inputs/docker/docker_test.go | 26 ++++++++++++++++++++++++ plugins/inputs/docker/docker_testdata.go | 25 +++++++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/plugins/inputs/docker/docker.go b/plugins/inputs/docker/docker.go index 0f5471eb11840..942c79d7dd8ce 100644 --- a/plugins/inputs/docker/docker.go +++ b/plugins/inputs/docker/docker.go @@ -309,6 +309,18 @@ func (d *Docker) gatherSwarmInfo(acc telegraf.Accumulator) error { tags["service_mode"] = "global" fields["tasks_running"] = running[service.ID] fields["tasks_desired"] = tasksNoShutdown[service.ID] + } else if service.Spec.Mode.ReplicatedJob != nil { + tags["service_mode"] = "replicated_job" + fields["tasks_running"] = running[service.ID] + if service.Spec.Mode.ReplicatedJob.MaxConcurrent != nil { + fields["max_concurrent"] = *service.Spec.Mode.ReplicatedJob.MaxConcurrent + } + if service.Spec.Mode.ReplicatedJob.TotalCompletions != nil { + fields["total_completions"] = *service.Spec.Mode.ReplicatedJob.TotalCompletions + } + } else if service.Spec.Mode.GlobalJob != nil { + tags["service_mode"] = "global_job" + fields["tasks_running"] = running[service.ID] } else { d.Log.Error("Unknown replica mode") } diff --git a/plugins/inputs/docker/docker_test.go b/plugins/inputs/docker/docker_test.go index 600227e5b1653..24d2c59469267 100644 --- a/plugins/inputs/docker/docker_test.go +++ b/plugins/inputs/docker/docker_test.go @@ -1102,6 +1102,32 @@ func TestDockerGatherSwarmInfo(t *testing.T) { "service_mode": "global", }, ) + + acc.AssertContainsTaggedFields(t, + "docker_swarm", + map[string]interface{}{ + "tasks_running": int(0), + "max_concurrent": uint64(2), + "total_completions": uint64(2), + }, + map[string]string{ + "service_id": "rfmqydhe8cluzl9hayyrhw5ga", + "service_name": "test3", + "service_mode": "replicated_job", + }, + ) + + acc.AssertContainsTaggedFields(t, + "docker_swarm", + map[string]interface{}{ + "tasks_running": int(0), + }, + map[string]string{ + "service_id": "mp50lo68vqgkory4e26ts8f9d", + "service_name": "test4", + "service_mode": "global_job", + }, + ) } func TestContainerStateFilter(t *testing.T) { diff --git a/plugins/inputs/docker/docker_testdata.go b/plugins/inputs/docker/docker_testdata.go index 57be5a8cb1773..e0b5cb6f6cda0 100644 --- a/plugins/inputs/docker/docker_testdata.go +++ b/plugins/inputs/docker/docker_testdata.go @@ -196,6 +196,31 @@ var serviceList = []swarm.Service{ }, }, }, + { + ID: "rfmqydhe8cluzl9hayyrhw5ga", + Spec: swarm.ServiceSpec{ + Annotations: swarm.Annotations{ + Name: "test3", + }, + Mode: swarm.ServiceMode{ + ReplicatedJob: &swarm.ReplicatedJob{ + MaxConcurrent: &two, + TotalCompletions: &two, + }, + }, + }, + }, + { + ID: "mp50lo68vqgkory4e26ts8f9d", + Spec: swarm.ServiceSpec{ + Annotations: swarm.Annotations{ + Name: "test4", + }, + Mode: swarm.ServiceMode{ + GlobalJob: &swarm.GlobalJob{}, + }, + }, + }, } var taskList = []swarm.Task{ From 6f80899e13d461239c1612f9cc297b82d77d1eb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20=C5=BBak?= Date: Tue, 17 Dec 2024 15:33:30 +0100 Subject: [PATCH 11/21] chore: Fix linter findings for `revive:unused-receiver` in `plugins/inputs/[f-k]` (#16308) --- .../filesystem_helpers_notwindows.go | 2 +- plugins/inputs/fireboard/fireboard.go | 4 +-- plugins/inputs/gnmi/gnmi.go | 2 +- plugins/inputs/gnmi/gnmi_test.go | 6 ++--- plugins/inputs/gnmi/tag_store.go | 6 ++--- .../google_cloud_storage.go | 2 +- plugins/inputs/graylog/graylog_test.go | 4 +-- plugins/inputs/haproxy/haproxy_test.go | 10 +++----- plugins/inputs/hddtemp/go-hddtemp/hddtemp.go | 2 +- plugins/inputs/hddtemp/hddtemp_test.go | 2 +- plugins/inputs/http/http.go | 2 +- .../http_listener_v2/http_listener_v2.go | 2 +- plugins/inputs/hugepages/hugepages.go | 6 ++--- plugins/inputs/icinga2/icinga2.go | 16 ++++++------ plugins/inputs/infiniband/infiniband_linux.go | 2 +- .../influxdb_listener/influxdb_listener.go | 2 +- .../influxdb_v2_listener.go | 2 +- .../inputs/intel_baseband/intel_baseband.go | 2 +- .../inputs/intel_baseband/log_connector.go | 8 +++--- .../intel_baseband/log_connector_test.go | 4 +-- plugins/inputs/intel_dlb/intel_dlb.go | 2 +- plugins/inputs/intel_pmt/intel_pmt.go | 2 +- plugins/inputs/intel_pmu/intel_pmu_test.go | 12 ++++----- plugins/inputs/intel_powerstat/options.go | 2 +- plugins/inputs/intel_rdt/intel_rdt.go | 2 +- plugins/inputs/intel_rdt/intel_rdt_test.go | 2 +- plugins/inputs/intel_rdt/processes.go | 2 +- plugins/inputs/ipmi_sensor/ipmi_sensor.go | 4 +-- .../inputs/ipmi_sensor/ipmi_sensor_test.go | 6 +---- plugins/inputs/ipset/ipset.go | 2 +- .../jti_openconfig_telemetry.go | 2 +- .../jti_openconfig_telemetry_test.go | 25 ++++++++----------- .../inputs/kafka_consumer/kafka_consumer.go | 2 +- .../kafka_consumer/kafka_consumer_test.go | 22 ++++++++-------- plugins/inputs/kernel/kernel.go | 14 +++++------ plugins/inputs/kernel/kernel_test.go | 14 ++--------- plugins/inputs/kibana/kibana.go | 2 +- plugins/inputs/kube_inventory/certificate.go | 4 +-- plugins/inputs/kube_inventory/endpoint.go | 4 +-- .../inputs/kube_inventory/endpoint_test.go | 7 +----- plugins/inputs/kube_inventory/ingress.go | 4 +-- plugins/inputs/kube_inventory/ingress_test.go | 7 +----- plugins/inputs/kube_inventory/node.go | 4 +-- plugins/inputs/kube_inventory/node_test.go | 2 +- .../inputs/kube_inventory/persistentvolume.go | 4 +-- .../kube_inventory/persistentvolume_test.go | 6 +---- 46 files changed, 104 insertions(+), 141 deletions(-) diff --git a/plugins/inputs/filecount/filesystem_helpers_notwindows.go b/plugins/inputs/filecount/filesystem_helpers_notwindows.go index e1a11c78ec2a4..7f4c6e2ced1e8 100644 --- a/plugins/inputs/filecount/filesystem_helpers_notwindows.go +++ b/plugins/inputs/filecount/filesystem_helpers_notwindows.go @@ -40,7 +40,7 @@ func (f fakeFileInfo) ModTime() time.Time { return f.modtime } func (f fakeFileInfo) IsDir() bool { return f.isdir } func (f fakeFileInfo) Sys() interface{} { return f.sys } -func (f fakeFileSystem) open(name string) (file, error) { +func (fakeFileSystem) open(name string) (file, error) { return nil, &os.PathError{Op: "Open", Path: name, Err: errors.New("not implemented by fake filesystem")} } diff --git a/plugins/inputs/fireboard/fireboard.go b/plugins/inputs/fireboard/fireboard.go index bf45fbe53172c..07398b66f9281 100644 --- a/plugins/inputs/fireboard/fireboard.go +++ b/plugins/inputs/fireboard/fireboard.go @@ -87,7 +87,7 @@ func (r *Fireboard) Gather(acc telegraf.Accumulator) error { } // Range over all devices, gathering stats. Returns early in case of any error. for _, s := range stats { - r.gatherTemps(s, acc) + gatherTemps(s, acc) } return nil } @@ -105,7 +105,7 @@ func scale(n int) string { } // Gathers stats from a single device, adding them to the accumulator -func (r *Fireboard) gatherTemps(s fireboardStats, acc telegraf.Accumulator) { +func gatherTemps(s fireboardStats, acc telegraf.Accumulator) { // Construct lookup for scale values for _, t := range s.LatestTemps { diff --git a/plugins/inputs/gnmi/gnmi.go b/plugins/inputs/gnmi/gnmi.go index 3832669b8110d..dcf7101ff10e0 100644 --- a/plugins/inputs/gnmi/gnmi.go +++ b/plugins/inputs/gnmi/gnmi.go @@ -314,7 +314,7 @@ func (c *GNMI) Start(acc telegraf.Accumulator) error { return nil } -func (c *GNMI) Gather(_ telegraf.Accumulator) error { +func (*GNMI) Gather(telegraf.Accumulator) error { return nil } diff --git a/plugins/inputs/gnmi/gnmi_test.go b/plugins/inputs/gnmi/gnmi_test.go index 4f0feae0fcfc1..f9af63369027a 100644 --- a/plugins/inputs/gnmi/gnmi_test.go +++ b/plugins/inputs/gnmi/gnmi_test.go @@ -51,15 +51,15 @@ type mockServer struct { grpcServer *grpc.Server } -func (s *mockServer) Capabilities(context.Context, *gnmi.CapabilityRequest) (*gnmi.CapabilityResponse, error) { +func (*mockServer) Capabilities(context.Context, *gnmi.CapabilityRequest) (*gnmi.CapabilityResponse, error) { return nil, nil } -func (s *mockServer) Get(context.Context, *gnmi.GetRequest) (*gnmi.GetResponse, error) { +func (*mockServer) Get(context.Context, *gnmi.GetRequest) (*gnmi.GetResponse, error) { return nil, nil } -func (s *mockServer) Set(context.Context, *gnmi.SetRequest) (*gnmi.SetResponse, error) { +func (*mockServer) Set(context.Context, *gnmi.SetRequest) (*gnmi.SetResponse, error) { return nil, nil } diff --git a/plugins/inputs/gnmi/tag_store.go b/plugins/inputs/gnmi/tag_store.go index af6b2b55f2bcf..1ab48bfd50c10 100644 --- a/plugins/inputs/gnmi/tag_store.go +++ b/plugins/inputs/gnmi/tag_store.go @@ -89,7 +89,7 @@ func (s *tagStore) insert(subscription tagSubscription, path *pathInfo, values [ } } case "elements": - key, match := s.getElementsKeys(path, subscription.Elements) + key, match := getElementsKeys(path, subscription.Elements) if !match || len(values) == 0 { return nil } @@ -141,7 +141,7 @@ func (s *tagStore) lookup(path *pathInfo, metricTags map[string]string) map[stri // Match elements for _, requiredKeys := range s.elements.required { - key, match := s.getElementsKeys(path, requiredKeys) + key, match := getElementsKeys(path, requiredKeys) if !match { continue } @@ -153,7 +153,7 @@ func (s *tagStore) lookup(path *pathInfo, metricTags map[string]string) map[stri return tags } -func (s *tagStore) getElementsKeys(path *pathInfo, elements []string) (string, bool) { +func getElementsKeys(path *pathInfo, elements []string) (string, bool) { // Search for the required path elements and collect a ordered // list of their values to in the form // elementName1={keyA=valueA,keyB=valueB,...},...,elementNameN={keyY=valueY,keyZ=valueZ} diff --git a/plugins/inputs/google_cloud_storage/google_cloud_storage.go b/plugins/inputs/google_cloud_storage/google_cloud_storage.go index 920e66b8e0642..2c45552aeb1da 100644 --- a/plugins/inputs/google_cloud_storage/google_cloud_storage.go +++ b/plugins/inputs/google_cloud_storage/google_cloud_storage.go @@ -57,7 +57,7 @@ func (gcs *GCS) Init() error { return gcs.setOffset() } -func (gcs *GCS) SampleConfig() string { +func (*GCS) SampleConfig() string { return sampleConfig } diff --git a/plugins/inputs/graylog/graylog_test.go b/plugins/inputs/graylog/graylog_test.go index 0662dc058566b..ecbb40b03cc3a 100644 --- a/plugins/inputs/graylog/graylog_test.go +++ b/plugins/inputs/graylog/graylog_test.go @@ -119,10 +119,10 @@ func (c *mockHTTPClient) makeRequest(req *http.Request) (*http.Response, error) return &resp, nil } -func (c *mockHTTPClient) setHTTPClient(_ *http.Client) { +func (*mockHTTPClient) setHTTPClient(*http.Client) { } -func (c *mockHTTPClient) httpClient() *http.Client { +func (*mockHTTPClient) httpClient() *http.Client { return nil } diff --git a/plugins/inputs/haproxy/haproxy_test.go b/plugins/inputs/haproxy/haproxy_test.go index 884dcdc8dc76f..ec34e817bb87f 100644 --- a/plugins/inputs/haproxy/haproxy_test.go +++ b/plugins/inputs/haproxy/haproxy_test.go @@ -17,9 +17,7 @@ import ( "github.com/influxdata/telegraf/testutil" ) -type statServer struct{} - -func (s statServer) serverSocket(l net.Listener) { +func serverSocket(l net.Listener) { for { conn, err := l.Accept() if err != nil { @@ -151,8 +149,7 @@ func TestHaproxyGeneratesMetricsUsingSocket(t *testing.T) { sockets[i] = sock defer sock.Close() //nolint:revive,gocritic // done on purpose, closing will be executed properly - s := statServer{} - go s.serverSocket(sock) + go serverSocket(sock) } r := &HAProxy{ @@ -191,8 +188,7 @@ func TestHaproxyGeneratesMetricsUsingTcp(t *testing.T) { } defer l.Close() - s := statServer{} - go s.serverSocket(l) + go serverSocket(l) r := &HAProxy{ Servers: []string{"tcp://" + l.Addr().String()}, diff --git a/plugins/inputs/hddtemp/go-hddtemp/hddtemp.go b/plugins/inputs/hddtemp/go-hddtemp/hddtemp.go index 7c58cfbea321b..1e511b5e9bb28 100644 --- a/plugins/inputs/hddtemp/go-hddtemp/hddtemp.go +++ b/plugins/inputs/hddtemp/go-hddtemp/hddtemp.go @@ -25,7 +25,7 @@ func New() *hddtemp { } // Fetch gathers disks data from hddtemp daemon. -func (h *hddtemp) Fetch(address string) ([]Disk, error) { +func (*hddtemp) Fetch(address string) ([]Disk, error) { var ( err error conn net.Conn diff --git a/plugins/inputs/hddtemp/hddtemp_test.go b/plugins/inputs/hddtemp/hddtemp_test.go index b266600a95682..f1dd99cf8df5f 100644 --- a/plugins/inputs/hddtemp/hddtemp_test.go +++ b/plugins/inputs/hddtemp/hddtemp_test.go @@ -12,7 +12,7 @@ import ( type mockFetcher struct { } -func (h *mockFetcher) Fetch(_ string) ([]hddtemp.Disk, error) { +func (*mockFetcher) Fetch(string) ([]hddtemp.Disk, error) { return []hddtemp.Disk{ { DeviceName: "Disk1", diff --git a/plugins/inputs/http/http.go b/plugins/inputs/http/http.go index 5cf8eb5919af1..5557d5bd108d5 100644 --- a/plugins/inputs/http/http.go +++ b/plugins/inputs/http/http.go @@ -86,7 +86,7 @@ func (h *HTTP) SetParserFunc(fn telegraf.ParserFunc) { h.parserFunc = fn } -func (h *HTTP) Start(_ telegraf.Accumulator) error { +func (*HTTP) Start(telegraf.Accumulator) error { return nil } diff --git a/plugins/inputs/http_listener_v2/http_listener_v2.go b/plugins/inputs/http_listener_v2/http_listener_v2.go index 825da44535801..e940620fb2eed 100644 --- a/plugins/inputs/http_listener_v2/http_listener_v2.go +++ b/plugins/inputs/http_listener_v2/http_listener_v2.go @@ -197,7 +197,7 @@ func (h *HTTPListenerV2) Start(acc telegraf.Accumulator) error { return nil } -func (h *HTTPListenerV2) Gather(_ telegraf.Accumulator) error { +func (*HTTPListenerV2) Gather(telegraf.Accumulator) error { return nil } diff --git a/plugins/inputs/hugepages/hugepages.go b/plugins/inputs/hugepages/hugepages.go index fb7a719179ef1..ebd31845816aa 100644 --- a/plugins/inputs/hugepages/hugepages.go +++ b/plugins/inputs/hugepages/hugepages.go @@ -118,7 +118,7 @@ func (h *Hugepages) Gather(acc telegraf.Accumulator) error { // gatherStatsPerNode collects root hugepages statistics func (h *Hugepages) gatherRootStats(acc telegraf.Accumulator) error { - return h.gatherFromHugepagePath(acc, "hugepages_"+rootHugepages, h.rootHugepagePath, hugepagesMetricsRoot, nil) + return gatherFromHugepagePath(acc, "hugepages_"+rootHugepages, h.rootHugepagePath, hugepagesMetricsRoot, nil) } // gatherStatsPerNode collects hugepages statistics per NUMA node @@ -144,7 +144,7 @@ func (h *Hugepages) gatherStatsPerNode(acc telegraf.Accumulator) error { "node": nodeNumber, } hugepagesPath := filepath.Join(h.numaNodePath, nodeDir.Name(), "hugepages") - err = h.gatherFromHugepagePath(acc, "hugepages_"+perNodeHugepages, hugepagesPath, hugepagesMetricsPerNUMANode, perNodeTags) + err = gatherFromHugepagePath(acc, "hugepages_"+perNodeHugepages, hugepagesPath, hugepagesMetricsPerNUMANode, perNodeTags) if err != nil { return err } @@ -152,7 +152,7 @@ func (h *Hugepages) gatherStatsPerNode(acc telegraf.Accumulator) error { return nil } -func (h *Hugepages) gatherFromHugepagePath(acc telegraf.Accumulator, measurement, path string, fileFilter, defaultTags map[string]string) error { +func gatherFromHugepagePath(acc telegraf.Accumulator, measurement, path string, fileFilter, defaultTags map[string]string) error { // read metrics from: hugepages/hugepages-*/* hugepagesDirs, err := os.ReadDir(path) if err != nil { diff --git a/plugins/inputs/icinga2/icinga2.go b/plugins/inputs/icinga2/icinga2.go index 7f85b3c0f01b9..a0aa1bca47a6b 100644 --- a/plugins/inputs/icinga2/icinga2.go +++ b/plugins/inputs/icinga2/icinga2.go @@ -121,7 +121,7 @@ func (i *Icinga2) Gather(acc telegraf.Accumulator) error { } result := resultObject{} - err = i.parseObjectResponse(resp, &result) + err = parseObjectResponse(resp, &result) if err != nil { return fmt.Errorf("could not parse object response: %w", err) } @@ -145,13 +145,13 @@ func (i *Icinga2) Gather(acc telegraf.Accumulator) error { switch statusType { case "ApiListener": - fields, err = i.parsePerfdataResponse(resp) + fields, err = parsePerfdataResponse(resp) case "CIB": - fields, err = i.parseCIBResponse(resp) + fields, err = parseCIBResponse(resp) case "IdoMysqlConnection": - fields, err = i.parsePerfdataResponse(resp) + fields, err = parsePerfdataResponse(resp) case "IdoPgsqlConnection": - fields, err = i.parsePerfdataResponse(resp) + fields, err = parsePerfdataResponse(resp) } if err != nil { @@ -233,7 +233,7 @@ func (i *Icinga2) icingaRequest(address string) (*http.Response, error) { return resp, nil } -func (i *Icinga2) parseObjectResponse(resp *http.Response, result *resultObject) error { +func parseObjectResponse(resp *http.Response, result *resultObject) error { err := json.NewDecoder(resp.Body).Decode(&result) if err != nil { return err @@ -246,7 +246,7 @@ func (i *Icinga2) parseObjectResponse(resp *http.Response, result *resultObject) return nil } -func (i *Icinga2) parseCIBResponse(resp *http.Response) (map[string]interface{}, error) { +func parseCIBResponse(resp *http.Response) (map[string]interface{}, error) { result := resultCIB{} err := json.NewDecoder(resp.Body).Decode(&result) @@ -262,7 +262,7 @@ func (i *Icinga2) parseCIBResponse(resp *http.Response) (map[string]interface{}, return result.Results[0].Status, nil } -func (i *Icinga2) parsePerfdataResponse(resp *http.Response) (map[string]interface{}, error) { +func parsePerfdataResponse(resp *http.Response) (map[string]interface{}, error) { result := resultPerfdata{} err := json.NewDecoder(resp.Body).Decode(&result) diff --git a/plugins/inputs/infiniband/infiniband_linux.go b/plugins/inputs/infiniband/infiniband_linux.go index 72bbc4714d763..214ba8e08fd67 100644 --- a/plugins/inputs/infiniband/infiniband_linux.go +++ b/plugins/inputs/infiniband/infiniband_linux.go @@ -12,7 +12,7 @@ import ( ) // Gather statistics from our infiniband cards -func (i *Infiniband) Gather(acc telegraf.Accumulator) error { +func (*Infiniband) Gather(acc telegraf.Accumulator) error { rdmaDevices := rdmamap.GetRdmaDeviceList() if len(rdmaDevices) == 0 { diff --git a/plugins/inputs/influxdb_listener/influxdb_listener.go b/plugins/inputs/influxdb_listener/influxdb_listener.go index 9186ecac8e54a..044b4c1050d26 100644 --- a/plugins/inputs/influxdb_listener/influxdb_listener.go +++ b/plugins/inputs/influxdb_listener/influxdb_listener.go @@ -76,7 +76,7 @@ func (*InfluxDBListener) SampleConfig() string { return sampleConfig } -func (h *InfluxDBListener) Gather(_ telegraf.Accumulator) error { +func (*InfluxDBListener) Gather(telegraf.Accumulator) error { return nil } diff --git a/plugins/inputs/influxdb_v2_listener/influxdb_v2_listener.go b/plugins/inputs/influxdb_v2_listener/influxdb_v2_listener.go index fb8a03de83949..52854d6f6e4d6 100644 --- a/plugins/inputs/influxdb_v2_listener/influxdb_v2_listener.go +++ b/plugins/inputs/influxdb_v2_listener/influxdb_v2_listener.go @@ -120,7 +120,7 @@ func (h *InfluxDBV2Listener) Init() error { return nil } -func (h *InfluxDBV2Listener) Gather(_ telegraf.Accumulator) error { +func (*InfluxDBV2Listener) Gather(telegraf.Accumulator) error { return nil } diff --git a/plugins/inputs/intel_baseband/intel_baseband.go b/plugins/inputs/intel_baseband/intel_baseband.go index 4017cb74b8946..ae22b7acee11b 100644 --- a/plugins/inputs/intel_baseband/intel_baseband.go +++ b/plugins/inputs/intel_baseband/intel_baseband.go @@ -56,7 +56,7 @@ type Baseband struct { sockConn *socketConnector } -func (b *Baseband) SampleConfig() string { +func (*Baseband) SampleConfig() string { return sampleConfig } diff --git a/plugins/inputs/intel_baseband/log_connector.go b/plugins/inputs/intel_baseband/log_connector.go index cfb67b57c1947..82c5367b5f1b1 100644 --- a/plugins/inputs/intel_baseband/log_connector.go +++ b/plugins/inputs/intel_baseband/log_connector.go @@ -135,7 +135,7 @@ func (lc *logConnector) readNumVFs() error { continue } - numVFs, err := lc.parseNumVFs(line) + numVFs, err := parseNumVFs(line) if err != nil { lc.numVFs = -1 return err @@ -189,7 +189,7 @@ func (lc *logConnector) getMetric(offsetLine int, name string) (int, *logMetric, return offsetLine, nil, err } - operationName := lc.parseOperationName(line) + operationName := parseOperationName(line) if len(operationName) == 0 { return offsetLine, nil, errors.New("valid operation name wasn't found in log") } @@ -221,7 +221,7 @@ func (lc *logConnector) getMetric(offsetLine int, name string) (int, *logMetric, } // Example value = Thu Apr 13 13:28:40 2023:INFO:Device Status:: 2 VFs -func (lc *logConnector) parseNumVFs(s string) (int, error) { +func parseNumVFs(s string) (int, error) { i := strings.LastIndex(s, deviceStatusStartPrefix) if i == -1 { return 0, errors.New("couldn't find device status prefix in line") @@ -244,7 +244,7 @@ func (lc *logConnector) parseNumVFs(s string) (int, error) { // Parse Operation name // Example = Thu Apr 13 13:28:40 2023:INFO:5GUL counters: Code Blocks // Output: 5GUL -func (lc *logConnector) parseOperationName(s string) string { +func parseOperationName(s string) string { i := strings.Index(s, infoLine) if i >= 0 { j := strings.Index(s[i:], countersLine) diff --git a/plugins/inputs/intel_baseband/log_connector_test.go b/plugins/inputs/intel_baseband/log_connector_test.go index 9d07e93ff754b..b2b286dab6d26 100644 --- a/plugins/inputs/intel_baseband/log_connector_test.go +++ b/plugins/inputs/intel_baseband/log_connector_test.go @@ -240,11 +240,9 @@ func TestParseOperationName(t *testing.T) { {"", ""}, } - logConnector := prepareLogConnMock() - require.NotNil(t, logConnector) for _, tc := range testCases { t.Run("expected "+tc.expected, func(t *testing.T) { - operationName := logConnector.parseOperationName(tc.input) + operationName := parseOperationName(tc.input) require.Equal(t, tc.expected, operationName) }) } diff --git a/plugins/inputs/intel_dlb/intel_dlb.go b/plugins/inputs/intel_dlb/intel_dlb.go index 643713ce1cba8..ddbe40c1adf58 100644 --- a/plugins/inputs/intel_dlb/intel_dlb.go +++ b/plugins/inputs/intel_dlb/intel_dlb.go @@ -50,7 +50,7 @@ type IntelDLB struct { maxInitMessageLength uint32 } -func (d *IntelDLB) SampleConfig() string { +func (*IntelDLB) SampleConfig() string { return sampleConfig } diff --git a/plugins/inputs/intel_pmt/intel_pmt.go b/plugins/inputs/intel_pmt/intel_pmt.go index f61980b7626e4..54e91613caefa 100644 --- a/plugins/inputs/intel_pmt/intel_pmt.go +++ b/plugins/inputs/intel_pmt/intel_pmt.go @@ -56,7 +56,7 @@ type fileInfo struct { pciBdf string // PCI Bus:Device.Function (BDF) } -func (p *IntelPMT) SampleConfig() string { +func (*IntelPMT) SampleConfig() string { return sampleConfig } diff --git a/plugins/inputs/intel_pmu/intel_pmu_test.go b/plugins/inputs/intel_pmu/intel_pmu_test.go index 6c75f68f68378..2910c905ad100 100644 --- a/plugins/inputs/intel_pmu/intel_pmu_test.go +++ b/plugins/inputs/intel_pmu/intel_pmu_test.go @@ -547,9 +547,9 @@ type fakeFileInfo struct { fileMode os.FileMode } -func (f fakeFileInfo) Name() string { return "" } -func (f fakeFileInfo) Size() int64 { return 0 } -func (f fakeFileInfo) Mode() os.FileMode { return f.fileMode } -func (f fakeFileInfo) ModTime() time.Time { return time.Time{} } -func (f fakeFileInfo) IsDir() bool { return false } -func (f fakeFileInfo) Sys() interface{} { return nil } +func (fakeFileInfo) Name() string { return "" } +func (fakeFileInfo) Size() int64 { return 0 } +func (f fakeFileInfo) Mode() os.FileMode { return f.fileMode } +func (fakeFileInfo) ModTime() time.Time { return time.Time{} } +func (fakeFileInfo) IsDir() bool { return false } +func (fakeFileInfo) Sys() interface{} { return nil } diff --git a/plugins/inputs/intel_powerstat/options.go b/plugins/inputs/intel_powerstat/options.go index 7e422b26bc01f..5e4fd4df4ad72 100644 --- a/plugins/inputs/intel_powerstat/options.go +++ b/plugins/inputs/intel_powerstat/options.go @@ -33,7 +33,7 @@ type optGenerator struct{} // generate takes plugin configuration options and generates options needed // to gather requested metrics. -func (g *optGenerator) generate(cfg optConfig) []ptel.Option { +func (*optGenerator) generate(cfg optConfig) []ptel.Option { opts := make([]ptel.Option, 0) if len(cfg.includedCPUs) != 0 { opts = append(opts, ptel.WithIncludedCPUs(cfg.includedCPUs)) diff --git a/plugins/inputs/intel_rdt/intel_rdt.go b/plugins/inputs/intel_rdt/intel_rdt.go index 1c0685e634bf7..d1427fbdb4f00 100644 --- a/plugins/inputs/intel_rdt/intel_rdt.go +++ b/plugins/inputs/intel_rdt/intel_rdt.go @@ -100,7 +100,7 @@ func (r *IntelRDT) Start(acc telegraf.Accumulator) error { return nil } -func (r *IntelRDT) Gather(_ telegraf.Accumulator) error { +func (*IntelRDT) Gather(telegraf.Accumulator) error { return nil } diff --git a/plugins/inputs/intel_rdt/intel_rdt_test.go b/plugins/inputs/intel_rdt/intel_rdt_test.go index e9468521276fb..7f4dc00919695 100644 --- a/plugins/inputs/intel_rdt/intel_rdt_test.go +++ b/plugins/inputs/intel_rdt/intel_rdt_test.go @@ -12,7 +12,7 @@ import ( type mockProc struct{} -func (m *mockProc) getAllProcesses() ([]process, error) { +func (*mockProc) getAllProcesses() ([]process, error) { procs := []process{ {Name: "process", PID: 1000}, {Name: "process2", PID: 1002}, diff --git a/plugins/inputs/intel_rdt/processes.go b/plugins/inputs/intel_rdt/processes.go index 63c8622aa1875..975760b0bfe40 100644 --- a/plugins/inputs/intel_rdt/processes.go +++ b/plugins/inputs/intel_rdt/processes.go @@ -19,7 +19,7 @@ func newProcessor() processesHandler { return &processManager{} } -func (p *processManager) getAllProcesses() ([]process, error) { +func (*processManager) getAllProcesses() ([]process, error) { allProcesses, err := procfs.AllProcs() if err != nil { return nil, err diff --git a/plugins/inputs/ipmi_sensor/ipmi_sensor.go b/plugins/inputs/ipmi_sensor/ipmi_sensor.go index b70de8d0e3c2b..403e7b3fe7a59 100644 --- a/plugins/inputs/ipmi_sensor/ipmi_sensor.go +++ b/plugins/inputs/ipmi_sensor/ipmi_sensor.go @@ -179,7 +179,7 @@ func (m *Ipmi) parse(acc telegraf.Accumulator, server, sensor string) error { return m.parseV1(acc, hostname, out, timestamp) } case "chassis_power_status": - return m.parseChassisPowerStatus(acc, hostname, out, timestamp) + return parseChassisPowerStatus(acc, hostname, out, timestamp) case "dcmi_power_reading": return m.parseDCMIPowerReading(acc, hostname, out, timestamp) } @@ -187,7 +187,7 @@ func (m *Ipmi) parse(acc telegraf.Accumulator, server, sensor string) error { return fmt.Errorf("unknown sensor type %q", sensor) } -func (m *Ipmi) parseChassisPowerStatus(acc telegraf.Accumulator, hostname string, cmdOut []byte, measuredAt time.Time) error { +func parseChassisPowerStatus(acc telegraf.Accumulator, hostname string, cmdOut []byte, measuredAt time.Time) error { // each line will look something like // Chassis Power is on // Chassis Power is off diff --git a/plugins/inputs/ipmi_sensor/ipmi_sensor_test.go b/plugins/inputs/ipmi_sensor/ipmi_sensor_test.go index 74dab5eb18b8f..03c0c316d28a4 100644 --- a/plugins/inputs/ipmi_sensor/ipmi_sensor_test.go +++ b/plugins/inputs/ipmi_sensor/ipmi_sensor_test.go @@ -820,14 +820,10 @@ func Test_parsePowerStatus(t *testing.T) { }, } - ipmi := &Ipmi{ - Log: testutil.Logger{}, - } - for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var acc testutil.Accumulator - err := ipmi.parseChassisPowerStatus(&acc, tt.args.hostname, tt.args.cmdOut, tt.args.measuredAt) + err := parseChassisPowerStatus(&acc, tt.args.hostname, tt.args.cmdOut, tt.args.measuredAt) require.NoError(t, err) testutil.RequireMetricsEqual(t, tt.expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime()) }) diff --git a/plugins/inputs/ipset/ipset.go b/plugins/inputs/ipset/ipset.go index 4a7e0938a31ff..be177d10a26ac 100644 --- a/plugins/inputs/ipset/ipset.go +++ b/plugins/inputs/ipset/ipset.go @@ -38,7 +38,7 @@ func (*Ipset) SampleConfig() string { return sampleConfig } -func (i *Ipset) Init() error { +func (*Ipset) Init() error { _, err := exec.LookPath("ipset") if err != nil { return err diff --git a/plugins/inputs/jti_openconfig_telemetry/jti_openconfig_telemetry.go b/plugins/inputs/jti_openconfig_telemetry/jti_openconfig_telemetry.go index e60dda37245bb..b6c7d039515fa 100644 --- a/plugins/inputs/jti_openconfig_telemetry/jti_openconfig_telemetry.go +++ b/plugins/inputs/jti_openconfig_telemetry/jti_openconfig_telemetry.go @@ -172,7 +172,7 @@ func (m *OpenConfigTelemetry) Start(acc telegraf.Accumulator) error { return nil } -func (m *OpenConfigTelemetry) Gather(_ telegraf.Accumulator) error { +func (*OpenConfigTelemetry) Gather(telegraf.Accumulator) error { return nil } diff --git a/plugins/inputs/jti_openconfig_telemetry/jti_openconfig_telemetry_test.go b/plugins/inputs/jti_openconfig_telemetry/jti_openconfig_telemetry_test.go index 758beb1ff9eef..b98f9100ae92f 100644 --- a/plugins/inputs/jti_openconfig_telemetry/jti_openconfig_telemetry_test.go +++ b/plugins/inputs/jti_openconfig_telemetry/jti_openconfig_telemetry_test.go @@ -58,10 +58,7 @@ type openConfigTelemetryServer struct { telemetry.UnimplementedOpenConfigTelemetryServer } -func (s *openConfigTelemetryServer) TelemetrySubscribe( - req *telemetry.SubscriptionRequest, - stream telemetry.OpenConfigTelemetry_TelemetrySubscribeServer, -) error { +func (*openConfigTelemetryServer) TelemetrySubscribe(req *telemetry.SubscriptionRequest, stream telemetry.OpenConfigTelemetry_TelemetrySubscribeServer) error { path := req.PathList[0].Path switch path { case "/sensor": @@ -78,28 +75,28 @@ func (s *openConfigTelemetryServer) TelemetrySubscribe( return nil } -func (s *openConfigTelemetryServer) CancelTelemetrySubscription( - _ context.Context, - _ *telemetry.CancelSubscriptionRequest, +func (*openConfigTelemetryServer) CancelTelemetrySubscription( + context.Context, + *telemetry.CancelSubscriptionRequest, ) (*telemetry.CancelSubscriptionReply, error) { return nil, nil } -func (s *openConfigTelemetryServer) GetTelemetrySubscriptions( - _ context.Context, - _ *telemetry.GetSubscriptionsRequest, +func (*openConfigTelemetryServer) GetTelemetrySubscriptions( + context.Context, + *telemetry.GetSubscriptionsRequest, ) (*telemetry.GetSubscriptionsReply, error) { return nil, nil } -func (s *openConfigTelemetryServer) GetTelemetryOperationalState( - _ context.Context, - _ *telemetry.GetOperationalStateRequest, +func (*openConfigTelemetryServer) GetTelemetryOperationalState( + context.Context, + *telemetry.GetOperationalStateRequest, ) (*telemetry.GetOperationalStateReply, error) { return nil, nil } -func (s *openConfigTelemetryServer) GetDataEncodings(_ context.Context, _ *telemetry.DataEncodingRequest) (*telemetry.DataEncodingReply, error) { +func (*openConfigTelemetryServer) GetDataEncodings(context.Context, *telemetry.DataEncodingRequest) (*telemetry.DataEncodingReply, error) { return nil, nil } diff --git a/plugins/inputs/kafka_consumer/kafka_consumer.go b/plugins/inputs/kafka_consumer/kafka_consumer.go index 104ba26156ff6..ac335eec0810f 100644 --- a/plugins/inputs/kafka_consumer/kafka_consumer.go +++ b/plugins/inputs/kafka_consumer/kafka_consumer.go @@ -299,7 +299,7 @@ func (k *KafkaConsumer) Start(acc telegraf.Accumulator) error { return nil } -func (k *KafkaConsumer) Gather(_ telegraf.Accumulator) error { +func (*KafkaConsumer) Gather(telegraf.Accumulator) error { return nil } diff --git a/plugins/inputs/kafka_consumer/kafka_consumer_test.go b/plugins/inputs/kafka_consumer/kafka_consumer_test.go index 568da16b6a095..94bc4096617f5 100644 --- a/plugins/inputs/kafka_consumer/kafka_consumer_test.go +++ b/plugins/inputs/kafka_consumer/kafka_consumer_test.go @@ -240,53 +240,53 @@ type FakeConsumerGroupSession struct { ctx context.Context } -func (s *FakeConsumerGroupSession) Claims() map[string][]int32 { +func (*FakeConsumerGroupSession) Claims() map[string][]int32 { panic("not implemented") } -func (s *FakeConsumerGroupSession) MemberID() string { +func (*FakeConsumerGroupSession) MemberID() string { panic("not implemented") } -func (s *FakeConsumerGroupSession) GenerationID() int32 { +func (*FakeConsumerGroupSession) GenerationID() int32 { panic("not implemented") } -func (s *FakeConsumerGroupSession) MarkOffset(_ string, _ int32, _ int64, _ string) { +func (*FakeConsumerGroupSession) MarkOffset(string, int32, int64, string) { panic("not implemented") } -func (s *FakeConsumerGroupSession) ResetOffset(_ string, _ int32, _ int64, _ string) { +func (*FakeConsumerGroupSession) ResetOffset(string, int32, int64, string) { panic("not implemented") } -func (s *FakeConsumerGroupSession) MarkMessage(_ *sarama.ConsumerMessage, _ string) { +func (*FakeConsumerGroupSession) MarkMessage(*sarama.ConsumerMessage, string) { } func (s *FakeConsumerGroupSession) Context() context.Context { return s.ctx } -func (s *FakeConsumerGroupSession) Commit() { +func (*FakeConsumerGroupSession) Commit() { } type FakeConsumerGroupClaim struct { messages chan *sarama.ConsumerMessage } -func (c *FakeConsumerGroupClaim) Topic() string { +func (*FakeConsumerGroupClaim) Topic() string { panic("not implemented") } -func (c *FakeConsumerGroupClaim) Partition() int32 { +func (*FakeConsumerGroupClaim) Partition() int32 { panic("not implemented") } -func (c *FakeConsumerGroupClaim) InitialOffset() int64 { +func (*FakeConsumerGroupClaim) InitialOffset() int64 { panic("not implemented") } -func (c *FakeConsumerGroupClaim) HighWaterMarkOffset() int64 { +func (*FakeConsumerGroupClaim) HighWaterMarkOffset() int64 { panic("not implemented") } diff --git a/plugins/inputs/kernel/kernel.go b/plugins/inputs/kernel/kernel.go index 88c18c2101c45..7ddf0d714762e 100644 --- a/plugins/inputs/kernel/kernel.go +++ b/plugins/inputs/kernel/kernel.go @@ -68,12 +68,12 @@ func (k *Kernel) Init() error { } func (k *Kernel) Gather(acc telegraf.Accumulator) error { - data, err := k.getProcValueBytes(k.statFile) + data, err := getProcValueBytes(k.statFile) if err != nil { return err } - entropyValue, err := k.getProcValueInt(k.entropyStatFile) + entropyValue, err := getProcValueInt(k.entropyStatFile) if err != nil { return err } @@ -137,7 +137,7 @@ func (k *Kernel) Gather(acc telegraf.Accumulator) error { extraStats := []string{"general_profit"} for _, f := range stats { - m, err := k.getProcValueInt(filepath.Join(k.ksmStatsDir, f)) + m, err := getProcValueInt(filepath.Join(k.ksmStatsDir, f)) if err != nil { return err } @@ -146,7 +146,7 @@ func (k *Kernel) Gather(acc telegraf.Accumulator) error { } for _, f := range extraStats { - m, err := k.getProcValueInt(filepath.Join(k.ksmStatsDir, f)) + m, err := getProcValueInt(filepath.Join(k.ksmStatsDir, f)) if err != nil { // if an extraStats metric doesn't exist in our kernel version, ignore it. continue @@ -166,7 +166,7 @@ func (k *Kernel) Gather(acc telegraf.Accumulator) error { return nil } -func (k *Kernel) getProcValueBytes(path string) ([]byte, error) { +func getProcValueBytes(path string) ([]byte, error) { if _, err := os.Stat(path); os.IsNotExist(err) { return nil, fmt.Errorf("path %q does not exist", path) } else if err != nil { @@ -181,8 +181,8 @@ func (k *Kernel) getProcValueBytes(path string) ([]byte, error) { return data, nil } -func (k *Kernel) getProcValueInt(path string) (int64, error) { - data, err := k.getProcValueBytes(path) +func getProcValueInt(path string) (int64, error) { + data, err := getProcValueBytes(path) if err != nil { return -1, err } diff --git a/plugins/inputs/kernel/kernel_test.go b/plugins/inputs/kernel/kernel_test.go index da3f3aa46cf3d..23d72949d742a 100644 --- a/plugins/inputs/kernel/kernel_test.go +++ b/plugins/inputs/kernel/kernel_test.go @@ -14,23 +14,13 @@ import ( ) func TestGetProcValueInt(t *testing.T) { - k := Kernel{ - statFile: "testdata/stat_file_full", - entropyStatFile: "testdata/entropy_stat_file_full", - } - - d, err := k.getProcValueInt(k.entropyStatFile) + d, err := getProcValueInt("testdata/entropy_stat_file_full") require.NoError(t, err) require.IsType(t, int64(1), d) } func TestGetProcValueByte(t *testing.T) { - k := Kernel{ - statFile: "testdata/stat_file_full", - entropyStatFile: "testdata/entropy_stat_file_full", - } - - d, err := k.getProcValueBytes(k.entropyStatFile) + d, err := getProcValueBytes("testdata/entropy_stat_file_full") require.NoError(t, err) require.IsType(t, []byte("test"), d) } diff --git a/plugins/inputs/kibana/kibana.go b/plugins/inputs/kibana/kibana.go index 622030728c74b..702b288ea01a1 100644 --- a/plugins/inputs/kibana/kibana.go +++ b/plugins/inputs/kibana/kibana.go @@ -101,7 +101,7 @@ func (*Kibana) SampleConfig() string { return sampleConfig } -func (k *Kibana) Start(_ telegraf.Accumulator) error { +func (*Kibana) Start(telegraf.Accumulator) error { return nil } diff --git a/plugins/inputs/kube_inventory/certificate.go b/plugins/inputs/kube_inventory/certificate.go index 5cf3603288c99..ba71f013b2f83 100644 --- a/plugins/inputs/kube_inventory/certificate.go +++ b/plugins/inputs/kube_inventory/certificate.go @@ -19,7 +19,7 @@ func collectSecrets(ctx context.Context, acc telegraf.Accumulator, ki *Kubernete return } for _, i := range list.Items { - ki.gatherCertificates(i, acc) + gatherCertificates(i, acc) } } @@ -59,7 +59,7 @@ func getTags(cert *x509.Certificate) map[string]string { return tags } -func (ki *KubernetesInventory) gatherCertificates(r corev1.Secret, acc telegraf.Accumulator) { +func gatherCertificates(r corev1.Secret, acc telegraf.Accumulator) { now := time.Now() for resourceName, val := range r.Data { diff --git a/plugins/inputs/kube_inventory/endpoint.go b/plugins/inputs/kube_inventory/endpoint.go index 1eb86eea13b76..742512f6824fe 100644 --- a/plugins/inputs/kube_inventory/endpoint.go +++ b/plugins/inputs/kube_inventory/endpoint.go @@ -15,11 +15,11 @@ func collectEndpoints(ctx context.Context, acc telegraf.Accumulator, ki *Kuberne return } for _, i := range list.Items { - ki.gatherEndpoint(i, acc) + gatherEndpoint(i, acc) } } -func (ki *KubernetesInventory) gatherEndpoint(e corev1.Endpoints, acc telegraf.Accumulator) { +func gatherEndpoint(e corev1.Endpoints, acc telegraf.Accumulator) { creationTs := e.GetCreationTimestamp() if creationTs.IsZero() { return diff --git a/plugins/inputs/kube_inventory/endpoint_test.go b/plugins/inputs/kube_inventory/endpoint_test.go index f5be722c925bc..c5a8a7509ed31 100644 --- a/plugins/inputs/kube_inventory/endpoint_test.go +++ b/plugins/inputs/kube_inventory/endpoint_test.go @@ -13,8 +13,6 @@ import ( ) func TestEndpoint(t *testing.T) { - cli := &client{} - now := time.Now() now = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 1, 36, 0, now.Location()) @@ -256,12 +254,9 @@ func TestEndpoint(t *testing.T) { } for _, v := range tests { - ks := &KubernetesInventory{ - client: cli, - } acc := new(testutil.Accumulator) for _, endpoint := range ((v.handler.responseMap["/endpoints/"]).(*v1.EndpointsList)).Items { - ks.gatherEndpoint(endpoint, acc) + gatherEndpoint(endpoint, acc) } err := acc.FirstError() diff --git a/plugins/inputs/kube_inventory/ingress.go b/plugins/inputs/kube_inventory/ingress.go index f8a966bc15a46..41890e44c0479 100644 --- a/plugins/inputs/kube_inventory/ingress.go +++ b/plugins/inputs/kube_inventory/ingress.go @@ -15,11 +15,11 @@ func collectIngress(ctx context.Context, acc telegraf.Accumulator, ki *Kubernete return } for _, i := range list.Items { - ki.gatherIngress(i, acc) + gatherIngress(i, acc) } } -func (ki *KubernetesInventory) gatherIngress(i netv1.Ingress, acc telegraf.Accumulator) { +func gatherIngress(i netv1.Ingress, acc telegraf.Accumulator) { creationTs := i.GetCreationTimestamp() if creationTs.IsZero() { return diff --git a/plugins/inputs/kube_inventory/ingress_test.go b/plugins/inputs/kube_inventory/ingress_test.go index a391b3808c29b..0ba519b69bf11 100644 --- a/plugins/inputs/kube_inventory/ingress_test.go +++ b/plugins/inputs/kube_inventory/ingress_test.go @@ -13,8 +13,6 @@ import ( ) func TestIngress(t *testing.T) { - cli := &client{} - now := time.Now() now = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 1, 36, 0, now.Location()) @@ -219,12 +217,9 @@ func TestIngress(t *testing.T) { } for _, v := range tests { - ks := &KubernetesInventory{ - client: cli, - } acc := new(testutil.Accumulator) for _, ingress := range ((v.handler.responseMap["/ingress/"]).(netv1.IngressList)).Items { - ks.gatherIngress(ingress, acc) + gatherIngress(ingress, acc) } err := acc.FirstError() diff --git a/plugins/inputs/kube_inventory/node.go b/plugins/inputs/kube_inventory/node.go index 8aa4e979a65c3..3660c00c7ad90 100644 --- a/plugins/inputs/kube_inventory/node.go +++ b/plugins/inputs/kube_inventory/node.go @@ -15,14 +15,14 @@ func collectNodes(ctx context.Context, acc telegraf.Accumulator, ki *KubernetesI return } - ki.gatherNodeCount(len(list.Items), acc) + gatherNodeCount(len(list.Items), acc) for i := range list.Items { ki.gatherNode(&list.Items[i], acc) } } -func (ki *KubernetesInventory) gatherNodeCount(count int, acc telegraf.Accumulator) { +func gatherNodeCount(count int, acc telegraf.Accumulator) { fields := map[string]interface{}{"node_count": count} tags := make(map[string]string) diff --git a/plugins/inputs/kube_inventory/node_test.go b/plugins/inputs/kube_inventory/node_test.go index 5527bca1d020e..00d9093887f7a 100644 --- a/plugins/inputs/kube_inventory/node_test.go +++ b/plugins/inputs/kube_inventory/node_test.go @@ -173,7 +173,7 @@ func TestNode(t *testing.T) { if v.name == "no nodes" { nodeCount := len((v.handler.responseMap["/nodes/"]).(corev1.NodeList).Items) - ks.gatherNodeCount(nodeCount, acc) + gatherNodeCount(nodeCount, acc) } require.Len(t, acc.Metrics, len(v.output)) testutil.RequireMetricsEqual(t, acc.GetTelegrafMetrics(), v.output, testutil.IgnoreTime()) diff --git a/plugins/inputs/kube_inventory/persistentvolume.go b/plugins/inputs/kube_inventory/persistentvolume.go index 808db450dbcb1..6fb65e9c46874 100644 --- a/plugins/inputs/kube_inventory/persistentvolume.go +++ b/plugins/inputs/kube_inventory/persistentvolume.go @@ -16,11 +16,11 @@ func collectPersistentVolumes(ctx context.Context, acc telegraf.Accumulator, ki return } for i := range list.Items { - ki.gatherPersistentVolume(&list.Items[i], acc) + gatherPersistentVolume(&list.Items[i], acc) } } -func (ki *KubernetesInventory) gatherPersistentVolume(pv *corev1.PersistentVolume, acc telegraf.Accumulator) { +func gatherPersistentVolume(pv *corev1.PersistentVolume, acc telegraf.Accumulator) { phaseType := 5 switch strings.ToLower(string(pv.Status.Phase)) { case "bound": diff --git a/plugins/inputs/kube_inventory/persistentvolume_test.go b/plugins/inputs/kube_inventory/persistentvolume_test.go index 2e3c15b4824a7..1a93f9b2b7a61 100644 --- a/plugins/inputs/kube_inventory/persistentvolume_test.go +++ b/plugins/inputs/kube_inventory/persistentvolume_test.go @@ -13,7 +13,6 @@ import ( ) func TestPersistentVolume(t *testing.T) { - cli := &client{} now := time.Now() now = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 1, 36, 0, now.Location()) @@ -77,13 +76,10 @@ func TestPersistentVolume(t *testing.T) { } for _, v := range tests { - ks := &KubernetesInventory{ - client: cli, - } acc := new(testutil.Accumulator) items := ((v.handler.responseMap["/persistentvolumes/"]).(*corev1.PersistentVolumeList)).Items for i := range items { - ks.gatherPersistentVolume(&items[i], acc) + gatherPersistentVolume(&items[i], acc) } err := acc.FirstError() From b89f127b4582f583ece2562bdab5972e7b88542b Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 17 Dec 2024 11:04:44 -0600 Subject: [PATCH 12/21] chore(deps): Bump super-linter/super-linter from 7.2.0 to 7.2.1 (#16313) --- .github/workflows/linter.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml index 9fd8d494f046a..c16cbfeb88094 100644 --- a/.github/workflows/linter.yml +++ b/.github/workflows/linter.yml @@ -54,7 +54,7 @@ jobs: # Run Linter against code base # ################################ - name: Lint Code Base - uses: super-linter/super-linter@v7.2.0 + uses: super-linter/super-linter@v7.2.1 env: VALIDATE_ALL_CODEBASE: false DEFAULT_BRANCH: master From a4fc9244e698244a00679d3e7b2a6369a416174a Mon Sep 17 00:00:00 2001 From: David Ashpole Date: Tue, 17 Dec 2024 12:05:09 -0500 Subject: [PATCH 13/21] docs(parsers.openmetrics): Update link to specification (#16312) Signed-off-by: David Ashpole --- plugins/parsers/openmetrics/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/parsers/openmetrics/README.md b/plugins/parsers/openmetrics/README.md index a9328a8d96072..e582941fc31f7 100644 --- a/plugins/parsers/openmetrics/README.md +++ b/plugins/parsers/openmetrics/README.md @@ -8,7 +8,7 @@ but can also be used by e.g. The plugin allows to output different metric formats as described in the [Metric Formats section](#metric-formats). -[OpenMetrics Text Format]: https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md +[OpenMetrics Text Format]: https://github.com/prometheus/OpenMetrics/blob/v1.0.0/specification/OpenMetrics.md ## Configuration From d0a045d56bb0870a4c088a4d68cb3ca2870b1701 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 17 Dec 2024 11:05:28 -0600 Subject: [PATCH 14/21] chore(deps): Bump github.com/fatih/color from 1.17.0 to 1.18.0 (#16317) --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index bd7f76df4317e..e30b292ba6e1b 100644 --- a/go.mod +++ b/go.mod @@ -80,7 +80,7 @@ require ( github.com/eclipse/paho.golang v0.21.0 github.com/eclipse/paho.mqtt.golang v1.5.0 github.com/facebook/time v0.0.0-20240626113945-18207c5d8ddc - github.com/fatih/color v1.17.0 + github.com/fatih/color v1.18.0 github.com/go-ldap/ldap/v3 v3.4.8 github.com/go-logfmt/logfmt v0.6.0 github.com/go-ole/go-ole v1.3.0 diff --git a/go.sum b/go.sum index 628bb6e28e1ee..2951a27105d8a 100644 --- a/go.sum +++ b/go.sum @@ -1194,8 +1194,8 @@ github.com/facebookgo/stackerr v0.0.0-20150612192056-c2fcf88613f4/go.mod h1:SBHk github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= -github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4= -github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI= +github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM= +github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU= github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/flynn/noise v1.0.1 h1:vPp/jdQLXC6ppsXSj/pM3W1BIJ5FEHE2TulSJBpb43Y= From 686cda839ba8374bf1a33922f338486cc57fe2d7 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 17 Dec 2024 11:05:53 -0600 Subject: [PATCH 15/21] chore(deps): Bump github.com/IBM/nzgo/v12 from 12.0.9-0.20231115043259-49c27f2dfe48 to 12.0.9 (#16319) --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index e30b292ba6e1b..e47fcf260b100 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,7 @@ require ( github.com/BurntSushi/toml v1.4.0 github.com/ClickHouse/clickhouse-go v1.5.4 github.com/DATA-DOG/go-sqlmock v1.5.2 - github.com/IBM/nzgo/v12 v12.0.9-0.20231115043259-49c27f2dfe48 + github.com/IBM/nzgo/v12 v12.0.9 github.com/IBM/sarama v1.43.3 github.com/Masterminds/semver/v3 v3.3.0 github.com/Masterminds/sprig v2.22.0+incompatible diff --git a/go.sum b/go.sum index 2951a27105d8a..c3d14f7137813 100644 --- a/go.sum +++ b/go.sum @@ -735,8 +735,8 @@ github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapp github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.48.1/go.mod h1:viRWSEhtMZqz1rhwmOVKkWl6SwmVowfL9O2YR5gI2PE= github.com/HdrHistogram/hdrhistogram-go v1.1.2 h1:5IcZpTvzydCQeHzK4Ef/D5rrSqwxob0t8PQPMybUNFM= github.com/HdrHistogram/hdrhistogram-go v1.1.2/go.mod h1:yDgFjdqOqDEKOvasDdhWNXYg9BVp4O+o5f6V/ehm6Oo= -github.com/IBM/nzgo/v12 v12.0.9-0.20231115043259-49c27f2dfe48 h1:TBb4IxmBH0ssmWTUg0C6c9ZnfDmZospTF8f+YbHnbbA= -github.com/IBM/nzgo/v12 v12.0.9-0.20231115043259-49c27f2dfe48/go.mod h1:4pvfEkfsrAdqlljsp8HNwv/uzNKy2fzoXBB1aRIssJg= +github.com/IBM/nzgo/v12 v12.0.9 h1:SwzYFU5ooXsTZsQhU6OsbUhs/fQyLvCtlJYSEZ58mN0= +github.com/IBM/nzgo/v12 v12.0.9/go.mod h1:4pvfEkfsrAdqlljsp8HNwv/uzNKy2fzoXBB1aRIssJg= github.com/IBM/sarama v1.43.3 h1:Yj6L2IaNvb2mRBop39N7mmJAHBVY3dTPncr3qGVkxPA= github.com/IBM/sarama v1.43.3/go.mod h1:FVIRaLrhK3Cla/9FfRF5X9Zua2KpS3SYIXxhac1H+FQ= github.com/JohnCGriffin/overflow v0.0.0-20211019200055-46fa312c352c h1:RGWPOewvKIROun94nF7v2cua9qP+thov/7M50KEoeSU= From 7360f5dc5a18f1ea49b41a6638f7527f8b15092e Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 17 Dec 2024 11:08:00 -0600 Subject: [PATCH 16/21] chore(deps): Bump github.com/prometheus/common from 0.60.0 to 0.61.0 (#16318) --- go.mod | 10 +++++----- go.sum | 19 ++++++++++--------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index e47fcf260b100..584286c7008ff 100644 --- a/go.mod +++ b/go.mod @@ -164,7 +164,7 @@ require ( github.com/prometheus-community/pro-bing v0.4.1 github.com/prometheus/client_golang v1.20.5 github.com/prometheus/client_model v0.6.1 - github.com/prometheus/common v0.60.0 + github.com/prometheus/common v0.61.0 github.com/prometheus/procfs v0.15.1 github.com/prometheus/prometheus v0.54.1 github.com/rabbitmq/amqp091-go v1.10.0 @@ -186,7 +186,7 @@ require ( github.com/snowflakedb/gosnowflake v1.11.2 github.com/srebhan/cborquery v1.0.1 github.com/srebhan/protobufquery v1.0.1 - github.com/stretchr/testify v1.9.0 + github.com/stretchr/testify v1.10.0 github.com/tbrandon/mbserver v0.0.0-20170611213546-993e1772cc62 github.com/testcontainers/testcontainers-go v0.34.0 github.com/testcontainers/testcontainers-go/modules/kafka v0.34.0 @@ -213,8 +213,8 @@ require ( go.step.sm/crypto v0.54.0 golang.org/x/crypto v0.31.0 golang.org/x/mod v0.21.0 - golang.org/x/net v0.31.0 - golang.org/x/oauth2 v0.23.0 + golang.org/x/net v0.32.0 + golang.org/x/oauth2 v0.24.0 golang.org/x/sync v0.10.0 golang.org/x/sys v0.28.0 golang.org/x/term v0.27.0 @@ -224,7 +224,7 @@ require ( google.golang.org/api v0.203.0 google.golang.org/genproto/googleapis/api v0.0.0-20241007155032-5fefd90f89a9 google.golang.org/grpc v1.68.0 - google.golang.org/protobuf v1.35.1 + google.golang.org/protobuf v1.35.2 gopkg.in/gorethink/gorethink.v3 v3.0.5 gopkg.in/olivere/elastic.v5 v5.0.86 gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 diff --git a/go.sum b/go.sum index c3d14f7137813..c11b686ddd6af 100644 --- a/go.sum +++ b/go.sum @@ -2136,8 +2136,8 @@ github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7q github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= github.com/prometheus/common v0.9.1/go.mod h1:yhUN8i9wzaXS3w1O07YhxHEBxD+W35wd8bs7vj7HSQ4= -github.com/prometheus/common v0.60.0 h1:+V9PAREWNvJMAuJ1x1BaWl9dewMW4YrHZQbx0sJNllA= -github.com/prometheus/common v0.60.0/go.mod h1:h0LYf1R1deLSKtD4Vdg8gy4RuOvENW2J/h19V5NADQw= +github.com/prometheus/common v0.61.0 h1:3gv/GThfX0cV2lpO7gkTUwZru38mxevy90Bj8YFSRQQ= +github.com/prometheus/common v0.61.0/go.mod h1:zr29OCN/2BsJRaFwG8QOBr41D6kkchKbpeNH7pAjb/s= github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= @@ -2313,8 +2313,9 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/t3rm1n4l/go-mega v0.0.0-20240219080617-d494b6a8ace7 h1:Jtcrb09q0AVWe3BGe8qtuuGxNSHWGkTWr43kHTJ+CpA= github.com/t3rm1n4l/go-mega v0.0.0-20240219080617-d494b6a8ace7/go.mod h1:suDIky6yrK07NnaBadCB4sS0CqFOvUK91lH7CR+JlDA= @@ -2704,8 +2705,8 @@ golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY= golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= -golang.org/x/net v0.31.0 h1:68CPQngjLL0r2AlUKiSxtQFKvzRVbnzLwMUn5SzcLHo= -golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM= +golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI= +golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -2735,8 +2736,8 @@ golang.org/x/oauth2 v0.4.0/go.mod h1:RznEsdpjGAINPTOF0UH/t+xJ75L18YO3Ho6Pyn+uRec golang.org/x/oauth2 v0.5.0/go.mod h1:9/XBHVqLaWO3/BRHs5jbpYCnOZVjj5V0ndyaAM7KB4I= golang.org/x/oauth2 v0.6.0/go.mod h1:ycmewcwgD4Rpr3eZJLSB4Kyyljb3qDh40vJ8STE5HKw= golang.org/x/oauth2 v0.7.0/go.mod h1:hPLQkd9LyjfXTiRohC/41GhcFqxisoUQ99sCUOHO9x4= -golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs= -golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= +golang.org/x/oauth2 v0.24.0 h1:KTBBxWqUa0ykRPLtV69rRto9TLXcqYkeswu48x/gvNE= +golang.org/x/oauth2 v0.24.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -3318,8 +3319,8 @@ google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqw google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= google.golang.org/protobuf v1.29.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= -google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA= -google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= +google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io= +google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= From 3b87986f42535e68e73767fa9f4b4ac0c7b8b744 Mon Sep 17 00:00:00 2001 From: Dmitry Khamitov Date: Tue, 17 Dec 2024 17:09:13 +0000 Subject: [PATCH 17/21] fix(inputs.mongodb): Do not dereference nil pointer if gathering database stats fails (#16310) --- plugins/inputs/mongodb/mongodb_server.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/inputs/mongodb/mongodb_server.go b/plugins/inputs/mongodb/mongodb_server.go index b0ea0bb35ad29..c8369c68bb89b 100644 --- a/plugins/inputs/mongodb/mongodb_server.go +++ b/plugins/inputs/mongodb/mongodb_server.go @@ -327,7 +327,8 @@ func (s *server) gatherData(acc telegraf.Accumulator, gatherClusterStatus, gathe for _, name := range names { db, err := s.gatherDBStats(name) if err != nil { - s.log.Debugf("Error getting db stats from %q: %s", name, err.Error()) + s.log.Errorf("Error getting db stats from %q: %v", name, err) + continue } dbStats.Dbs = append(dbStats.Dbs, *db) } From e2b5a9910b9804ff517543f9a0a40e3b71a35f34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20=C5=BBak?= Date: Tue, 17 Dec 2024 18:10:18 +0100 Subject: [PATCH 18/21] chore: Fix linter findings for `revive:exported` in `plugins/inputs/p*` (#16307) --- plugins/inputs/p4runtime/p4runtime_test.go | 18 +- plugins/inputs/passenger/passenger.go | 72 +++--- plugins/inputs/passenger/passenger_test.go | 8 +- plugins/inputs/pf/pf.go | 158 ++++++------ plugins/inputs/pgbouncer/pgbouncer.go | 18 +- plugins/inputs/phpfpm/child.go | 41 +--- plugins/inputs/phpfpm/fcgi_client.go | 2 +- plugins/inputs/phpfpm/fcgi_test.go | 4 +- plugins/inputs/phpfpm/phpfpm.go | 95 ++++--- plugins/inputs/phpfpm/phpfpm_test.go | 26 +- plugins/inputs/ping/ping.go | 144 +++++------ plugins/inputs/ping/ping_windows_test.go | 8 +- plugins/inputs/postfix/postfix.go | 60 ++--- plugins/inputs/postfix/postfix_windows.go | 6 +- plugins/inputs/postgresql/postgresql.go | 12 +- .../postgresql_extensible.go | 18 +- plugins/inputs/powerdns/powerdns.go | 9 +- .../powerdns_recursor/powerdns_recursor.go | 4 +- .../inputs/processes/processes_notwindows.go | 10 +- plugins/inputs/procstat/filter.go | 30 +-- plugins/inputs/procstat/native_finder.go | 52 ++-- plugins/inputs/procstat/native_finder_test.go | 20 +- plugins/inputs/procstat/os_linux.go | 28 +-- plugins/inputs/procstat/os_others.go | 16 +- plugins/inputs/procstat/os_windows.go | 20 +- plugins/inputs/procstat/pgrep.go | 30 +-- plugins/inputs/procstat/process.go | 68 ++--- plugins/inputs/procstat/procstat.go | 130 +++++----- plugins/inputs/procstat/procstat_test.go | 100 ++++---- plugins/inputs/procstat/service_finders.go | 15 +- plugins/inputs/prometheus/consul.go | 22 +- plugins/inputs/prometheus/kubernetes.go | 38 +-- plugins/inputs/prometheus/kubernetes_test.go | 48 ++-- plugins/inputs/prometheus/prometheus.go | 232 +++++++++--------- plugins/inputs/prometheus/prometheus_test.go | 2 +- plugins/inputs/proxmox/proxmox.go | 36 +-- plugins/inputs/proxmox/structs.go | 20 +- plugins/inputs/puppetagent/puppetagent.go | 9 +- 38 files changed, 777 insertions(+), 852 deletions(-) diff --git a/plugins/inputs/p4runtime/p4runtime_test.go b/plugins/inputs/p4runtime/p4runtime_test.go index 58dbb8336ceaa..2972963fc0fed 100644 --- a/plugins/inputs/p4runtime/p4runtime_test.go +++ b/plugins/inputs/p4runtime/p4runtime_test.go @@ -43,7 +43,7 @@ func createEntityCounterEntry( } } -func NewTestP4RuntimeClient( +func newTestP4RuntimeClient( p4RuntimeClient *fakeP4RuntimeClient, addr string, t *testing.T, @@ -102,7 +102,7 @@ func TestErrorGetP4Info(t *testing.T) { listener, err := net.Listen("tcp", "127.0.0.1:0") require.NoError(t, err) - plugin := NewTestP4RuntimeClient(p4RtClient, listener.Addr().String(), t) + plugin := newTestP4RuntimeClient(p4RtClient, listener.Addr().String(), t) var acc testutil.Accumulator require.Error(t, plugin.Gather(&acc)) @@ -245,7 +245,7 @@ func TestOneCounterRead(t *testing.T) { listener, err := net.Listen("tcp", "127.0.0.1:0") require.NoError(t, err) - plugin := NewTestP4RuntimeClient(p4RtClient, listener.Addr().String(), t) + plugin := newTestP4RuntimeClient(p4RtClient, listener.Addr().String(), t) var acc testutil.Accumulator require.NoError(t, plugin.Gather(&acc)) @@ -333,7 +333,7 @@ func TestMultipleEntitiesSingleCounterRead(t *testing.T) { listener, err := net.Listen("tcp", "127.0.0.1:0") require.NoError(t, err) - plugin := NewTestP4RuntimeClient(p4RtClient, listener.Addr().String(), t) + plugin := newTestP4RuntimeClient(p4RtClient, listener.Addr().String(), t) var acc testutil.Accumulator require.NoError(t, plugin.Gather(&acc)) @@ -425,7 +425,7 @@ func TestSingleEntitiesMultipleCounterRead(t *testing.T) { listener, err := net.Listen("tcp", "127.0.0.1:0") require.NoError(t, err) - plugin := NewTestP4RuntimeClient(p4RtClient, listener.Addr().String(), t) + plugin := newTestP4RuntimeClient(p4RtClient, listener.Addr().String(), t) var acc testutil.Accumulator require.NoError(t, plugin.Gather(&acc)) @@ -457,7 +457,7 @@ func TestNoCountersAvailable(t *testing.T) { listener, err := net.Listen("tcp", "127.0.0.1:0") require.NoError(t, err) - plugin := NewTestP4RuntimeClient(p4RtClient, listener.Addr().String(), t) + plugin := newTestP4RuntimeClient(p4RtClient, listener.Addr().String(), t) var acc testutil.Accumulator require.NoError(t, plugin.Gather(&acc)) @@ -484,7 +484,7 @@ func TestFilterCounters(t *testing.T) { listener, err := net.Listen("tcp", "127.0.0.1:0") require.NoError(t, err) - plugin := NewTestP4RuntimeClient(p4RtClient, listener.Addr().String(), t) + plugin := newTestP4RuntimeClient(p4RtClient, listener.Addr().String(), t) plugin.CounterNamesInclude = []string{"oof"} @@ -534,7 +534,7 @@ func TestFailReadCounterEntryFromEntry(t *testing.T) { listener, err := net.Listen("tcp", "127.0.0.1:0") require.NoError(t, err) - plugin := NewTestP4RuntimeClient(p4RtClient, listener.Addr().String(), t) + plugin := newTestP4RuntimeClient(p4RtClient, listener.Addr().String(), t) var acc testutil.Accumulator require.NoError(t, plugin.Gather(&acc)) @@ -577,7 +577,7 @@ func TestFailReadAllEntries(t *testing.T) { listener, err := net.Listen("tcp", "127.0.0.1:0") require.NoError(t, err) - plugin := NewTestP4RuntimeClient(p4RtClient, listener.Addr().String(), t) + plugin := newTestP4RuntimeClient(p4RtClient, listener.Addr().String(), t) var acc testutil.Accumulator require.NoError(t, plugin.Gather(&acc)) diff --git a/plugins/inputs/passenger/passenger.go b/plugins/inputs/passenger/passenger.go index 7123cc70b012d..0175a5000ced5 100644 --- a/plugins/inputs/passenger/passenger.go +++ b/plugins/inputs/passenger/passenger.go @@ -19,22 +19,8 @@ import ( //go:embed sample.conf var sampleConfig string -type passenger struct { - Command string -} - -func (p *passenger) parseCommand() (string, []string) { - var arguments []string - if !strings.Contains(p.Command, " ") { - return p.Command, arguments - } - - arguments = strings.Split(p.Command, " ") - if len(arguments) == 1 { - return arguments[0], arguments[1:] - } - - return arguments[0], arguments[1:] +type Passenger struct { + Command string `toml:"command"` } type info struct { @@ -91,6 +77,39 @@ type process struct { ProcessGroupID string `xml:"process_group_id"` } +func (*Passenger) SampleConfig() string { + return sampleConfig +} + +func (p *Passenger) Gather(acc telegraf.Accumulator) error { + if p.Command == "" { + p.Command = "passenger-status -v --show=xml" + } + + cmd, args := p.parseCommand() + out, err := exec.Command(cmd, args...).Output() + + if err != nil { + return err + } + + return importMetric(out, acc) +} + +func (p *Passenger) parseCommand() (string, []string) { + var arguments []string + if !strings.Contains(p.Command, " ") { + return p.Command, arguments + } + + arguments = strings.Split(p.Command, " ") + if len(arguments) == 1 { + return arguments[0], arguments[1:] + } + + return arguments[0], arguments[1:] +} + func (p *process) getUptime() int64 { if p.Uptime == "" { return 0 @@ -131,25 +150,6 @@ func (p *process) getUptime() int64 { return uptime } -func (*passenger) SampleConfig() string { - return sampleConfig -} - -func (p *passenger) Gather(acc telegraf.Accumulator) error { - if p.Command == "" { - p.Command = "passenger-status -v --show=xml" - } - - cmd, args := p.parseCommand() - out, err := exec.Command(cmd, args...).Output() - - if err != nil { - return err - } - - return importMetric(out, acc) -} - func importMetric(stat []byte, acc telegraf.Accumulator) error { var p info @@ -231,6 +231,6 @@ func importMetric(stat []byte, acc telegraf.Accumulator) error { func init() { inputs.Add("passenger", func() telegraf.Input { - return &passenger{} + return &Passenger{} }) } diff --git a/plugins/inputs/passenger/passenger_test.go b/plugins/inputs/passenger/passenger_test.go index 6c53578d7e636..49411d04919d5 100644 --- a/plugins/inputs/passenger/passenger_test.go +++ b/plugins/inputs/passenger/passenger_test.go @@ -39,7 +39,7 @@ func teardown(tempFilePath string) { } func Test_Invalid_Passenger_Status_Cli(t *testing.T) { - r := &passenger{ + r := &Passenger{ Command: "an-invalid-command passenger-status", } @@ -55,7 +55,7 @@ func Test_Invalid_Xml(t *testing.T) { require.NoError(t, err) defer teardown(tempFilePath) - r := &passenger{ + r := &Passenger{ Command: tempFilePath, } @@ -72,7 +72,7 @@ func Test_Default_Config_Load_Default_Command(t *testing.T) { require.NoError(t, err) defer teardown(tempFilePath) - r := &passenger{} + r := &Passenger{} var acc testutil.Accumulator @@ -87,7 +87,7 @@ func TestPassengerGenerateMetric(t *testing.T) { defer teardown(tempFilePath) // Now we tested again above server, with our authentication data - r := &passenger{ + r := &Passenger{ Command: tempFilePath, } diff --git a/plugins/inputs/pf/pf.go b/plugins/inputs/pf/pf.go index 204c30a5dbc96..20709aaf750d9 100644 --- a/plugins/inputs/pf/pf.go +++ b/plugins/inputs/pf/pf.go @@ -18,26 +18,81 @@ import ( //go:embed sample.conf var sampleConfig string -const measurement = "pf" -const pfctlCommand = "pfctl" +var ( + errParseHeader = fmt.Errorf("cannot find header in %s output", pfctlCommand) + anyTableHeaderRE = regexp.MustCompile("^[A-Z]") + stateTableRE = regexp.MustCompile(`^ (.*?)\s+(\d+)`) + counterTableRE = regexp.MustCompile(`^ (.*?)\s+(\d+)`) + execLookPath = exec.LookPath + execCommand = exec.Command + pfctlOutputStanzas = []*pfctlOutputStanza{ + { + headerRE: regexp.MustCompile("^State Table"), + parseFunc: parseStateTable, + }, + { + headerRE: regexp.MustCompile("^Counters"), + parseFunc: parseCounterTable, + }, + } + stateTable = []*entry{ + {"entries", "current entries", -1}, + {"searches", "searches", -1}, + {"inserts", "inserts", -1}, + {"removals", "removals", -1}, + } + counterTable = []*entry{ + {"match", "match", -1}, + {"bad-offset", "bad-offset", -1}, + {"fragment", "fragment", -1}, + {"short", "short", -1}, + {"normalize", "normalize", -1}, + {"memory", "memory", -1}, + {"bad-timestamp", "bad-timestamp", -1}, + {"congestion", "congestion", -1}, + {"ip-option", "ip-option", -1}, + {"proto-cksum", "proto-cksum", -1}, + {"state-mismatch", "state-mismatch", -1}, + {"state-insert", "state-insert", -1}, + {"state-limit", "state-limit", -1}, + {"src-limit", "src-limit", -1}, + {"synproxy", "synproxy", -1}, + } +) + +const ( + measurement = "pf" + pfctlCommand = "pfctl" +) type PF struct { - PfctlCommand string - PfctlArgs []string - UseSudo bool - StateTable []*Entry + UseSudo bool `toml:"use_sudo"` + + pfctlCommand string + pfctlArgs []string infoFunc func() (string, error) } +type pfctlOutputStanza struct { + headerRE *regexp.Regexp + parseFunc func([]string, map[string]interface{}) error + found bool +} + +type entry struct { + field string + pfctlTitle string + value int64 +} + func (*PF) SampleConfig() string { return sampleConfig } -// Gather is the entrypoint for the plugin. func (pf *PF) Gather(acc telegraf.Accumulator) error { - if pf.PfctlCommand == "" { + if pf.pfctlCommand == "" { var err error - if pf.PfctlCommand, pf.PfctlArgs, err = pf.buildPfctlCmd(); err != nil { + if pf.pfctlCommand, pf.pfctlArgs, err = pf.buildPfctlCmd(); err != nil { acc.AddError(fmt.Errorf("can't construct pfctl commandline: %w", err)) return nil } @@ -55,38 +110,17 @@ func (pf *PF) Gather(acc telegraf.Accumulator) error { return nil } -var errParseHeader = fmt.Errorf("cannot find header in %s output", pfctlCommand) - func errMissingData(tag string) error { return fmt.Errorf("struct data for tag %q not found in %s output", tag, pfctlCommand) } -type pfctlOutputStanza struct { - HeaderRE *regexp.Regexp - ParseFunc func([]string, map[string]interface{}) error - Found bool -} - -var pfctlOutputStanzas = []*pfctlOutputStanza{ - { - HeaderRE: regexp.MustCompile("^State Table"), - ParseFunc: parseStateTable, - }, - { - HeaderRE: regexp.MustCompile("^Counters"), - ParseFunc: parseCounterTable, - }, -} - -var anyTableHeaderRE = regexp.MustCompile("^[A-Z]") - func (pf *PF) parsePfctlOutput(pfoutput string, acc telegraf.Accumulator) error { fields := make(map[string]interface{}) scanner := bufio.NewScanner(strings.NewReader(pfoutput)) for scanner.Scan() { line := scanner.Text() for _, s := range pfctlOutputStanzas { - if s.HeaderRE.MatchString(line) { + if s.headerRE.MatchString(line) { var stanzaLines []string scanner.Scan() line = scanner.Text() @@ -98,15 +132,15 @@ func (pf *PF) parsePfctlOutput(pfoutput string, acc telegraf.Accumulator) error } line = scanner.Text() } - if perr := s.ParseFunc(stanzaLines, fields); perr != nil { + if perr := s.parseFunc(stanzaLines, fields); perr != nil { return perr } - s.Found = true + s.found = true } } } for _, s := range pfctlOutputStanzas { - if !s.Found { + if !s.found { return errParseHeader } } @@ -115,57 +149,22 @@ func (pf *PF) parsePfctlOutput(pfoutput string, acc telegraf.Accumulator) error return nil } -type Entry struct { - Field string - PfctlTitle string - Value int64 -} - -var StateTable = []*Entry{ - {"entries", "current entries", -1}, - {"searches", "searches", -1}, - {"inserts", "inserts", -1}, - {"removals", "removals", -1}, -} - -var stateTableRE = regexp.MustCompile(`^ (.*?)\s+(\d+)`) - func parseStateTable(lines []string, fields map[string]interface{}) error { - return storeFieldValues(lines, stateTableRE, fields, StateTable) + return storeFieldValues(lines, stateTableRE, fields, stateTable) } -var CounterTable = []*Entry{ - {"match", "match", -1}, - {"bad-offset", "bad-offset", -1}, - {"fragment", "fragment", -1}, - {"short", "short", -1}, - {"normalize", "normalize", -1}, - {"memory", "memory", -1}, - {"bad-timestamp", "bad-timestamp", -1}, - {"congestion", "congestion", -1}, - {"ip-option", "ip-option", -1}, - {"proto-cksum", "proto-cksum", -1}, - {"state-mismatch", "state-mismatch", -1}, - {"state-insert", "state-insert", -1}, - {"state-limit", "state-limit", -1}, - {"src-limit", "src-limit", -1}, - {"synproxy", "synproxy", -1}, -} - -var counterTableRE = regexp.MustCompile(`^ (.*?)\s+(\d+)`) - func parseCounterTable(lines []string, fields map[string]interface{}) error { - return storeFieldValues(lines, counterTableRE, fields, CounterTable) + return storeFieldValues(lines, counterTableRE, fields, counterTable) } -func storeFieldValues(lines []string, regex *regexp.Regexp, fields map[string]interface{}, entryTable []*Entry) error { +func storeFieldValues(lines []string, regex *regexp.Regexp, fields map[string]interface{}, entryTable []*entry) error { for _, v := range lines { entries := regex.FindStringSubmatch(v) if entries != nil { for _, f := range entryTable { - if f.PfctlTitle == entries[1] { + if f.pfctlTitle == entries[1] { var err error - if f.Value, err = strconv.ParseInt(entries[2], 10, 64); err != nil { + if f.value, err = strconv.ParseInt(entries[2], 10, 64); err != nil { return err } } @@ -174,17 +173,17 @@ func storeFieldValues(lines []string, regex *regexp.Regexp, fields map[string]in } for _, v := range entryTable { - if v.Value == -1 { - return errMissingData(v.PfctlTitle) + if v.value == -1 { + return errMissingData(v.pfctlTitle) } - fields[v.Field] = v.Value + fields[v.field] = v.value } return nil } func (pf *PF) callPfctl() (string, error) { - cmd := execCommand(pf.PfctlCommand, pf.PfctlArgs...) + cmd := execCommand(pf.pfctlCommand, pf.pfctlArgs...) out, oerr := cmd.Output() if oerr != nil { var ee *exec.ExitError @@ -196,9 +195,6 @@ func (pf *PF) callPfctl() (string, error) { return string(out), oerr } -var execLookPath = exec.LookPath -var execCommand = exec.Command - func (pf *PF) buildPfctlCmd() (string, []string, error) { cmd, err := execLookPath(pfctlCommand) if err != nil { diff --git a/plugins/inputs/pgbouncer/pgbouncer.go b/plugins/inputs/pgbouncer/pgbouncer.go index 4d079e1731f0a..2c6ccf43bc4bd 100644 --- a/plugins/inputs/pgbouncer/pgbouncer.go +++ b/plugins/inputs/pgbouncer/pgbouncer.go @@ -16,6 +16,11 @@ import ( //go:embed sample.conf var sampleConfig string +var ignoredColumns = map[string]bool{"user": true, "database": true, "pool_mode": true, + "avg_req": true, "avg_recv": true, "avg_sent": true, "avg_query": true, + "force_user": true, "host": true, "port": true, "name": true, +} + type PgBouncer struct { ShowCommands []string `toml:"show_commands"` postgresql.Config @@ -23,11 +28,6 @@ type PgBouncer struct { service *postgresql.Service } -var ignoredColumns = map[string]bool{"user": true, "database": true, "pool_mode": true, - "avg_req": true, "avg_recv": true, "avg_sent": true, "avg_query": true, - "force_user": true, "host": true, "port": true, "name": true, -} - func (*PgBouncer) SampleConfig() string { return sampleConfig } @@ -58,10 +58,6 @@ func (p *PgBouncer) Start(_ telegraf.Accumulator) error { return p.service.Start() } -func (p *PgBouncer) Stop() { - p.service.Stop() -} - func (p *PgBouncer) Gather(acc telegraf.Accumulator) error { for _, cmd := range p.ShowCommands { switch cmd { @@ -87,6 +83,10 @@ func (p *PgBouncer) Gather(acc telegraf.Accumulator) error { return nil } +func (p *PgBouncer) Stop() { + p.service.Stop() +} + func (p *PgBouncer) accRow(row *sql.Rows, columns []string) (map[string]string, map[string]*interface{}, error) { var dbname bytes.Buffer diff --git a/plugins/inputs/phpfpm/child.go b/plugins/inputs/phpfpm/child.go index 3448db40be4a9..f921dc4bf13d2 100644 --- a/plugins/inputs/phpfpm/child.go +++ b/plugins/inputs/phpfpm/child.go @@ -10,10 +10,8 @@ import ( "errors" "fmt" "io" - "net" "net/http" "net/http/cgi" - "os" "strings" "sync" "time" @@ -164,13 +162,13 @@ var errCloseConn = errors.New("fcgi: connection should be closed") var emptyBody = io.NopCloser(strings.NewReader("")) -// ErrRequestAborted is returned by Read when a handler attempts to read the +// errRequestAborted is returned by Read when a handler attempts to read the // body of a request that has been aborted by the web server. -var ErrRequestAborted = errors.New("fcgi: request aborted by web server") +var errRequestAborted = errors.New("fcgi: request aborted by web server") -// ErrConnClosed is returned by Read when a handler attempts to read the body of +// errConnClosed is returned by Read when a handler attempts to read the body of // a request after the connection to the web server has been closed. -var ErrConnClosed = errors.New("fcgi: connection to web server closed") +var errConnClosed = errors.New("fcgi: connection to web server closed") func (c *child) handleRecord(rec *record) error { c.mu.Lock() @@ -249,7 +247,7 @@ func (c *child) handleRecord(rec *record) error { return err } if req.pw != nil { - req.pw.CloseWithError(ErrRequestAborted) + req.pw.CloseWithError(errRequestAborted) } if !req.keepConn { // connection will close upon return @@ -306,34 +304,7 @@ func (c *child) cleanUp() { if req.pw != nil { // race with call to Close in c.serveRequest doesn't matter because // Pipe(Reader|Writer).Close are idempotent - req.pw.CloseWithError(ErrConnClosed) + req.pw.CloseWithError(errConnClosed) } } } - -// Serve accepts incoming FastCGI connections on the listener l, creating a new -// goroutine for each. The goroutine reads requests and then calls handler -// to reply to them. -// If l is nil, Serve accepts connections from os.Stdin. -// If handler is nil, http.DefaultServeMux is used. -func Serve(l net.Listener, handler http.Handler) error { - if l == nil { - var err error - l, err = net.FileListener(os.Stdin) - if err != nil { - return err - } - defer l.Close() - } - if handler == nil { - handler = http.DefaultServeMux - } - for { - rw, err := l.Accept() - if err != nil { - return err - } - c := newChild(rw, handler) - go c.serve() - } -} diff --git a/plugins/inputs/phpfpm/fcgi_client.go b/plugins/inputs/phpfpm/fcgi_client.go index f33b68d0af9a5..e982471b3d0e6 100644 --- a/plugins/inputs/phpfpm/fcgi_client.go +++ b/plugins/inputs/phpfpm/fcgi_client.go @@ -44,7 +44,7 @@ func newFcgiClient(timeout time.Duration, h string, args ...interface{}) (*conn, return &conn{rwc: con}, nil } -func (c *conn) Request(env map[string]string, requestData string) (retout, reterr []byte, err error) { +func (c *conn) request(env map[string]string, requestData string) (retout, reterr []byte, err error) { defer c.rwc.Close() var reqID uint16 = 1 diff --git a/plugins/inputs/phpfpm/fcgi_test.go b/plugins/inputs/phpfpm/fcgi_test.go index f96c22b6fec90..d039685bb05f8 100644 --- a/plugins/inputs/phpfpm/fcgi_test.go +++ b/plugins/inputs/phpfpm/fcgi_test.go @@ -206,7 +206,7 @@ var cleanUpTests = []struct { makeRecord(typeAbortRequest, nil), }, nil), - ErrRequestAborted, + errRequestAborted, }, // confirm that child.serve closes all pipes after error reading record { @@ -215,7 +215,7 @@ var cleanUpTests = []struct { nil, }, nil), - ErrConnClosed, + errConnClosed, }, } diff --git a/plugins/inputs/phpfpm/phpfpm.go b/plugins/inputs/phpfpm/phpfpm.go index e1b3ce515fd30..9b3c5dc2704c4 100644 --- a/plugins/inputs/phpfpm/phpfpm.go +++ b/plugins/inputs/phpfpm/phpfpm.go @@ -26,22 +26,31 @@ import ( var sampleConfig string const ( - PfPool = "pool" - PfProcessManager = "process manager" - PfStartSince = "start since" - PfAcceptedConn = "accepted conn" - PfListenQueue = "listen queue" - PfMaxListenQueue = "max listen queue" - PfListenQueueLen = "listen queue len" - PfIdleProcesses = "idle processes" - PfActiveProcesses = "active processes" - PfTotalProcesses = "total processes" - PfMaxActiveProcesses = "max active processes" - PfMaxChildrenReached = "max children reached" - PfSlowRequests = "slow requests" + pfPool = "pool" + pfStartSince = "start since" + pfAcceptedConn = "accepted conn" + pfListenQueue = "listen queue" + pfMaxListenQueue = "max listen queue" + pfListenQueueLen = "listen queue len" + pfIdleProcesses = "idle processes" + pfActiveProcesses = "active processes" + pfTotalProcesses = "total processes" + pfMaxActiveProcesses = "max active processes" + pfMaxChildrenReached = "max children reached" + pfSlowRequests = "slow requests" ) -type JSONMetrics struct { +type Phpfpm struct { + Format string `toml:"format"` + Timeout config.Duration `toml:"timeout"` + Urls []string `toml:"urls"` + Log telegraf.Logger `toml:"-"` + tls.ClientConfig + + client *http.Client +} + +type jsonMetrics struct { Pool string `json:"pool"` ProcessManager string `json:"process manager"` StartTime int `json:"start time"` @@ -76,21 +85,11 @@ type JSONMetrics struct { type metricStat map[string]int64 type poolStat map[string]metricStat -type phpfpm struct { - Format string `toml:"format"` - Timeout config.Duration `toml:"timeout"` - Urls []string `toml:"urls"` - Log telegraf.Logger `toml:"-"` - tls.ClientConfig - - client *http.Client -} - -func (*phpfpm) SampleConfig() string { +func (*Phpfpm) SampleConfig() string { return sampleConfig } -func (p *phpfpm) Init() error { +func (p *Phpfpm) Init() error { if len(p.Urls) == 0 { p.Urls = []string{"http://127.0.0.1/status"} } @@ -118,9 +117,7 @@ func (p *phpfpm) Init() error { return nil } -// Reads stats from all configured servers accumulates stats. -// Returns one of the errors encountered while gather stats (if any). -func (p *phpfpm) Gather(acc telegraf.Accumulator) error { +func (p *Phpfpm) Gather(acc telegraf.Accumulator) error { var wg sync.WaitGroup for _, serv := range expandUrls(acc, p.Urls) { wg.Add(1) @@ -136,7 +133,7 @@ func (p *phpfpm) Gather(acc telegraf.Accumulator) error { } // Request status page to get stat raw data and import it -func (p *phpfpm) gatherServer(addr string, acc telegraf.Accumulator) error { +func (p *Phpfpm) gatherServer(addr string, acc telegraf.Accumulator) error { if strings.HasPrefix(addr, "http://") || strings.HasPrefix(addr, "https://") { return p.gatherHTTP(addr, acc) } @@ -187,8 +184,8 @@ func (p *phpfpm) gatherServer(addr string, acc telegraf.Accumulator) error { } // Gather stat using fcgi protocol -func (p *phpfpm) gatherFcgi(fcgi *conn, statusPath string, acc telegraf.Accumulator, addr string) error { - fpmOutput, fpmErr, err := fcgi.Request(map[string]string{ +func (p *Phpfpm) gatherFcgi(fcgi *conn, statusPath string, acc telegraf.Accumulator, addr string) error { + fpmOutput, fpmErr, err := fcgi.request(map[string]string{ "SCRIPT_NAME": "/" + statusPath, "SCRIPT_FILENAME": statusPath, "REQUEST_METHOD": "GET", @@ -206,7 +203,7 @@ func (p *phpfpm) gatherFcgi(fcgi *conn, statusPath string, acc telegraf.Accumula } // Gather stat using http protocol -func (p *phpfpm) gatherHTTP(addr string, acc telegraf.Accumulator) error { +func (p *Phpfpm) gatherHTTP(addr string, acc telegraf.Accumulator) error { u, err := url.Parse(addr) if err != nil { return fmt.Errorf("unable parse server address %q: %w", addr, err) @@ -232,7 +229,7 @@ func (p *phpfpm) gatherHTTP(addr string, acc telegraf.Accumulator) error { } // Import stat data into Telegraf system -func (p *phpfpm) importMetric(r io.Reader, acc telegraf.Accumulator, addr string) { +func (p *Phpfpm) importMetric(r io.Reader, acc telegraf.Accumulator, addr string) { if p.Format == "json" { p.parseJSON(r, acc, addr) } else { @@ -254,7 +251,7 @@ func parseLines(r io.Reader, acc telegraf.Accumulator, addr string) { } fieldName := strings.Trim(keyvalue[0], " ") // We start to gather data for a new pool here - if fieldName == PfPool { + if fieldName == pfPool { currentPool = strings.Trim(keyvalue[1], " ") stats[currentPool] = make(metricStat) continue @@ -262,17 +259,17 @@ func parseLines(r io.Reader, acc telegraf.Accumulator, addr string) { // Start to parse metric for current pool switch fieldName { - case PfStartSince, - PfAcceptedConn, - PfListenQueue, - PfMaxListenQueue, - PfListenQueueLen, - PfIdleProcesses, - PfActiveProcesses, - PfTotalProcesses, - PfMaxActiveProcesses, - PfMaxChildrenReached, - PfSlowRequests: + case pfStartSince, + pfAcceptedConn, + pfListenQueue, + pfMaxListenQueue, + pfListenQueueLen, + pfIdleProcesses, + pfActiveProcesses, + pfTotalProcesses, + pfMaxActiveProcesses, + pfMaxChildrenReached, + pfSlowRequests: fieldValue, err := strconv.ParseInt(strings.Trim(keyvalue[1], " "), 10, 64) if err == nil { stats[currentPool][fieldName] = fieldValue @@ -294,8 +291,8 @@ func parseLines(r io.Reader, acc telegraf.Accumulator, addr string) { } } -func (p *phpfpm) parseJSON(r io.Reader, acc telegraf.Accumulator, addr string) { - var metrics JSONMetrics +func (p *Phpfpm) parseJSON(r io.Reader, acc telegraf.Accumulator, addr string) { + var metrics jsonMetrics if err := json.NewDecoder(r).Decode(&metrics); err != nil { p.Log.Errorf("Unable to decode JSON response: %s", err) return @@ -402,6 +399,6 @@ func isNetworkURL(addr string) bool { func init() { inputs.Add("phpfpm", func() telegraf.Input { - return &phpfpm{} + return &Phpfpm{} }) } diff --git a/plugins/inputs/phpfpm/phpfpm_test.go b/plugins/inputs/phpfpm/phpfpm_test.go index 92b3affa7ad08..802c761532ccc 100644 --- a/plugins/inputs/phpfpm/phpfpm_test.go +++ b/plugins/inputs/phpfpm/phpfpm_test.go @@ -56,7 +56,7 @@ func TestPhpFpmGeneratesMetrics_From_Http(t *testing.T) { defer ts.Close() url := ts.URL + "?test=ok" - r := &phpfpm{ + r := &Phpfpm{ Urls: []string{url}, Log: &testutil.Logger{}, } @@ -106,7 +106,7 @@ func TestPhpFpmGeneratesJSONMetrics_From_Http(t *testing.T) { expected, err := testutil.ParseMetricsFromFile("testdata/expected.out", parser) require.NoError(t, err) - input := &phpfpm{ + input := &Phpfpm{ Urls: []string{server.URL + "?full&json"}, Format: "json", Log: &testutil.Logger{}, @@ -128,7 +128,7 @@ func TestPhpFpmGeneratesMetrics_From_Fcgi(t *testing.T) { go fcgi.Serve(tcp, s) //nolint:errcheck // ignore the returned error as we cannot do anything about it anyway // Now we tested again above server - r := &phpfpm{ + r := &Phpfpm{ Urls: []string{"fcgi://" + tcp.Addr().String() + "/status"}, Log: &testutil.Logger{}, } @@ -179,7 +179,7 @@ func TestPhpFpmTimeout_From_Fcgi(t *testing.T) { }() // Now we tested again above server - r := &phpfpm{ + r := &Phpfpm{ Urls: []string{"fcgi://" + tcp.Addr().String() + "/status"}, Timeout: config.Duration(timeout), Log: &testutil.Logger{}, @@ -211,7 +211,7 @@ func TestPhpFpmCrashWithTimeout_From_Fcgi(t *testing.T) { const timeout = 200 * time.Millisecond // Now we tested again above server - r := &phpfpm{ + r := &Phpfpm{ Urls: []string{"fcgi://" + tcpAddress + "/status"}, Timeout: config.Duration(timeout), Log: &testutil.Logger{}, @@ -237,7 +237,7 @@ func TestPhpFpmGeneratesMetrics_From_Socket(t *testing.T) { s := statServer{} go fcgi.Serve(tcp, s) //nolint:errcheck // ignore the returned error as we cannot do anything about it anyway - r := &phpfpm{ + r := &Phpfpm{ Urls: []string{tcp.Addr().String()}, Log: &testutil.Logger{}, } @@ -289,7 +289,7 @@ func TestPhpFpmGeneratesMetrics_From_Multiple_Sockets_With_Glob(t *testing.T) { go fcgi.Serve(tcp1, s) //nolint:errcheck // ignore the returned error as we cannot do anything about it anyway go fcgi.Serve(tcp2, s) //nolint:errcheck // ignore the returned error as we cannot do anything about it anyway - r := &phpfpm{ + r := &Phpfpm{ Urls: []string{"/tmp/test-fpm[\\-0-9]*.sock"}, Log: &testutil.Logger{}, } @@ -340,7 +340,7 @@ func TestPhpFpmGeneratesMetrics_From_Socket_Custom_Status_Path(t *testing.T) { s := statServer{} go fcgi.Serve(tcp, s) //nolint:errcheck // ignore the returned error as we cannot do anything about it anyway - r := &phpfpm{ + r := &Phpfpm{ Urls: []string{tcp.Addr().String() + ":custom-status-path"}, Log: &testutil.Logger{}, } @@ -374,7 +374,7 @@ func TestPhpFpmGeneratesMetrics_From_Socket_Custom_Status_Path(t *testing.T) { // When not passing server config, we default to localhost // We just want to make sure we did request stat from localhost func TestPhpFpmDefaultGetFromLocalhost(t *testing.T) { - r := &phpfpm{ + r := &Phpfpm{ Urls: []string{"http://bad.localhost:62001/status"}, Log: &testutil.Logger{}, } @@ -389,7 +389,7 @@ func TestPhpFpmGeneratesMetrics_Throw_Error_When_Fpm_Status_Is_Not_Responding(t t.Skip("Skipping long test in short mode") } - r := &phpfpm{ + r := &Phpfpm{ Urls: []string{"http://aninvalidone"}, Log: &testutil.Logger{}, } @@ -402,7 +402,7 @@ func TestPhpFpmGeneratesMetrics_Throw_Error_When_Fpm_Status_Is_Not_Responding(t } func TestPhpFpmGeneratesMetrics_Throw_Error_When_Socket_Path_Is_Invalid(t *testing.T) { - r := &phpfpm{ + r := &Phpfpm{ Urls: []string{"/tmp/invalid.sock"}, Log: &testutil.Logger{}, } @@ -435,7 +435,7 @@ var outputSampleJSON []byte func TestPhpFpmParseJSON_Log_Error_Without_Panic_When_When_JSON_Is_Invalid(t *testing.T) { // Capture the logging output for checking logger := &testutil.CaptureLogger{Name: "inputs.phpfpm"} - plugin := &phpfpm{Log: logger} + plugin := &Phpfpm{Log: logger} require.NoError(t, plugin.Init()) // parse valid JSON without panic and without log output @@ -459,7 +459,7 @@ func TestGatherDespiteUnavailable(t *testing.T) { go fcgi.Serve(tcp, s) //nolint:errcheck // ignore the returned error as we cannot do anything about it anyway // Now we tested again above server - r := &phpfpm{ + r := &Phpfpm{ Urls: []string{"fcgi://" + tcp.Addr().String() + "/status", "/lala"}, Log: &testutil.Logger{}, } diff --git a/plugins/inputs/ping/ping.go b/plugins/inputs/ping/ping.go index 9f2e692f1cf70..8538d394bc809 100644 --- a/plugins/inputs/ping/ping.go +++ b/plugins/inputs/ping/ping.go @@ -28,71 +28,69 @@ const ( defaultPingDataBytesSize = 56 ) -// HostPinger is a function that runs the "ping" function using a list of -// passed arguments. This can be easily switched with a mocked ping function -// for unit test purposes (see ping_test.go) -type HostPinger func(binary string, timeout float64, args ...string) (string, error) - type Ping struct { - // wg is used to wait for ping with multiple URLs - wg sync.WaitGroup - - // Pre-calculated interval and timeout - calcInterval time.Duration - calcTimeout time.Duration - - sourceAddress string - - Log telegraf.Logger `toml:"-"` - - // Interval at which to ping (ping -i ) - PingInterval float64 `toml:"ping_interval"` - - // Number of pings to send (ping -c ) - Count int - - // Per-ping timeout, in seconds. 0 means no timeout (ping -W ) - Timeout float64 - - // Ping deadline, in seconds. 0 means no deadline. (ping -w ) - Deadline int - - // Interface or source address to send ping from (ping -I/-S ) - Interface string - - // URLs to ping - Urls []string - - // Method defines how to ping (native or exec) - Method string + Urls []string `toml:"urls"` // URLs to ping + Method string `toml:"method"` // Method defines how to ping (native or exec) + Count int `toml:"count"` // Number of pings to send (ping -c ) + PingInterval float64 `toml:"ping_interval"` // Interval at which to ping (ping -i ) + Timeout float64 `toml:"timeout"` // Per-ping timeout, in seconds. 0 means no timeout (ping -W ) + Deadline int `toml:"deadline"` // Ping deadline, in seconds. 0 means no deadline. (ping -w ) + Interface string `toml:"interface"` // Interface or source address to send ping from (ping -I/-S ) + Percentiles []int `toml:"percentiles"` // Calculate the given percentiles when using native method + Binary string `toml:"binary"` // Ping executable binary + // Arguments for ping command. When arguments are not empty, system binary will be used and other options (ping_interval, timeout, etc.) will be ignored + Arguments []string `toml:"arguments"` + IPv4 bool `toml:"ipv4"` // Whether to resolve addresses using ipv4 or not. + IPv6 bool `toml:"ipv6"` // Whether to resolve addresses using ipv6 or not. + Size *int `toml:"size"` // Packet size + Log telegraf.Logger `toml:"-"` + + wg sync.WaitGroup // wg is used to wait for ping with multiple URLs + calcInterval time.Duration // Pre-calculated interval and timeout + calcTimeout time.Duration + sourceAddress string + pingHost hostPingerFunc // host ping function + nativePingFunc nativePingFunc +} - // Ping executable binary - Binary string +// hostPingerFunc is a function that runs the "ping" function using a list of +// passed arguments. This can be easily switched with a mocked ping function +// for unit test purposes (see ping_test.go) +type hostPingerFunc func(binary string, timeout float64, args ...string) (string, error) - // Arguments for ping command. When arguments is not empty, system binary will be used and - // other options (ping_interval, timeout, etc.) will be ignored - Arguments []string +type nativePingFunc func(destination string) (*pingStats, error) - // Whether to resolve addresses using ipv4 or not. - IPv4 bool +type durationSlice []time.Duration - // Whether to resolve addresses using ipv6 or not. - IPv6 bool +type pingStats struct { + ping.Statistics + ttl int +} - // host ping function - pingHost HostPinger +func (*Ping) SampleConfig() string { + return sampleConfig +} - nativePingFunc NativePingFunc +func (p *Ping) Init() error { + if p.Count < 1 { + return errors.New("bad number of packets to transmit") + } - // Calculate the given percentiles when using native method - Percentiles []int + // The interval cannot be below 0.2 seconds, matching ping implementation: https://linux.die.net/man/8/ping + if p.PingInterval < 0.2 { + p.calcInterval = time.Duration(.2 * float64(time.Second)) + } else { + p.calcInterval = time.Duration(p.PingInterval * float64(time.Second)) + } - // Packet size - Size *int -} + // If no timeout is given default to 5 seconds, matching original implementation + if p.Timeout == 0 { + p.calcTimeout = time.Duration(5) * time.Second + } else { + p.calcTimeout = time.Duration(p.Timeout) * time.Second + } -func (*Ping) SampleConfig() string { - return sampleConfig + return nil } func (p *Ping) Gather(acc telegraf.Accumulator) error { @@ -115,13 +113,6 @@ func (p *Ping) Gather(acc telegraf.Accumulator) error { return nil } -type pingStats struct { - ping.Statistics - ttl int -} - -type NativePingFunc func(destination string) (*pingStats, error) - func (p *Ping) nativePing(destination string) (*pingStats, error) { ps := &pingStats{} @@ -259,11 +250,11 @@ func (p *Ping) pingToURLNative(destination string, acc telegraf.Accumulator) { acc.AddFields("ping", fields, tags) } -type durationSlice []time.Duration +func (p durationSlice) Len() int { return len(p) } -func (p durationSlice) Len() int { return len(p) } func (p durationSlice) Less(i, j int) bool { return p[i] < p[j] } -func (p durationSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } + +func (p durationSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } // R7 from Hyndman and Fan (1996), which matches Excel func percentile(values durationSlice, perc int) time.Duration { @@ -292,29 +283,6 @@ func percentile(values durationSlice, perc int) time.Duration { return lower + time.Duration(rankFraction*float64(upper-lower)) } -// Init ensures the plugin is configured correctly. -func (p *Ping) Init() error { - if p.Count < 1 { - return errors.New("bad number of packets to transmit") - } - - // The interval cannot be below 0.2 seconds, matching ping implementation: https://linux.die.net/man/8/ping - if p.PingInterval < 0.2 { - p.calcInterval = time.Duration(.2 * float64(time.Second)) - } else { - p.calcInterval = time.Duration(p.PingInterval * float64(time.Second)) - } - - // If no timeout is given default to 5 seconds, matching original implementation - if p.Timeout == 0 { - p.calcTimeout = time.Duration(5) * time.Second - } else { - p.calcTimeout = time.Duration(p.Timeout) * time.Second - } - - return nil -} - func hostPinger(binary string, timeout float64, args ...string) (string, error) { bin, err := exec.LookPath(binary) if err != nil { diff --git a/plugins/inputs/ping/ping_windows_test.go b/plugins/inputs/ping/ping_windows_test.go index 4517bf8f33736..93b2bd04ff99a 100644 --- a/plugins/inputs/ping/ping_windows_test.go +++ b/plugins/inputs/ping/ping_windows_test.go @@ -261,7 +261,7 @@ func TestFatalPingGather(t *testing.T) { "Fatal ping should not have packet measurements") } -var UnreachablePingOutput = ` +var unreachablePingOutput = ` Pinging www.google.pl [8.8.8.8] with 32 bytes of data: Request timed out. Request timed out. @@ -273,7 +273,7 @@ Ping statistics for 8.8.8.8: ` func mockUnreachableHostPinger(string, float64, ...string) (string, error) { - return UnreachablePingOutput, errors.New("so very bad") + return unreachablePingOutput, errors.New("so very bad") } // Reply from 185.28.251.217: TTL expired in transit. @@ -312,7 +312,7 @@ func TestUnreachablePingGather(t *testing.T) { "Fatal ping should not have packet measurements") } -var TTLExpiredPingOutput = ` +var ttlExpiredPingOutput = ` Pinging www.google.pl [8.8.8.8] with 32 bytes of data: Request timed out. Request timed out. @@ -324,7 +324,7 @@ Ping statistics for 8.8.8.8: ` func mockTTLExpiredPinger(string, float64, ...string) (string, error) { - return TTLExpiredPingOutput, errors.New("so very bad") + return ttlExpiredPingOutput, errors.New("so very bad") } // in case 'Destination net unreachable' ping app return receive packet which is not what we need diff --git a/plugins/inputs/postfix/postfix.go b/plugins/inputs/postfix/postfix.go index cc5c7024c57e8..f657404d2882e 100644 --- a/plugins/inputs/postfix/postfix.go +++ b/plugins/inputs/postfix/postfix.go @@ -21,6 +21,36 @@ import ( //go:embed sample.conf var sampleConfig string +type Postfix struct { + QueueDirectory string `toml:"queue_directory"` +} + +func (*Postfix) SampleConfig() string { + return sampleConfig +} + +func (p *Postfix) Gather(acc telegraf.Accumulator) error { + if p.QueueDirectory == "" { + var err error + p.QueueDirectory, err = getQueueDirectory() + if err != nil { + return fmt.Errorf("unable to determine queue directory: %w", err) + } + } + + for _, q := range []string{"active", "hold", "incoming", "maildrop", "deferred"} { + fields, err := qScan(filepath.Join(p.QueueDirectory, q), acc) + if err != nil { + acc.AddError(fmt.Errorf("error scanning queue %q: %w", q, err)) + continue + } + + acc.AddFields("postfix_queue", fields, map[string]string{"queue": q}) + } + + return nil +} + func getQueueDirectory() (string, error) { qd, err := exec.Command("postconf", "-h", "queue_directory").Output() if err != nil { @@ -75,36 +105,6 @@ func qScan(path string, acc telegraf.Accumulator) (map[string]interface{}, error return fields, nil } -type Postfix struct { - QueueDirectory string -} - -func (*Postfix) SampleConfig() string { - return sampleConfig -} - -func (p *Postfix) Gather(acc telegraf.Accumulator) error { - if p.QueueDirectory == "" { - var err error - p.QueueDirectory, err = getQueueDirectory() - if err != nil { - return fmt.Errorf("unable to determine queue directory: %w", err) - } - } - - for _, q := range []string{"active", "hold", "incoming", "maildrop", "deferred"} { - fields, err := qScan(filepath.Join(p.QueueDirectory, q), acc) - if err != nil { - acc.AddError(fmt.Errorf("error scanning queue %q: %w", q, err)) - continue - } - - acc.AddFields("postfix_queue", fields, map[string]string{"queue": q}) - } - - return nil -} - func init() { inputs.Add("postfix", func() telegraf.Input { return &Postfix{ diff --git a/plugins/inputs/postfix/postfix_windows.go b/plugins/inputs/postfix/postfix_windows.go index 3b027f24a2ade..9831787ff7194 100644 --- a/plugins/inputs/postfix/postfix_windows.go +++ b/plugins/inputs/postfix/postfix_windows.go @@ -16,11 +16,13 @@ type Postfix struct { Log telegraf.Logger `toml:"-"` } +func (*Postfix) SampleConfig() string { return sampleConfig } + func (p *Postfix) Init() error { - p.Log.Warn("current platform is not supported") + p.Log.Warn("Current platform is not supported") return nil } -func (*Postfix) SampleConfig() string { return sampleConfig } + func (*Postfix) Gather(_ telegraf.Accumulator) error { return nil } func init() { diff --git a/plugins/inputs/postgresql/postgresql.go b/plugins/inputs/postgresql/postgresql.go index dc8f37ca8d6d3..46b2354874cb2 100644 --- a/plugins/inputs/postgresql/postgresql.go +++ b/plugins/inputs/postgresql/postgresql.go @@ -16,6 +16,8 @@ import ( //go:embed sample.conf var sampleConfig string +var ignoredColumns = map[string]bool{"stats_reset": true} + type Postgresql struct { Databases []string `toml:"databases"` IgnoredDatabases []string `toml:"ignored_databases"` @@ -25,8 +27,6 @@ type Postgresql struct { service *postgresql.Service } -var ignoredColumns = map[string]bool{"stats_reset": true} - func (*Postgresql) SampleConfig() string { return sampleConfig } @@ -47,10 +47,6 @@ func (p *Postgresql) Start(_ telegraf.Accumulator) error { return p.service.Start() } -func (p *Postgresql) Stop() { - p.service.Stop() -} - func (p *Postgresql) Gather(acc telegraf.Accumulator) error { var query string if len(p.Databases) == 0 && len(p.IgnoredDatabases) == 0 { @@ -106,6 +102,10 @@ func (p *Postgresql) Gather(acc telegraf.Accumulator) error { return bgWriterRow.Err() } +func (p *Postgresql) Stop() { + p.service.Stop() +} + func (p *Postgresql) accRow(row *sql.Rows, acc telegraf.Accumulator, columns []string) error { var dbname bytes.Buffer diff --git a/plugins/inputs/postgresql_extensible/postgresql_extensible.go b/plugins/inputs/postgresql_extensible/postgresql_extensible.go index a4d867b8435c6..cb10f266bcedd 100644 --- a/plugins/inputs/postgresql_extensible/postgresql_extensible.go +++ b/plugins/inputs/postgresql_extensible/postgresql_extensible.go @@ -21,6 +21,8 @@ import ( //go:embed sample.conf var sampleConfig string +var ignoredColumns = map[string]bool{"stats_reset": true} + type Postgresql struct { Databases []string `deprecated:"1.22.4;use the sqlquery option to specify database to use"` Query []query `toml:"query"` @@ -45,7 +47,9 @@ type query struct { additionalTags map[string]bool } -var ignoredColumns = map[string]bool{"stats_reset": true} +type scanner interface { + Scan(dest ...interface{}) error +} func (*Postgresql) SampleConfig() string { return sampleConfig @@ -102,10 +106,6 @@ func (p *Postgresql) Start(_ telegraf.Accumulator) error { return p.service.Start() } -func (p *Postgresql) Stop() { - p.service.Stop() -} - func (p *Postgresql) Gather(acc telegraf.Accumulator) error { // Retrieving the database version query := `SELECT setting::integer / 100 AS version FROM pg_settings WHERE name = 'server_version_num'` @@ -128,6 +128,10 @@ func (p *Postgresql) Gather(acc telegraf.Accumulator) error { return nil } +func (p *Postgresql) Stop() { + p.service.Stop() +} + func (p *Postgresql) gatherMetricsFromQuery(acc telegraf.Accumulator, q query, timestamp time.Time) error { rows, err := p.service.DB.Query(q.Sqlquery) if err != nil { @@ -150,10 +154,6 @@ func (p *Postgresql) gatherMetricsFromQuery(acc telegraf.Accumulator, q query, t return nil } -type scanner interface { - Scan(dest ...interface{}) error -} - func (p *Postgresql) accRow(acc telegraf.Accumulator, row scanner, columns []string, q query, timestamp time.Time) error { // this is where we'll store the column name with its *interface{} columnMap := make(map[string]*interface{}) diff --git a/plugins/inputs/powerdns/powerdns.go b/plugins/inputs/powerdns/powerdns.go index 44c765348a646..5ac8397e077fc 100644 --- a/plugins/inputs/powerdns/powerdns.go +++ b/plugins/inputs/powerdns/powerdns.go @@ -19,14 +19,13 @@ import ( //go:embed sample.conf var sampleConfig string -type Powerdns struct { - UnixSockets []string +const defaultTimeout = 5 * time.Second - Log telegraf.Logger `toml:"-"` +type Powerdns struct { + UnixSockets []string `toml:"unix_sockets"` + Log telegraf.Logger `toml:"-"` } -var defaultTimeout = 5 * time.Second - func (*Powerdns) SampleConfig() string { return sampleConfig } diff --git a/plugins/inputs/powerdns_recursor/powerdns_recursor.go b/plugins/inputs/powerdns_recursor/powerdns_recursor.go index 48e83179a4746..48a77518f5a6a 100644 --- a/plugins/inputs/powerdns_recursor/powerdns_recursor.go +++ b/plugins/inputs/powerdns_recursor/powerdns_recursor.go @@ -14,6 +14,8 @@ import ( //go:embed sample.conf var sampleConfig string +const defaultTimeout = 5 * time.Second + type PowerdnsRecursor struct { UnixSockets []string `toml:"unix_sockets"` SocketDir string `toml:"socket_dir"` @@ -26,8 +28,6 @@ type PowerdnsRecursor struct { gatherFromServer func(address string, acc telegraf.Accumulator) error } -var defaultTimeout = 5 * time.Second - func (*PowerdnsRecursor) SampleConfig() string { return sampleConfig } diff --git a/plugins/inputs/processes/processes_notwindows.go b/plugins/inputs/processes/processes_notwindows.go index c574238fd5a23..e476e8ff2454f 100644 --- a/plugins/inputs/processes/processes_notwindows.go +++ b/plugins/inputs/processes/processes_notwindows.go @@ -19,15 +19,13 @@ import ( ) type Processes struct { - UseSudo bool `toml:"use_sudo"` + UseSudo bool `toml:"use_sudo"` + Log telegraf.Logger `toml:"-"` execPS func(UseSudo bool) ([]byte, error) readProcFile func(filename string) ([]byte, error) - - Log telegraf.Logger - - forcePS bool - forceProc bool + forcePS bool + forceProc bool } func (p *Processes) Gather(acc telegraf.Accumulator) error { diff --git a/plugins/inputs/procstat/filter.go b/plugins/inputs/procstat/filter.go index 3c090549c0718..d8f621048b77e 100644 --- a/plugins/inputs/procstat/filter.go +++ b/plugins/inputs/procstat/filter.go @@ -7,13 +7,13 @@ import ( "strconv" "strings" - "github.com/shirou/gopsutil/v4/process" + gopsprocess "github.com/shirou/gopsutil/v4/process" "github.com/influxdata/telegraf" - "github.com/influxdata/telegraf/filter" + telegraf_filter "github.com/influxdata/telegraf/filter" ) -type Filter struct { +type filter struct { Name string `toml:"name"` PidFiles []string `toml:"pid_files"` SystemdUnits []string `toml:"systemd_units"` @@ -29,13 +29,13 @@ type Filter struct { filterSupervisorUnit string filterCmds []*regexp.Regexp - filterUser filter.Filter - filterExecutable filter.Filter - filterProcessName filter.Filter + filterUser telegraf_filter.Filter + filterExecutable telegraf_filter.Filter + filterProcessName telegraf_filter.Filter finder *processFinder } -func (f *Filter) Init() error { +func (f *filter) init() error { if f.Name == "" { return errors.New("filter must be named") } @@ -74,13 +74,13 @@ func (f *Filter) Init() error { f.filterSupervisorUnit = strings.TrimSpace(strings.Join(f.SupervisorUnits, " ")) var err error - if f.filterUser, err = filter.Compile(f.Users); err != nil { + if f.filterUser, err = telegraf_filter.Compile(f.Users); err != nil { return fmt.Errorf("compiling users filter for %q failed: %w", f.Name, err) } - if f.filterExecutable, err = filter.Compile(f.Executables); err != nil { + if f.filterExecutable, err = telegraf_filter.Compile(f.Executables); err != nil { return fmt.Errorf("compiling executables filter for %q failed: %w", f.Name, err) } - if f.filterProcessName, err = filter.Compile(f.ProcessNames); err != nil { + if f.filterProcessName, err = telegraf_filter.Compile(f.ProcessNames); err != nil { return fmt.Errorf("compiling process-names filter for %q failed: %w", f.Name, err) } @@ -89,7 +89,7 @@ func (f *Filter) Init() error { return nil } -func (f *Filter) ApplyFilter() ([]processGroup, error) { +func (f *filter) applyFilter() ([]processGroup, error) { // Determine processes on service level. if there is no constraint on the // services, use all processes for matching. var groups []processGroup @@ -125,7 +125,7 @@ func (f *Filter) ApplyFilter() ([]processGroup, error) { } groups = append(groups, g...) default: - procs, err := process.Processes() + procs, err := gopsprocess.Processes() if err != nil { return nil, err } @@ -135,7 +135,7 @@ func (f *Filter) ApplyFilter() ([]processGroup, error) { // Filter by additional properties such as users, patterns etc result := make([]processGroup, 0, len(groups)) for _, g := range groups { - var matched []*process.Process + var matched []*gopsprocess.Process for _, p := range g.processes { // Users if f.filterUser != nil { @@ -218,13 +218,13 @@ func (f *Filter) ApplyFilter() ([]processGroup, error) { return result, nil } -func getChildren(p *process.Process) ([]*process.Process, error) { +func getChildren(p *gopsprocess.Process) ([]*gopsprocess.Process, error) { children, err := p.Children() // Check for cases that do not really mean error but rather means that there // is no match. switch { case err == nil, - errors.Is(err, process.ErrorNoChildren), + errors.Is(err, gopsprocess.ErrorNoChildren), strings.Contains(err.Error(), "exit status 1"): return children, nil } diff --git a/plugins/inputs/procstat/native_finder.go b/plugins/inputs/procstat/native_finder.go index 5f9812782b094..192a431acd503 100644 --- a/plugins/inputs/procstat/native_finder.go +++ b/plugins/inputs/procstat/native_finder.go @@ -7,16 +7,16 @@ import ( "strconv" "strings" - "github.com/shirou/gopsutil/v4/process" + gopsprocess "github.com/shirou/gopsutil/v4/process" ) // NativeFinder uses gopsutil to find processes type NativeFinder struct{} // Uid will return all pids for the given user -func (pg *NativeFinder) UID(user string) ([]PID, error) { - var dst []PID - procs, err := process.Processes() +func (pg *NativeFinder) uid(user string) ([]pid, error) { + var dst []pid + procs, err := gopsprocess.Processes() if err != nil { return dst, err } @@ -27,35 +27,35 @@ func (pg *NativeFinder) UID(user string) ([]PID, error) { continue } if username == user { - dst = append(dst, PID(p.Pid)) + dst = append(dst, pid(p.Pid)) } } return dst, nil } // PidFile returns the pid from the pid file given. -func (pg *NativeFinder) PidFile(path string) ([]PID, error) { - var pids []PID +func (pg *NativeFinder) pidFile(path string) ([]pid, error) { + var pids []pid pidString, err := os.ReadFile(path) if err != nil { return pids, fmt.Errorf("failed to read pidfile %q: %w", path, err) } - pid, err := strconv.ParseInt(strings.TrimSpace(string(pidString)), 10, 32) + processID, err := strconv.ParseInt(strings.TrimSpace(string(pidString)), 10, 32) if err != nil { return pids, err } - pids = append(pids, PID(pid)) + pids = append(pids, pid(processID)) return pids, nil } // FullPattern matches on the command line when the process was executed -func (pg *NativeFinder) FullPattern(pattern string) ([]PID, error) { - var pids []PID +func (pg *NativeFinder) fullPattern(pattern string) ([]pid, error) { + var pids []pid regxPattern, err := regexp.Compile(pattern) if err != nil { return pids, err } - procs, err := pg.FastProcessList() + procs, err := pg.fastProcessList() if err != nil { return pids, err } @@ -66,18 +66,18 @@ func (pg *NativeFinder) FullPattern(pattern string) ([]PID, error) { continue } if regxPattern.MatchString(cmd) { - pids = append(pids, PID(p.Pid)) + pids = append(pids, pid(p.Pid)) } } return pids, err } // Children matches children pids on the command line when the process was executed -func (pg *NativeFinder) Children(pid PID) ([]PID, error) { +func (pg *NativeFinder) children(processID pid) ([]pid, error) { // Get all running processes - p, err := process.NewProcess(int32(pid)) + p, err := gopsprocess.NewProcess(int32(processID)) if err != nil { - return nil, fmt.Errorf("getting process %d failed: %w", pid, err) + return nil, fmt.Errorf("getting process %d failed: %w", processID, err) } // Get all children of the current process @@ -85,35 +85,35 @@ func (pg *NativeFinder) Children(pid PID) ([]PID, error) { if err != nil { return nil, fmt.Errorf("unable to get children of process %d: %w", p.Pid, err) } - pids := make([]PID, 0, len(children)) + pids := make([]pid, 0, len(children)) for _, child := range children { - pids = append(pids, PID(child.Pid)) + pids = append(pids, pid(child.Pid)) } return pids, err } -func (pg *NativeFinder) FastProcessList() ([]*process.Process, error) { - pids, err := process.Pids() +func (pg *NativeFinder) fastProcessList() ([]*gopsprocess.Process, error) { + pids, err := gopsprocess.Pids() if err != nil { return nil, err } - result := make([]*process.Process, 0, len(pids)) + result := make([]*gopsprocess.Process, 0, len(pids)) for _, pid := range pids { - result = append(result, &process.Process{Pid: pid}) + result = append(result, &gopsprocess.Process{Pid: pid}) } return result, nil } // Pattern matches on the process name -func (pg *NativeFinder) Pattern(pattern string) ([]PID, error) { - var pids []PID +func (pg *NativeFinder) pattern(pattern string) ([]pid, error) { + var pids []pid regxPattern, err := regexp.Compile(pattern) if err != nil { return pids, err } - procs, err := pg.FastProcessList() + procs, err := pg.fastProcessList() if err != nil { return pids, err } @@ -124,7 +124,7 @@ func (pg *NativeFinder) Pattern(pattern string) ([]PID, error) { continue } if regxPattern.MatchString(name) { - pids = append(pids, PID(p.Pid)) + pids = append(pids, pid(p.Pid)) } } return pids, err diff --git a/plugins/inputs/procstat/native_finder_test.go b/plugins/inputs/procstat/native_finder_test.go index 1e6c6d84ade0c..e4e6e0bb8726d 100644 --- a/plugins/inputs/procstat/native_finder_test.go +++ b/plugins/inputs/procstat/native_finder_test.go @@ -14,7 +14,7 @@ import ( func BenchmarkPattern(b *testing.B) { finder := &NativeFinder{} for n := 0; n < b.N; n++ { - _, err := finder.Pattern(".*") + _, err := finder.pattern(".*") require.NoError(b, err) } } @@ -22,7 +22,7 @@ func BenchmarkPattern(b *testing.B) { func BenchmarkFullPattern(b *testing.B) { finder := &NativeFinder{} for n := 0; n < b.N; n++ { - _, err := finder.FullPattern(".*") + _, err := finder.fullPattern(".*") require.NoError(b, err) } } @@ -37,26 +37,26 @@ func TestChildPattern(t *testing.T) { require.NoError(t, err) // Spawn two child processes and get their PIDs - expected := make([]PID, 0, 2) + expected := make([]pid, 0, 2) ctx, cancel := context.WithCancel(context.Background()) defer cancel() // First process cmd1 := exec.CommandContext(ctx, "/bin/sh") require.NoError(t, cmd1.Start(), "starting first command failed") - expected = append(expected, PID(cmd1.Process.Pid)) + expected = append(expected, pid(cmd1.Process.Pid)) // Second process cmd2 := exec.CommandContext(ctx, "/bin/sh") require.NoError(t, cmd2.Start(), "starting first command failed") - expected = append(expected, PID(cmd2.Process.Pid)) + expected = append(expected, pid(cmd2.Process.Pid)) // Use the plugin to find the children finder := &NativeFinder{} - parent, err := finder.Pattern(parentName) + parent, err := finder.pattern(parentName) require.NoError(t, err) require.Len(t, parent, 1) - children, err := finder.Children(parent[0]) + children, err := finder.children(parent[0]) require.NoError(t, err) require.ElementsMatch(t, expected, children) } @@ -66,7 +66,7 @@ func TestGather_RealPatternIntegration(t *testing.T) { t.Skip("Skipping integration test in short mode") } pg := &NativeFinder{} - pids, err := pg.Pattern(`procstat`) + pids, err := pg.pattern(`procstat`) require.NoError(t, err) require.NotEmpty(t, pids) } @@ -79,7 +79,7 @@ func TestGather_RealFullPatternIntegration(t *testing.T) { t.Skip("Skipping integration test on Non-Windows OS") } pg := &NativeFinder{} - pids, err := pg.FullPattern(`%procstat%`) + pids, err := pg.fullPattern(`%procstat%`) require.NoError(t, err) require.NotEmpty(t, pids) } @@ -92,7 +92,7 @@ func TestGather_RealUserIntegration(t *testing.T) { require.NoError(t, err) pg := &NativeFinder{} - pids, err := pg.UID(currentUser.Username) + pids, err := pg.uid(currentUser.Username) require.NoError(t, err) require.NotEmpty(t, pids) } diff --git a/plugins/inputs/procstat/os_linux.go b/plugins/inputs/procstat/os_linux.go index 6c9d906faa276..cec134ee33232 100644 --- a/plugins/inputs/procstat/os_linux.go +++ b/plugins/inputs/procstat/os_linux.go @@ -13,15 +13,15 @@ import ( "github.com/coreos/go-systemd/v22/dbus" "github.com/prometheus/procfs" - "github.com/shirou/gopsutil/v4/net" - "github.com/shirou/gopsutil/v4/process" + gopsnet "github.com/shirou/gopsutil/v4/net" + gopsprocess "github.com/shirou/gopsutil/v4/process" "github.com/vishvananda/netlink" "golang.org/x/sys/unix" "github.com/influxdata/telegraf/internal" ) -func processName(p *process.Process) (string, error) { +func processName(p *gopsprocess.Process) (string, error) { return p.Exe() } @@ -29,7 +29,7 @@ func queryPidWithWinServiceName(_ string) (uint32, error) { return 0, errors.New("os not supporting win_service option") } -func collectMemmap(proc Process, prefix string, fields map[string]any) { +func collectMemmap(proc process, prefix string, fields map[string]any) { memMapStats, err := proc.MemoryMaps(true) if err == nil && len(*memMapStats) == 1 { memMap := (*memMapStats)[0] @@ -70,12 +70,12 @@ func findBySystemdUnits(units []string) ([]processGroup, error) { if !ok { return nil, fmt.Errorf("failed to parse PID %v of unit %q: invalid type %T", raw, u, raw) } - p, err := process.NewProcess(int32(pid)) + p, err := gopsprocess.NewProcess(int32(pid)) if err != nil { return nil, fmt.Errorf("failed to find process for PID %d of unit %q: %w", pid, u, err) } groups = append(groups, processGroup{ - processes: []*process.Process{p}, + processes: []*gopsprocess.Process{p}, tags: map[string]string{"systemd_unit": u.Name}, }) } @@ -87,14 +87,14 @@ func findByWindowsServices(_ []string) ([]processGroup, error) { return nil, nil } -func collectTotalReadWrite(proc Process) (r, w uint64, err error) { +func collectTotalReadWrite(proc process) (r, w uint64, err error) { path := internal.GetProcPath() fs, err := procfs.NewFS(path) if err != nil { return 0, 0, err } - p, err := fs.Proc(int(proc.PID())) + p, err := fs.Proc(int(proc.pid())) if err != nil { return 0, 0, err } @@ -177,7 +177,7 @@ func mapFdToInode(pid int32, fd uint32) (uint32, error) { return uint32(inode), nil } -func statsTCP(conns []net.ConnectionStat, family uint8) ([]map[string]interface{}, error) { +func statsTCP(conns []gopsnet.ConnectionStat, family uint8) ([]map[string]interface{}, error) { if len(conns) == 0 { return nil, nil } @@ -185,7 +185,7 @@ func statsTCP(conns []net.ConnectionStat, family uint8) ([]map[string]interface{ // For TCP we need the inode for each connection to relate the connection // statistics to the actual process socket. Therefore, map the // file-descriptors to inodes using the /proc//fd entries. - inodes := make(map[uint32]net.ConnectionStat, len(conns)) + inodes := make(map[uint32]gopsnet.ConnectionStat, len(conns)) for _, c := range conns { inode, err := mapFdToInode(c.Pid, c.Fd) if err != nil { @@ -240,7 +240,7 @@ func statsTCP(conns []net.ConnectionStat, family uint8) ([]map[string]interface{ return fieldslist, nil } -func statsUDP(conns []net.ConnectionStat, family uint8) ([]map[string]interface{}, error) { +func statsUDP(conns []gopsnet.ConnectionStat, family uint8) ([]map[string]interface{}, error) { if len(conns) == 0 { return nil, nil } @@ -248,7 +248,7 @@ func statsUDP(conns []net.ConnectionStat, family uint8) ([]map[string]interface{ // For UDP we need the inode for each connection to relate the connection // statistics to the actual process socket. Therefore, map the // file-descriptors to inodes using the /proc//fd entries. - inodes := make(map[uint32]net.ConnectionStat, len(conns)) + inodes := make(map[uint32]gopsnet.ConnectionStat, len(conns)) for _, c := range conns { inode, err := mapFdToInode(c.Pid, c.Fd) if err != nil { @@ -299,7 +299,7 @@ func statsUDP(conns []net.ConnectionStat, family uint8) ([]map[string]interface{ return fieldslist, nil } -func statsUnix(conns []net.ConnectionStat) ([]map[string]interface{}, error) { +func statsUnix(conns []gopsnet.ConnectionStat) ([]map[string]interface{}, error) { if len(conns) == 0 { return nil, nil } @@ -307,7 +307,7 @@ func statsUnix(conns []net.ConnectionStat) ([]map[string]interface{}, error) { // We need to read the inode for each connection to relate the connection // statistics to the actual process socket. Therefore, map the // file-descriptors to inodes using the /proc//fd entries. - inodes := make(map[uint32]net.ConnectionStat, len(conns)) + inodes := make(map[uint32]gopsnet.ConnectionStat, len(conns)) for _, c := range conns { inode, err := mapFdToInode(c.Pid, c.Fd) if err != nil { diff --git a/plugins/inputs/procstat/os_others.go b/plugins/inputs/procstat/os_others.go index 62334f885ccda..ba34038072a21 100644 --- a/plugins/inputs/procstat/os_others.go +++ b/plugins/inputs/procstat/os_others.go @@ -6,11 +6,11 @@ import ( "errors" "syscall" - "github.com/shirou/gopsutil/v4/net" - "github.com/shirou/gopsutil/v4/process" + gopsnet "github.com/shirou/gopsutil/v4/net" + gopsprocess "github.com/shirou/gopsutil/v4/process" ) -func processName(p *process.Process) (string, error) { +func processName(p *gopsprocess.Process) (string, error) { return p.Exe() } @@ -18,7 +18,7 @@ func queryPidWithWinServiceName(string) (uint32, error) { return 0, errors.New("os not supporting win_service option") } -func collectMemmap(Process, string, map[string]any) {} +func collectMemmap(process, string, map[string]any) {} func findBySystemdUnits([]string) ([]processGroup, error) { return nil, nil @@ -28,11 +28,11 @@ func findByWindowsServices([]string) ([]processGroup, error) { return nil, nil } -func collectTotalReadWrite(Process) (r, w uint64, err error) { +func collectTotalReadWrite(process) (r, w uint64, err error) { return 0, 0, errors.ErrUnsupported } -func statsTCP(conns []net.ConnectionStat, _ uint8) ([]map[string]interface{}, error) { +func statsTCP(conns []gopsnet.ConnectionStat, _ uint8) ([]map[string]interface{}, error) { if len(conns) == 0 { return nil, nil } @@ -65,7 +65,7 @@ func statsTCP(conns []net.ConnectionStat, _ uint8) ([]map[string]interface{}, er return fieldslist, nil } -func statsUDP(conns []net.ConnectionStat, _ uint8) ([]map[string]interface{}, error) { +func statsUDP(conns []gopsnet.ConnectionStat, _ uint8) ([]map[string]interface{}, error) { if len(conns) == 0 { return nil, nil } @@ -98,6 +98,6 @@ func statsUDP(conns []net.ConnectionStat, _ uint8) ([]map[string]interface{}, er return fieldslist, nil } -func statsUnix([]net.ConnectionStat) ([]map[string]interface{}, error) { +func statsUnix([]gopsnet.ConnectionStat) ([]map[string]interface{}, error) { return nil, errors.ErrUnsupported } diff --git a/plugins/inputs/procstat/os_windows.go b/plugins/inputs/procstat/os_windows.go index 05ada5a4748bc..b15e424d405f7 100644 --- a/plugins/inputs/procstat/os_windows.go +++ b/plugins/inputs/procstat/os_windows.go @@ -8,13 +8,13 @@ import ( "syscall" "unsafe" - "github.com/shirou/gopsutil/v4/net" - "github.com/shirou/gopsutil/v4/process" + gopsnet "github.com/shirou/gopsutil/v4/net" + gopsprocess "github.com/shirou/gopsutil/v4/process" "golang.org/x/sys/windows" "golang.org/x/sys/windows/svc/mgr" ) -func processName(p *process.Process) (string, error) { +func processName(p *gopsprocess.Process) (string, error) { return p.Name() } @@ -57,7 +57,7 @@ func queryPidWithWinServiceName(winServiceName string) (uint32, error) { return p.ProcessId, nil } -func collectMemmap(Process, string, map[string]any) {} +func collectMemmap(process, string, map[string]any) {} func findBySystemdUnits([]string) ([]processGroup, error) { return nil, nil @@ -71,13 +71,13 @@ func findByWindowsServices(services []string) ([]processGroup, error) { return nil, fmt.Errorf("failed to query PID of service %q: %w", service, err) } - p, err := process.NewProcess(int32(pid)) + p, err := gopsprocess.NewProcess(int32(pid)) if err != nil { return nil, fmt.Errorf("failed to find process for PID %d of service %q: %w", pid, service, err) } groups = append(groups, processGroup{ - processes: []*process.Process{p}, + processes: []*gopsprocess.Process{p}, tags: map[string]string{"win_service": service}, }) } @@ -85,11 +85,11 @@ func findByWindowsServices(services []string) ([]processGroup, error) { return groups, nil } -func collectTotalReadWrite(Process) (r, w uint64, err error) { +func collectTotalReadWrite(process) (r, w uint64, err error) { return 0, 0, errors.ErrUnsupported } -func statsTCP(conns []net.ConnectionStat, _ uint8) ([]map[string]interface{}, error) { +func statsTCP(conns []gopsnet.ConnectionStat, _ uint8) ([]map[string]interface{}, error) { if len(conns) == 0 { return nil, nil } @@ -122,7 +122,7 @@ func statsTCP(conns []net.ConnectionStat, _ uint8) ([]map[string]interface{}, er return fieldslist, nil } -func statsUDP(conns []net.ConnectionStat, _ uint8) ([]map[string]interface{}, error) { +func statsUDP(conns []gopsnet.ConnectionStat, _ uint8) ([]map[string]interface{}, error) { if len(conns) == 0 { return nil, nil } @@ -155,6 +155,6 @@ func statsUDP(conns []net.ConnectionStat, _ uint8) ([]map[string]interface{}, er return fieldslist, nil } -func statsUnix([]net.ConnectionStat) ([]map[string]interface{}, error) { +func statsUnix([]gopsnet.ConnectionStat) ([]map[string]interface{}, error) { return nil, nil } diff --git a/plugins/inputs/procstat/pgrep.go b/plugins/inputs/procstat/pgrep.go index 8451210e94530..add3a2dfb120d 100644 --- a/plugins/inputs/procstat/pgrep.go +++ b/plugins/inputs/procstat/pgrep.go @@ -11,54 +11,54 @@ import ( ) // Implementation of PIDGatherer that execs pgrep to find processes -type Pgrep struct { +type pgrep struct { path string } -func newPgrepFinder() (PIDFinder, error) { +func newPgrepFinder() (pidFinder, error) { path, err := exec.LookPath("pgrep") if err != nil { return nil, fmt.Errorf("could not find pgrep binary: %w", err) } - return &Pgrep{path}, nil + return &pgrep{path}, nil } -func (pg *Pgrep) PidFile(path string) ([]PID, error) { - var pids []PID +func (pg *pgrep) pidFile(path string) ([]pid, error) { + var pids []pid pidString, err := os.ReadFile(path) if err != nil { return pids, fmt.Errorf("failed to read pidfile %q: %w", path, err) } - pid, err := strconv.ParseInt(strings.TrimSpace(string(pidString)), 10, 32) + processID, err := strconv.ParseInt(strings.TrimSpace(string(pidString)), 10, 32) if err != nil { return pids, err } - pids = append(pids, PID(pid)) + pids = append(pids, pid(processID)) return pids, nil } -func (pg *Pgrep) Pattern(pattern string) ([]PID, error) { +func (pg *pgrep) pattern(pattern string) ([]pid, error) { args := []string{pattern} return pg.find(args) } -func (pg *Pgrep) UID(user string) ([]PID, error) { +func (pg *pgrep) uid(user string) ([]pid, error) { args := []string{"-u", user} return pg.find(args) } -func (pg *Pgrep) FullPattern(pattern string) ([]PID, error) { +func (pg *pgrep) fullPattern(pattern string) ([]pid, error) { args := []string{"-f", pattern} return pg.find(args) } -func (pg *Pgrep) Children(pid PID) ([]PID, error) { +func (pg *pgrep) children(pid pid) ([]pid, error) { args := []string{"-P", strconv.FormatInt(int64(pid), 10)} return pg.find(args) } -func (pg *Pgrep) find(args []string) ([]PID, error) { +func (pg *pgrep) find(args []string) ([]pid, error) { // Execute pgrep with the given arguments buf, err := exec.Command(pg.path, args...).Output() if err != nil { @@ -73,13 +73,13 @@ func (pg *Pgrep) find(args []string) ([]PID, error) { // Parse the command output to extract the PIDs fields := strings.Fields(out) - pids := make([]PID, 0, len(fields)) + pids := make([]pid, 0, len(fields)) for _, field := range fields { - pid, err := strconv.ParseInt(field, 10, 32) + processID, err := strconv.ParseInt(field, 10, 32) if err != nil { return nil, err } - pids = append(pids, PID(pid)) + pids = append(pids, pid(processID)) } return pids, nil } diff --git a/plugins/inputs/procstat/process.go b/plugins/inputs/procstat/process.go index a0e8e60c880f0..c5eeb831d8b73 100644 --- a/plugins/inputs/procstat/process.go +++ b/plugins/inputs/procstat/process.go @@ -9,41 +9,41 @@ import ( "time" gopsnet "github.com/shirou/gopsutil/v4/net" - "github.com/shirou/gopsutil/v4/process" + gopsprocess "github.com/shirou/gopsutil/v4/process" "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/metric" ) -type Process interface { - PID() PID +type process interface { Name() (string, error) - SetTag(string, string) - MemoryMaps(bool) (*[]process.MemoryMapsStat, error) - Metrics(string, *collectionConfig, time.Time) ([]telegraf.Metric, error) + MemoryMaps(bool) (*[]gopsprocess.MemoryMapsStat, error) + pid() pid + setTag(string, string) + metrics(string, *collectionConfig, time.Time) ([]telegraf.Metric, error) } -type PIDFinder interface { - PidFile(path string) ([]PID, error) - Pattern(pattern string) ([]PID, error) - UID(user string) ([]PID, error) - FullPattern(path string) ([]PID, error) - Children(pid PID) ([]PID, error) +type pidFinder interface { + pidFile(path string) ([]pid, error) + pattern(pattern string) ([]pid, error) + uid(user string) ([]pid, error) + fullPattern(path string) ([]pid, error) + children(pid pid) ([]pid, error) } -type Proc struct { +type proc struct { hasCPUTimes bool tags map[string]string - *process.Process + *gopsprocess.Process } -func newProc(pid PID) (Process, error) { - p, err := process.NewProcess(int32(pid)) +func newProc(pid pid) (process, error) { + p, err := gopsprocess.NewProcess(int32(pid)) if err != nil { return nil, err } - proc := &Proc{ + proc := &proc{ Process: p, hasCPUTimes: false, tags: make(map[string]string), @@ -51,15 +51,15 @@ func newProc(pid PID) (Process, error) { return proc, nil } -func (p *Proc) PID() PID { - return PID(p.Process.Pid) +func (p *proc) pid() pid { + return pid(p.Process.Pid) } -func (p *Proc) SetTag(k, v string) { +func (p *proc) setTag(k, v string) { p.tags[k] = v } -func (p *Proc) percent(_ time.Duration) (float64, error) { +func (p *proc) percent(_ time.Duration) (float64, error) { cpuPerc, err := p.Process.Percent(time.Duration(0)) if !p.hasCPUTimes && err == nil { p.hasCPUTimes = true @@ -68,8 +68,8 @@ func (p *Proc) percent(_ time.Duration) (float64, error) { return cpuPerc, err } -// Add metrics a single Process -func (p *Proc) Metrics(prefix string, cfg *collectionConfig, t time.Time) ([]telegraf.Metric, error) { +// Add metrics a single process +func (p *proc) metrics(prefix string, cfg *collectionConfig, t time.Time) ([]telegraf.Metric, error) { if prefix != "" { prefix += "_" } @@ -163,27 +163,27 @@ func (p *Proc) Metrics(prefix string, cfg *collectionConfig, t time.Time) ([]tel for _, rlim := range rlims { var name string switch rlim.Resource { - case process.RLIMIT_CPU: + case gopsprocess.RLIMIT_CPU: name = "cpu_time" - case process.RLIMIT_DATA: + case gopsprocess.RLIMIT_DATA: name = "memory_data" - case process.RLIMIT_STACK: + case gopsprocess.RLIMIT_STACK: name = "memory_stack" - case process.RLIMIT_RSS: + case gopsprocess.RLIMIT_RSS: name = "memory_rss" - case process.RLIMIT_NOFILE: + case gopsprocess.RLIMIT_NOFILE: name = "num_fds" - case process.RLIMIT_MEMLOCK: + case gopsprocess.RLIMIT_MEMLOCK: name = "memory_locked" - case process.RLIMIT_AS: + case gopsprocess.RLIMIT_AS: name = "memory_vms" - case process.RLIMIT_LOCKS: + case gopsprocess.RLIMIT_LOCKS: name = "file_locks" - case process.RLIMIT_SIGPENDING: + case gopsprocess.RLIMIT_SIGPENDING: name = "signals_pending" - case process.RLIMIT_NICE: + case gopsprocess.RLIMIT_NICE: name = "nice_priority" - case process.RLIMIT_RTPRIO: + case gopsprocess.RLIMIT_RTPRIO: name = "realtime_priority" default: continue diff --git a/plugins/inputs/procstat/procstat.go b/plugins/inputs/procstat/procstat.go index 4e3e4df6d38c0..6bf1e8402dc69 100644 --- a/plugins/inputs/procstat/procstat.go +++ b/plugins/inputs/procstat/procstat.go @@ -15,7 +15,7 @@ import ( "strings" "time" - "github.com/shirou/gopsutil/v4/process" + gopsprocess "github.com/shirou/gopsutil/v4/process" "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/internal/choice" @@ -28,14 +28,7 @@ var sampleConfig string // execCommand is so tests can mock out exec.Command usage. var execCommand = exec.Command -type PID int32 - -type collectionConfig struct { - solarisMode bool - tagging map[string]bool - features map[string]bool - socketProtos []string -} +type pid int32 type Procstat struct { PidFinder string `toml:"pid_finder"` @@ -57,24 +50,31 @@ type Procstat struct { Properties []string `toml:"properties"` SocketProtocols []string `toml:"socket_protocols"` TagWith []string `toml:"tag_with"` - Filter []Filter `toml:"filter"` + Filter []filter `toml:"filter"` Log telegraf.Logger `toml:"-"` - finder PIDFinder - processes map[PID]Process + finder pidFinder + processes map[pid]process cfg collectionConfig oldMode bool - createProcess func(PID) (Process, error) + createProcess func(pid) (process, error) +} + +type collectionConfig struct { + solarisMode bool + tagging map[string]bool + features map[string]bool + socketProtos []string } -type PidsTags struct { - PIDs []PID +type pidsTags struct { + PIDs []pid Tags map[string]string } type processGroup struct { - processes []*process.Process + processes []*gopsprocess.Process tags map[string]string } @@ -196,14 +196,14 @@ func (p *Procstat) Init() error { // New-style operations for i := range p.Filter { p.Filter[i].Log = p.Log - if err := p.Filter[i].Init(); err != nil { + if err := p.Filter[i].init(); err != nil { return fmt.Errorf("initializing filter %d failed: %w", i, err) } } } // Initialize the running process cache - p.processes = make(map[PID]Process) + p.processes = make(map[pid]process) return nil } @@ -240,7 +240,7 @@ func (p *Procstat) gatherOld(acc telegraf.Accumulator) error { } var count int - running := make(map[PID]bool) + running := make(map[pid]bool) for _, r := range results { if len(r.PIDs) < 1 && len(p.SupervisorUnits) > 0 { continue @@ -271,16 +271,16 @@ func (p *Procstat) gatherOld(acc telegraf.Accumulator) error { // Add initial tags for k, v := range r.Tags { - proc.SetTag(k, v) + proc.setTag(k, v) } if p.ProcessName != "" { - proc.SetTag("process_name", p.ProcessName) + proc.setTag("process_name", p.ProcessName) } p.processes[pid] = proc } running[pid] = true - metrics, err := proc.Metrics(p.Prefix, &p.cfg, now) + metrics, err := proc.metrics(p.Prefix, &p.cfg, now) if err != nil { // Continue after logging an error as there might still be // metrics available @@ -324,9 +324,9 @@ func (p *Procstat) gatherOld(acc telegraf.Accumulator) error { func (p *Procstat) gatherNew(acc telegraf.Accumulator) error { now := time.Now() - running := make(map[PID]bool) + running := make(map[pid]bool) for _, f := range p.Filter { - groups, err := f.ApplyFilter() + groups, err := f.applyFilter() if err != nil { // Add lookup error-metric acc.AddFields( @@ -357,8 +357,8 @@ func (p *Procstat) gatherNew(acc telegraf.Accumulator) error { // Use the cached processes as we need the existing instances // to compute delta-metrics (e.g. cpu-usage). - pid := PID(gp.Pid) - proc, found := p.processes[pid] + pid := pid(gp.Pid) + process, found := p.processes[pid] if !found { //nolint:errcheck // Assumption: if a process has no name, it probably does not exist if name, _ := gp.Name(); name == "" { @@ -372,19 +372,19 @@ func (p *Procstat) gatherNew(acc telegraf.Accumulator) error { tags[k] = v } if p.ProcessName != "" { - proc.SetTag("process_name", p.ProcessName) + process.setTag("process_name", p.ProcessName) } tags["filter"] = f.Name - proc = &Proc{ + process = &proc{ Process: gp, hasCPUTimes: false, tags: tags, } - p.processes[pid] = proc + p.processes[pid] = process } running[pid] = true - metrics, err := proc.Metrics(p.Prefix, &p.cfg, now) + metrics, err := process.metrics(p.Prefix, &p.cfg, now) if err != nil { // Continue after logging an error as there might still be // metrics available @@ -422,7 +422,7 @@ func (p *Procstat) gatherNew(acc telegraf.Accumulator) error { } // Get matching PIDs and their initial tags -func (p *Procstat) findPids() ([]PidsTags, error) { +func (p *Procstat) findPids() ([]pidsTags, error) { switch { case len(p.SupervisorUnits) > 0: return p.findSupervisorUnits() @@ -434,65 +434,65 @@ func (p *Procstat) findPids() ([]PidsTags, error) { return nil, err } tags := map[string]string{"win_service": p.WinService} - return []PidsTags{{pids, tags}}, nil + return []pidsTags{{pids, tags}}, nil case p.CGroup != "": return p.cgroupPIDs() case p.PidFile != "": - pids, err := p.finder.PidFile(p.PidFile) + pids, err := p.finder.pidFile(p.PidFile) if err != nil { return nil, err } tags := map[string]string{"pidfile": p.PidFile} - return []PidsTags{{pids, tags}}, nil + return []pidsTags{{pids, tags}}, nil case p.Exe != "": - pids, err := p.finder.Pattern(p.Exe) + pids, err := p.finder.pattern(p.Exe) if err != nil { return nil, err } tags := map[string]string{"exe": p.Exe} - return []PidsTags{{pids, tags}}, nil + return []pidsTags{{pids, tags}}, nil case p.Pattern != "": - pids, err := p.finder.FullPattern(p.Pattern) + pids, err := p.finder.fullPattern(p.Pattern) if err != nil { return nil, err } tags := map[string]string{"pattern": p.Pattern} - return []PidsTags{{pids, tags}}, nil + return []pidsTags{{pids, tags}}, nil case p.User != "": - pids, err := p.finder.UID(p.User) + pids, err := p.finder.uid(p.User) if err != nil { return nil, err } tags := map[string]string{"user": p.User} - return []PidsTags{{pids, tags}}, nil + return []pidsTags{{pids, tags}}, nil } return nil, errors.New("no filter option set") } -func (p *Procstat) findSupervisorUnits() ([]PidsTags, error) { +func (p *Procstat) findSupervisorUnits() ([]pidsTags, error) { groups, groupsTags, err := p.supervisorPIDs() if err != nil { return nil, fmt.Errorf("getting supervisor PIDs failed: %w", err) } // According to the PID, find the system process number and get the child processes - pidTags := make([]PidsTags, 0, len(groups)) + pidTags := make([]pidsTags, 0, len(groups)) for _, group := range groups { grppid := groupsTags[group]["pid"] if grppid == "" { - pidTags = append(pidTags, PidsTags{nil, groupsTags[group]}) + pidTags = append(pidTags, pidsTags{nil, groupsTags[group]}) continue } - pid, err := strconv.ParseInt(grppid, 10, 32) + processID, err := strconv.ParseInt(grppid, 10, 32) if err != nil { return nil, fmt.Errorf("converting PID %q failed: %w", grppid, err) } // Get all children of the supervisor unit - pids, err := p.finder.Children(PID(pid)) + pids, err := p.finder.children(pid(processID)) if err != nil { - return nil, fmt.Errorf("getting children for %d failed: %w", pid, err) + return nil, fmt.Errorf("getting children for %d failed: %w", processID, err) } tags := map[string]string{"pattern": p.Pattern, "parent_pid": p.Pattern} @@ -510,7 +510,7 @@ func (p *Procstat) findSupervisorUnits() ([]PidsTags, error) { } // Remove duplicate pid tags delete(tags, "pid") - pidTags = append(pidTags, PidsTags{pids, tags}) + pidTags = append(pidTags, pidsTags{pids, tags}) } return pidTags, nil } @@ -559,30 +559,30 @@ func (p *Procstat) supervisorPIDs() ([]string, map[string]map[string]string, err return p.SupervisorUnits, mainPids, nil } -func (p *Procstat) systemdUnitPIDs() ([]PidsTags, error) { +func (p *Procstat) systemdUnitPIDs() ([]pidsTags, error) { if p.IncludeSystemdChildren { p.CGroup = "systemd/system.slice/" + p.SystemdUnit return p.cgroupPIDs() } - var pidTags []PidsTags + var pidTags []pidsTags pids, err := p.simpleSystemdUnitPIDs() if err != nil { return nil, err } tags := map[string]string{"systemd_unit": p.SystemdUnit} - pidTags = append(pidTags, PidsTags{pids, tags}) + pidTags = append(pidTags, pidsTags{pids, tags}) return pidTags, nil } -func (p *Procstat) simpleSystemdUnitPIDs() ([]PID, error) { +func (p *Procstat) simpleSystemdUnitPIDs() ([]pid, error) { out, err := execCommand("systemctl", "show", p.SystemdUnit).Output() if err != nil { return nil, err } lines := bytes.Split(out, []byte{'\n'}) - pids := make([]PID, 0, len(lines)) + pids := make([]pid, 0, len(lines)) for _, line := range lines { kv := bytes.SplitN(line, []byte{'='}, 2) if len(kv) != 2 { @@ -594,17 +594,17 @@ func (p *Procstat) simpleSystemdUnitPIDs() ([]PID, error) { if len(kv[1]) == 0 || bytes.Equal(kv[1], []byte("0")) { return nil, nil } - pid, err := strconv.ParseInt(string(kv[1]), 10, 32) + processID, err := strconv.ParseInt(string(kv[1]), 10, 32) if err != nil { return nil, fmt.Errorf("invalid pid %q", kv[1]) } - pids = append(pids, PID(pid)) + pids = append(pids, pid(processID)) } return pids, nil } -func (p *Procstat) cgroupPIDs() ([]PidsTags, error) { +func (p *Procstat) cgroupPIDs() ([]pidsTags, error) { procsPath := p.CGroup if procsPath[0] != '/' { procsPath = "/sys/fs/cgroup/" + procsPath @@ -615,20 +615,20 @@ func (p *Procstat) cgroupPIDs() ([]PidsTags, error) { return nil, fmt.Errorf("glob failed: %w", err) } - pidTags := make([]PidsTags, 0, len(items)) + pidTags := make([]pidsTags, 0, len(items)) for _, item := range items { pids, err := p.singleCgroupPIDs(item) if err != nil { return nil, err } tags := map[string]string{"cgroup": p.CGroup, "cgroup_full": item} - pidTags = append(pidTags, PidsTags{pids, tags}) + pidTags = append(pidTags, pidsTags{pids, tags}) } return pidTags, nil } -func (p *Procstat) singleCgroupPIDs(path string) ([]PID, error) { +func (p *Procstat) singleCgroupPIDs(path string) ([]pid, error) { ok, err := isDir(path) if err != nil { return nil, err @@ -643,16 +643,16 @@ func (p *Procstat) singleCgroupPIDs(path string) ([]PID, error) { } lines := bytes.Split(out, []byte{'\n'}) - pids := make([]PID, 0, len(lines)) + pids := make([]pid, 0, len(lines)) for _, pidBS := range lines { if len(pidBS) == 0 { continue } - pid, err := strconv.ParseInt(string(pidBS), 10, 32) + processID, err := strconv.ParseInt(string(pidBS), 10, 32) if err != nil { return nil, fmt.Errorf("invalid pid %q", pidBS) } - pids = append(pids, PID(pid)) + pids = append(pids, pid(processID)) } return pids, nil @@ -666,15 +666,15 @@ func isDir(path string) (bool, error) { return result.IsDir(), nil } -func (p *Procstat) winServicePIDs() ([]PID, error) { - var pids []PID +func (p *Procstat) winServicePIDs() ([]pid, error) { + var pids []pid - pid, err := queryPidWithWinServiceName(p.WinService) + processID, err := queryPidWithWinServiceName(p.WinService) if err != nil { return pids, err } - pids = append(pids, PID(pid)) + pids = append(pids, pid(processID)) return pids, nil } diff --git a/plugins/inputs/procstat/procstat_test.go b/plugins/inputs/procstat/procstat_test.go index aa833a86f9b24..4256f08e24234 100644 --- a/plugins/inputs/procstat/procstat_test.go +++ b/plugins/inputs/procstat/procstat_test.go @@ -12,7 +12,7 @@ import ( "testing" "time" - "github.com/shirou/gopsutil/v4/process" + gopsprocess "github.com/shirou/gopsutil/v4/process" "github.com/stretchr/testify/require" "github.com/influxdata/telegraf" @@ -77,73 +77,69 @@ TestGather_STARTINGsupervisorUnitPIDs STARTING`) } type testPgrep struct { - pids []PID + pids []pid err error } -func newTestFinder(pids []PID) PIDFinder { +func newTestFinder(pids []pid) pidFinder { return &testPgrep{ pids: pids, err: nil, } } -func (pg *testPgrep) PidFile(_ string) ([]PID, error) { +func (pg *testPgrep) pidFile(_ string) ([]pid, error) { return pg.pids, pg.err } -func (p *testProc) Cmdline() (string, error) { - return "test_proc", nil -} - -func (pg *testPgrep) Pattern(_ string) ([]PID, error) { +func (pg *testPgrep) pattern(_ string) ([]pid, error) { return pg.pids, pg.err } -func (pg *testPgrep) UID(_ string) ([]PID, error) { +func (pg *testPgrep) uid(_ string) ([]pid, error) { return pg.pids, pg.err } -func (pg *testPgrep) FullPattern(_ string) ([]PID, error) { +func (pg *testPgrep) fullPattern(_ string) ([]pid, error) { return pg.pids, pg.err } -func (pg *testPgrep) Children(_ PID) ([]PID, error) { - pids := []PID{7311, 8111, 8112} +func (pg *testPgrep) children(_ pid) ([]pid, error) { + pids := []pid{7311, 8111, 8112} return pids, pg.err } type testProc struct { - pid PID - tags map[string]string + procID pid + tags map[string]string } -func newTestProc(pid PID) (Process, error) { +func newTestProc(pid pid) (process, error) { proc := &testProc{ - pid: pid, - tags: make(map[string]string), + procID: pid, + tags: make(map[string]string), } return proc, nil } -func (p *testProc) PID() PID { - return p.pid +func (p *testProc) pid() pid { + return p.procID } func (p *testProc) Name() (string, error) { return "test_proc", nil } -func (p *testProc) SetTag(k, v string) { +func (p *testProc) setTag(k, v string) { p.tags[k] = v } -func (p *testProc) MemoryMaps(bool) (*[]process.MemoryMapsStat, error) { - stats := make([]process.MemoryMapsStat, 0) +func (p *testProc) MemoryMaps(bool) (*[]gopsprocess.MemoryMapsStat, error) { + stats := make([]gopsprocess.MemoryMapsStat, 0) return &stats, nil } -func (p *testProc) Metrics(prefix string, cfg *collectionConfig, t time.Time) ([]telegraf.Metric, error) { +func (p *testProc) metrics(prefix string, cfg *collectionConfig, t time.Time) ([]telegraf.Metric, error) { if prefix != "" { prefix += "_" } @@ -190,9 +186,9 @@ func (p *testProc) Metrics(prefix string, cfg *collectionConfig, t time.Time) ([ } if cfg.tagging["pid"] { - tags["pid"] = strconv.Itoa(int(p.pid)) + tags["pid"] = strconv.Itoa(int(p.procID)) } else { - fields["pid"] = int32(p.pid) + fields["pid"] = int32(p.procID) } if cfg.tagging["ppid"] { @@ -216,7 +212,7 @@ func (p *testProc) Metrics(prefix string, cfg *collectionConfig, t time.Time) ([ return []telegraf.Metric{metric.New("procstat", tags, fields, t)}, nil } -var pid = PID(42) +var processID = pid(42) var exe = "foo" func TestInitInvalidFinder(t *testing.T) { @@ -277,8 +273,8 @@ func TestGather_CreateProcessErrorOk(t *testing.T) { PidFinder: "test", Properties: []string{"cpu", "memory", "mmap"}, Log: testutil.Logger{}, - finder: newTestFinder([]PID{pid}), - createProcess: func(PID) (Process, error) { + finder: newTestFinder([]pid{processID}), + createProcess: func(pid) (process, error) { return nil, errors.New("createProcess error") }, } @@ -350,7 +346,7 @@ func TestGather_ProcessName(t *testing.T) { PidFinder: "test", Properties: []string{"cpu", "memory", "mmap"}, Log: testutil.Logger{}, - finder: newTestFinder([]PID{pid}), + finder: newTestFinder([]pid{processID}), createProcess: newTestProc, } require.NoError(t, p.Init()) @@ -362,14 +358,14 @@ func TestGather_ProcessName(t *testing.T) { } func TestGather_NoProcessNameUsesReal(t *testing.T) { - pid := PID(os.Getpid()) + processID := pid(os.Getpid()) p := Procstat{ Exe: exe, PidFinder: "test", Properties: []string{"cpu", "memory", "mmap"}, Log: testutil.Logger{}, - finder: newTestFinder([]PID{pid}), + finder: newTestFinder([]pid{processID}), createProcess: newTestProc, } require.NoError(t, p.Init()) @@ -386,7 +382,7 @@ func TestGather_NoPidTag(t *testing.T) { PidFinder: "test", Properties: []string{"cpu", "memory", "mmap"}, Log: testutil.Logger{}, - finder: newTestFinder([]PID{pid}), + finder: newTestFinder([]pid{processID}), createProcess: newTestProc, } require.NoError(t, p.Init()) @@ -405,7 +401,7 @@ func TestGather_PidTag(t *testing.T) { PidFinder: "test", Properties: []string{"cpu", "memory", "mmap"}, Log: testutil.Logger{}, - finder: newTestFinder([]PID{pid}), + finder: newTestFinder([]pid{processID}), createProcess: newTestProc, } require.NoError(t, p.Init()) @@ -424,7 +420,7 @@ func TestGather_Prefix(t *testing.T) { PidFinder: "test", Properties: []string{"cpu", "memory", "mmap"}, Log: testutil.Logger{}, - finder: newTestFinder([]PID{pid}), + finder: newTestFinder([]pid{processID}), createProcess: newTestProc, } require.NoError(t, p.Init()) @@ -441,7 +437,7 @@ func TestGather_Exe(t *testing.T) { PidFinder: "test", Properties: []string{"cpu", "memory", "mmap"}, Log: testutil.Logger{}, - finder: newTestFinder([]PID{pid}), + finder: newTestFinder([]pid{processID}), createProcess: newTestProc, } require.NoError(t, p.Init()) @@ -460,7 +456,7 @@ func TestGather_User(t *testing.T) { PidFinder: "test", Properties: []string{"cpu", "memory", "mmap"}, Log: testutil.Logger{}, - finder: newTestFinder([]PID{pid}), + finder: newTestFinder([]pid{processID}), createProcess: newTestProc, } require.NoError(t, p.Init()) @@ -479,7 +475,7 @@ func TestGather_Pattern(t *testing.T) { PidFinder: "test", Properties: []string{"cpu", "memory", "mmap"}, Log: testutil.Logger{}, - finder: newTestFinder([]PID{pid}), + finder: newTestFinder([]pid{processID}), createProcess: newTestProc, } require.NoError(t, p.Init()) @@ -498,7 +494,7 @@ func TestGather_PidFile(t *testing.T) { PidFinder: "test", Properties: []string{"cpu", "memory", "mmap"}, Log: testutil.Logger{}, - finder: newTestFinder([]PID{pid}), + finder: newTestFinder([]pid{processID}), createProcess: newTestProc, } require.NoError(t, p.Init()) @@ -510,7 +506,7 @@ func TestGather_PidFile(t *testing.T) { } func TestGather_PercentFirstPass(t *testing.T) { - pid := PID(os.Getpid()) + processID := pid(os.Getpid()) p := Procstat{ Pattern: "foo", @@ -518,7 +514,7 @@ func TestGather_PercentFirstPass(t *testing.T) { PidFinder: "test", Properties: []string{"cpu", "memory", "mmap"}, Log: testutil.Logger{}, - finder: newTestFinder([]PID{pid}), + finder: newTestFinder([]pid{processID}), createProcess: newProc, } require.NoError(t, p.Init()) @@ -531,7 +527,7 @@ func TestGather_PercentFirstPass(t *testing.T) { } func TestGather_PercentSecondPass(t *testing.T) { - pid := PID(os.Getpid()) + processID := pid(os.Getpid()) p := Procstat{ Pattern: "foo", @@ -539,7 +535,7 @@ func TestGather_PercentSecondPass(t *testing.T) { PidFinder: "test", Properties: []string{"cpu", "memory", "mmap"}, Log: testutil.Logger{}, - finder: newTestFinder([]PID{pid}), + finder: newTestFinder([]pid{processID}), createProcess: newProc, } require.NoError(t, p.Init()) @@ -558,7 +554,7 @@ func TestGather_systemdUnitPIDs(t *testing.T) { PidFinder: "test", Properties: []string{"cpu", "memory", "mmap"}, Log: testutil.Logger{}, - finder: newTestFinder([]PID{pid}), + finder: newTestFinder([]pid{processID}), } require.NoError(t, p.Init()) @@ -566,7 +562,7 @@ func TestGather_systemdUnitPIDs(t *testing.T) { require.NoError(t, err) for _, pidsTag := range pidsTags { - require.Equal(t, []PID{11408}, pidsTag.PIDs) + require.Equal(t, []pid{11408}, pidsTag.PIDs) require.Equal(t, "TestGather_systemdUnitPIDs", pidsTag.Tags["systemd_unit"]) } } @@ -585,14 +581,14 @@ func TestGather_cgroupPIDs(t *testing.T) { PidFinder: "test", Properties: []string{"cpu", "memory", "mmap"}, Log: testutil.Logger{}, - finder: newTestFinder([]PID{pid}), + finder: newTestFinder([]pid{processID}), } require.NoError(t, p.Init()) pidsTags, err := p.findPids() require.NoError(t, err) for _, pidsTag := range pidsTags { - require.Equal(t, []PID{1234, 5678}, pidsTag.PIDs) + require.Equal(t, []pid{1234, 5678}, pidsTag.PIDs) require.Equal(t, td, pidsTag.Tags["cgroup"]) } } @@ -603,7 +599,7 @@ func TestProcstatLookupMetric(t *testing.T) { PidFinder: "test", Properties: []string{"cpu", "memory", "mmap"}, Log: testutil.Logger{}, - finder: newTestFinder([]PID{543}), + finder: newTestFinder([]pid{543}), createProcess: newProc, } require.NoError(t, p.Init()) @@ -621,7 +617,7 @@ func TestGather_SameTimestamps(t *testing.T) { PidFinder: "test", Properties: []string{"cpu", "memory", "mmap"}, Log: testutil.Logger{}, - finder: newTestFinder([]PID{pid}), + finder: newTestFinder([]pid{processID}), createProcess: newTestProc, } require.NoError(t, p.Init()) @@ -641,14 +637,14 @@ func TestGather_supervisorUnitPIDs(t *testing.T) { PidFinder: "test", Properties: []string{"cpu", "memory", "mmap"}, Log: testutil.Logger{}, - finder: newTestFinder([]PID{pid}), + finder: newTestFinder([]pid{processID}), } require.NoError(t, p.Init()) pidsTags, err := p.findPids() require.NoError(t, err) for _, pidsTag := range pidsTags { - require.Equal(t, []PID{7311, 8111, 8112}, pidsTag.PIDs) + require.Equal(t, []pid{7311, 8111, 8112}, pidsTag.PIDs) require.Equal(t, "TestGather_supervisorUnitPIDs", pidsTag.Tags["supervisor_unit"]) } } @@ -659,7 +655,7 @@ func TestGather_MoresupervisorUnitPIDs(t *testing.T) { PidFinder: "test", Properties: []string{"cpu", "memory", "mmap"}, Log: testutil.Logger{}, - finder: newTestFinder([]PID{pid}), + finder: newTestFinder([]pid{processID}), } require.NoError(t, p.Init()) diff --git a/plugins/inputs/procstat/service_finders.go b/plugins/inputs/procstat/service_finders.go index 169c64f70957c..df9bc039d326f 100644 --- a/plugins/inputs/procstat/service_finders.go +++ b/plugins/inputs/procstat/service_finders.go @@ -8,8 +8,9 @@ import ( "strconv" "strings" + gopsprocess "github.com/shirou/gopsutil/v4/process" + "github.com/influxdata/telegraf" - "github.com/shirou/gopsutil/v4/process" ) type processFinder struct { @@ -36,13 +37,13 @@ func (f *processFinder) findByPidFiles(paths []string) ([]processGroup, error) { return nil, fmt.Errorf("failed to parse PID in file %q: %w", path, err) } - p, err := process.NewProcess(int32(pid)) + p, err := gopsprocess.NewProcess(int32(pid)) if err != nil && !f.errPidFiles[path] { f.log.Errorf("failed to find process for PID %d of file %q: %v", pid, path, err) f.errPidFiles[path] = true } groups = append(groups, processGroup{ - processes: []*process.Process{p}, + processes: []*gopsprocess.Process{p}, tags: map[string]string{"pidfile": path}, }) } @@ -76,7 +77,7 @@ func findByCgroups(cgroups []string) ([]processGroup, error) { return nil, err } lines := bytes.Split(buf, []byte{'\n'}) - procs := make([]*process.Process, 0, len(lines)) + procs := make([]*gopsprocess.Process, 0, len(lines)) for _, l := range lines { l := strings.TrimSpace(string(l)) if len(l) == 0 { @@ -86,7 +87,7 @@ func findByCgroups(cgroups []string) ([]processGroup, error) { if err != nil { return nil, fmt.Errorf("failed to parse PID %q in file %q", l, fpath) } - p, err := process.NewProcess(int32(pid)) + p, err := gopsprocess.NewProcess(int32(pid)) if err != nil { return nil, fmt.Errorf("failed to find process for PID %d of %q: %w", pid, fpath, err) } @@ -130,7 +131,7 @@ func findBySupervisorUnits(units string) ([]processGroup, error) { "status": status, } - var procs []*process.Process + var procs []*gopsprocess.Process switch status { case "FATAL", "EXITED", "BACKOFF", "STOPPING": tags["error"] = strings.Join(kv[2:], " ") @@ -141,7 +142,7 @@ func findBySupervisorUnits(units string) ([]processGroup, error) { if err != nil { return nil, fmt.Errorf("failed to parse group PID %q: %w", rawpid, err) } - p, err := process.NewProcess(int32(grouppid)) + p, err := gopsprocess.NewProcess(int32(grouppid)) if err != nil { return nil, fmt.Errorf("failed to find process for PID %d of unit %q: %w", grouppid, name, err) } diff --git a/plugins/inputs/prometheus/consul.go b/plugins/inputs/prometheus/consul.go index 431e3231996e0..9020929f598ab 100644 --- a/plugins/inputs/prometheus/consul.go +++ b/plugins/inputs/prometheus/consul.go @@ -14,17 +14,17 @@ import ( "github.com/influxdata/telegraf/config" ) -type ConsulConfig struct { +type consulConfig struct { // Address of the Consul agent. The address must contain a hostname or an IP address // and optionally a port (format: "host:port"). Enabled bool `toml:"enabled"` Agent string `toml:"agent"` QueryInterval config.Duration `toml:"query_interval"` - Queries []*ConsulQuery `toml:"query"` + Queries []*consulQuery `toml:"query"` } // One Consul service discovery query -type ConsulQuery struct { +type consulQuery struct { // A name of the searched services (not ID) ServiceName string `toml:"name"` @@ -128,7 +128,7 @@ func (p *Prometheus) startConsul(ctx context.Context) error { } func (p *Prometheus) refreshConsulServices(c *api.Catalog) error { - consulServiceURLs := make(map[string]URLAndAddress) + consulServiceURLs := make(map[string]urlAndAddress) p.Log.Debugf("Refreshing Consul services") @@ -165,8 +165,8 @@ func (p *Prometheus) refreshConsulServices(c *api.Catalog) error { p.Log.Infof("Created scrape URLs from Consul for Service (%s, %s)", q.ServiceName, q.ServiceTag) } q.lastQueryFailed = false - p.Log.Debugf("Adding scrape URL from Consul for Service (%s, %s): %s", q.ServiceName, q.ServiceTag, uaa.URL.String()) - consulServiceURLs[uaa.URL.String()] = *uaa + p.Log.Debugf("Adding scrape URL from Consul for Service (%s, %s): %s", q.ServiceName, q.ServiceTag, uaa.url.String()) + consulServiceURLs[uaa.url.String()] = *uaa } } @@ -177,7 +177,7 @@ func (p *Prometheus) refreshConsulServices(c *api.Catalog) error { return nil } -func (p *Prometheus) getConsulServiceURL(q *ConsulQuery, s *api.CatalogService) (*URLAndAddress, error) { +func (p *Prometheus) getConsulServiceURL(q *consulQuery, s *api.CatalogService) (*urlAndAddress, error) { var buffer bytes.Buffer buffer.Reset() err := q.serviceURLTemplate.Execute(&buffer, s) @@ -201,9 +201,9 @@ func (p *Prometheus) getConsulServiceURL(q *ConsulQuery, s *api.CatalogService) p.Log.Debugf("Will scrape metrics from Consul Service %s", serviceURL.String()) - return &URLAndAddress{ - URL: serviceURL, - OriginalURL: serviceURL, - Tags: extraTags, + return &urlAndAddress{ + url: serviceURL, + originalURL: serviceURL, + tags: extraTags, }, nil } diff --git a/plugins/inputs/prometheus/kubernetes.go b/plugins/inputs/prometheus/kubernetes.go index eefe5a215a8cf..2c4ef136c18ca 100644 --- a/plugins/inputs/prometheus/kubernetes.go +++ b/plugins/inputs/prometheus/kubernetes.go @@ -124,11 +124,11 @@ func shouldScrapePod(pod *corev1.Pod, p *Prometheus) bool { var shouldScrape bool switch p.MonitorKubernetesPodsMethod { - case MonitorMethodAnnotations: // must have 'true' annotation to be scraped + case monitorMethodAnnotations: // must have 'true' annotation to be scraped shouldScrape = pod.Annotations != nil && pod.Annotations["prometheus.io/scrape"] == "true" - case MonitorMethodSettings: // will be scraped regardless of annotation + case monitorMethodSettings: // will be scraped regardless of annotation shouldScrape = true - case MonitorMethodSettingsAndAnnotations: // will be scraped unless opts out with 'false' annotation + case monitorMethodSettingsAndAnnotations: // will be scraped unless opts out with 'false' annotation shouldScrape = pod.Annotations == nil || pod.Annotations["prometheus.io/scrape"] != "false" } @@ -194,7 +194,7 @@ func (p *Prometheus) watchPod(ctx context.Context, clientset *kubernetes.Clients if err != nil { p.Log.Errorf("getting key from cache %s", err.Error()) } - podID := PodID(key) + podID := podID(key) if shouldScrapePod(newPod, p) { // When Informers re-Lists, pod might already be registered, // do nothing if it is, register otherwise @@ -209,7 +209,7 @@ func (p *Prometheus) watchPod(ctx context.Context, clientset *kubernetes.Clients DeleteFunc: func(oldObj interface{}) { key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(oldObj) if err == nil { - unregisterPod(PodID(key), p) + unregisterPod(podID(key), p) } }, }) @@ -280,7 +280,7 @@ func updateCadvisorPodList(p *Prometheus, req *http.Request) error { // Updating pod list to be latest cadvisor response p.lock.Lock() - p.kubernetesPods = make(map[PodID]URLAndAddress) + p.kubernetesPods = make(map[podID]urlAndAddress) // Register pod only if it has an annotation to scrape, if it is ready, // and if namespace and selectors are specified and match @@ -419,7 +419,7 @@ func registerPod(pod *corev1.Pod, p *Prometheus) { tags[k] = v } } - podURL := p.AddressToURL(targetURL, targetURL.Hostname()) + podURL := p.addressToURL(targetURL, targetURL.Hostname()) // Locks earlier if using cAdvisor calls - makes a new list each time // rather than updating and removing from the same list @@ -427,12 +427,12 @@ func registerPod(pod *corev1.Pod, p *Prometheus) { p.lock.Lock() defer p.lock.Unlock() } - p.kubernetesPods[PodID(pod.GetNamespace()+"/"+pod.GetName())] = URLAndAddress{ - URL: podURL, - Address: targetURL.Hostname(), - OriginalURL: targetURL, - Tags: tags, - Namespace: pod.GetNamespace(), + p.kubernetesPods[podID(pod.GetNamespace()+"/"+pod.GetName())] = urlAndAddress{ + url: podURL, + address: targetURL.Hostname(), + originalURL: targetURL, + tags: tags, + namespace: pod.GetNamespace(), } } @@ -446,15 +446,15 @@ func getScrapeURL(pod *corev1.Pod, p *Prometheus) (*url.URL, error) { var scheme, pathAndQuery, port string - if p.MonitorKubernetesPodsMethod == MonitorMethodSettings || - p.MonitorKubernetesPodsMethod == MonitorMethodSettingsAndAnnotations { + if p.MonitorKubernetesPodsMethod == monitorMethodSettings || + p.MonitorKubernetesPodsMethod == monitorMethodSettingsAndAnnotations { scheme = p.MonitorKubernetesPodsScheme pathAndQuery = p.MonitorKubernetesPodsPath port = strconv.Itoa(p.MonitorKubernetesPodsPort) } - if p.MonitorKubernetesPodsMethod == MonitorMethodAnnotations || - p.MonitorKubernetesPodsMethod == MonitorMethodSettingsAndAnnotations { + if p.MonitorKubernetesPodsMethod == monitorMethodAnnotations || + p.MonitorKubernetesPodsMethod == monitorMethodSettingsAndAnnotations { if ann := pod.Annotations["prometheus.io/scheme"]; ann != "" { scheme = ann } @@ -489,12 +489,12 @@ func getScrapeURL(pod *corev1.Pod, p *Prometheus) (*url.URL, error) { return base, nil } -func unregisterPod(podID PodID, p *Prometheus) { +func unregisterPod(podID podID, p *Prometheus) { p.lock.Lock() defer p.lock.Unlock() if v, ok := p.kubernetesPods[podID]; ok { p.Log.Debugf("registered a delete request for %s", podID) delete(p.kubernetesPods, podID) - p.Log.Debugf("will stop scraping for %q", v.URL.String()) + p.Log.Debugf("will stop scraping for %q", v.url.String()) } } diff --git a/plugins/inputs/prometheus/kubernetes_test.go b/plugins/inputs/prometheus/kubernetes_test.go index 98be067b395d1..5e2e2e3ca8cfb 100644 --- a/plugins/inputs/prometheus/kubernetes_test.go +++ b/plugins/inputs/prometheus/kubernetes_test.go @@ -18,8 +18,8 @@ func initPrometheus() *Prometheus { prom.MonitorKubernetesPodsScheme = "http" prom.MonitorKubernetesPodsPort = 9102 prom.MonitorKubernetesPodsPath = "/metrics" - prom.MonitorKubernetesPodsMethod = MonitorMethodAnnotations - prom.kubernetesPods = map[PodID]URLAndAddress{} + prom.MonitorKubernetesPodsMethod = monitorMethodAnnotations + prom.kubernetesPods = map[podID]urlAndAddress{} return prom } @@ -34,7 +34,7 @@ func TestScrapeURLNoAnnotations(t *testing.T) { func TestScrapeURLNoAnnotationsScrapeConfig(t *testing.T) { prom := initPrometheus() - prom.MonitorKubernetesPodsMethod = MonitorMethodSettingsAndAnnotations + prom.MonitorKubernetesPodsMethod = monitorMethodSettingsAndAnnotations p := pod() p.Annotations = map[string]string{} @@ -45,7 +45,7 @@ func TestScrapeURLNoAnnotationsScrapeConfig(t *testing.T) { func TestScrapeURLScrapeConfigCustom(t *testing.T) { prom := initPrometheus() - prom.MonitorKubernetesPodsMethod = MonitorMethodSettingsAndAnnotations + prom.MonitorKubernetesPodsMethod = monitorMethodSettingsAndAnnotations prom.MonitorKubernetesPodsScheme = "https" prom.MonitorKubernetesPodsPort = 9999 @@ -66,7 +66,7 @@ func TestScrapeURLAnnotations(t *testing.T) { func TestScrapeURLAnnotationsScrapeConfig(t *testing.T) { prom := initPrometheus() - prom.MonitorKubernetesPodsMethod = MonitorMethodSettingsAndAnnotations + prom.MonitorKubernetesPodsMethod = monitorMethodSettingsAndAnnotations p := pod() url, err := getScrapeURL(p, prom) require.NoError(t, err) @@ -84,7 +84,7 @@ func TestScrapeURLAnnotationsCustomPort(t *testing.T) { func TestScrapeURLAnnotationsCustomPortScrapeConfig(t *testing.T) { prom := initPrometheus() - prom.MonitorKubernetesPodsMethod = MonitorMethodSettingsAndAnnotations + prom.MonitorKubernetesPodsMethod = monitorMethodSettingsAndAnnotations p := pod() p.Annotations = map[string]string{"prometheus.io/port": "9000"} url, err := getScrapeURL(p, prom) @@ -129,7 +129,7 @@ func TestScrapeURLAnnotationsCustomPathWithFragment(t *testing.T) { } func TestAddPod(t *testing.T) { - prom := &Prometheus{Log: testutil.Logger{}, kubernetesPods: map[PodID]URLAndAddress{}} + prom := &Prometheus{Log: testutil.Logger{}, kubernetesPods: map[podID]urlAndAddress{}} p := pod() p.Annotations = map[string]string{"prometheus.io/scrape": "true"} @@ -139,7 +139,7 @@ func TestAddPod(t *testing.T) { func TestAddPodScrapeConfig(t *testing.T) { prom := initPrometheus() - prom.MonitorKubernetesPodsMethod = MonitorMethodSettingsAndAnnotations + prom.MonitorKubernetesPodsMethod = monitorMethodSettingsAndAnnotations p := pod() p.Annotations = map[string]string{} @@ -148,7 +148,7 @@ func TestAddPodScrapeConfig(t *testing.T) { } func TestAddMultipleDuplicatePods(t *testing.T) { - prom := &Prometheus{Log: testutil.Logger{}, kubernetesPods: map[PodID]URLAndAddress{}} + prom := &Prometheus{Log: testutil.Logger{}, kubernetesPods: map[podID]urlAndAddress{}} p := pod() p.Annotations = map[string]string{"prometheus.io/scrape": "true"} @@ -156,13 +156,13 @@ func TestAddMultipleDuplicatePods(t *testing.T) { p.Name = "Pod2" registerPod(p, prom) - urls, err := prom.GetAllURLs() + urls, err := prom.getAllURLs() require.NoError(t, err) require.Len(t, urls, 1) } func TestAddMultiplePods(t *testing.T) { - prom := &Prometheus{Log: testutil.Logger{}, kubernetesPods: map[PodID]URLAndAddress{}} + prom := &Prometheus{Log: testutil.Logger{}, kubernetesPods: map[podID]urlAndAddress{}} p := pod() p.Annotations = map[string]string{"prometheus.io/scrape": "true"} @@ -174,41 +174,41 @@ func TestAddMultiplePods(t *testing.T) { } func TestDeletePods(t *testing.T) { - prom := &Prometheus{Log: testutil.Logger{}, kubernetesPods: map[PodID]URLAndAddress{}} + prom := &Prometheus{Log: testutil.Logger{}, kubernetesPods: map[podID]urlAndAddress{}} p := pod() p.Annotations = map[string]string{"prometheus.io/scrape": "true"} registerPod(p, prom) - podID, err := cache.MetaNamespaceKeyFunc(p) + id, err := cache.MetaNamespaceKeyFunc(p) require.NoError(t, err) - unregisterPod(PodID(podID), prom) + unregisterPod(podID(id), prom) require.Empty(t, prom.kubernetesPods) } func TestKeepDefaultNamespaceLabelName(t *testing.T) { - prom := &Prometheus{Log: testutil.Logger{}, kubernetesPods: map[PodID]URLAndAddress{}} + prom := &Prometheus{Log: testutil.Logger{}, kubernetesPods: map[podID]urlAndAddress{}} p := pod() p.Annotations = map[string]string{"prometheus.io/scrape": "true"} registerPod(p, prom) - podID, err := cache.MetaNamespaceKeyFunc(p) + id, err := cache.MetaNamespaceKeyFunc(p) require.NoError(t, err) - tags := prom.kubernetesPods[PodID(podID)].Tags + tags := prom.kubernetesPods[podID(id)].tags require.Equal(t, "default", tags["namespace"]) } func TestChangeNamespaceLabelName(t *testing.T) { - prom := &Prometheus{Log: testutil.Logger{}, PodNamespaceLabelName: "pod_namespace", kubernetesPods: map[PodID]URLAndAddress{}} + prom := &Prometheus{Log: testutil.Logger{}, PodNamespaceLabelName: "pod_namespace", kubernetesPods: map[podID]urlAndAddress{}} p := pod() p.Annotations = map[string]string{"prometheus.io/scrape": "true"} registerPod(p, prom) - podID, err := cache.MetaNamespaceKeyFunc(p) + id, err := cache.MetaNamespaceKeyFunc(p) require.NoError(t, err) - tags := prom.kubernetesPods[PodID(podID)].Tags + tags := prom.kubernetesPods[podID(id)].tags require.Equal(t, "default", tags["pod_namespace"]) require.Equal(t, "", tags["namespace"]) } @@ -300,14 +300,14 @@ func TestAnnotationFilters(t *testing.T) { for _, tc := range cases { t.Run(tc.desc, func(t *testing.T) { - prom := &Prometheus{Log: testutil.Logger{}, kubernetesPods: map[PodID]URLAndAddress{}} + prom := &Prometheus{Log: testutil.Logger{}, kubernetesPods: map[podID]urlAndAddress{}} prom.PodAnnotationInclude = tc.include prom.PodAnnotationExclude = tc.exclude require.NoError(t, prom.initFilters()) registerPod(p, prom) for _, pd := range prom.kubernetesPods { for _, tagKey := range tc.expectedTags { - require.Contains(t, pd.Tags, tagKey) + require.Contains(t, pd.tags, tagKey) } } }) @@ -345,14 +345,14 @@ func TestLabelFilters(t *testing.T) { for _, tc := range cases { t.Run(tc.desc, func(t *testing.T) { - prom := &Prometheus{Log: testutil.Logger{}, kubernetesPods: map[PodID]URLAndAddress{}} + prom := &Prometheus{Log: testutil.Logger{}, kubernetesPods: map[podID]urlAndAddress{}} prom.PodLabelInclude = tc.include prom.PodLabelExclude = tc.exclude require.NoError(t, prom.initFilters()) registerPod(p, prom) for _, pd := range prom.kubernetesPods { for _, tagKey := range tc.expectedTags { - require.Contains(t, pd.Tags, tagKey) + require.Contains(t, pd.tags, tagKey) } } }) diff --git a/plugins/inputs/prometheus/prometheus.go b/plugins/inputs/prometheus/prometheus.go index 191d27dd29a58..8b557a9cab979 100644 --- a/plugins/inputs/prometheus/prometheus.go +++ b/plugins/inputs/prometheus/prometheus.go @@ -34,18 +34,14 @@ import ( //go:embed sample.conf var sampleConfig string -const acceptHeader = `application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited;q=0.7,text/plain;version=0.0.4;q=0.3` - -type MonitorMethod string - const ( - MonitorMethodNone MonitorMethod = "" - MonitorMethodAnnotations MonitorMethod = "annotations" - MonitorMethodSettings MonitorMethod = "settings" - MonitorMethodSettingsAndAnnotations MonitorMethod = "settings+annotations" -) + acceptHeader = `application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited;q=0.7,text/plain;version=0.0.4;q=0.3` -type PodID string + monitorMethodNone monitorMethod = "" + monitorMethodAnnotations monitorMethod = "annotations" + monitorMethodSettings monitorMethod = "settings" + monitorMethodSettingsAndAnnotations monitorMethod = "settings+annotations" +) type Prometheus struct { URLs []string `toml:"urls"` @@ -72,7 +68,7 @@ type Prometheus struct { KubeConfig string `toml:"kube_config"` KubernetesLabelSelector string `toml:"kubernetes_label_selector"` KubernetesFieldSelector string `toml:"kubernetes_field_selector"` - MonitorKubernetesPodsMethod MonitorMethod `toml:"monitor_kubernetes_pods_method"` + MonitorKubernetesPodsMethod monitorMethod `toml:"monitor_kubernetes_pods_method"` MonitorKubernetesPodsScheme string `toml:"monitor_kubernetes_pods_scheme"` MonitorKubernetesPodsPath string `toml:"monitor_kubernetes_pods_path"` MonitorKubernetesPodsPort int `toml:"monitor_kubernetes_pods_port"` @@ -85,7 +81,7 @@ type Prometheus struct { CacheRefreshInterval int `toml:"cache_refresh_interval"` // Consul discovery - ConsulConfig ConsulConfig `toml:"consul"` + ConsulConfig consulConfig `toml:"consul"` Log telegraf.Logger `toml:"-"` common_http.HTTPClientConfig @@ -100,7 +96,7 @@ type Prometheus struct { // Should we scrape Kubernetes services for prometheus annotations lock sync.Mutex - kubernetesPods map[PodID]URLAndAddress + kubernetesPods map[podID]urlAndAddress cancel context.CancelFunc wg sync.WaitGroup @@ -114,9 +110,21 @@ type Prometheus struct { podLabelExcludeFilter filter.Filter // List of consul services to scrape - consulServices map[string]URLAndAddress + consulServices map[string]urlAndAddress } +type urlAndAddress struct { + originalURL *url.URL + url *url.URL + address string + tags map[string]string + namespace string +} + +type monitorMethod string + +type podID string + func (*Prometheus) SampleConfig() string { return sampleConfig } @@ -164,8 +172,8 @@ func (p *Prometheus) Init() error { p.Log.Infof("Using pod scrape scope at node level to get pod list using cAdvisor.") } - if p.MonitorKubernetesPodsMethod == MonitorMethodNone { - p.MonitorKubernetesPodsMethod = MonitorMethodAnnotations + if p.MonitorKubernetesPodsMethod == monitorMethodNone { + p.MonitorKubernetesPodsMethod = monitorMethodAnnotations } // Parse label and field selectors - will be used to filter pods after cAdvisor call @@ -239,11 +247,65 @@ func (p *Prometheus) Init() error { "Accept": acceptHeader, } - p.kubernetesPods = make(map[PodID]URLAndAddress) + p.kubernetesPods = make(map[podID]urlAndAddress) return nil } +// Start will start the Kubernetes and/or Consul scraping if enabled in the configuration +func (p *Prometheus) Start(_ telegraf.Accumulator) error { + var ctx context.Context + p.wg = sync.WaitGroup{} + ctx, p.cancel = context.WithCancel(context.Background()) + + if p.ConsulConfig.Enabled && len(p.ConsulConfig.Queries) > 0 { + if err := p.startConsul(ctx); err != nil { + return err + } + } + if p.MonitorPods { + if err := p.startK8s(ctx); err != nil { + return err + } + } + return nil +} + +func (p *Prometheus) Gather(acc telegraf.Accumulator) error { + var wg sync.WaitGroup + + allURLs, err := p.getAllURLs() + if err != nil { + return err + } + for _, URL := range allURLs { + wg.Add(1) + go func(serviceURL urlAndAddress) { + defer wg.Done() + requestFields, tags, err := p.gatherURL(serviceURL, acc) + acc.AddError(err) + + // Add metrics + if p.EnableRequestMetrics { + acc.AddFields("prometheus_request", requestFields, tags) + } + }(URL) + } + + wg.Wait() + + return nil +} + +func (p *Prometheus) Stop() { + p.cancel() + p.wg.Wait() + + if p.client != nil { + p.client.CloseIdleConnections() + } +} + func (p *Prometheus) initFilters() error { if p.PodAnnotationExclude != nil { podAnnotationExclude, err := filter.Compile(p.PodAnnotationExclude) @@ -276,7 +338,7 @@ func (p *Prometheus) initFilters() error { return nil } -func (p *Prometheus) AddressToURL(u *url.URL, address string) *url.URL { +func (p *Prometheus) addressToURL(u *url.URL, address string) *url.URL { host := address if u.Port() != "" { host = address + ":" + u.Port() @@ -295,23 +357,15 @@ func (p *Prometheus) AddressToURL(u *url.URL, address string) *url.URL { return reconstructedURL } -type URLAndAddress struct { - OriginalURL *url.URL - URL *url.URL - Address string - Tags map[string]string - Namespace string -} - -func (p *Prometheus) GetAllURLs() (map[string]URLAndAddress, error) { - allURLs := make(map[string]URLAndAddress, len(p.URLs)+len(p.consulServices)+len(p.kubernetesPods)) +func (p *Prometheus) getAllURLs() (map[string]urlAndAddress, error) { + allURLs := make(map[string]urlAndAddress, len(p.URLs)+len(p.consulServices)+len(p.kubernetesPods)) for _, u := range p.URLs { address, err := url.Parse(u) if err != nil { p.Log.Errorf("Could not parse %q, skipping it. Error: %s", u, err.Error()) continue } - allURLs[address.String()] = URLAndAddress{URL: address, OriginalURL: address} + allURLs[address.String()] = urlAndAddress{url: address, originalURL: address} } p.lock.Lock() @@ -322,8 +376,8 @@ func (p *Prometheus) GetAllURLs() (map[string]URLAndAddress, error) { } // loop through all pods scraped via the prometheus annotation on the pods for _, v := range p.kubernetesPods { - if namespaceAnnotationMatch(v.Namespace, p) { - allURLs[v.URL.String()] = v + if namespaceAnnotationMatch(v.namespace, p) { + allURLs[v.url.String()] = v } } @@ -339,62 +393,34 @@ func (p *Prometheus) GetAllURLs() (map[string]URLAndAddress, error) { continue } for _, resolved := range resolvedAddresses { - serviceURL := p.AddressToURL(address, resolved) - allURLs[serviceURL.String()] = URLAndAddress{ - URL: serviceURL, - Address: resolved, - OriginalURL: address, + serviceURL := p.addressToURL(address, resolved) + allURLs[serviceURL.String()] = urlAndAddress{ + url: serviceURL, + address: resolved, + originalURL: address, } } } return allURLs, nil } -// Reads stats from all configured servers accumulates stats. -// Returns one of the errors encountered while gather stats (if any). -func (p *Prometheus) Gather(acc telegraf.Accumulator) error { - var wg sync.WaitGroup - - allURLs, err := p.GetAllURLs() - if err != nil { - return err - } - for _, URL := range allURLs { - wg.Add(1) - go func(serviceURL URLAndAddress) { - defer wg.Done() - requestFields, tags, err := p.gatherURL(serviceURL, acc) - acc.AddError(err) - - // Add metrics - if p.EnableRequestMetrics { - acc.AddFields("prometheus_request", requestFields, tags) - } - }(URL) - } - - wg.Wait() - - return nil -} - -func (p *Prometheus) gatherURL(u URLAndAddress, acc telegraf.Accumulator) (map[string]interface{}, map[string]string, error) { +func (p *Prometheus) gatherURL(u urlAndAddress, acc telegraf.Accumulator) (map[string]interface{}, map[string]string, error) { var req *http.Request var uClient *http.Client requestFields := make(map[string]interface{}) - tags := make(map[string]string, len(u.Tags)+2) + tags := make(map[string]string, len(u.tags)+2) if p.URLTag != "" { - tags[p.URLTag] = u.OriginalURL.String() + tags[p.URLTag] = u.originalURL.String() } - if u.Address != "" { - tags["address"] = u.Address + if u.address != "" { + tags["address"] = u.address } - for k, v := range u.Tags { + for k, v := range u.tags { tags[k] = v } - if u.URL.Scheme == "unix" { - path := u.URL.Query().Get("path") + if u.url.Scheme == "unix" { + path := u.url.Query().Get("path") if path == "" { path = "/metrics" } @@ -413,19 +439,19 @@ func (p *Prometheus) gatherURL(u URLAndAddress, acc telegraf.Accumulator) (map[s TLSClientConfig: tlsCfg, DisableKeepAlives: true, Dial: func(string, string) (net.Conn, error) { - c, err := net.Dial("unix", u.URL.Path) + c, err := net.Dial("unix", u.url.Path) return c, err }, }, } } else { - if u.URL.Path == "" { - u.URL.Path = "/metrics" + if u.url.Path == "" { + u.url.Path = "/metrics" } var err error - req, err = http.NewRequest("GET", u.URL.String(), nil) + req, err = http.NewRequest("GET", u.url.String(), nil) if err != nil { - return nil, nil, fmt.Errorf("unable to create new request %q: %w", u.URL.String(), err) + return nil, nil, fmt.Errorf("unable to create new request %q: %w", u.url.String(), err) } } @@ -469,7 +495,7 @@ func (p *Prometheus) gatherURL(u URLAndAddress, acc telegraf.Accumulator) (map[s var err error var resp *http.Response var start time.Time - if u.URL.Scheme != "unix" { + if u.url.Scheme != "unix" { start = time.Now() //nolint:bodyclose // False positive (because of if-else) - body will be closed in `defer` resp, err = p.client.Do(req) @@ -480,14 +506,14 @@ func (p *Prometheus) gatherURL(u URLAndAddress, acc telegraf.Accumulator) (map[s } end := time.Since(start).Seconds() if err != nil { - return requestFields, tags, fmt.Errorf("error making HTTP request to %q: %w", u.URL, err) + return requestFields, tags, fmt.Errorf("error making HTTP request to %q: %w", u.url, err) } requestFields["response_time"] = end defer resp.Body.Close() if resp.StatusCode != http.StatusOK { - return requestFields, tags, fmt.Errorf("%q returned HTTP status %q", u.URL, resp.Status) + return requestFields, tags, fmt.Errorf("%q returned HTTP status %q", u.url, resp.Status) } var body []byte @@ -504,7 +530,7 @@ func (p *Prometheus) gatherURL(u URLAndAddress, acc telegraf.Accumulator) (map[s return requestFields, tags, fmt.Errorf("error reading body: %w", err) } if int64(len(body)) > limit { - p.Log.Infof("skipping %s: content length exceeded maximum body size (%d)", u.URL, limit) + p.Log.Infof("skipping %s: content length exceeded maximum body size (%d)", u.url, limit) return requestFields, tags, nil } } else { @@ -539,20 +565,20 @@ func (p *Prometheus) gatherURL(u URLAndAddress, acc telegraf.Accumulator) (map[s } metrics, err := metricParser.Parse(body) if err != nil { - return requestFields, tags, fmt.Errorf("error reading metrics for %q: %w", u.URL, err) + return requestFields, tags, fmt.Errorf("error reading metrics for %q: %w", u.url, err) } for _, metric := range metrics { tags := metric.Tags() // strip user and password from URL - u.OriginalURL.User = nil + u.originalURL.User = nil if p.URLTag != "" { - tags[p.URLTag] = u.OriginalURL.String() + tags[p.URLTag] = u.originalURL.String() } - if u.Address != "" { - tags["address"] = u.Address + if u.address != "" { + tags["address"] = u.address } - for k, v := range u.Tags { + for k, v := range u.tags { tags[k] = v } @@ -603,39 +629,11 @@ func fieldSelectorIsSupported(fieldSelector fields.Selector) (bool, string) { return true, "" } -// Start will start the Kubernetes and/or Consul scraping if enabled in the configuration -func (p *Prometheus) Start(_ telegraf.Accumulator) error { - var ctx context.Context - p.wg = sync.WaitGroup{} - ctx, p.cancel = context.WithCancel(context.Background()) - - if p.ConsulConfig.Enabled && len(p.ConsulConfig.Queries) > 0 { - if err := p.startConsul(ctx); err != nil { - return err - } - } - if p.MonitorPods { - if err := p.startK8s(ctx); err != nil { - return err - } - } - return nil -} - -func (p *Prometheus) Stop() { - p.cancel() - p.wg.Wait() - - if p.client != nil { - p.client.CloseIdleConnections() - } -} - func init() { inputs.Add("prometheus", func() telegraf.Input { return &Prometheus{ - kubernetesPods: make(map[PodID]URLAndAddress), - consulServices: make(map[string]URLAndAddress), + kubernetesPods: make(map[podID]urlAndAddress), + consulServices: make(map[string]urlAndAddress), URLTag: "url", } }) diff --git a/plugins/inputs/prometheus/prometheus_test.go b/plugins/inputs/prometheus/prometheus_test.go index 995ec9c8dcb8c..cdb723da3d357 100644 --- a/plugins/inputs/prometheus/prometheus_test.go +++ b/plugins/inputs/prometheus/prometheus_test.go @@ -630,7 +630,7 @@ func TestInitConfigSelectors(t *testing.T) { URLs: nil, URLTag: "url", MonitorPods: true, - MonitorKubernetesPodsMethod: MonitorMethodSettings, + MonitorKubernetesPodsMethod: monitorMethodSettings, PodScrapeInterval: 60, KubernetesLabelSelector: "app=test", KubernetesFieldSelector: "spec.nodeName=node-0", diff --git a/plugins/inputs/proxmox/proxmox.go b/plugins/inputs/proxmox/proxmox.go index 22729e5ce76c2..7d4cebf2b9a95 100644 --- a/plugins/inputs/proxmox/proxmox.go +++ b/plugins/inputs/proxmox/proxmox.go @@ -23,18 +23,6 @@ func (*Proxmox) SampleConfig() string { return sampleConfig } -func (px *Proxmox) Gather(acc telegraf.Accumulator) error { - err := getNodeSearchDomain(px) - if err != nil { - return err - } - - gatherLxcData(px, acc) - gatherQemuData(px, acc) - - return nil -} - func (px *Proxmox) Init() error { // Set hostname as default node name for backwards compatibility if px.NodeName == "" { @@ -57,12 +45,16 @@ func (px *Proxmox) Init() error { return nil } -func init() { - inputs.Add("proxmox", func() telegraf.Input { - return &Proxmox{ - requestFunction: performRequest, - } - }) +func (px *Proxmox) Gather(acc telegraf.Accumulator) error { + err := getNodeSearchDomain(px) + if err != nil { + return err + } + + gatherLxcData(px, acc) + gatherQemuData(px, acc) + + return nil } func getNodeSearchDomain(px *Proxmox) error { @@ -274,3 +266,11 @@ func getTags(px *Proxmox, name string, vmConfig vmConfig, rt resourceType) map[s "vm_type": string(rt), } } + +func init() { + inputs.Add("proxmox", func() telegraf.Input { + return &Proxmox{ + requestFunction: performRequest, + } + }) +} diff --git a/plugins/inputs/proxmox/structs.go b/plugins/inputs/proxmox/structs.go index 941af52fb8a2b..47b6856f61e86 100644 --- a/plugins/inputs/proxmox/structs.go +++ b/plugins/inputs/proxmox/structs.go @@ -10,28 +10,28 @@ import ( "github.com/influxdata/telegraf/plugins/common/tls" ) +var ( + qemu resourceType = "qemu" + lxc resourceType = "lxc" +) + type Proxmox struct { BaseURL string `toml:"base_url"` APIToken string `toml:"api_token"` ResponseTimeout config.Duration `toml:"response_timeout"` NodeName string `toml:"node_name"` - tls.ClientConfig - httpClient *http.Client - nodeSearchDomain string + Log telegraf.Logger `toml:"-"` - requestFunction func(px *Proxmox, apiUrl string, method string, data url.Values) ([]byte, error) - Log telegraf.Logger `toml:"-"` + httpClient *http.Client + + nodeSearchDomain string + requestFunction func(px *Proxmox, apiUrl string, method string, data url.Values) ([]byte, error) } type resourceType string -var ( - qemu resourceType = "qemu" - lxc resourceType = "lxc" -) - type vmStats struct { Data []vmStat `json:"data"` } diff --git a/plugins/inputs/puppetagent/puppetagent.go b/plugins/inputs/puppetagent/puppetagent.go index f4332858d9d29..d7cc5d882d877 100644 --- a/plugins/inputs/puppetagent/puppetagent.go +++ b/plugins/inputs/puppetagent/puppetagent.go @@ -17,12 +17,11 @@ import ( //go:embed sample.conf var sampleConfig string -// PuppetAgent is a PuppetAgent plugin type PuppetAgent struct { - Location string + Location string `toml:"location"` } -type State struct { +type state struct { Events event Resources resource Changes change @@ -101,7 +100,7 @@ func (pa *PuppetAgent) Gather(acc telegraf.Accumulator) error { return err } - var puppetState State + var puppetState state err = yaml.Unmarshal(fh, &puppetState) if err != nil { @@ -114,7 +113,7 @@ func (pa *PuppetAgent) Gather(acc telegraf.Accumulator) error { return nil } -func structPrinter(s *State, acc telegraf.Accumulator, tags map[string]string) { +func structPrinter(s *state, acc telegraf.Accumulator, tags map[string]string) { e := reflect.ValueOf(s).Elem() fields := make(map[string]interface{}) From 1c3abfed7546d59e63494f16b0061006f0b40acf Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 17 Dec 2024 20:58:11 +0100 Subject: [PATCH 19/21] chore(deps): Bump github.com/Azure/go-autorest/autorest/adal from 0.9.23 to 0.9.24 (#16315) --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 584286c7008ff..a55322b9d062e 100644 --- a/go.mod +++ b/go.mod @@ -17,7 +17,7 @@ require ( github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.2.0 github.com/Azure/azure-storage-queue-go v0.0.0-20230531184854-c06a8eff66fe github.com/Azure/go-autorest/autorest v0.11.29 - github.com/Azure/go-autorest/autorest/adal v0.9.23 + github.com/Azure/go-autorest/autorest/adal v0.9.24 github.com/Azure/go-autorest/autorest/azure/auth v0.5.13 github.com/BurntSushi/toml v1.4.0 github.com/ClickHouse/clickhouse-go v1.5.4 diff --git a/go.sum b/go.sum index c11b686ddd6af..9a8cdfa42f411 100644 --- a/go.sum +++ b/go.sum @@ -689,8 +689,8 @@ github.com/Azure/go-autorest/autorest v0.11.29 h1:I4+HL/JDvErx2LjyzaVxllw2lRDB5/ github.com/Azure/go-autorest/autorest v0.11.29/go.mod h1:ZtEzC4Jy2JDrZLxvWs8LrBWEBycl1hbT1eknI8MtfAs= github.com/Azure/go-autorest/autorest/adal v0.9.18/go.mod h1:XVVeme+LZwABT8K5Lc3hA4nAe8LDBVle26gTrguhhPQ= github.com/Azure/go-autorest/autorest/adal v0.9.22/go.mod h1:XuAbAEUv2Tta//+voMI038TrJBqjKam0me7qR+L8Cmk= -github.com/Azure/go-autorest/autorest/adal v0.9.23 h1:Yepx8CvFxwNKpH6ja7RZ+sKX+DWYNldbLiALMC3BTz8= -github.com/Azure/go-autorest/autorest/adal v0.9.23/go.mod h1:5pcMqFkdPhviJdlEy3kC/v1ZLnQl0MH6XA5YCcMhy4c= +github.com/Azure/go-autorest/autorest/adal v0.9.24 h1:BHZfgGsGwdkHDyZdtQRQk1WeUdW0m2WPAwuHZwUi5i4= +github.com/Azure/go-autorest/autorest/adal v0.9.24/go.mod h1:7T1+g0PYFmACYW5LlG2fcoPiPlFHjClyRGL7dRlP5c8= github.com/Azure/go-autorest/autorest/azure/auth v0.5.13 h1:Ov8avRZi2vmrE2JcXw+tu5K/yB41r7xK9GZDiBF7NdM= github.com/Azure/go-autorest/autorest/azure/auth v0.5.13/go.mod h1:5BAVfWLWXihP47vYrPuBKKf4cS0bXI+KM9Qx6ETDJYo= github.com/Azure/go-autorest/autorest/azure/cli v0.4.6 h1:w77/uPk80ZET2F+AfQExZyEWtn+0Rk/uw17m9fv5Ajc= From e766c86a0ff89e8a088d0d1b798c8cedf484f66e Mon Sep 17 00:00:00 2001 From: Sven Rebhan <36194019+srebhan@users.noreply.github.com> Date: Wed, 18 Dec 2024 15:48:03 +0100 Subject: [PATCH 20/21] chore(deps): Bump github.com/vapourismo/knx-go from v0.0.0-20240217175130-922a0d50c241 to v0.0.0-20240915133544-a6ab43471c11 (#16324) --- go.mod | 2 +- go.sum | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index a55322b9d062e..fd1daea38d548 100644 --- a/go.mod +++ b/go.mod @@ -195,7 +195,7 @@ require ( github.com/tidwall/wal v1.1.7 github.com/tinylib/msgp v1.2.0 github.com/urfave/cli/v2 v2.27.2 - github.com/vapourismo/knx-go v0.0.0-20240217175130-922a0d50c241 + github.com/vapourismo/knx-go v0.0.0-20240915133544-a6ab43471c11 github.com/vishvananda/netlink v1.3.0 github.com/vishvananda/netns v0.0.5 github.com/vjeantet/grok v1.0.1 diff --git a/go.sum b/go.sum index 9a8cdfa42f411..b982de44cc6b4 100644 --- a/go.sum +++ b/go.sum @@ -2371,8 +2371,8 @@ github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI= github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= -github.com/vapourismo/knx-go v0.0.0-20240217175130-922a0d50c241 h1:3r4OPQ/jPYQA0C7i149kevHLGSG4JZtrQv2986fXSCo= -github.com/vapourismo/knx-go v0.0.0-20240217175130-922a0d50c241/go.mod h1:aGkV5xHz9sBkAckp2hez7khfehKp4YvyBwAmVdVEulg= +github.com/vapourismo/knx-go v0.0.0-20240915133544-a6ab43471c11 h1:YzrpNqpAuAgUQ0vseiI3mAVz7zr0rM5LWdaGCCr6Ipc= +github.com/vapourismo/knx-go v0.0.0-20240915133544-a6ab43471c11/go.mod h1:+iC7aAxEwuJ4mvdKaY0zCGT0dpIC/AtHt4yv2jr5FOo= github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE= github.com/vishvananda/netlink v1.3.0 h1:X7l42GfcV4S6E4vHTsw48qbrV+9PVojNfIhZcwQdrZk= github.com/vishvananda/netlink v1.3.0/go.mod h1:i6NetklAujEcC6fK0JPjT8qSwWyO0HLn4UKG+hGqeJs= @@ -2705,6 +2705,7 @@ golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY= golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= +golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI= golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= From d829a5b29cbb06f53c7aa92d11d803a237d87c16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20=C5=BBak?= Date: Wed, 18 Dec 2024 19:47:47 +0100 Subject: [PATCH 21/21] chore: Fix linter findings for `revive:unused-receiver` in `plugins/inputs/[l-r]` (#16325) --- plugins/inputs/lanz/lanz.go | 2 +- plugins/inputs/leofs/leofs.go | 6 +-- plugins/inputs/libvirt/libvirt.go | 2 +- .../inputs/libvirt/libvirt_metric_format.go | 40 +++++++++---------- plugins/inputs/linux_cpu/linux_cpu.go | 2 +- plugins/inputs/logstash/logstash.go | 25 +++++------- plugins/inputs/lvm/lvm.go | 4 -- plugins/inputs/mcrouter/mcrouter.go | 8 ++-- plugins/inputs/mcrouter/mcrouter_test.go | 8 +--- plugins/inputs/modbus/configuration_metric.go | 6 +-- .../inputs/modbus/configuration_register.go | 18 ++++----- .../inputs/modbus/configuration_request.go | 10 ++--- plugins/inputs/modbus/modbus.go | 10 ++--- plugins/inputs/monit/monit_test.go | 4 +- .../mqtt_consumer/mqtt_consumer_test.go | 22 +++++----- plugins/inputs/mysql/mysql.go | 30 +++++++------- plugins/inputs/nats_consumer/nats_consumer.go | 2 +- plugins/inputs/neptune_apex/neptune_apex.go | 4 +- .../inputs/neptune_apex/neptune_apex_test.go | 6 +-- plugins/inputs/netflow/netflow.go | 2 +- plugins/inputs/netflow/netflow_v5.go | 4 +- .../nginx_upstream_check.go | 4 +- plugins/inputs/nsq_consumer/nsq_consumer.go | 2 +- plugins/inputs/nvidia_smi/nvidia_smi.go | 2 +- plugins/inputs/opentelemetry/writer.go | 2 +- plugins/inputs/pf/pf.go | 4 +- plugins/inputs/phpfpm/fcgi_test.go | 6 +-- plugins/inputs/phpfpm/phpfpm_test.go | 2 +- .../inputs/powerdns/powerdns_linux_test.go | 7 +--- .../powerdns_recursor/powerdns_recursor.go | 2 +- .../inputs/powerdns_recursor/protocol_v3.go | 2 +- plugins/inputs/procstat/native_finder.go | 16 ++++---- plugins/inputs/procstat/pgrep.go | 2 +- plugins/inputs/procstat/procstat.go | 4 +- plugins/inputs/procstat/procstat_test.go | 4 +- plugins/inputs/prometheus/kubernetes.go | 2 +- plugins/inputs/prometheus/prometheus.go | 4 +- plugins/inputs/radius/radius.go | 2 +- plugins/inputs/raindrops/raindrops.go | 4 +- plugins/inputs/raindrops/raindrops_test.go | 3 +- plugins/inputs/redis/redis.go | 6 +-- plugins/inputs/redis/redis_test.go | 8 ++-- plugins/inputs/rethinkdb/rethinkdb.go | 6 +-- .../riemann_listener/riemann_listener.go | 2 +- 44 files changed, 145 insertions(+), 166 deletions(-) diff --git a/plugins/inputs/lanz/lanz.go b/plugins/inputs/lanz/lanz.go index bb2040d317cc9..a9589bb701777 100644 --- a/plugins/inputs/lanz/lanz.go +++ b/plugins/inputs/lanz/lanz.go @@ -58,7 +58,7 @@ func (l *Lanz) Start(acc telegraf.Accumulator) error { return nil } -func (l *Lanz) Gather(_ telegraf.Accumulator) error { +func (*Lanz) Gather(telegraf.Accumulator) error { return nil } diff --git a/plugins/inputs/leofs/leofs.go b/plugins/inputs/leofs/leofs.go index 3845384a19179..19e0878b2f608 100644 --- a/plugins/inputs/leofs/leofs.go +++ b/plugins/inputs/leofs/leofs.go @@ -159,7 +159,7 @@ func (*LeoFS) SampleConfig() string { func (l *LeoFS) Gather(acc telegraf.Accumulator) error { if len(l.Servers) == 0 { - return l.gatherServer(defaultEndpoint, serverTypeManagerMaster, acc) + return gatherServer(defaultEndpoint, serverTypeManagerMaster, acc) } var wg sync.WaitGroup for _, endpoint := range l.Servers { @@ -185,14 +185,14 @@ func (l *LeoFS) Gather(acc telegraf.Accumulator) error { wg.Add(1) go func(endpoint string, st serverType) { defer wg.Done() - acc.AddError(l.gatherServer(endpoint, st, acc)) + acc.AddError(gatherServer(endpoint, st, acc)) }(endpoint, st) } wg.Wait() return nil } -func (l *LeoFS) gatherServer(endpoint string, serverType serverType, acc telegraf.Accumulator) error { +func gatherServer(endpoint string, serverType serverType, acc telegraf.Accumulator) error { cmd := exec.Command("snmpwalk", "-v2c", "-cpublic", "-On", endpoint, oid) stdout, err := cmd.StdoutPipe() if err != nil { diff --git a/plugins/inputs/libvirt/libvirt.go b/plugins/inputs/libvirt/libvirt.go index 4a32eaf761773..a1fe4363f205b 100644 --- a/plugins/inputs/libvirt/libvirt.go +++ b/plugins/inputs/libvirt/libvirt.go @@ -47,7 +47,7 @@ type Libvirt struct { domainsMap map[string]struct{} } -func (l *Libvirt) SampleConfig() string { +func (*Libvirt) SampleConfig() string { return sampleConfig } diff --git a/plugins/inputs/libvirt/libvirt_metric_format.go b/plugins/inputs/libvirt/libvirt_metric_format.go index 91946ed2cc66e..c87ebe1c880dd 100644 --- a/plugins/inputs/libvirt/libvirt_metric_format.go +++ b/plugins/inputs/libvirt/libvirt_metric_format.go @@ -17,31 +17,31 @@ var ( ) func (l *Libvirt) addMetrics(stats []golibvirt.DomainStatsRecord, vcpuInfos map[string][]vcpuAffinity, acc telegraf.Accumulator) { - domainsMetrics := l.translateMetrics(stats) + domainsMetrics := translateMetrics(stats) for domainName, metrics := range domainsMetrics { for metricType, values := range metrics { switch metricType { case "state": - l.addStateMetrics(values, domainName, acc) + addStateMetrics(values, domainName, acc) case "cpu": - l.addCPUMetrics(values, domainName, acc) + addCPUMetrics(values, domainName, acc) case "balloon": - l.addBalloonMetrics(values, domainName, acc) + addBalloonMetrics(values, domainName, acc) case "vcpu": l.addVcpuMetrics(values, domainName, vcpuInfos[domainName], acc) case "net": - l.addInterfaceMetrics(values, domainName, acc) + addInterfaceMetrics(values, domainName, acc) case "perf": - l.addPerfMetrics(values, domainName, acc) + addPerfMetrics(values, domainName, acc) case "block": - l.addBlockMetrics(values, domainName, acc) + addBlockMetrics(values, domainName, acc) case "iothread": - l.addIothreadMetrics(values, domainName, acc) + addIothreadMetrics(values, domainName, acc) case "memory": - l.addMemoryMetrics(values, domainName, acc) + addMemoryMetrics(values, domainName, acc) case "dirtyrate": - l.addDirtyrateMetrics(values, domainName, acc) + addDirtyrateMetrics(values, domainName, acc) } } } @@ -61,7 +61,7 @@ func (l *Libvirt) addMetrics(stats []golibvirt.DomainStatsRecord, vcpuInfos map[ } } -func (l *Libvirt) translateMetrics(stats []golibvirt.DomainStatsRecord) map[string]map[string]map[string]golibvirt.TypedParamValue { +func translateMetrics(stats []golibvirt.DomainStatsRecord) map[string]map[string]map[string]golibvirt.TypedParamValue { metrics := make(map[string]map[string]map[string]golibvirt.TypedParamValue) for _, stat := range stats { if stat.Params != nil { @@ -83,7 +83,7 @@ func (l *Libvirt) translateMetrics(stats []golibvirt.DomainStatsRecord) map[stri return metrics } -func (l *Libvirt) addStateMetrics(metrics map[string]golibvirt.TypedParamValue, domainName string, acc telegraf.Accumulator) { +func addStateMetrics(metrics map[string]golibvirt.TypedParamValue, domainName string, acc telegraf.Accumulator) { var stateFields = make(map[string]interface{}) var stateTags = map[string]string{ "domain_name": domainName, @@ -101,7 +101,7 @@ func (l *Libvirt) addStateMetrics(metrics map[string]golibvirt.TypedParamValue, } } -func (l *Libvirt) addCPUMetrics(metrics map[string]golibvirt.TypedParamValue, domainName string, acc telegraf.Accumulator) { +func addCPUMetrics(metrics map[string]golibvirt.TypedParamValue, domainName string, acc telegraf.Accumulator) { var cpuFields = make(map[string]interface{}) var cpuCacheMonitorTotalFields = make(map[string]interface{}) @@ -188,7 +188,7 @@ func (l *Libvirt) addCPUMetrics(metrics map[string]golibvirt.TypedParamValue, do } } -func (l *Libvirt) addBalloonMetrics(metrics map[string]golibvirt.TypedParamValue, domainName string, acc telegraf.Accumulator) { +func addBalloonMetrics(metrics map[string]golibvirt.TypedParamValue, domainName string, acc telegraf.Accumulator) { var balloonFields = make(map[string]interface{}) var balloonTags = map[string]string{ "domain_name": domainName, @@ -283,7 +283,7 @@ func (l *Libvirt) getCurrentPCPUForVCPU(vcpuID string, vcpuInfos []vcpuAffinity) return -1 } -func (l *Libvirt) addInterfaceMetrics(metrics map[string]golibvirt.TypedParamValue, domainName string, acc telegraf.Accumulator) { +func addInterfaceMetrics(metrics map[string]golibvirt.TypedParamValue, domainName string, acc telegraf.Accumulator) { var netTotalFields = make(map[string]interface{}) var netData = make(map[string]map[string]interface{}) @@ -330,7 +330,7 @@ func (l *Libvirt) addInterfaceMetrics(metrics map[string]golibvirt.TypedParamVal } } -func (l *Libvirt) addPerfMetrics(metrics map[string]golibvirt.TypedParamValue, domainName string, acc telegraf.Accumulator) { +func addPerfMetrics(metrics map[string]golibvirt.TypedParamValue, domainName string, acc telegraf.Accumulator) { var perfFields = make(map[string]interface{}) var perfTags = map[string]string{ "domain_name": domainName, @@ -351,7 +351,7 @@ func (l *Libvirt) addPerfMetrics(metrics map[string]golibvirt.TypedParamValue, d } } -func (l *Libvirt) addBlockMetrics(metrics map[string]golibvirt.TypedParamValue, domainName string, acc telegraf.Accumulator) { +func addBlockMetrics(metrics map[string]golibvirt.TypedParamValue, domainName string, acc telegraf.Accumulator) { var blockTotalFields = make(map[string]interface{}) var blockData = make(map[string]map[string]interface{}) @@ -399,7 +399,7 @@ func (l *Libvirt) addBlockMetrics(metrics map[string]golibvirt.TypedParamValue, } } -func (l *Libvirt) addIothreadMetrics(metrics map[string]golibvirt.TypedParamValue, domainName string, acc telegraf.Accumulator) { +func addIothreadMetrics(metrics map[string]golibvirt.TypedParamValue, domainName string, acc telegraf.Accumulator) { var iothreadTotalFields = make(map[string]interface{}) var iothreadData = make(map[string]map[string]interface{}) @@ -446,7 +446,7 @@ func (l *Libvirt) addIothreadMetrics(metrics map[string]golibvirt.TypedParamValu } } -func (l *Libvirt) addMemoryMetrics(metrics map[string]golibvirt.TypedParamValue, domainName string, acc telegraf.Accumulator) { +func addMemoryMetrics(metrics map[string]golibvirt.TypedParamValue, domainName string, acc telegraf.Accumulator) { var memoryBandwidthMonitorTotalFields = make(map[string]interface{}) var memoryBandwidthMonitorData = make(map[string]map[string]interface{}) @@ -528,7 +528,7 @@ func (l *Libvirt) addMemoryMetrics(metrics map[string]golibvirt.TypedParamValue, } } -func (l *Libvirt) addDirtyrateMetrics(metrics map[string]golibvirt.TypedParamValue, domainName string, acc telegraf.Accumulator) { +func addDirtyrateMetrics(metrics map[string]golibvirt.TypedParamValue, domainName string, acc telegraf.Accumulator) { var dirtyrateFields = make(map[string]interface{}) var dirtyrateVcpuData = make(map[string]map[string]interface{}) diff --git a/plugins/inputs/linux_cpu/linux_cpu.go b/plugins/inputs/linux_cpu/linux_cpu.go index e7839d4e172c7..1457184c9da06 100644 --- a/plugins/inputs/linux_cpu/linux_cpu.go +++ b/plugins/inputs/linux_cpu/linux_cpu.go @@ -47,7 +47,7 @@ type prop struct { optional bool } -func (g *LinuxCPU) SampleConfig() string { +func (*LinuxCPU) SampleConfig() string { return sampleConfig } diff --git a/plugins/inputs/logstash/logstash.go b/plugins/inputs/logstash/logstash.go index da65773c46f39..4fe48035e7a5c 100644 --- a/plugins/inputs/logstash/logstash.go +++ b/plugins/inputs/logstash/logstash.go @@ -283,12 +283,7 @@ func (logstash *Logstash) gatherProcessStats(address string, accumulator telegra } // gatherPluginsStats go through a list of plugins and add their metrics to the accumulator -func (logstash *Logstash) gatherPluginsStats( - plugins []plugin, - pluginType string, - tags map[string]string, - accumulator telegraf.Accumulator, -) error { +func gatherPluginsStats(plugins []plugin, pluginType string, tags map[string]string, accumulator telegraf.Accumulator) error { for _, plugin := range plugins { pluginTags := map[string]string{ "plugin_name": plugin.Name, @@ -370,7 +365,7 @@ func (logstash *Logstash) gatherPluginsStats( return nil } -func (logstash *Logstash) gatherQueueStats(queue pipelineQueue, tags map[string]string, acc telegraf.Accumulator) error { +func gatherQueueStats(queue pipelineQueue, tags map[string]string, acc telegraf.Accumulator) error { queueTags := map[string]string{ "queue_type": queue.Type, } @@ -438,20 +433,20 @@ func (logstash *Logstash) gatherPipelineStats(address string, accumulator telegr } accumulator.AddFields("logstash_events", flattener.Fields, tags) - err = logstash.gatherPluginsStats(pipelineStats.Pipeline.Plugins.Inputs, "input", tags, accumulator) + err = gatherPluginsStats(pipelineStats.Pipeline.Plugins.Inputs, "input", tags, accumulator) if err != nil { return err } - err = logstash.gatherPluginsStats(pipelineStats.Pipeline.Plugins.Filters, "filter", tags, accumulator) + err = gatherPluginsStats(pipelineStats.Pipeline.Plugins.Filters, "filter", tags, accumulator) if err != nil { return err } - err = logstash.gatherPluginsStats(pipelineStats.Pipeline.Plugins.Outputs, "output", tags, accumulator) + err = gatherPluginsStats(pipelineStats.Pipeline.Plugins.Outputs, "output", tags, accumulator) if err != nil { return err } - err = logstash.gatherQueueStats(pipelineStats.Pipeline.Queue, tags, accumulator) + err = gatherQueueStats(pipelineStats.Pipeline.Queue, tags, accumulator) if err != nil { return err } @@ -484,20 +479,20 @@ func (logstash *Logstash) gatherPipelinesStats(address string, accumulator teleg } accumulator.AddFields("logstash_events", flattener.Fields, tags) - err = logstash.gatherPluginsStats(pipeline.Plugins.Inputs, "input", tags, accumulator) + err = gatherPluginsStats(pipeline.Plugins.Inputs, "input", tags, accumulator) if err != nil { return err } - err = logstash.gatherPluginsStats(pipeline.Plugins.Filters, "filter", tags, accumulator) + err = gatherPluginsStats(pipeline.Plugins.Filters, "filter", tags, accumulator) if err != nil { return err } - err = logstash.gatherPluginsStats(pipeline.Plugins.Outputs, "output", tags, accumulator) + err = gatherPluginsStats(pipeline.Plugins.Outputs, "output", tags, accumulator) if err != nil { return err } - err = logstash.gatherQueueStats(pipeline.Queue, tags, accumulator) + err = gatherQueueStats(pipeline.Queue, tags, accumulator) if err != nil { return err } diff --git a/plugins/inputs/lvm/lvm.go b/plugins/inputs/lvm/lvm.go index 0efb7270b9d5d..e1c246dddf7f9 100644 --- a/plugins/inputs/lvm/lvm.go +++ b/plugins/inputs/lvm/lvm.go @@ -33,10 +33,6 @@ func (*LVM) SampleConfig() string { return sampleConfig } -func (lvm *LVM) Init() error { - return nil -} - func (lvm *LVM) Gather(acc telegraf.Accumulator) error { if err := lvm.gatherPhysicalVolumes(acc); err != nil { return err diff --git a/plugins/inputs/mcrouter/mcrouter.go b/plugins/inputs/mcrouter/mcrouter.go index 37202fa300db0..0cf91573a88f8 100644 --- a/plugins/inputs/mcrouter/mcrouter.go +++ b/plugins/inputs/mcrouter/mcrouter.go @@ -128,14 +128,14 @@ func (m *Mcrouter) Gather(acc telegraf.Accumulator) error { } for _, serverAddress := range m.Servers { - acc.AddError(m.gatherServer(ctx, serverAddress, acc)) + acc.AddError(gatherServer(ctx, serverAddress, acc)) } return nil } // parseAddress parses an address string into 'host:port' and 'protocol' parts -func (m *Mcrouter) parseAddress(address string) (parsedAddress, protocol string, err error) { +func parseAddress(address string) (parsedAddress, protocol string, err error) { var host string var port string @@ -181,13 +181,13 @@ func (m *Mcrouter) parseAddress(address string) (parsedAddress, protocol string, return parsedAddress, protocol, nil } -func (m *Mcrouter) gatherServer(ctx context.Context, address string, acc telegraf.Accumulator) error { +func gatherServer(ctx context.Context, address string, acc telegraf.Accumulator) error { var conn net.Conn var err error var protocol string var dialer net.Dialer - address, protocol, err = m.parseAddress(address) + address, protocol, err = parseAddress(address) if err != nil { return err } diff --git a/plugins/inputs/mcrouter/mcrouter_test.go b/plugins/inputs/mcrouter/mcrouter_test.go index 47f658d256afa..a0d1414ff7d0b 100644 --- a/plugins/inputs/mcrouter/mcrouter_test.go +++ b/plugins/inputs/mcrouter/mcrouter_test.go @@ -15,10 +15,6 @@ import ( ) func TestAddressParsing(t *testing.T) { - m := &Mcrouter{ - Servers: []string{"tcp://" + testutil.GetLocalHost()}, - } - var acceptTests = [][3]string{ {"tcp://localhost:8086", "localhost:8086", "tcp"}, {"tcp://localhost", "localhost:" + defaultServerURL.Port(), "tcp"}, @@ -32,7 +28,7 @@ func TestAddressParsing(t *testing.T) { } for _, args := range acceptTests { - address, protocol, err := m.parseAddress(args[0]) + address, protocol, err := parseAddress(args[0]) require.NoError(t, err, args[0]) require.Equal(t, args[1], address, args[0]) @@ -40,7 +36,7 @@ func TestAddressParsing(t *testing.T) { } for _, addr := range rejectTests { - address, protocol, err := m.parseAddress(addr) + address, protocol, err := parseAddress(addr) require.Error(t, err, addr) require.Empty(t, address, addr) diff --git a/plugins/inputs/modbus/configuration_metric.go b/plugins/inputs/modbus/configuration_metric.go index c0301728e0e39..959690a1eb89f 100644 --- a/plugins/inputs/modbus/configuration_metric.go +++ b/plugins/inputs/modbus/configuration_metric.go @@ -42,7 +42,7 @@ type configurationPerMetric struct { logger telegraf.Logger } -func (c *configurationPerMetric) sampleConfigPart() string { +func (*configurationPerMetric) sampleConfigPart() string { return sampleConfigPartPerMetric } @@ -366,7 +366,7 @@ func (c *configurationPerMetric) fieldID(seed maphash.Seed, def metricDefinition return mh.Sum64() } -func (c *configurationPerMetric) determineOutputDatatype(input string) (string, error) { +func (*configurationPerMetric) determineOutputDatatype(input string) (string, error) { // Handle our special types switch input { case "INT8L", "INT8H", "INT16", "INT32", "INT64": @@ -381,7 +381,7 @@ func (c *configurationPerMetric) determineOutputDatatype(input string) (string, return "unknown", fmt.Errorf("invalid input datatype %q for determining output", input) } -func (c *configurationPerMetric) determineFieldLength(input string, length uint16) (uint16, error) { +func (*configurationPerMetric) determineFieldLength(input string, length uint16) (uint16, error) { // Handle our special types switch input { case "BIT", "INT8L", "INT8H", "UINT8L", "UINT8H": diff --git a/plugins/inputs/modbus/configuration_register.go b/plugins/inputs/modbus/configuration_register.go index 9bd70caca6caa..9d47af5553298 100644 --- a/plugins/inputs/modbus/configuration_register.go +++ b/plugins/inputs/modbus/configuration_register.go @@ -31,7 +31,7 @@ type configurationOriginal struct { logger telegraf.Logger } -func (c *configurationOriginal) sampleConfigPart() string { +func (*configurationOriginal) sampleConfigPart() string { return sampleConfigPartPerRegister } @@ -43,19 +43,19 @@ func (c *configurationOriginal) check() error { return fmt.Errorf("invalid 'string_register_location' %q", c.workarounds.StringRegisterLocation) } - if err := c.validateFieldDefinitions(c.DiscreteInputs, cDiscreteInputs); err != nil { + if err := validateFieldDefinitions(c.DiscreteInputs, cDiscreteInputs); err != nil { return err } - if err := c.validateFieldDefinitions(c.Coils, cCoils); err != nil { + if err := validateFieldDefinitions(c.Coils, cCoils); err != nil { return err } - if err := c.validateFieldDefinitions(c.HoldingRegisters, cHoldingRegisters); err != nil { + if err := validateFieldDefinitions(c.HoldingRegisters, cHoldingRegisters); err != nil { return err } - return c.validateFieldDefinitions(c.InputRegisters, cInputRegisters) + return validateFieldDefinitions(c.InputRegisters, cInputRegisters) } func (c *configurationOriginal) process() (map[byte]requestSet, error) { @@ -182,7 +182,7 @@ func (c *configurationOriginal) newFieldFromDefinition(def fieldDefinition, type return f, nil } -func (c *configurationOriginal) validateFieldDefinitions(fieldDefs []fieldDefinition, registerType string) error { +func validateFieldDefinitions(fieldDefs []fieldDefinition, registerType string) error { nameEncountered := make(map[string]bool, len(fieldDefs)) for _, item := range fieldDefs { // check empty name @@ -276,7 +276,7 @@ func (c *configurationOriginal) validateFieldDefinitions(fieldDefs []fieldDefini return nil } -func (c *configurationOriginal) normalizeInputDatatype(dataType string, words int) (string, error) { +func (*configurationOriginal) normalizeInputDatatype(dataType string, words int) (string, error) { if dataType == "FLOAT32" { config.PrintOptionValueDeprecationNotice("input.modbus", "data_type", "FLOAT32", telegraf.DeprecationInfo{ Since: "1.16.0", @@ -323,7 +323,7 @@ func (c *configurationOriginal) normalizeInputDatatype(dataType string, words in return normalizeInputDatatype(dataType) } -func (c *configurationOriginal) normalizeOutputDatatype(dataType string) (string, error) { +func (*configurationOriginal) normalizeOutputDatatype(dataType string) (string, error) { // Handle our special types switch dataType { case "FIXED", "FLOAT32", "UFIXED": @@ -332,7 +332,7 @@ func (c *configurationOriginal) normalizeOutputDatatype(dataType string) (string return normalizeOutputDatatype("native") } -func (c *configurationOriginal) normalizeByteOrder(byteOrder string) (string, error) { +func (*configurationOriginal) normalizeByteOrder(byteOrder string) (string, error) { // Handle our special types switch byteOrder { case "AB", "ABCDEFGH": diff --git a/plugins/inputs/modbus/configuration_request.go b/plugins/inputs/modbus/configuration_request.go index 6288b0c1b5f99..13cfc36c3d710 100644 --- a/plugins/inputs/modbus/configuration_request.go +++ b/plugins/inputs/modbus/configuration_request.go @@ -45,7 +45,7 @@ type configurationPerRequest struct { logger telegraf.Logger } -func (c *configurationPerRequest) sampleConfigPart() string { +func (*configurationPerRequest) sampleConfigPart() string { return sampleConfigPartPerRequest } @@ -300,7 +300,7 @@ func (c *configurationPerRequest) newFieldFromDefinition(def requestFieldDefinit fieldLength := uint16(1) if typed { - if fieldLength, err = c.determineFieldLength(def.InputType, def.Length); err != nil { + if fieldLength, err = determineFieldLength(def.InputType, def.Length); err != nil { return field{}, err } } @@ -338,7 +338,7 @@ func (c *configurationPerRequest) newFieldFromDefinition(def requestFieldDefinit // For non-scaling cases we should choose the output corresponding to the input class // i.e. INT64 for INT*, UINT64 for UINT* etc. var err error - if def.OutputType, err = c.determineOutputDatatype(def.InputType); err != nil { + if def.OutputType, err = determineOutputDatatype(def.InputType); err != nil { return field{}, err } } else { @@ -406,7 +406,7 @@ func (c *configurationPerRequest) fieldID(seed maphash.Seed, def requestDefiniti return mh.Sum64() } -func (c *configurationPerRequest) determineOutputDatatype(input string) (string, error) { +func determineOutputDatatype(input string) (string, error) { // Handle our special types switch input { case "INT8L", "INT8H", "INT16", "INT32", "INT64": @@ -421,7 +421,7 @@ func (c *configurationPerRequest) determineOutputDatatype(input string) (string, return "unknown", fmt.Errorf("invalid input datatype %q for determining output", input) } -func (c *configurationPerRequest) determineFieldLength(input string, length uint16) (uint16, error) { +func determineFieldLength(input string, length uint16) (uint16, error) { // Handle our special types switch input { case "BIT", "INT8L", "INT8H", "UINT8L", "UINT8H": diff --git a/plugins/inputs/modbus/modbus.go b/plugins/inputs/modbus/modbus.go index 0d95d3987ced6..eeb6577a8c5ae 100644 --- a/plugins/inputs/modbus/modbus.go +++ b/plugins/inputs/modbus/modbus.go @@ -251,22 +251,22 @@ func (m *Modbus) Gather(acc telegraf.Accumulator) error { if !m.ExcludeRegisterTypeTag { tags["type"] = cCoils } - m.collectFields(grouper, timestamp, tags, requests.coil) + collectFields(grouper, timestamp, tags, requests.coil) if !m.ExcludeRegisterTypeTag { tags["type"] = cDiscreteInputs } - m.collectFields(grouper, timestamp, tags, requests.discrete) + collectFields(grouper, timestamp, tags, requests.discrete) if !m.ExcludeRegisterTypeTag { tags["type"] = cHoldingRegisters } - m.collectFields(grouper, timestamp, tags, requests.holding) + collectFields(grouper, timestamp, tags, requests.holding) if !m.ExcludeRegisterTypeTag { tags["type"] = cInputRegisters } - m.collectFields(grouper, timestamp, tags, requests.input) + collectFields(grouper, timestamp, tags, requests.input) // Add the metrics grouped by series to the accumulator for _, x := range grouper.Metrics() { @@ -532,7 +532,7 @@ func (m *Modbus) gatherRequestsInput(requests []request) error { return nil } -func (m *Modbus) collectFields(grouper *metric.SeriesGrouper, timestamp time.Time, tags map[string]string, requests []request) { +func collectFields(grouper *metric.SeriesGrouper, timestamp time.Time, tags map[string]string, requests []request) { for _, request := range requests { for _, field := range request.fields { // Collect tags from global and per-request diff --git a/plugins/inputs/monit/monit_test.go b/plugins/inputs/monit/monit_test.go index cf4d79ce693ba..e83e51643cfd3 100644 --- a/plugins/inputs/monit/monit_test.go +++ b/plugins/inputs/monit/monit_test.go @@ -17,8 +17,8 @@ import ( type transportMock struct { } -func (t *transportMock) RoundTrip(_ *http.Request) (*http.Response, error) { - errorString := "Get http://127.0.0.1:2812/_status?format=xml: " + +func (*transportMock) RoundTrip(*http.Request) (*http.Response, error) { + errorString := "get http://127.0.0.1:2812/_status?format=xml: " + "read tcp 192.168.10.2:55610->127.0.0.1:2812: " + "read: connection reset by peer" return nil, errors.New(errorString) diff --git a/plugins/inputs/mqtt_consumer/mqtt_consumer_test.go b/plugins/inputs/mqtt_consumer/mqtt_consumer_test.go index a1ec7dd272eb1..32f5b7e9f1da5 100644 --- a/plugins/inputs/mqtt_consumer/mqtt_consumer_test.go +++ b/plugins/inputs/mqtt_consumer/mqtt_consumer_test.go @@ -64,15 +64,15 @@ type fakeParser struct{} // fakeParser satisfies telegraf.Parser var _ telegraf.Parser = &fakeParser{} -func (p *fakeParser) Parse(_ []byte) ([]telegraf.Metric, error) { +func (*fakeParser) Parse([]byte) ([]telegraf.Metric, error) { panic("not implemented") } -func (p *fakeParser) ParseLine(_ string) (telegraf.Metric, error) { +func (*fakeParser) ParseLine(string) (telegraf.Metric, error) { panic("not implemented") } -func (p *fakeParser) SetDefaultTags(_ map[string]string) { +func (*fakeParser) SetDefaultTags(map[string]string) { panic("not implemented") } @@ -84,15 +84,15 @@ type fakeToken struct { // fakeToken satisfies mqtt.Token var _ mqtt.Token = &fakeToken{} -func (t *fakeToken) Wait() bool { +func (*fakeToken) Wait() bool { return true } -func (t *fakeToken) WaitTimeout(time.Duration) bool { +func (*fakeToken) WaitTimeout(time.Duration) bool { return true } -func (t *fakeToken) Error() error { +func (*fakeToken) Error() error { return nil } @@ -166,7 +166,7 @@ type message struct { qos byte } -func (m *message) Duplicate() bool { +func (*message) Duplicate() bool { panic("not implemented") } @@ -174,7 +174,7 @@ func (m *message) Qos() byte { return m.qos } -func (m *message) Retained() bool { +func (*message) Retained() bool { panic("not implemented") } @@ -182,15 +182,15 @@ func (m *message) Topic() string { return m.topic } -func (m *message) MessageID() uint16 { +func (*message) MessageID() uint16 { panic("not implemented") } -func (m *message) Payload() []byte { +func (*message) Payload() []byte { return []byte("cpu time_idle=42i") } -func (m *message) Ack() { +func (*message) Ack() { panic("not implemented") } diff --git a/plugins/inputs/mysql/mysql.go b/plugins/inputs/mysql/mysql.go index fb37dfba571cf..174b2ea3c10a8 100644 --- a/plugins/inputs/mysql/mysql.go +++ b/plugins/inputs/mysql/mysql.go @@ -461,7 +461,7 @@ func (m *Mysql) gatherServer(server *config.Secret, acc telegraf.Accumulator) er } if m.GatherBinaryLogs { - err = m.gatherBinaryLogs(db, servtag, acc) + err = gatherBinaryLogs(db, servtag, acc) if err != nil { return err } @@ -510,35 +510,35 @@ func (m *Mysql) gatherServer(server *config.Secret, acc telegraf.Accumulator) er } if m.GatherTableIOWaits { - err = m.gatherPerfTableIOWaits(db, servtag, acc) + err = gatherPerfTableIOWaits(db, servtag, acc) if err != nil { return err } } if m.GatherIndexIOWaits { - err = m.gatherPerfIndexIOWaits(db, servtag, acc) + err = gatherPerfIndexIOWaits(db, servtag, acc) if err != nil { return err } } if m.GatherTableLockWaits { - err = m.gatherPerfTableLockWaits(db, servtag, acc) + err = gatherPerfTableLockWaits(db, servtag, acc) if err != nil { return err } } if m.GatherEventWaits { - err = m.gatherPerfEventWaits(db, servtag, acc) + err = gatherPerfEventWaits(db, servtag, acc) if err != nil { return err } } if m.GatherFileEventsStats { - err = m.gatherPerfFileEventsStatuses(db, servtag, acc) + err = gatherPerfFileEventsStatuses(db, servtag, acc) if err != nil { return err } @@ -712,7 +712,7 @@ func (m *Mysql) gatherSlaveStatuses(db *sql.DB, servtag string, acc telegraf.Acc // gatherBinaryLogs can be used to collect size and count of all binary files // binlogs metric requires the MySQL server to turn it on in configuration -func (m *Mysql) gatherBinaryLogs(db *sql.DB, servtag string, acc telegraf.Accumulator) error { +func gatherBinaryLogs(db *sql.DB, servtag string, acc telegraf.Accumulator) error { // run query rows, err := db.Query(binaryLogsQuery) if err != nil { @@ -1174,9 +1174,8 @@ func getColSlice(rows *sql.Rows) ([]interface{}, error) { return nil, fmt.Errorf("not Supported - %d columns", l) } -// gatherPerfTableIOWaits can be used to get total count and time -// of I/O wait event for each table and process -func (m *Mysql) gatherPerfTableIOWaits(db *sql.DB, servtag string, acc telegraf.Accumulator) error { +// gatherPerfTableIOWaits can be used to get total count and time of I/O wait event for each table and process +func gatherPerfTableIOWaits(db *sql.DB, servtag string, acc telegraf.Accumulator) error { rows, err := db.Query(perfTableIOWaitsQuery) if err != nil { return err @@ -1221,9 +1220,8 @@ func (m *Mysql) gatherPerfTableIOWaits(db *sql.DB, servtag string, acc telegraf. return nil } -// gatherPerfIndexIOWaits can be used to get total count and time -// of I/O wait event for each index and process -func (m *Mysql) gatherPerfIndexIOWaits(db *sql.DB, servtag string, acc telegraf.Accumulator) error { +// gatherPerfIndexIOWaits can be used to get total count and time of I/O wait event for each index and process +func gatherPerfIndexIOWaits(db *sql.DB, servtag string, acc telegraf.Accumulator) error { rows, err := db.Query(perfIndexIOWaitsQuery) if err != nil { return err @@ -1500,7 +1498,7 @@ func (m *Mysql) gatherPerfSummaryPerAccountPerEvent(db *sql.DB, servtag string, // the total number and time for SQL and external lock wait events // for each table and operation // requires the MySQL server to be enabled to save this metric -func (m *Mysql) gatherPerfTableLockWaits(db *sql.DB, servtag string, acc telegraf.Accumulator) error { +func gatherPerfTableLockWaits(db *sql.DB, servtag string, acc telegraf.Accumulator) error { // check if table exists, // if performance_schema is not enabled, tables do not exist // then there is no need to scan them @@ -1627,7 +1625,7 @@ func (m *Mysql) gatherPerfTableLockWaits(db *sql.DB, servtag string, acc telegra } // gatherPerfEventWaits can be used to get total time and number of event waits -func (m *Mysql) gatherPerfEventWaits(db *sql.DB, servtag string, acc telegraf.Accumulator) error { +func gatherPerfEventWaits(db *sql.DB, servtag string, acc telegraf.Accumulator) error { rows, err := db.Query(perfEventWaitsQuery) if err != nil { return err @@ -1658,7 +1656,7 @@ func (m *Mysql) gatherPerfEventWaits(db *sql.DB, servtag string, acc telegraf.Ac } // gatherPerfFileEvents can be used to get stats on file events -func (m *Mysql) gatherPerfFileEventsStatuses(db *sql.DB, servtag string, acc telegraf.Accumulator) error { +func gatherPerfFileEventsStatuses(db *sql.DB, servtag string, acc telegraf.Accumulator) error { rows, err := db.Query(perfFileEventsQuery) if err != nil { return err diff --git a/plugins/inputs/nats_consumer/nats_consumer.go b/plugins/inputs/nats_consumer/nats_consumer.go index 7904800499d89..43531cc53e912 100644 --- a/plugins/inputs/nats_consumer/nats_consumer.go +++ b/plugins/inputs/nats_consumer/nats_consumer.go @@ -186,7 +186,7 @@ func (n *NatsConsumer) Start(acc telegraf.Accumulator) error { return nil } -func (n *NatsConsumer) Gather(_ telegraf.Accumulator) error { +func (*NatsConsumer) Gather(telegraf.Accumulator) error { return nil } diff --git a/plugins/inputs/neptune_apex/neptune_apex.go b/plugins/inputs/neptune_apex/neptune_apex.go index d5485959177c7..97e02652419bf 100644 --- a/plugins/inputs/neptune_apex/neptune_apex.go +++ b/plugins/inputs/neptune_apex/neptune_apex.go @@ -83,12 +83,12 @@ func (n *NeptuneApex) gatherServer( if err != nil { return err } - return n.parseXML(acc, resp) + return parseXML(acc, resp) } // parseXML is strict on the input and does not do best-effort parsing. // This is because of the life-support nature of the Neptune Apex. -func (n *NeptuneApex) parseXML(acc telegraf.Accumulator, data []byte) error { +func parseXML(acc telegraf.Accumulator, data []byte) error { r := xmlReply{} err := xml.Unmarshal(data, &r) if err != nil { diff --git a/plugins/inputs/neptune_apex/neptune_apex_test.go b/plugins/inputs/neptune_apex/neptune_apex_test.go index a64374cd22bde..a6f65ec96ec81 100644 --- a/plugins/inputs/neptune_apex/neptune_apex_test.go +++ b/plugins/inputs/neptune_apex/neptune_apex_test.go @@ -57,9 +57,7 @@ func TestGather(t *testing.T) { } func TestParseXML(t *testing.T) { - n := &NeptuneApex{} - goodTime := time.Date(2018, 12, 22, 21, 55, 37, 0, - time.FixedZone("PST", 3600*-8)) + goodTime := time.Date(2018, 12, 22, 21, 55, 37, 0, time.FixedZone("PST", 3600*-8)) tests := []struct { name string xmlResponse []byte @@ -363,7 +361,7 @@ func TestParseXML(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { var acc testutil.Accumulator - err := n.parseXML(&acc, test.xmlResponse) + err := parseXML(&acc, test.xmlResponse) if test.wantErr { require.Error(t, err, "expected error but got ") return diff --git a/plugins/inputs/netflow/netflow.go b/plugins/inputs/netflow/netflow.go index 218d9b5296dba..162a6d82db6a2 100644 --- a/plugins/inputs/netflow/netflow.go +++ b/plugins/inputs/netflow/netflow.go @@ -114,7 +114,7 @@ func (n *NetFlow) Start(acc telegraf.Accumulator) error { return nil } -func (n *NetFlow) Gather(_ telegraf.Accumulator) error { +func (*NetFlow) Gather(telegraf.Accumulator) error { return nil } diff --git a/plugins/inputs/netflow/netflow_v5.go b/plugins/inputs/netflow/netflow_v5.go index 839a1d0943598..dadf3df0f2c80 100644 --- a/plugins/inputs/netflow/netflow_v5.go +++ b/plugins/inputs/netflow/netflow_v5.go @@ -15,14 +15,14 @@ import ( // Decoder structure type netflowv5Decoder struct{} -func (d *netflowv5Decoder) init() error { +func (*netflowv5Decoder) init() error { if err := initL4ProtoMapping(); err != nil { return fmt.Errorf("initializing layer 4 protocol mapping failed: %w", err) } return nil } -func (d *netflowv5Decoder) decode(srcIP net.IP, payload []byte) ([]telegraf.Metric, error) { +func (*netflowv5Decoder) decode(srcIP net.IP, payload []byte) ([]telegraf.Metric, error) { src := srcIP.String() // Decode the message diff --git a/plugins/inputs/nginx_upstream_check/nginx_upstream_check.go b/plugins/inputs/nginx_upstream_check/nginx_upstream_check.go index c1d02e5cae9f9..68edaa0cacd32 100644 --- a/plugins/inputs/nginx_upstream_check/nginx_upstream_check.go +++ b/plugins/inputs/nginx_upstream_check/nginx_upstream_check.go @@ -160,7 +160,7 @@ func (check *NginxUpstreamCheck) gatherStatusData(address string, accumulator te fields := map[string]interface{}{ "status": server.Status, - "status_code": check.getStatusCode(server.Status), + "status_code": getStatusCode(server.Status), "rise": server.Rise, "fall": server.Fall, } @@ -171,7 +171,7 @@ func (check *NginxUpstreamCheck) gatherStatusData(address string, accumulator te return nil } -func (check *NginxUpstreamCheck) getStatusCode(status string) uint8 { +func getStatusCode(status string) uint8 { switch status { case "up": return 1 diff --git a/plugins/inputs/nsq_consumer/nsq_consumer.go b/plugins/inputs/nsq_consumer/nsq_consumer.go index 69f2a0aea73a1..1516e4f2a1417 100644 --- a/plugins/inputs/nsq_consumer/nsq_consumer.go +++ b/plugins/inputs/nsq_consumer/nsq_consumer.go @@ -138,7 +138,7 @@ func (n *NSQConsumer) Start(ac telegraf.Accumulator) error { return nil } -func (n *NSQConsumer) Gather(_ telegraf.Accumulator) error { +func (*NSQConsumer) Gather(telegraf.Accumulator) error { return nil } diff --git a/plugins/inputs/nvidia_smi/nvidia_smi.go b/plugins/inputs/nvidia_smi/nvidia_smi.go index 695b8c6f601ee..e4714b0ff37f8 100644 --- a/plugins/inputs/nvidia_smi/nvidia_smi.go +++ b/plugins/inputs/nvidia_smi/nvidia_smi.go @@ -51,7 +51,7 @@ func (smi *NvidiaSMI) Start(telegraf.Accumulator) error { return nil } -func (smi *NvidiaSMI) Stop() {} +func (*NvidiaSMI) Stop() {} // Gather implements the telegraf interface func (smi *NvidiaSMI) Gather(acc telegraf.Accumulator) error { diff --git a/plugins/inputs/opentelemetry/writer.go b/plugins/inputs/opentelemetry/writer.go index b7701678edc17..6af00b6eb4af1 100644 --- a/plugins/inputs/opentelemetry/writer.go +++ b/plugins/inputs/opentelemetry/writer.go @@ -49,6 +49,6 @@ func (w *writeToAccumulator) EnqueuePoint( return nil } -func (w *writeToAccumulator) WriteBatch(_ context.Context) error { +func (*writeToAccumulator) WriteBatch(context.Context) error { return nil } diff --git a/plugins/inputs/pf/pf.go b/plugins/inputs/pf/pf.go index 20709aaf750d9..1e7eb4a63aab7 100644 --- a/plugins/inputs/pf/pf.go +++ b/plugins/inputs/pf/pf.go @@ -104,7 +104,7 @@ func (pf *PF) Gather(acc telegraf.Accumulator) error { return nil } - if perr := pf.parsePfctlOutput(o, acc); perr != nil { + if perr := parsePfctlOutput(o, acc); perr != nil { acc.AddError(perr) } return nil @@ -114,7 +114,7 @@ func errMissingData(tag string) error { return fmt.Errorf("struct data for tag %q not found in %s output", tag, pfctlCommand) } -func (pf *PF) parsePfctlOutput(pfoutput string, acc telegraf.Accumulator) error { +func parsePfctlOutput(pfoutput string, acc telegraf.Accumulator) error { fields := make(map[string]interface{}) scanner := bufio.NewScanner(strings.NewReader(pfoutput)) for scanner.Scan() { diff --git a/plugins/inputs/phpfpm/fcgi_test.go b/plugins/inputs/phpfpm/fcgi_test.go index d039685bb05f8..73f14cb776af9 100644 --- a/plugins/inputs/phpfpm/fcgi_test.go +++ b/plugins/inputs/phpfpm/fcgi_test.go @@ -72,7 +72,7 @@ type nilCloser struct { io.ReadWriter } -func (c *nilCloser) Close() error { return nil } +func (*nilCloser) Close() error { return nil } func TestStreams(t *testing.T) { var rec record @@ -125,11 +125,11 @@ func (c *writeOnlyConn) Write(p []byte) (int, error) { return len(p), nil } -func (c *writeOnlyConn) Read(_ []byte) (int, error) { +func (*writeOnlyConn) Read([]byte) (int, error) { return 0, errors.New("conn is write-only") } -func (c *writeOnlyConn) Close() error { +func (*writeOnlyConn) Close() error { return nil } diff --git a/plugins/inputs/phpfpm/phpfpm_test.go b/plugins/inputs/phpfpm/phpfpm_test.go index 802c761532ccc..d267b57ca2f28 100644 --- a/plugins/inputs/phpfpm/phpfpm_test.go +++ b/plugins/inputs/phpfpm/phpfpm_test.go @@ -31,7 +31,7 @@ import ( type statServer struct{} // We create a fake server to return test data -func (s statServer) ServeHTTP(w http.ResponseWriter, _ *http.Request) { +func (statServer) ServeHTTP(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "text/plain") w.Header().Set("Content-Length", strconv.Itoa(len(outputSample))) fmt.Fprint(w, outputSample) diff --git a/plugins/inputs/powerdns/powerdns_linux_test.go b/plugins/inputs/powerdns/powerdns_linux_test.go index 772bee4c4d46d..5bb576759a9f4 100644 --- a/plugins/inputs/powerdns/powerdns_linux_test.go +++ b/plugins/inputs/powerdns/powerdns_linux_test.go @@ -13,9 +13,7 @@ import ( "github.com/stretchr/testify/require" ) -type statServer struct{} - -func (s statServer) serverSocket(l net.Listener) { +func serverSocket(l net.Listener) { for { conn, err := l.Accept() if err != nil { @@ -46,8 +44,7 @@ func TestPowerdnsGeneratesMetrics(t *testing.T) { defer socket.Close() - s := statServer{} - go s.serverSocket(socket) + go serverSocket(socket) p := &Powerdns{ UnixSockets: []string{sockname}, diff --git a/plugins/inputs/powerdns_recursor/powerdns_recursor.go b/plugins/inputs/powerdns_recursor/powerdns_recursor.go index 48a77518f5a6a..3fd8e19c55a6c 100644 --- a/plugins/inputs/powerdns_recursor/powerdns_recursor.go +++ b/plugins/inputs/powerdns_recursor/powerdns_recursor.go @@ -53,7 +53,7 @@ func (p *PowerdnsRecursor) Init() error { case 2: p.gatherFromServer = p.gatherFromV2Server case 3: - p.gatherFromServer = p.gatherFromV3Server + p.gatherFromServer = gatherFromV3Server default: return fmt.Errorf("unknown control protocol version '%d', allowed values are 1, 2, 3", p.ControlProtocolVersion) } diff --git a/plugins/inputs/powerdns_recursor/protocol_v3.go b/plugins/inputs/powerdns_recursor/protocol_v3.go index b6e04e5ea58bb..9dbc9bd776fe4 100644 --- a/plugins/inputs/powerdns_recursor/protocol_v3.go +++ b/plugins/inputs/powerdns_recursor/protocol_v3.go @@ -16,7 +16,7 @@ import ( // status: uint32 // dataLength: size_t // data: byte[dataLength] -func (p *PowerdnsRecursor) gatherFromV3Server(address string, acc telegraf.Accumulator) error { +func gatherFromV3Server(address string, acc telegraf.Accumulator) error { conn, err := net.Dial("unix", address) if err != nil { return err diff --git a/plugins/inputs/procstat/native_finder.go b/plugins/inputs/procstat/native_finder.go index 192a431acd503..976cc79636ef7 100644 --- a/plugins/inputs/procstat/native_finder.go +++ b/plugins/inputs/procstat/native_finder.go @@ -14,7 +14,7 @@ import ( type NativeFinder struct{} // Uid will return all pids for the given user -func (pg *NativeFinder) uid(user string) ([]pid, error) { +func (*NativeFinder) uid(user string) ([]pid, error) { var dst []pid procs, err := gopsprocess.Processes() if err != nil { @@ -34,7 +34,7 @@ func (pg *NativeFinder) uid(user string) ([]pid, error) { } // PidFile returns the pid from the pid file given. -func (pg *NativeFinder) pidFile(path string) ([]pid, error) { +func (*NativeFinder) pidFile(path string) ([]pid, error) { var pids []pid pidString, err := os.ReadFile(path) if err != nil { @@ -49,13 +49,13 @@ func (pg *NativeFinder) pidFile(path string) ([]pid, error) { } // FullPattern matches on the command line when the process was executed -func (pg *NativeFinder) fullPattern(pattern string) ([]pid, error) { +func (*NativeFinder) fullPattern(pattern string) ([]pid, error) { var pids []pid regxPattern, err := regexp.Compile(pattern) if err != nil { return pids, err } - procs, err := pg.fastProcessList() + procs, err := fastProcessList() if err != nil { return pids, err } @@ -73,7 +73,7 @@ func (pg *NativeFinder) fullPattern(pattern string) ([]pid, error) { } // Children matches children pids on the command line when the process was executed -func (pg *NativeFinder) children(processID pid) ([]pid, error) { +func (*NativeFinder) children(processID pid) ([]pid, error) { // Get all running processes p, err := gopsprocess.NewProcess(int32(processID)) if err != nil { @@ -93,7 +93,7 @@ func (pg *NativeFinder) children(processID pid) ([]pid, error) { return pids, err } -func (pg *NativeFinder) fastProcessList() ([]*gopsprocess.Process, error) { +func fastProcessList() ([]*gopsprocess.Process, error) { pids, err := gopsprocess.Pids() if err != nil { return nil, err @@ -107,13 +107,13 @@ func (pg *NativeFinder) fastProcessList() ([]*gopsprocess.Process, error) { } // Pattern matches on the process name -func (pg *NativeFinder) pattern(pattern string) ([]pid, error) { +func (*NativeFinder) pattern(pattern string) ([]pid, error) { var pids []pid regxPattern, err := regexp.Compile(pattern) if err != nil { return pids, err } - procs, err := pg.fastProcessList() + procs, err := fastProcessList() if err != nil { return pids, err } diff --git a/plugins/inputs/procstat/pgrep.go b/plugins/inputs/procstat/pgrep.go index add3a2dfb120d..8e61fff4449e6 100644 --- a/plugins/inputs/procstat/pgrep.go +++ b/plugins/inputs/procstat/pgrep.go @@ -23,7 +23,7 @@ func newPgrepFinder() (pidFinder, error) { return &pgrep{path}, nil } -func (pg *pgrep) pidFile(path string) ([]pid, error) { +func (*pgrep) pidFile(path string) ([]pid, error) { var pids []pid pidString, err := os.ReadFile(path) if err != nil { diff --git a/plugins/inputs/procstat/procstat.go b/plugins/inputs/procstat/procstat.go index 6bf1e8402dc69..ecc8a978105be 100644 --- a/plugins/inputs/procstat/procstat.go +++ b/plugins/inputs/procstat/procstat.go @@ -617,7 +617,7 @@ func (p *Procstat) cgroupPIDs() ([]pidsTags, error) { pidTags := make([]pidsTags, 0, len(items)) for _, item := range items { - pids, err := p.singleCgroupPIDs(item) + pids, err := singleCgroupPIDs(item) if err != nil { return nil, err } @@ -628,7 +628,7 @@ func (p *Procstat) cgroupPIDs() ([]pidsTags, error) { return pidTags, nil } -func (p *Procstat) singleCgroupPIDs(path string) ([]pid, error) { +func singleCgroupPIDs(path string) ([]pid, error) { ok, err := isDir(path) if err != nil { return nil, err diff --git a/plugins/inputs/procstat/procstat_test.go b/plugins/inputs/procstat/procstat_test.go index 4256f08e24234..85282ffb5df46 100644 --- a/plugins/inputs/procstat/procstat_test.go +++ b/plugins/inputs/procstat/procstat_test.go @@ -126,7 +126,7 @@ func (p *testProc) pid() pid { return p.procID } -func (p *testProc) Name() (string, error) { +func (*testProc) Name() (string, error) { return "test_proc", nil } @@ -134,7 +134,7 @@ func (p *testProc) setTag(k, v string) { p.tags[k] = v } -func (p *testProc) MemoryMaps(bool) (*[]gopsprocess.MemoryMapsStat, error) { +func (*testProc) MemoryMaps(bool) (*[]gopsprocess.MemoryMapsStat, error) { stats := make([]gopsprocess.MemoryMapsStat, 0) return &stats, nil } diff --git a/plugins/inputs/prometheus/kubernetes.go b/plugins/inputs/prometheus/kubernetes.go index 2c4ef136c18ca..f1b303ecdb977 100644 --- a/plugins/inputs/prometheus/kubernetes.go +++ b/plugins/inputs/prometheus/kubernetes.go @@ -419,7 +419,7 @@ func registerPod(pod *corev1.Pod, p *Prometheus) { tags[k] = v } } - podURL := p.addressToURL(targetURL, targetURL.Hostname()) + podURL := addressToURL(targetURL, targetURL.Hostname()) // Locks earlier if using cAdvisor calls - makes a new list each time // rather than updating and removing from the same list diff --git a/plugins/inputs/prometheus/prometheus.go b/plugins/inputs/prometheus/prometheus.go index 8b557a9cab979..85d2de1f41cba 100644 --- a/plugins/inputs/prometheus/prometheus.go +++ b/plugins/inputs/prometheus/prometheus.go @@ -338,7 +338,7 @@ func (p *Prometheus) initFilters() error { return nil } -func (p *Prometheus) addressToURL(u *url.URL, address string) *url.URL { +func addressToURL(u *url.URL, address string) *url.URL { host := address if u.Port() != "" { host = address + ":" + u.Port() @@ -393,7 +393,7 @@ func (p *Prometheus) getAllURLs() (map[string]urlAndAddress, error) { continue } for _, resolved := range resolvedAddresses { - serviceURL := p.addressToURL(address, resolved) + serviceURL := addressToURL(address, resolved) allURLs[serviceURL.String()] = urlAndAddress{ url: serviceURL, address: resolved, diff --git a/plugins/inputs/radius/radius.go b/plugins/inputs/radius/radius.go index 984f31d93c2a9..efb71a1df9f2e 100644 --- a/plugins/inputs/radius/radius.go +++ b/plugins/inputs/radius/radius.go @@ -32,7 +32,7 @@ type Radius struct { //go:embed sample.conf var sampleConfig string -func (r *Radius) SampleConfig() string { +func (*Radius) SampleConfig() string { return sampleConfig } diff --git a/plugins/inputs/raindrops/raindrops.go b/plugins/inputs/raindrops/raindrops.go index 762d2af810ef3..de2c5a82bc458 100644 --- a/plugins/inputs/raindrops/raindrops.go +++ b/plugins/inputs/raindrops/raindrops.go @@ -89,7 +89,7 @@ func (r *Raindrops) gatherURL(addr *url.URL, acc telegraf.Accumulator) error { if err != nil { return err } - tags := r.getTags(addr) + tags := getTags(addr) fields := map[string]interface{}{ "calling": calling, "writing": writing, @@ -153,7 +153,7 @@ func (r *Raindrops) gatherURL(addr *url.URL, acc telegraf.Accumulator) error { } // Get tag(s) for the raindrops calling/writing plugin -func (r *Raindrops) getTags(addr *url.URL) map[string]string { +func getTags(addr *url.URL) map[string]string { h := addr.Host host, port, err := net.SplitHostPort(h) if err != nil { diff --git a/plugins/inputs/raindrops/raindrops_test.go b/plugins/inputs/raindrops/raindrops_test.go index 82def94f1484e..ac3c8692e96bb 100644 --- a/plugins/inputs/raindrops/raindrops_test.go +++ b/plugins/inputs/raindrops/raindrops_test.go @@ -35,11 +35,10 @@ writing: 200 // Verify that raindrops tags are properly parsed based on the server func TestRaindropsTags(t *testing.T) { urls := []string{"http://localhost/_raindrops", "http://localhost:80/_raindrops"} - r := &Raindrops{} for _, url1 := range urls { addr, err := url.Parse(url1) require.NoError(t, err) - tagMap := r.getTags(addr) + tagMap := getTags(addr) require.Contains(t, tagMap["server"], "localhost") } } diff --git a/plugins/inputs/redis/redis.go b/plugins/inputs/redis/redis.go index 4f31f6dda18b5..e0a19f3f18760 100644 --- a/plugins/inputs/redis/redis.go +++ b/plugins/inputs/redis/redis.go @@ -315,7 +315,7 @@ func (r *Redis) Gather(acc telegraf.Accumulator) error { wg.Add(1) go func(client Client) { defer wg.Done() - acc.AddError(r.gatherServer(client, acc)) + acc.AddError(gatherServer(client, acc)) acc.AddError(r.gatherCommandValues(client, acc)) }(client) } @@ -344,7 +344,7 @@ func (r *Redis) gatherCommandValues(client Client, acc telegraf.Accumulator) err return nil } -func (r *Redis) gatherServer(client Client, acc telegraf.Accumulator) error { +func gatherServer(client Client, acc telegraf.Accumulator) error { info, err := client.Info().Result() if err != nil { return err @@ -774,7 +774,7 @@ func coerceType(value interface{}, typ reflect.Type) reflect.Value { return reflect.ValueOf(value) } -func (r *Redis) Start(telegraf.Accumulator) error { +func (*Redis) Start(telegraf.Accumulator) error { return nil } diff --git a/plugins/inputs/redis/redis_test.go b/plugins/inputs/redis/redis_test.go index 0e96c49c358fe..f8f0d5b540f4d 100644 --- a/plugins/inputs/redis/redis_test.go +++ b/plugins/inputs/redis/redis_test.go @@ -17,19 +17,19 @@ import ( type testClient struct{} -func (t *testClient) BaseTags() map[string]string { +func (*testClient) BaseTags() map[string]string { return map[string]string{"host": "redis.net"} } -func (t *testClient) Info() *redis.StringCmd { +func (*testClient) Info() *redis.StringCmd { return nil } -func (t *testClient) Do(_ string, _ ...interface{}) (interface{}, error) { +func (*testClient) Do(string, ...interface{}) (interface{}, error) { return 2, nil } -func (t *testClient) Close() error { +func (*testClient) Close() error { return nil } diff --git a/plugins/inputs/rethinkdb/rethinkdb.go b/plugins/inputs/rethinkdb/rethinkdb.go index 79c42f583b1c2..2daf19312b4a2 100644 --- a/plugins/inputs/rethinkdb/rethinkdb.go +++ b/plugins/inputs/rethinkdb/rethinkdb.go @@ -30,7 +30,7 @@ func (*RethinkDB) SampleConfig() string { // Returns one of the errors encountered while gather stats (if any). func (r *RethinkDB) Gather(acc telegraf.Accumulator) error { if len(r.Servers) == 0 { - return r.gatherServer(localhost, acc) + return gatherServer(localhost, acc) } var wg sync.WaitGroup @@ -47,7 +47,7 @@ func (r *RethinkDB) Gather(acc telegraf.Accumulator) error { wg.Add(1) go func() { defer wg.Done() - acc.AddError(r.gatherServer(&Server{URL: u}, acc)) + acc.AddError(gatherServer(&Server{URL: u}, acc)) }() } @@ -56,7 +56,7 @@ func (r *RethinkDB) Gather(acc telegraf.Accumulator) error { return nil } -func (r *RethinkDB) gatherServer(server *Server, acc telegraf.Accumulator) error { +func gatherServer(server *Server, acc telegraf.Accumulator) error { var err error connectOpts := gorethink.ConnectOpts{ Address: server.URL.Host, diff --git a/plugins/inputs/riemann_listener/riemann_listener.go b/plugins/inputs/riemann_listener/riemann_listener.go index 526b7fda67fd9..e269f1bea8417 100644 --- a/plugins/inputs/riemann_listener/riemann_listener.go +++ b/plugins/inputs/riemann_listener/riemann_listener.go @@ -275,7 +275,7 @@ func (*RiemannSocketListener) SampleConfig() string { return sampleConfig } -func (rsl *RiemannSocketListener) Gather(_ telegraf.Accumulator) error { +func (*RiemannSocketListener) Gather(telegraf.Accumulator) error { return nil }