Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Linter fixes for plugins/inputs/p* #10066

Merged
merged 8 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: Linter fixes for plugins/inputs/[p]*
  • Loading branch information
Pawel Zak authored and Pawel Zak committed Nov 5, 2021
commit 1fb18d048636d837fec29ca28f785d9c9402ee66
7 changes: 3 additions & 4 deletions plugins/inputs/passenger/passenger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf/testutil"
Expand Down Expand Up @@ -49,7 +48,7 @@ func Test_Invalid_Passenger_Status_Cli(t *testing.T) {

err := r.Gather(&acc)
require.Error(t, err)
assert.Contains(t, err.Error(), `exec: "an-invalid-command": executable file not found in `)
require.Contains(t, err.Error(), `exec: "an-invalid-command": executable file not found in `)
}

func Test_Invalid_Xml(t *testing.T) {
Expand All @@ -65,7 +64,7 @@ func Test_Invalid_Xml(t *testing.T) {

err = r.Gather(&acc)
require.Error(t, err)
assert.Equal(t, "cannot parse input with error: EOF", err.Error())
require.Equal(t, "cannot parse input with error: EOF", err.Error())
}

// We test this by ensure that the error message match the path of default cli
Expand All @@ -80,7 +79,7 @@ func Test_Default_Config_Load_Default_Command(t *testing.T) {

err = r.Gather(&acc)
require.Error(t, err)
assert.Contains(t, err.Error(), "exec: \"passenger-status\": executable file not found in ")
require.Contains(t, err.Error(), "exec: \"passenger-status\": executable file not found in ")
}

func TestPassengerGenerateMetric(t *testing.T) {
Expand Down
14 changes: 7 additions & 7 deletions plugins/inputs/pgbouncer/pgbouncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"fmt"
"testing"

"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf/plugins/inputs/postgresql"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestPgBouncerGeneratesMetricsIntegration(t *testing.T) {
Expand Down Expand Up @@ -55,20 +55,20 @@ func TestPgBouncerGeneratesMetricsIntegration(t *testing.T) {
metricsCounted := 0

for _, metric := range intMetricsPgBouncer {
assert.True(t, acc.HasInt64Field("pgbouncer", metric))
require.True(t, acc.HasInt64Field("pgbouncer", metric))
metricsCounted++
}

for _, metric := range intMetricsPgBouncerPools {
assert.True(t, acc.HasInt64Field("pgbouncer_pools", metric))
require.True(t, acc.HasInt64Field("pgbouncer_pools", metric))
metricsCounted++
}

for _, metric := range int32Metrics {
assert.True(t, acc.HasInt32Field("pgbouncer", metric))
require.True(t, acc.HasInt32Field("pgbouncer", metric))
metricsCounted++
}

assert.True(t, metricsCounted > 0)
assert.Equal(t, len(intMetricsPgBouncer)+len(intMetricsPgBouncerPools)+len(int32Metrics), metricsCounted)
require.True(t, metricsCounted > 0)
require.Equal(t, len(intMetricsPgBouncer)+len(intMetricsPgBouncerPools)+len(int32Metrics), metricsCounted)
}
12 changes: 6 additions & 6 deletions plugins/inputs/phpfpm/phpfpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import (
"net/http/httptest"
"testing"

"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf/testutil"
)

type statServer struct{}
Expand Down Expand Up @@ -283,7 +283,7 @@ func TestPhpFpmDefaultGetFromLocalhost(t *testing.T) {

err := acc.GatherError(r.Gather)
require.Error(t, err)
assert.Contains(t, err.Error(), "/status")
require.Contains(t, err.Error(), "/status")
}

func TestPhpFpmGeneratesMetrics_Throw_Error_When_Fpm_Status_Is_Not_Responding(t *testing.T) {
Expand All @@ -297,8 +297,8 @@ func TestPhpFpmGeneratesMetrics_Throw_Error_When_Fpm_Status_Is_Not_Responding(t

err := acc.GatherError(r.Gather)
require.Error(t, err)
assert.Contains(t, err.Error(), `unable to connect to phpfpm status page 'http://aninvalidone'`)
assert.Contains(t, err.Error(), `lookup aninvalidone`)
require.Contains(t, err.Error(), `unable to connect to phpfpm status page 'http://aninvalidone'`)
require.Contains(t, err.Error(), `lookup aninvalidone`)
}

func TestPhpFpmGeneratesMetrics_Throw_Error_When_Socket_Path_Is_Invalid(t *testing.T) {
Expand All @@ -312,7 +312,7 @@ func TestPhpFpmGeneratesMetrics_Throw_Error_When_Socket_Path_Is_Invalid(t *testi

err := acc.GatherError(r.Gather)
require.Error(t, err)
assert.Equal(t, `socket doesn't exist "/tmp/invalid.sock"`, err.Error())
require.Equal(t, `socket doesn't exist "/tmp/invalid.sock"`, err.Error())
}

const outputSample = `
Expand Down
17 changes: 16 additions & 1 deletion plugins/inputs/ping/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"github.com/go-ping/ping"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/inputs"
Expand Down Expand Up @@ -82,6 +83,20 @@ 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
powersj marked this conversation as resolved.
Show resolved Hide resolved
}

func (*Ping) Description() string {
return "Ping given url(s) and return statistics"
}
Expand Down Expand Up @@ -262,7 +277,7 @@ func (p *Ping) pingToURLNative(destination string, acc telegraf.Accumulator) {

sort.Sort(durationSlice(stats.Rtts))
for _, perc := range p.Percentiles {
var value = percentile(durationSlice(stats.Rtts), perc)
var value = percentile(stats.Rtts, perc)
powersj marked this conversation as resolved.
Show resolved Hide resolved
var field = fmt.Sprintf("percentile%v_ms", perc)
fields[field] = float64(value.Nanoseconds()) / float64(time.Millisecond)
}
Expand Down
86 changes: 51 additions & 35 deletions plugins/inputs/ping/ping_notwindows.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (p *Ping) pingToURL(u string, acc telegraf.Accumulator) {
return
}
}
trans, rec, ttl, min, avg, max, stddev, err := processPingOutput(out)
stats, err := processPingOutput(out)
if err != nil {
// fatal error
acc.AddError(fmt.Errorf("%s: %s", err, u))
Expand All @@ -67,25 +67,25 @@ func (p *Ping) pingToURL(u string, acc telegraf.Accumulator) {
}

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

fields["packets_transmitted"] = trans
fields["packets_received"] = rec
fields["packets_transmitted"] = stats.trans
fields["packets_received"] = stats.recv
fields["percent_packet_loss"] = loss
if ttl >= 0 {
fields["ttl"] = ttl
if stats.ttl >= 0 {
fields["ttl"] = stats.ttl
}
if min >= 0 {
fields["minimum_response_ms"] = min
if stats.min >= 0 {
fields["minimum_response_ms"] = stats.min
}
if avg >= 0 {
fields["average_response_ms"] = avg
if stats.avg >= 0 {
fields["average_response_ms"] = stats.avg
}
if max >= 0 {
fields["maximum_response_ms"] = max
if stats.max >= 0 {
fields["maximum_response_ms"] = stats.max
}
if stddev >= 0 {
fields["standard_deviation_ms"] = stddev
if stats.stddev >= 0 {
fields["standard_deviation_ms"] = stats.stddev
}
acc.AddFields("ping", fields, tags)
}
Expand Down Expand Up @@ -165,29 +165,39 @@ 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) (int, int, int, float64, float64, float64, float64, error) {
var trans, recv, ttl = 0, 0, -1
var min, avg, max, stddev = -1.0, -1.0, -1.0, -1.0
func processPingOutput(out string) (stats, error) {
stats := stats{
trans: 0,
recv: 0,
ttl: -1,
roundTripTimeStats: roundTripTimeStats{
min: -1.0,
avg: -1.0,
max: -1.0,
stddev: -1.0,
},
}

// Set this error to nil if we find a 'transmitted' line
err := errors.New("fatal error processing ping output")
lines := strings.Split(out, "\n")
for _, line := range lines {
// Reading only first TTL, ignoring other TTL messages
if ttl == -1 && (strings.Contains(line, "ttl=") || strings.Contains(line, "hlim=")) {
ttl, err = getTTL(line)
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") {
trans, recv, err = getPacketStats(line)
stats.trans, stats.recv, err = getPacketStats(line)
if err != nil {
return trans, recv, ttl, min, avg, max, stddev, err
return stats, err
}
} else if strings.Contains(line, "min/avg/max") {
min, avg, max, stddev, err = checkRoundTripTimeStats(line)
stats.roundTripTimeStats, err = checkRoundTripTimeStats(line)
if err != nil {
return trans, recv, ttl, min, avg, max, stddev, err
return stats, err
}
}
}
return trans, recv, ttl, min, avg, max, stddev, err
return stats, err
}

func getPacketStats(line string) (trans int, recv int, err error) {
Expand All @@ -210,29 +220,35 @@ func getTTL(line string) (int, error) {
return strconv.Atoi(ttlMatch[2])
}

func checkRoundTripTimeStats(line string) (min float64, avg float64, max float64, stddev float64, err error) {
min, avg, max, stddev = -1.0, -1.0, -1.0, -1.0
func checkRoundTripTimeStats(line string) (roundTripTimeStats, error) {
roundTripTimeStats := roundTripTimeStats{
min: -1.0,
avg: -1.0,
max: -1.0,
stddev: -1.0,
}

stats := strings.Split(line, " ")[3]
data := strings.Split(stats, "/")

min, err = strconv.ParseFloat(data[0], 64)
var err error
roundTripTimeStats.min, err = strconv.ParseFloat(data[0], 64)
if err != nil {
return min, avg, max, stddev, err
return roundTripTimeStats, err
}
avg, err = strconv.ParseFloat(data[1], 64)
roundTripTimeStats.avg, err = strconv.ParseFloat(data[1], 64)
if err != nil {
return min, avg, max, stddev, err
return roundTripTimeStats, err
}
max, err = strconv.ParseFloat(data[2], 64)
roundTripTimeStats.max, err = strconv.ParseFloat(data[2], 64)
if err != nil {
return min, avg, max, stddev, err
return roundTripTimeStats, err
}
if len(data) == 4 {
stddev, err = strconv.ParseFloat(data[3], 64)
roundTripTimeStats.stddev, err = strconv.ParseFloat(data[3], 64)
if err != nil {
return min, avg, max, stddev, err
return roundTripTimeStats, err
}
}
return min, avg, max, stddev, err
return roundTripTimeStats, err
}
Loading