chore: Fix linter findings for revive:exported in plugins/inputs/[w-z]*#16703
Conversation
srebhan
left a comment
There was a problem hiding this comment.
Thanks a lot @zak-pawel! Just a few comments...
| 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 | ||
| } | ||
|
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
I see your point. Reverted to have single struct.
| 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) |
There was a problem hiding this comment.
As you already touching this, do you think it's worth directly migrating those test to require? Same for the places below.
There was a problem hiding this comment.
Sure, changed in whole zipkin package
| var ( | ||
| // defaultNetwork is the network to listen on; use only in tests. | ||
| defaultNetwork = "tcp" | ||
| ) | ||
|
|
There was a problem hiding this comment.
If it's only used in tests, why not define it there?
There was a problem hiding this comment.
It is used both in prod and test. In test it is used to overwrite network.
| const ( | ||
| defaultTimeout = 5 * time.Second | ||
| ) |
There was a problem hiding this comment.
I hate those definitions! They add three lines without any benefit instead of just using the value at the corresponding location...
There was a problem hiding this comment.
Just use this value
|
@srebhan |
|
Download PR build artifacts for linux_amd64.tar.gz, darwin_arm64.tar.gz, and windows_amd64.zip. 📦 Click here to get additional PR build artifactsArtifact URLs |
skartikey
left a comment
There was a problem hiding this comment.
@zak-pawel Great work on this PR!
| 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) |
There was a problem hiding this comment.
Do they really need to be variable functions? Can't they be used directly?
- lines, err := z.zdataset(properties)
+ lines, err := zdataset(properties)There was a problem hiding this comment.
Have you tried running the tests after this change? 🤔
There was a problem hiding this comment.
Okay, thanks for just nicely saying that it's needed to mock them for tests..
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:const,var,struct,func, etc) were changed to unexported, wherever they didn't need to be exported.Gather|Init|Start|Stop|SampleConfig|Parse|Add|Apply|Serialize|SerializeBatch|SetParser|SetParserFunc|GetState|SetState).initat 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:exportedrule ingolangci-lint.Checklist