Skip to content

chore: Fix linter findings for revive:exported in plugins/inputs/[w-z]*#16703

Merged
skartikey merged 7 commits intoinfluxdata:masterfrom
zak-pawel:zak-pawelrevive-exported-inputs_w-z
Apr 1, 2025
Merged

chore: Fix linter findings for revive:exported in plugins/inputs/[w-z]*#16703
skartikey merged 7 commits intoinfluxdata:masterfrom
zak-pawel:zak-pawelrevive-exported-inputs_w-z

Conversation

@zak-pawel
Copy link
Collaborator

Summary

Address findings for revive:exported in plugins/inputs/[w-z]*.

As part of this effort for files from plugins/inputs/[w-z]*, the following actions were taken:

  • Type names (const, var, struct, func, etc) were changed to unexported, wherever they didn't need to be exported.
  • All remaining exported types were given comments in the appropriate form – this does not apply to exported methods that implement "known" plugin interfaces (Gather|Init|Start|Stop|SampleConfig|Parse|Add|Apply|Serialize|SerializeBatch|SetParser|SetParserFunc|GetState|SetState).
  • The order of methods was organized (exported methods first, then unexported, with init at the very end).

It is only part of the bigger work (for issue: #15813).
After all findings of this type in whole project are handled, we can enable revive:exported rule in golangci-lint.

Checklist

  • No AI generated code was used in this PR

@telegraf-tiger telegraf-tiger bot added the chore label Mar 29, 2025
Copy link
Member

@srebhan srebhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @zak-pawel! Just a few comments...

Comment on lines 13 to 31
type Sysctl func(metric string) ([]string, error)
type Zpool func() ([]string, error)
type Zdataset func(properties []string) ([]string, error)
type Uname func() (string, error)

type Zfs struct {
KstatPath string
KstatMetrics []string
PoolMetrics bool
DatasetMetrics bool
Log telegraf.Logger `toml:"-"`

sysctl Sysctl //nolint:unused // False positive - this var is used for non-default build tag: freebsd
zpool Zpool //nolint:unused // False positive - this var is used for non-default build tag: freebsd
zdataset Zdataset //nolint:unused // False positive - this var is used for non-default build tag: freebsd
uname Uname //nolint:unused // False positive - this var is used for non-default build tag: freebsd
version int64 //nolint:unused // False positive - this var is used for non-default build tag: freebsd
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My problem with this change is that it will add redundancy for the config parameters i.e. if we add a new flag we now need to remember to all the different OS files. How about moving the "internal" variable to one OS-dependent implementation and have only one flag "nolint" member in the struct?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@srebhan

We once had a very similar discussion about ethtool: #13246 (comment)
It ended with very similar changes to what I’m proposing now: https://github.com/influxdata/telegraf/pull/13599/files#diff-949ac46ede5a4cd042a07cdba8e02ffa378666adb858898cad7d6c4bbbedc7b3

But if you insist, could you elaborate on your proposed fix? I’m not sure I fully understand it.

The issue concerns type definitions:

type sysctlF func(metric string) ([]string, error)
type zpoolF func() ([]string, error)
type zdatasetF func(properties []string) ([]string, error)
type unameF func() (string, error)

(which, after being changed to unexported, started being detected by the unused linter.)

And struct fields:

	DatasetMetrics bool `toml:"datasetMetrics"`

	sysctl   sysctlF
	zpool    zpoolF
	zdataset zdatasetF
	uname    unameF

(which are used only in freebsd)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well I thought about what we did in systemd_units but it seems like this looks the same... Anyway, as all other plugins do the same thing, I'm not vetoing this however, I'm questioning the usefulness of the new code. Up to now you could roll out a configuration for ZFS on all systems and would only see a log message. With your change, Telegraf will error out as some platforms now don't have certain parameters defined and are therefore failing at config-parsing state...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point. Reverted to have single struct.

Comment on lines 872 to 879
t.Run(tt.name, func(t *testing.T) {
got, err := TraceIDFromString(tt.s)
got, err := traceIDFromString(tt.s)
if (err != nil) != tt.wantErr {
t.Errorf("TraceIDFromString() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("traceIDFromString() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("TraceIDFromString() = %v, want %v", got, tt.want)
t.Errorf("traceIDFromString() = %v, want %v", got, tt.want)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you already touching this, do you think it's worth directly migrating those test to require? Same for the places below.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, changed in whole zipkin package

Comment on lines +25 to +29
var (
// defaultNetwork is the network to listen on; use only in tests.
defaultNetwork = "tcp"
)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's only used in tests, why not define it there?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is used both in prod and test. In test it is used to overwrite network.

Comment on lines 29 to 31
const (
defaultTimeout = 5 * time.Second
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hate those definitions! They add three lines without any benefit instead of just using the value at the corresponding location...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use this value

Copy link
Member

@srebhan srebhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @zak-pawel!

@srebhan srebhan added the ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review. label Mar 31, 2025
@srebhan srebhan assigned skartikey and mstrandboge and unassigned srebhan Mar 31, 2025
@zak-pawel
Copy link
Collaborator Author

@srebhan
I woke up at night screaming because I had nightmares about those //nolints.
So here's a slightly better version :)

@telegraf-tiger
Copy link
Contributor

telegraf-tiger bot commented Apr 1, 2025

Copy link
Contributor

@skartikey skartikey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zak-pawel Great work on this PR!

@skartikey skartikey merged commit ad76475 into influxdata:master Apr 1, 2025
26 of 27 checks passed
@github-actions github-actions bot added this to the v1.34.2 milestone Apr 1, 2025
@zak-pawel zak-pawel deleted the zak-pawelrevive-exported-inputs_w-z branch April 1, 2025 21:34
Comment on lines +18 to +28
type helper struct {
sysctl sysctlF
zpool zpoolF
zdataset zdatasetF
uname unameF
}

type sysctlF func(metric string) ([]string, error)
type zpoolF func() ([]string, error)
type zdatasetF func(properties []string) ([]string, error)
type unameF func() (string, error)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do they really need to be variable functions? Can't they be used directly?

-	lines, err := z.zdataset(properties)
+	lines, err := zdataset(properties)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tried running the tests after this change? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, thanks for just nicely saying that it's needed to mock them for tests..

skartikey pushed a commit to skartikey/telegraf that referenced this pull request Apr 14, 2025
srebhan pushed a commit that referenced this pull request Apr 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

chore linter ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

Comments