Skip to content

Commit

Permalink
fix: Linter fixes for plugins/outputs/[g-m]* (#10127)
Browse files Browse the repository at this point in the history
Co-authored-by: Pawel Zak <Pawel Zak>
  • Loading branch information
zak-pawel authored Nov 24, 2021
1 parent 2b1a79f commit 020b77b
Show file tree
Hide file tree
Showing 27 changed files with 302 additions and 239 deletions.
19 changes: 12 additions & 7 deletions plugins/outputs/graphite/graphite.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (g *Graphite) Connect() error {
func (g *Graphite) Close() error {
// Closing all connections
for _, conn := range g.conns {
conn.Close()
_ = conn.Close()
}
return nil
}
Expand All @@ -133,11 +133,16 @@ func (g *Graphite) Description() string {
// props to Tv via the authors of carbon-relay-ng` for this trick.
func (g *Graphite) checkEOF(conn net.Conn) {
b := make([]byte, 1024)
conn.SetReadDeadline(time.Now().Add(10 * time.Millisecond))

if err := conn.SetReadDeadline(time.Now().Add(10 * time.Millisecond)); err != nil {
g.Log.Errorf("Couldn't set read deadline for connection %s. closing conn explicitly", conn)
_ = conn.Close()
return
}
num, err := conn.Read(b)
if err == io.EOF {
g.Log.Errorf("Conn %s is closed. closing conn explicitly", conn)
conn.Close()
_ = conn.Close()
return
}
// just in case i misunderstand something or the remote behaves badly
Expand All @@ -147,7 +152,7 @@ func (g *Graphite) checkEOF(conn net.Conn) {
// Log non-timeout errors or close.
if e, ok := err.(net.Error); !(ok && e.Timeout()) {
g.Log.Errorf("conn %s checkEOF .conn.Read returned err != EOF, which is unexpected. closing conn. error: %s", conn, err)
conn.Close()
_ = conn.Close()
}
}

Expand All @@ -174,7 +179,7 @@ func (g *Graphite) Write(metrics []telegraf.Metric) error {
// try to reconnect and retry to send
if err != nil {
g.Log.Error("Graphite: Reconnecting and retrying...")
g.Connect()
_ = g.Connect()
err = g.send(batch)
}

Expand All @@ -189,14 +194,14 @@ func (g *Graphite) send(batch []byte) error {
p := rand.Perm(len(g.conns))
for _, n := range p {
if g.Timeout > 0 {
g.conns[n].SetWriteDeadline(time.Now().Add(time.Duration(g.Timeout) * time.Second))
_ = g.conns[n].SetWriteDeadline(time.Now().Add(time.Duration(g.Timeout) * time.Second))
}
g.checkEOF(g.conns[n])
if _, e := g.conns[n].Write(batch); e != nil {
// Error
g.Log.Errorf("Graphite Error: " + e.Error())
// Close explicitly and let's try the next one
g.conns[n].Close()
_ = g.conns[n].Close()
} else {
// Success
err = nil
Expand Down
64 changes: 31 additions & 33 deletions plugins/outputs/graphite/graphite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ import (
"testing"
"time"

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

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"

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

func TestGraphiteError(t *testing.T) {
Expand All @@ -39,7 +37,7 @@ func TestGraphiteError(t *testing.T) {
require.NoError(t, err1)
err2 := g.Write(metrics)
require.Error(t, err2)
assert.Equal(t, "could not write to any Graphite server in cluster", err2.Error())
require.Equal(t, "could not write to any Graphite server in cluster", err2.Error())
}

func TestGraphiteOK(t *testing.T) {
Expand Down Expand Up @@ -490,9 +488,9 @@ func TCPServer1(t *testing.T, wg *sync.WaitGroup) {
reader := bufio.NewReader(conn)
tp := textproto.NewReader(reader)
data1, _ := tp.ReadLine()
assert.Equal(t, "my.prefix.192_168_0_1.mymeasurement.myfield 3.14 1289430000", data1)
conn.Close()
tcpServer.Close()
require.Equal(t, "my.prefix.192_168_0_1.mymeasurement.myfield 3.14 1289430000", data1)
require.NoError(t, conn.Close())
require.NoError(t, tcpServer.Close())
}()
}

Expand All @@ -504,11 +502,11 @@ func TCPServer2(t *testing.T, wg *sync.WaitGroup) {
reader := bufio.NewReader(conn2)
tp := textproto.NewReader(reader)
data2, _ := tp.ReadLine()
assert.Equal(t, "my.prefix.192_168_0_1.mymeasurement 3.14 1289430000", data2)
require.Equal(t, "my.prefix.192_168_0_1.mymeasurement 3.14 1289430000", data2)
data3, _ := tp.ReadLine()
assert.Equal(t, "my.prefix.192_168_0_1.my_measurement 3.14 1289430000", data3)
conn2.Close()
tcpServer.Close()
require.Equal(t, "my.prefix.192_168_0_1.my_measurement 3.14 1289430000", data3)
require.NoError(t, conn2.Close())
require.NoError(t, tcpServer.Close())
}()
}

Expand All @@ -520,9 +518,9 @@ func TCPServer1WithMultipleTemplates(t *testing.T, wg *sync.WaitGroup) {
reader := bufio.NewReader(conn)
tp := textproto.NewReader(reader)
data1, _ := tp.ReadLine()
assert.Equal(t, "my.prefix.mymeasurement.valuetag.192_168_0_1.myfield 3.14 1289430000", data1)
conn.Close()
tcpServer.Close()
require.Equal(t, "my.prefix.mymeasurement.valuetag.192_168_0_1.myfield 3.14 1289430000", data1)
require.NoError(t, conn.Close())
require.NoError(t, tcpServer.Close())
}()
}

Expand All @@ -534,11 +532,11 @@ func TCPServer2WithMultipleTemplates(t *testing.T, wg *sync.WaitGroup) {
reader := bufio.NewReader(conn2)
tp := textproto.NewReader(reader)
data2, _ := tp.ReadLine()
assert.Equal(t, "my.prefix.mymeasurement.valuetag.192_168_0_1 3.14 1289430000", data2)
require.Equal(t, "my.prefix.mymeasurement.valuetag.192_168_0_1 3.14 1289430000", data2)
data3, _ := tp.ReadLine()
assert.Equal(t, "my.prefix.192_168_0_1.my_measurement.valuetag 3.14 1289430000", data3)
conn2.Close()
tcpServer.Close()
require.Equal(t, "my.prefix.192_168_0_1.my_measurement.valuetag 3.14 1289430000", data3)
require.NoError(t, conn2.Close())
require.NoError(t, tcpServer.Close())
}()
}

Expand All @@ -550,9 +548,9 @@ func TCPServer1WithTags(t *testing.T, wg *sync.WaitGroup) {
reader := bufio.NewReader(conn)
tp := textproto.NewReader(reader)
data1, _ := tp.ReadLine()
assert.Equal(t, "my.prefix.mymeasurement.myfield;host=192.168.0.1 3.14 1289430000", data1)
conn.Close()
tcpServer.Close()
require.Equal(t, "my.prefix.mymeasurement.myfield;host=192.168.0.1 3.14 1289430000", data1)
require.NoError(t, conn.Close())
require.NoError(t, tcpServer.Close())
}()
}

Expand All @@ -564,11 +562,11 @@ func TCPServer2WithTags(t *testing.T, wg *sync.WaitGroup) {
reader := bufio.NewReader(conn2)
tp := textproto.NewReader(reader)
data2, _ := tp.ReadLine()
assert.Equal(t, "my.prefix.mymeasurement;host=192.168.0.1 3.14 1289430000", data2)
require.Equal(t, "my.prefix.mymeasurement;host=192.168.0.1 3.14 1289430000", data2)
data3, _ := tp.ReadLine()
assert.Equal(t, "my.prefix.my_measurement;host=192.168.0.1 3.14 1289430000", data3)
conn2.Close()
tcpServer.Close()
require.Equal(t, "my.prefix.my_measurement;host=192.168.0.1 3.14 1289430000", data3)
require.NoError(t, conn2.Close())
require.NoError(t, tcpServer.Close())
}()
}

Expand All @@ -580,9 +578,9 @@ func TCPServer1WithTagsSeparatorUnderscore(t *testing.T, wg *sync.WaitGroup) {
reader := bufio.NewReader(conn)
tp := textproto.NewReader(reader)
data1, _ := tp.ReadLine()
assert.Equal(t, "my_prefix_mymeasurement_myfield;host=192.168.0.1 3.14 1289430000", data1)
conn.Close()
tcpServer.Close()
require.Equal(t, "my_prefix_mymeasurement_myfield;host=192.168.0.1 3.14 1289430000", data1)
require.NoError(t, conn.Close())
require.NoError(t, tcpServer.Close())
}()
}

Expand All @@ -594,10 +592,10 @@ func TCPServer2WithTagsSeparatorUnderscore(t *testing.T, wg *sync.WaitGroup) {
reader := bufio.NewReader(conn2)
tp := textproto.NewReader(reader)
data2, _ := tp.ReadLine()
assert.Equal(t, "my_prefix_mymeasurement;host=192.168.0.1 3.14 1289430000", data2)
require.Equal(t, "my_prefix_mymeasurement;host=192.168.0.1 3.14 1289430000", data2)
data3, _ := tp.ReadLine()
assert.Equal(t, "my_prefix_my_measurement;host=192.168.0.1 3.14 1289430000", data3)
conn2.Close()
tcpServer.Close()
require.Equal(t, "my_prefix_my_measurement;host=192.168.0.1 3.14 1289430000", data3)
require.NoError(t, conn2.Close())
require.NoError(t, tcpServer.Close())
}()
}
74 changes: 56 additions & 18 deletions plugins/outputs/graylog/graylog.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ func newGelfWriter(cfg gelfConfig, dialer *net.Dialer, tlsConfig *tls.Config) ge
}

func (g *gelfUDP) Write(message []byte) (n int, err error) {
compressed := g.compress(message)
compressed, err := g.compress(message)
if err != nil {
return 0, err
}

chunksize := g.gelfConfig.MaxChunkSizeWan
length := compressed.Len()
Expand All @@ -106,10 +109,17 @@ func (g *gelfUDP) Write(message []byte) (n int, err error) {
chunkCountInt := int(math.Ceil(float64(length) / float64(chunksize)))

id := make([]byte, 8)
rand.Read(id)
_, err = rand.Read(id)
if err != nil {
return 0, err
}

for i, index := 0, 0; i < length; i, index = i+chunksize, index+1 {
packet := g.createChunkedMessage(index, chunkCountInt, id, &compressed)
packet, err := g.createChunkedMessage(index, chunkCountInt, id, &compressed)
if err != nil {
return 0, err
}

err = g.send(packet.Bytes())
if err != nil {
return 0, err
Expand All @@ -136,21 +146,40 @@ func (g *gelfUDP) Close() (err error) {
return err
}

func (g *gelfUDP) createChunkedMessage(index int, chunkCountInt int, id []byte, compressed *bytes.Buffer) bytes.Buffer {
func (g *gelfUDP) createChunkedMessage(index int, chunkCountInt int, id []byte, compressed *bytes.Buffer) (bytes.Buffer, error) {
var packet bytes.Buffer

chunksize := g.getChunksize()

packet.Write(g.intToBytes(30))
packet.Write(g.intToBytes(15))
packet.Write(id)
b, err := g.intToBytes(30)
if err != nil {
return packet, err
}
packet.Write(b) //nolint:revive // from buffer.go: "err is always nil"

packet.Write(g.intToBytes(index))
packet.Write(g.intToBytes(chunkCountInt))
b, err = g.intToBytes(15)
if err != nil {
return packet, err
}
packet.Write(b) //nolint:revive // from buffer.go: "err is always nil"

packet.Write(compressed.Next(chunksize))
packet.Write(id) //nolint:revive // from buffer.go: "err is always nil"

return packet
b, err = g.intToBytes(index)
if err != nil {
return packet, err
}
packet.Write(b) //nolint:revive // from buffer.go: "err is always nil"

b, err = g.intToBytes(chunkCountInt)
if err != nil {
return packet, err
}
packet.Write(b) //nolint:revive // from buffer.go: "err is always nil"

packet.Write(compressed.Next(chunksize)) //nolint:revive // from buffer.go: "err is always nil"

return packet, nil
}

func (g *gelfUDP) getChunksize() int {
Expand All @@ -165,21 +194,30 @@ func (g *gelfUDP) getChunksize() int {
return g.gelfConfig.MaxChunkSizeWan
}

func (g *gelfUDP) intToBytes(i int) []byte {
func (g *gelfUDP) intToBytes(i int) ([]byte, error) {
buf := new(bytes.Buffer)

binary.Write(buf, binary.LittleEndian, int8(i))
return buf.Bytes()
err := binary.Write(buf, binary.LittleEndian, int8(i))
if err != nil {
return nil, err
}

return buf.Bytes(), err
}

func (g *gelfUDP) compress(b []byte) bytes.Buffer {
func (g *gelfUDP) compress(b []byte) (bytes.Buffer, error) {
var buf bytes.Buffer
comp := zlib.NewWriter(&buf)

comp.Write(b)
comp.Close()
if _, err := comp.Write(b); err != nil {
return bytes.Buffer{}, err
}

if err := comp.Close(); err != nil {
return bytes.Buffer{}, err
}

return buf
return buf, nil
}

func (g *gelfUDP) Connect() error {
Expand Down
24 changes: 12 additions & 12 deletions plugins/outputs/graylog/graylog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
"testing"
"time"

tlsint "github.com/influxdata/telegraf/plugins/common/tls"
"github.com/influxdata/telegraf/testutil"
reuse "github.com/libp2p/go-reuseport"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

tlsint "github.com/influxdata/telegraf/plugins/common/tls"
"github.com/influxdata/telegraf/testutil"
)

func TestWriteUDP(t *testing.T) {
Expand Down Expand Up @@ -183,14 +183,14 @@ func UDPServer(t *testing.T, wg *sync.WaitGroup, wg2 *sync.WaitGroup, config *Gr
var obj GelfObject
_ = json.Unmarshal(bufW.Bytes(), &obj)
require.NoError(t, err)
assert.Equal(t, obj["short_message"], "telegraf")
require.Equal(t, obj["short_message"], "telegraf")
if config.NameFieldNoPrefix {
assert.Equal(t, obj["name"], "test1")
require.Equal(t, obj["name"], "test1")
} else {
assert.Equal(t, obj["_name"], "test1")
require.Equal(t, obj["_name"], "test1")
}
assert.Equal(t, obj["_tag1"], "value1")
assert.Equal(t, obj["_value"], float64(1))
require.Equal(t, obj["_tag1"], "value1")
require.Equal(t, obj["_value"], float64(1))
}

// in UDP scenario all 4 messages are received
Expand Down Expand Up @@ -238,10 +238,10 @@ func TCPServer(t *testing.T, wg *sync.WaitGroup, wg2 *sync.WaitGroup, wg3 *sync.
var obj GelfObject
err = json.Unmarshal(bufW.Bytes(), &obj)
require.NoError(t, err)
assert.Equal(t, obj["short_message"], "telegraf")
assert.Equal(t, obj["_name"], "test1")
assert.Equal(t, obj["_tag1"], "value1")
assert.Equal(t, obj["_value"], float64(1))
require.Equal(t, obj["short_message"], "telegraf")
require.Equal(t, obj["_name"], "test1")
require.Equal(t, obj["_tag1"], "value1")
require.Equal(t, obj["_value"], float64(1))
}

conn := accept()
Expand Down
4 changes: 2 additions & 2 deletions plugins/outputs/health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,9 @@ func (h *Health) Close() error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

h.server.Shutdown(ctx)
err := h.server.Shutdown(ctx)
h.wg.Wait()
return nil
return err
}

// Origin returns the URL of the HTTP server.
Expand Down
Loading

0 comments on commit 020b77b

Please sign in to comment.