Skip to content

Commit

Permalink
chore: Fix linter findings for revive:exported in plugins/inputs/s*
Browse files Browse the repository at this point in the history
  • Loading branch information
zak-pawel committed Jan 2, 2025
1 parent 0c7c424 commit b6e7abf
Show file tree
Hide file tree
Showing 54 changed files with 2,447 additions and 2,466 deletions.
66 changes: 29 additions & 37 deletions plugins/inputs/s7comm/s7comm.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import (
//go:embed sample.conf
var sampleConfig string

const addressRegexp = `^(?P<area>[A-Z]+)(?P<no>[0-9]+)\.(?P<type>[A-Z]+)(?P<start>[0-9]+)(?:\.(?P<extra>.*))?$`

var (
regexAddr = regexp.MustCompile(addressRegexp)
// Area mapping taken from https://github.com/robinson/gos7/blob/master/client.go
Expand Down Expand Up @@ -60,9 +58,22 @@ var (
}
)

type metricFieldDefinition struct {
Name string `toml:"name"`
Address string `toml:"address"`
const addressRegexp = `^(?P<area>[A-Z]+)(?P<no>[0-9]+)\.(?P<type>[A-Z]+)(?P<start>[0-9]+)(?:\.(?P<extra>.*))?$`

type S7comm struct {
Server string `toml:"server"`
Rack int `toml:"rack"`
Slot int `toml:"slot"`
ConnectionType string `toml:"connection_type"`
BatchMaxSize int `toml:"pdu_size"`
Timeout config.Duration `toml:"timeout"`
DebugConnection bool `toml:"debug_connection" deprecated:"1.35.0;use 'log_level' 'trace' instead"`
Configs []metricDefinition `toml:"metric"`
Log telegraf.Logger `toml:"-"`

handler *gos7.TCPClientHandler
client gos7.Client
batches []batch
}

type metricDefinition struct {
Expand All @@ -71,7 +82,10 @@ type metricDefinition struct {
Tags map[string]string `toml:"tags"`
}

type converterFunc func([]byte) interface{}
type metricFieldDefinition struct {
Name string `toml:"name"`
Address string `toml:"address"`
}

type batch struct {
items []gos7.S7DataItem
Expand All @@ -85,30 +99,12 @@ type fieldMapping struct {
convert converterFunc
}

// S7comm represents the plugin
type S7comm struct {
Server string `toml:"server"`
Rack int `toml:"rack"`
Slot int `toml:"slot"`
ConnectionType string `toml:"connection_type"`
BatchMaxSize int `toml:"pdu_size"`
Timeout config.Duration `toml:"timeout"`
DebugConnection bool `toml:"debug_connection" deprecated:"1.35.0;use 'log_level' 'trace' instead"`
Configs []metricDefinition `toml:"metric"`
Log telegraf.Logger `toml:"-"`

handler *gos7.TCPClientHandler
client gos7.Client
batches []batch
}
type converterFunc func([]byte) interface{}

// SampleConfig returns a basic configuration for the plugin
func (*S7comm) SampleConfig() string {
return sampleConfig
}

// Init checks the config settings and prepares the plugin. It's called
// once by the Telegraf agent after parsing the config settings.
func (s *S7comm) Init() error {
// Check settings
if s.Server == "" {
Expand Down Expand Up @@ -150,8 +146,7 @@ func (s *S7comm) Init() error {
return s.createRequests()
}

// Start initializes the connection to the remote endpoint
func (s *S7comm) Start(_ telegraf.Accumulator) error {
func (s *S7comm) Start(telegraf.Accumulator) error {
s.Log.Debugf("Connecting to %q...", s.Server)
if err := s.handler.Connect(); err != nil {
return &internal.StartupError{
Expand All @@ -164,15 +159,6 @@ func (s *S7comm) Start(_ telegraf.Accumulator) error {
return nil
}

// Stop disconnects from the remote endpoint and cleans up
func (s *S7comm) Stop() {
if s.handler != nil {
s.Log.Debugf("Disconnecting from %q...", s.handler.Address)
s.handler.Close()
}
}

// Gather collects the data from the device
func (s *S7comm) Gather(acc telegraf.Accumulator) error {
timestamp := time.Now()
grouper := metric.NewSeriesGrouper()
Expand Down Expand Up @@ -208,7 +194,13 @@ func (s *S7comm) Gather(acc telegraf.Accumulator) error {
return nil
}

// Internal functions
func (s *S7comm) Stop() {
if s.handler != nil {
s.Log.Debugf("Disconnecting from %q...", s.handler.Address)
s.handler.Close()
}
}

func (s *S7comm) createRequests() error {
seed := maphash.MakeSeed()
seenFields := make(map[uint64]bool)
Expand Down
59 changes: 28 additions & 31 deletions plugins/inputs/s7comm/s7comm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,14 +711,14 @@ func TestMetricCollisions(t *testing.T) {

func TestConnectionLoss(t *testing.T) {
// Create fake S7 comm server that can accept connects
server, err := NewMockServer("127.0.0.1:0")
server, err := newMockServer()
require.NoError(t, err)
defer server.Close()
require.NoError(t, server.Start())
defer server.close()
server.start()

// Create the plugin and attempt a connection
plugin := &S7comm{
Server: server.Addr(),
Server: server.addr(),
Rack: 0,
Slot: 2,
DebugConnection: true,
Expand All @@ -742,20 +742,20 @@ func TestConnectionLoss(t *testing.T) {
require.NoError(t, plugin.Gather(&acc))
require.NoError(t, plugin.Gather(&acc))
plugin.Stop()
server.Close()
server.close()

require.Equal(t, uint32(3), server.ConnectionAttempts.Load())
require.Equal(t, uint32(3), server.connectionAttempts.Load())
}

func TestStartupErrorBehaviorError(t *testing.T) {
// Create fake S7 comm server that can accept connects
server, err := NewMockServer("127.0.0.1:0")
server, err := newMockServer()
require.NoError(t, err)
defer server.Close()
defer server.close()

// Setup the plugin and the model to be able to use the startup retry strategy
plugin := &S7comm{
Server: server.Addr(),
Server: server.addr(),
Rack: 0,
Slot: 2,
DebugConnection: true,
Expand Down Expand Up @@ -784,18 +784,18 @@ func TestStartupErrorBehaviorError(t *testing.T) {

// Starting the plugin will fail with an error because the server does not listen
var acc testutil.Accumulator
require.ErrorContains(t, model.Start(&acc), "connecting to \""+server.Addr()+"\" failed")
require.ErrorContains(t, model.Start(&acc), "connecting to \""+server.addr()+"\" failed")
}

func TestStartupErrorBehaviorIgnore(t *testing.T) {
// Create fake S7 comm server that can accept connects
server, err := NewMockServer("127.0.0.1:0")
server, err := newMockServer()
require.NoError(t, err)
defer server.Close()
defer server.close()

// Setup the plugin and the model to be able to use the startup retry strategy
plugin := &S7comm{
Server: server.Addr(),
Server: server.addr(),
Rack: 0,
Slot: 2,
DebugConnection: true,
Expand Down Expand Up @@ -828,20 +828,20 @@ func TestStartupErrorBehaviorIgnore(t *testing.T) {
// the plugin.
var acc testutil.Accumulator
err = model.Start(&acc)
require.ErrorContains(t, err, "connecting to \""+server.Addr()+"\" failed")
require.ErrorContains(t, err, "connecting to \""+server.addr()+"\" failed")
var fatalErr *internal.FatalError
require.ErrorAs(t, err, &fatalErr)
}

func TestStartupErrorBehaviorRetry(t *testing.T) {
// Create fake S7 comm server that can accept connects
server, err := NewMockServer("127.0.0.1:0")
server, err := newMockServer()
require.NoError(t, err)
defer server.Close()
defer server.close()

// Setup the plugin and the model to be able to use the startup retry strategy
plugin := &S7comm{
Server: server.Addr(),
Server: server.addr(),
Rack: 0,
Slot: 2,
DebugConnection: true,
Expand Down Expand Up @@ -880,37 +880,36 @@ func TestStartupErrorBehaviorRetry(t *testing.T) {
require.Equal(t, int64(2), model.StartupErrors.Get())

// Allow connection in the server, now the connection should succeed
require.NoError(t, server.Start())
server.start()
defer model.Stop()
require.NoError(t, model.Gather(&acc))
}

type MockServer struct {
ConnectionAttempts atomic.Uint32

listener net.Listener
type mockServer struct {
connectionAttempts atomic.Uint32
listener net.Listener
}

func NewMockServer(addr string) (*MockServer, error) {
l, err := net.Listen("tcp", addr)
func newMockServer() (*mockServer, error) {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return nil, err
}
return &MockServer{listener: l}, nil
return &mockServer{listener: l}, nil
}

func (s *MockServer) Addr() string {
func (s *mockServer) addr() string {
return s.listener.Addr().String()
}

func (s *MockServer) Close() error {
func (s *mockServer) close() error {
if s.listener != nil {
return s.listener.Close()
}
return nil
}

func (s *MockServer) Start() error {
func (s *mockServer) start() {
go func() {
defer s.listener.Close()
for {
Expand All @@ -924,7 +923,7 @@ func (s *MockServer) Start() error {
}

// Count the number of connection attempts
s.ConnectionAttempts.Add(1)
s.connectionAttempts.Add(1)

buf := make([]byte, 4096)

Expand Down Expand Up @@ -961,6 +960,4 @@ func (s *MockServer) Start() error {
conn.Close()
}
}()

return nil
}
Loading

0 comments on commit b6e7abf

Please sign in to comment.