Skip to content

Commit

Permalink
chore: Fix linter findings for revive:exported in plugins/inputs/h*
Browse files Browse the repository at this point in the history
  • Loading branch information
zak-pawel committed Oct 20, 2024
1 parent e257c14 commit 2470d53
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 202 deletions.
58 changes: 29 additions & 29 deletions plugins/inputs/haproxy/haproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,41 @@ import (
//go:embed sample.conf
var sampleConfig string

var (
typeNames = []string{"frontend", "backend", "server", "listener"}
fieldRenames = map[string]string{
"pxname": "proxy",
"svname": "sv",
"act": "active_servers",
"bck": "backup_servers",
"cli_abrt": "cli_abort",
"srv_abrt": "srv_abort",
"hrsp_1xx": "http_response.1xx",
"hrsp_2xx": "http_response.2xx",
"hrsp_3xx": "http_response.3xx",
"hrsp_4xx": "http_response.4xx",
"hrsp_5xx": "http_response.5xx",
"hrsp_other": "http_response.other",
}
)

// CSV format: https://cbonte.github.io/haproxy-dconv/1.5/configuration.html#9.1

type haproxy struct {
Servers []string
KeepFieldNames bool
Username string
Password string
type HAProxy struct {
Servers []string `toml:"servers"`
KeepFieldNames bool `toml:"keep_field_names"`
Username string `toml:"username"`
Password string `toml:"password"`
tls.ClientConfig

client *http.Client
}

func (*haproxy) SampleConfig() string {
func (*HAProxy) SampleConfig() string {
return sampleConfig
}

// Reads stats from all configured servers accumulates stats.
// Returns one of the errors encountered while gather stats (if any).
func (h *haproxy) Gather(acc telegraf.Accumulator) error {
func (h *HAProxy) Gather(acc telegraf.Accumulator) error {
if len(h.Servers) == 0 {
return h.gatherServer("http://127.0.0.1:1936/haproxy?stats", acc)
}
Expand Down Expand Up @@ -85,7 +101,7 @@ func (h *haproxy) Gather(acc telegraf.Accumulator) error {
return nil
}

func (h *haproxy) gatherServerSocket(addr string, acc telegraf.Accumulator) error {
func (h *HAProxy) gatherServerSocket(addr string, acc telegraf.Accumulator) error {
var network, address string
if strings.HasPrefix(addr, "tcp://") {
network = "tcp"
Expand All @@ -108,7 +124,7 @@ func (h *haproxy) gatherServerSocket(addr string, acc telegraf.Accumulator) erro
return h.importCsvResult(c, acc, address)
}

func (h *haproxy) gatherServer(addr string, acc telegraf.Accumulator) error {
func (h *HAProxy) gatherServer(addr string, acc telegraf.Accumulator) error {
if !strings.HasPrefix(addr, "http") {
return h.gatherServerSocket(addr, acc)
}
Expand Down Expand Up @@ -179,23 +195,7 @@ func getSocketAddr(sock string) string {
return socketAddr[0]
}

var typeNames = []string{"frontend", "backend", "server", "listener"}
var fieldRenames = map[string]string{
"pxname": "proxy",
"svname": "sv",
"act": "active_servers",
"bck": "backup_servers",
"cli_abrt": "cli_abort",
"srv_abrt": "srv_abort",
"hrsp_1xx": "http_response.1xx",
"hrsp_2xx": "http_response.2xx",
"hrsp_3xx": "http_response.3xx",
"hrsp_4xx": "http_response.4xx",
"hrsp_5xx": "http_response.5xx",
"hrsp_other": "http_response.other",
}

func (h *haproxy) importCsvResult(r io.Reader, acc telegraf.Accumulator, host string) error {
func (h *HAProxy) importCsvResult(r io.Reader, acc telegraf.Accumulator, host string) error {
csvr := csv.NewReader(r)
now := time.Now()

Expand Down Expand Up @@ -278,6 +278,6 @@ func (h *haproxy) importCsvResult(r io.Reader, acc telegraf.Accumulator, host st

func init() {
inputs.Add("haproxy", func() telegraf.Input {
return &haproxy{}
return &HAProxy{}
})
}
26 changes: 13 additions & 13 deletions plugins/inputs/haproxy/haproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestHaproxyGeneratesMetricsWithAuthentication(t *testing.T) {
defer ts.Close()

// Now we tested again above server, with our authentication data
r := &haproxy{
r := &HAProxy{
Servers: []string{strings.Replace(ts.URL, "http://", "http://user:password@", 1)},
}

Expand All @@ -82,11 +82,11 @@ func TestHaproxyGeneratesMetricsWithAuthentication(t *testing.T) {
"type": "server",
}

fields := HaproxyGetFieldValues()
fields := haproxyGetFieldValues()
acc.AssertContainsTaggedFields(t, "haproxy", fields, tags)

// Here, we should get error because we don't pass authentication data
r = &haproxy{
r = &HAProxy{
Servers: []string{ts.URL},
}

Expand All @@ -101,7 +101,7 @@ func TestHaproxyGeneratesMetricsWithoutAuthentication(t *testing.T) {
}))
defer ts.Close()

r := &haproxy{
r := &HAProxy{
Servers: []string{ts.URL},
}

Expand All @@ -116,7 +116,7 @@ func TestHaproxyGeneratesMetricsWithoutAuthentication(t *testing.T) {
"type": "server",
}

fields := HaproxyGetFieldValues()
fields := haproxyGetFieldValues()
acc.AssertContainsTaggedFields(t, "haproxy", fields, tags)
}

Expand All @@ -143,7 +143,7 @@ func TestHaproxyGeneratesMetricsUsingSocket(t *testing.T) {
go s.serverSocket(sock)
}

r := &haproxy{
r := &HAProxy{
Servers: []string{_globmask},
}

Expand All @@ -152,7 +152,7 @@ func TestHaproxyGeneratesMetricsUsingSocket(t *testing.T) {
err := r.Gather(&acc)
require.NoError(t, err)

fields := HaproxyGetFieldValues()
fields := haproxyGetFieldValues()

for _, sock := range sockets {
tags := map[string]string{
Expand Down Expand Up @@ -182,14 +182,14 @@ func TestHaproxyGeneratesMetricsUsingTcp(t *testing.T) {
s := statServer{}
go s.serverSocket(l)

r := &haproxy{
r := &HAProxy{
Servers: []string{"tcp://" + l.Addr().String()},
}

var acc testutil.Accumulator
require.NoError(t, r.Gather(&acc))

fields := HaproxyGetFieldValues()
fields := haproxyGetFieldValues()

tags := map[string]string{
"server": l.Addr().String(),
Expand All @@ -206,7 +206,7 @@ func TestHaproxyGeneratesMetricsUsingTcp(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 TestHaproxyDefaultGetFromLocalhost(t *testing.T) {
r := &haproxy{}
r := &HAProxy{}

var acc testutil.Accumulator

Expand All @@ -222,7 +222,7 @@ func TestHaproxyKeepFieldNames(t *testing.T) {
}))
defer ts.Close()

r := &haproxy{
r := &HAProxy{
Servers: []string{ts.URL},
KeepFieldNames: true,
}
Expand All @@ -238,7 +238,7 @@ func TestHaproxyKeepFieldNames(t *testing.T) {
"type": "server",
}

fields := HaproxyGetFieldValues()
fields := haproxyGetFieldValues()
fields["act"] = fields["active_servers"]
delete(fields, "active_servers")
fields["bck"] = fields["backup_servers"]
Expand Down Expand Up @@ -273,7 +273,7 @@ func mustReadSampleOutput() []byte {
return data
}

func HaproxyGetFieldValues() map[string]interface{} {
func haproxyGetFieldValues() map[string]interface{} {
fields := map[string]interface{}{
"active_servers": uint64(1),
"backup_servers": uint64(0),
Expand Down
6 changes: 4 additions & 2 deletions plugins/inputs/hddtemp/go-hddtemp/hddtemp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
)

// Disk contains disk data gathered from hddtemp
type Disk struct {
DeviceName string
Model string
Expand All @@ -16,13 +17,14 @@ type Disk struct {
Status string
}

type hddtemp struct {
}
type hddtemp struct{}

// New creates hddtemp
func New() *hddtemp {
return &hddtemp{}
}

// Fetch gathers disks data from hddtemp daemon.
func (h *hddtemp) Fetch(address string) ([]Disk, error) {
var (
err error
Expand Down
9 changes: 5 additions & 4 deletions plugins/inputs/hddtemp/hddtemp.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ var sampleConfig string
const defaultAddress = "127.0.0.1:7634"

type HDDTemp struct {
Address string
Devices []string
fetcher Fetcher
Address string `toml:"address"`
Devices []string `toml:"devices"`

fetcher fetcher
}

type Fetcher interface {
type fetcher interface {
Fetch(address string) ([]gohddtemp.Disk, error)
}

Expand Down
1 change: 1 addition & 0 deletions plugins/inputs/hddtemp/hddtemp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func (h *mockFetcher) Fetch(_ string) ([]hddtemp.Disk, error) {
},
}, nil
}

func newMockFetcher() *mockFetcher {
return &mockFetcher{}
}
Expand Down
11 changes: 4 additions & 7 deletions plugins/inputs/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ func (h *HTTP) Init() error {
return nil
}

func (h *HTTP) SetParserFunc(fn telegraf.ParserFunc) {
h.parserFunc = fn
}

func (h *HTTP) Start(_ telegraf.Accumulator) error {
return nil
}

// Gather takes in an accumulator and adds the metrics that the Input
// gathers. This is called every "interval"
func (h *HTTP) Gather(acc telegraf.Accumulator) error {
var wg sync.WaitGroup
for _, u := range h.URLs {
Expand All @@ -111,11 +113,6 @@ func (h *HTTP) Stop() {
}
}

// SetParserFunc takes the data_format from the config and finds the right parser for that format
func (h *HTTP) SetParserFunc(fn telegraf.ParserFunc) {
h.parserFunc = fn
}

// Gathers data from a particular URL
// Parameters:
//
Expand Down
6 changes: 3 additions & 3 deletions plugins/inputs/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func TestBodyAndContentEncoding(t *testing.T) {
}
}

type TestHandlerFunc func(t *testing.T, w http.ResponseWriter, r *http.Request)
type testHandlerFunc func(t *testing.T, w http.ResponseWriter, r *http.Request)

func TestOAuthClientCredentialsGrant(t *testing.T) {
ts := httptest.NewServer(http.NotFoundHandler())
Expand All @@ -331,8 +331,8 @@ func TestOAuthClientCredentialsGrant(t *testing.T) {
tests := []struct {
name string
plugin *httpplugin.HTTP
tokenHandler TestHandlerFunc
handler TestHandlerFunc
tokenHandler testHandlerFunc
handler testHandlerFunc
}{
{
name: "no credentials",
Expand Down
Loading

0 comments on commit 2470d53

Please sign in to comment.