diff --git a/plugins/outputs/socket_writer/socket_writer.go b/plugins/outputs/socket_writer/socket_writer.go index 32baa40c466ca..503130c624f45 100644 --- a/plugins/outputs/socket_writer/socket_writer.go +++ b/plugins/outputs/socket_writer/socket_writer.go @@ -124,6 +124,16 @@ func (sw *SocketWriter) Write(metrics []telegraf.Metric) error { return nil } +// Close closes the connection. Noop if already closed. +func (sw *SocketWriter) Close() error { + if sw.Conn == nil { + return nil + } + err := sw.Conn.Close() + sw.Conn = nil + return err +} + func newSocketWriter() *SocketWriter { s, _ := serializers.NewInfluxSerializer() return &SocketWriter{ diff --git a/plugins/outputs/socket_writer/socket_writer_test.go b/plugins/outputs/socket_writer/socket_writer_test.go index 6be2b0905f7a6..0c5747d31bbbd 100644 --- a/plugins/outputs/socket_writer/socket_writer_test.go +++ b/plugins/outputs/socket_writer/socket_writer_test.go @@ -143,7 +143,7 @@ func TestSocketWriter_Write_err(t *testing.T) { // close the socket to generate an error lconn.Close() - sw.Close() + sw.Conn.Close() err = sw.Write(metrics) require.Error(t, err) assert.Nil(t, sw.Conn)