Skip to content

Commit

Permalink
chore: Fix linter findings for Windows (part4) (influxdata#13246)
Browse files Browse the repository at this point in the history
Co-authored-by: Pawel Zak <Pawel Zak>
Co-authored-by: pzak <pzak>
  • Loading branch information
zak-pawel authored Jun 9, 2023
1 parent 8b815cb commit a2f65d5
Show file tree
Hide file tree
Showing 24 changed files with 272 additions and 239 deletions.
4 changes: 2 additions & 2 deletions agent/agent_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ package agent

import "os"

func watchForFlushSignal(flushRequested chan os.Signal) {
func watchForFlushSignal(_ chan os.Signal) {
// not supported
}

func stopListeningForFlushSignal(flushRequested chan os.Signal) {
func stopListeningForFlushSignal(_ chan os.Signal) {
// not supported
}
11 changes: 2 additions & 9 deletions cmd/telegraf/telegraf_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type program struct {
*Telegraf
}

func (p *program) Start(s service.Service) error {
func (p *program) Start(_ service.Service) error {
go func() {
stop = make(chan struct{})
err := p.reloadLoop()
Expand All @@ -87,14 +87,7 @@ func (p *program) Start(s service.Service) error {
return nil
}

func (p *program) run(errChan chan error) {
stop = make(chan struct{})
err := p.reloadLoop()
errChan <- err
close(stop)
}

func (p *program) Stop(s service.Service) error {
func (p *program) Stop(_ service.Service) error {
var empty struct{}
stop <- empty // signal reloadLoop to finish (context cancel)
<-stop // wait for reloadLoop to finish and close channel
Expand Down
2 changes: 1 addition & 1 deletion logger/event_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type eventLoggerCreator struct {
logger *eventlog.Log
}

func (e *eventLoggerCreator) CreateLogger(config LogConfig) (io.Writer, error) {
func (e *eventLoggerCreator) CreateLogger(_ LogConfig) (io.Writer, error) {
return wlog.NewWriter(&eventLogger{logger: e.logger}), nil
}

Expand Down
1 change: 0 additions & 1 deletion plugins/inputs/diskio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.

```toml @sample.conf
# Read metrics about disk IO by device
# This plugin ONLY supports Linux
[[inputs.diskio]]
## By default, telegraf will gather stats for all devices including
## disk partitions.
Expand Down
1 change: 0 additions & 1 deletion plugins/inputs/diskio/sample.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Read metrics about disk IO by device
# This plugin ONLY supports Linux
[[inputs.diskio]]
## By default, telegraf will gather stats for all devices including
## disk partitions.
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/execd/execd_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/influxdata/telegraf"
)

func (e *Execd) Gather(acc telegraf.Accumulator) error {
func (e *Execd) Gather(_ telegraf.Accumulator) error {
if e.process == nil {
return nil
}
Expand Down
43 changes: 1 addition & 42 deletions plugins/inputs/filecount/filesystem_helpers.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package filecount

import (
"errors"
"io"
"os"
"time"
)

/*
The code below is lifted from numerous articles and originates from Andrew Gerrand's 10 things you (probably) don't know about Go.
it allows for mocking a filesystem; this allows for consistent testing of this code across platforms (directory sizes reported
It allows for mocking a filesystem; this allows for consistent testing of this code across platforms (directory sizes reported
differently by different platforms, for example), while preserving the rest of the functionality as-is, without modification.
*/

Expand All @@ -31,42 +29,3 @@ type osFS struct{}

func (osFS) Open(name string) (file, error) { return os.Open(name) }
func (osFS) Stat(name string) (os.FileInfo, error) { return os.Stat(name) }

/*
The following are for mocking the filesystem - this allows us to mock Stat() files. This means that we can set file attributes, and know that they
will be the same regardless of the platform sitting underneath our tests (directory sizes vary see https://github.com/influxdata/telegraf/issues/6011)
NOTE: still need the on-disk file structure to mirror this because the 3rd party library ("github.com/karrick/godirwalk") uses its own
walk functions, that we cannot mock from here.
*/

type fakeFileSystem struct {
files map[string]fakeFileInfo
}

type fakeFileInfo struct {
name string
size int64
filemode uint32
modtime time.Time
isdir bool
sys interface{}
}

func (f fakeFileInfo) Name() string { return f.name }
func (f fakeFileInfo) Size() int64 { return f.size }
func (f fakeFileInfo) Mode() os.FileMode { return os.FileMode(f.filemode) }
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) {
return nil, &os.PathError{Op: "Open", Path: name, Err: errors.New("Not implemented by fake filesystem")}
}

func (f fakeFileSystem) Stat(name string) (os.FileInfo, error) {
if fakeInfo, found := f.files[name]; found {
return fakeInfo, nil
}
return nil, &os.PathError{Op: "Stat", Path: name, Err: errors.New("No such file or directory")}
}
52 changes: 52 additions & 0 deletions plugins/inputs/filecount/filesystem_helpers_notwindows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//go:build !windows

// TODO: These types are not used in Windows tests because they are disabled for Windows.
// They can be moved to filesystem_helpers.go when following bug is fixed:
// https://github.com/influxdata/telegraf/issues/6248

package filecount

import (
"errors"
"os"
"time"
)

/*
The following are for mocking the filesystem - this allows us to mock Stat() files. This means that we can set file attributes, and know that they
will be the same regardless of the platform sitting underneath our tests (directory sizes vary see https://github.com/influxdata/telegraf/issues/6011)
NOTE: still need the on-disk file structure to mirror this because the 3rd party library ("github.com/karrick/godirwalk") uses its own
walk functions, that we cannot mock from here.
*/

type fakeFileSystem struct {
files map[string]fakeFileInfo
}

type fakeFileInfo struct {
name string
size int64
filemode uint32
modtime time.Time
isdir bool
sys interface{}
}

func (f fakeFileInfo) Name() string { return f.name }
func (f fakeFileInfo) Size() int64 { return f.size }
func (f fakeFileInfo) Mode() os.FileMode { return os.FileMode(f.filemode) }
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) {
return nil, &os.PathError{Op: "Open", Path: name, Err: errors.New("not implemented by fake filesystem")}
}

func (f fakeFileSystem) Stat(name string) (os.FileInfo, error) {
if fakeInfo, found := f.files[name]; found {
return fakeInfo, nil
}
return nil, &os.PathError{Op: "Stat", Path: name, Err: errors.New("no such file or directory")}
}
16 changes: 1 addition & 15 deletions plugins/inputs/ping/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type Ping struct {
Binary string

// Arguments for ping command. When arguments is not empty, system binary will be used and
// other options (ping_interval, timeout, etc) will be ignored
// other options (ping_interval, timeout, etc.) will be ignored
Arguments []string

// Whether to resolve addresses using ipv6 or not.
Expand All @@ -88,20 +88,6 @@ type Ping struct {
Size *int
}

type roundTripTimeStats struct {
min float64
avg float64
max float64
stddev float64
}

type stats struct {
trans int
recv int
ttl int
roundTripTimeStats
}

func (*Ping) SampleConfig() string {
return sampleConfig
}
Expand Down
34 changes: 24 additions & 10 deletions plugins/inputs/ping/ping_notwindows.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ import (
"github.com/influxdata/telegraf"
)

type roundTripTimeStats struct {
min float64
avg float64
max float64
stddev float64
}

type statistics struct {
packetsTransmitted int
packetsReceived int
ttl int
roundTripTimeStats
}

func (p *Ping) pingToURL(u string, acc telegraf.Accumulator) {
tags := map[string]string{"url": u}
fields := map[string]interface{}{"result_code": 0}
Expand Down Expand Up @@ -67,11 +81,11 @@ func (p *Ping) pingToURL(u string, acc telegraf.Accumulator) {
}

// Calculate packet loss percentage
loss := float64(stats.trans-stats.recv) / float64(stats.trans) * 100.0
percentPacketLoss := float64(stats.packetsTransmitted-stats.packetsReceived) / float64(stats.packetsTransmitted) * 100.0

fields["packets_transmitted"] = stats.trans
fields["packets_received"] = stats.recv
fields["percent_packet_loss"] = loss
fields["packets_transmitted"] = stats.packetsTransmitted
fields["packets_received"] = stats.packetsReceived
fields["percent_packet_loss"] = percentPacketLoss
if stats.ttl >= 0 {
fields["ttl"] = stats.ttl
}
Expand Down Expand Up @@ -165,11 +179,11 @@ func (p *Ping) args(url string, system string) []string {
// round-trip min/avg/max/stddev = 34.843/43.508/52.172/8.664 ms
//
// It returns (<transmitted packets>, <received packets>, <average response>)
func processPingOutput(out string) (stats, error) {
stats := stats{
trans: 0,
recv: 0,
ttl: -1,
func processPingOutput(out string) (statistics, error) {
stats := statistics{
packetsTransmitted: 0,
packetsReceived: 0,
ttl: -1,
roundTripTimeStats: roundTripTimeStats{
min: -1.0,
avg: -1.0,
Expand All @@ -186,7 +200,7 @@ func processPingOutput(out string) (stats, error) {
if stats.ttl == -1 && (strings.Contains(line, "ttl=") || strings.Contains(line, "hlim=")) {
stats.ttl, err = getTTL(line)
} else if strings.Contains(line, "transmitted") && strings.Contains(line, "received") {
stats.trans, stats.recv, err = getPacketStats(line)
stats.packetsTransmitted, stats.packetsReceived, err = getPacketStats(line)
if err != nil {
return stats, err
}
Expand Down
20 changes: 10 additions & 10 deletions plugins/inputs/ping/ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ func TestProcessPingOutput(t *testing.T) {
stats, err := processPingOutput(bsdPingOutput)
require.NoError(t, err)
require.Equal(t, 55, stats.ttl, "ttl value is 55")
require.Equal(t, 5, stats.trans, "5 packets were transmitted")
require.Equal(t, 5, stats.recv, "5 packets were received")
require.Equal(t, 5, stats.packetsTransmitted, "5 packets were transmitted")
require.Equal(t, 5, stats.packetsReceived, "5 packets were received")
require.InDelta(t, 15.087, stats.min, 0.001)
require.InDelta(t, 20.224, stats.avg, 0.001)
require.InDelta(t, 27.263, stats.max, 0.001)
Expand All @@ -92,8 +92,8 @@ func TestProcessPingOutput(t *testing.T) {
stats, err = processPingOutput(freebsdPing6Output)
require.NoError(t, err)
require.Equal(t, 117, stats.ttl, "ttl value is 117")
require.Equal(t, 5, stats.trans, "5 packets were transmitted")
require.Equal(t, 5, stats.recv, "5 packets were received")
require.Equal(t, 5, stats.packetsTransmitted, "5 packets were transmitted")
require.Equal(t, 5, stats.packetsReceived, "5 packets were received")
require.InDelta(t, 35.727, stats.min, 0.001)
require.InDelta(t, 53.211, stats.avg, 0.001)
require.InDelta(t, 93.870, stats.max, 0.001)
Expand All @@ -102,8 +102,8 @@ func TestProcessPingOutput(t *testing.T) {
stats, err = processPingOutput(linuxPingOutput)
require.NoError(t, err)
require.Equal(t, 63, stats.ttl, "ttl value is 63")
require.Equal(t, 5, stats.trans, "5 packets were transmitted")
require.Equal(t, 5, stats.recv, "5 packets were received")
require.Equal(t, 5, stats.packetsTransmitted, "5 packets were transmitted")
require.Equal(t, 5, stats.packetsReceived, "5 packets were received")
require.InDelta(t, 35.225, stats.min, 0.001)
require.InDelta(t, 43.628, stats.avg, 0.001)
require.InDelta(t, 51.806, stats.max, 0.001)
Expand All @@ -112,8 +112,8 @@ func TestProcessPingOutput(t *testing.T) {
stats, err = processPingOutput(busyBoxPingOutput)
require.NoError(t, err)
require.Equal(t, 56, stats.ttl, "ttl value is 56")
require.Equal(t, 4, stats.trans, "4 packets were transmitted")
require.Equal(t, 4, stats.recv, "4 packets were received")
require.Equal(t, 4, stats.packetsTransmitted, "4 packets were transmitted")
require.Equal(t, 4, stats.packetsReceived, "4 packets were received")
require.InDelta(t, 15.810, stats.min, 0.001)
require.InDelta(t, 17.611, stats.avg, 0.001)
require.InDelta(t, 22.559, stats.max, 0.001)
Expand All @@ -139,8 +139,8 @@ func TestProcessPingOutputWithVaryingTTL(t *testing.T) {
stats, err := processPingOutput(linuxPingOutputWithVaryingTTL)
require.NoError(t, err)
require.Equal(t, 63, stats.ttl, "ttl value is 63")
require.Equal(t, 5, stats.trans, "5 packets were transmitted")
require.Equal(t, 5, stats.recv, "5 packets were transmitted")
require.Equal(t, 5, stats.packetsTransmitted, "5 packets were transmitted")
require.Equal(t, 5, stats.packetsReceived, "5 packets were transmitted")
require.InDelta(t, 35.225, stats.min, 0.001)
require.InDelta(t, 43.628, stats.avg, 0.001)
require.InDelta(t, 51.806, stats.max, 0.001)
Expand Down
Loading

0 comments on commit a2f65d5

Please sign in to comment.