diff --git a/audit/options_test.go b/audit/options_test.go index cec71bc6e0..62cf817b1d 100644 --- a/audit/options_test.go +++ b/audit/options_test.go @@ -50,16 +50,16 @@ func TestOptions_WithFormat(t *testing.T) { tc := tc t.Run(name, func(t *testing.T) { t.Parallel() - options := &options{} + opts := &options{} applyOption := WithFormat(tc.Value) - err := applyOption(options) + err := applyOption(opts) switch { case tc.IsErrorExpected: require.Error(t, err) require.EqualError(t, err, tc.ExpectedErrorMessage) default: require.NoError(t, err) - require.Equal(t, tc.ExpectedValue, options.withFormat) + require.Equal(t, tc.ExpectedValue, opts.withFormat) } }) } @@ -95,16 +95,16 @@ func TestOptions_WithSubtype(t *testing.T) { tc := tc t.Run(name, func(t *testing.T) { t.Parallel() - options := &options{} + opts := &options{} applyOption := WithSubtype(tc.Value) - err := applyOption(options) + err := applyOption(opts) switch { case tc.IsErrorExpected: require.Error(t, err) require.EqualError(t, err, tc.ExpectedErrorMessage) default: require.NoError(t, err) - require.Equal(t, tc.ExpectedValue, options.withSubtype) + require.Equal(t, tc.ExpectedValue, opts.withSubtype) } }) } @@ -136,16 +136,16 @@ func TestOptions_WithNow(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - options := &options{} + opts := &options{} applyOption := WithNow(tc.Value) - err := applyOption(options) + err := applyOption(opts) switch { case tc.IsErrorExpected: require.Error(t, err) require.EqualError(t, err, tc.ExpectedErrorMessage) default: require.NoError(t, err) - require.Equal(t, tc.ExpectedValue, options.withNow) + require.Equal(t, tc.ExpectedValue, opts.withNow) } }) } @@ -181,16 +181,16 @@ func TestOptions_WithID(t *testing.T) { tc := tc t.Run(name, func(t *testing.T) { t.Parallel() - options := &options{} + opts := &options{} applyOption := WithID(tc.Value) - err := applyOption(options) + err := applyOption(opts) switch { case tc.IsErrorExpected: require.Error(t, err) require.EqualError(t, err, tc.ExpectedErrorMessage) default: require.NoError(t, err) - require.Equal(t, tc.ExpectedValue, options.withID) + require.Equal(t, tc.ExpectedValue, opts.withID) } }) } @@ -226,16 +226,16 @@ func TestOptions_WithPrefix(t *testing.T) { tc := tc t.Run(name, func(t *testing.T) { t.Parallel() - options := &options{} + opts := &options{} applyOption := WithPrefix(tc.Value) - err := applyOption(options) + err := applyOption(opts) switch { case tc.IsErrorExpected: require.Error(t, err) require.EqualError(t, err, tc.ExpectedErrorMessage) default: require.NoError(t, err) - require.Equal(t, tc.ExpectedValue, options.withPrefix) + require.Equal(t, tc.ExpectedValue, opts.withPrefix) } }) } @@ -262,11 +262,11 @@ func TestOptions_WithRaw(t *testing.T) { tc := tc t.Run(name, func(t *testing.T) { t.Parallel() - options := &options{} + opts := &options{} applyOption := WithRaw(tc.Value) - err := applyOption(options) + err := applyOption(opts) require.NoError(t, err) - require.Equal(t, tc.ExpectedValue, options.withRaw) + require.Equal(t, tc.ExpectedValue, opts.withRaw) }) } } @@ -292,11 +292,11 @@ func TestOptions_WithElision(t *testing.T) { tc := tc t.Run(name, func(t *testing.T) { t.Parallel() - options := &options{} + opts := &options{} applyOption := WithElision(tc.Value) - err := applyOption(options) + err := applyOption(opts) require.NoError(t, err) - require.Equal(t, tc.ExpectedValue, options.withElision) + require.Equal(t, tc.ExpectedValue, opts.withElision) }) } } @@ -322,11 +322,11 @@ func TestOptions_WithHMACAccessor(t *testing.T) { tc := tc t.Run(name, func(t *testing.T) { t.Parallel() - options := &options{} + opts := &options{} applyOption := WithHMACAccessor(tc.Value) - err := applyOption(options) + err := applyOption(opts) require.NoError(t, err) - require.Equal(t, tc.ExpectedValue, options.withHMACAccessor) + require.Equal(t, tc.ExpectedValue, opts.withHMACAccessor) }) } } @@ -352,11 +352,11 @@ func TestOptions_WithOmitTime(t *testing.T) { tc := tc t.Run(name, func(t *testing.T) { t.Parallel() - options := &options{} + opts := &options{} applyOption := WithOmitTime(tc.Value) - err := applyOption(options) + err := applyOption(opts) require.NoError(t, err) - require.Equal(t, tc.ExpectedValue, options.withOmitTime) + require.Equal(t, tc.ExpectedValue, opts.withOmitTime) }) } } diff --git a/internal/observability/event/event_type.go b/internal/observability/event/event_type.go index 63b18dc709..35dfb6411f 100644 --- a/internal/observability/event/event_type.go +++ b/internal/observability/event/event_type.go @@ -18,13 +18,13 @@ const ( ) // Validate ensures that EventType is one of the set of allowed event types. -func (et EventType) Validate() error { +func (t EventType) Validate() error { const op = "event.(EventType).Validate" - switch et { + switch t { case AuditType: return nil default: - return fmt.Errorf("%s: '%s' is not a valid event type: %w", op, et, ErrInvalidParameter) + return fmt.Errorf("%s: '%s' is not a valid event type: %w", op, t, ErrInvalidParameter) } } diff --git a/internal/observability/event/options_test.go b/internal/observability/event/options_test.go index a245846b54..f52e240599 100644 --- a/internal/observability/event/options_test.go +++ b/internal/observability/event/options_test.go @@ -82,16 +82,16 @@ func TestOptions_WithID(t *testing.T) { tc := tc t.Run(name, func(t *testing.T) { t.Parallel() - options := &options{} + opts := &options{} applyOption := WithID(tc.Value) - err := applyOption(options) + err := applyOption(opts) switch { case tc.IsErrorExpected: require.Error(t, err) require.EqualError(t, err, tc.ExpectedErrorMessage) default: require.NoError(t, err) - require.Equal(t, tc.ExpectedValue, options.withID) + require.Equal(t, tc.ExpectedValue, opts.withID) } }) } @@ -222,11 +222,11 @@ func TestOptions_WithFacility(t *testing.T) { tc := tc t.Run(name, func(t *testing.T) { t.Parallel() - options := &options{} + opts := &options{} applyOption := WithFacility(tc.Value) - err := applyOption(options) + err := applyOption(opts) require.NoError(t, err) - require.Equal(t, tc.ExpectedValue, options.withFacility) + require.Equal(t, tc.ExpectedValue, opts.withFacility) }) } } @@ -260,11 +260,11 @@ func TestOptions_WithTag(t *testing.T) { tc := tc t.Run(name, func(t *testing.T) { t.Parallel() - options := &options{} + opts := &options{} applyOption := WithTag(tc.Value) - err := applyOption(options) + err := applyOption(opts) require.NoError(t, err) - require.Equal(t, tc.ExpectedValue, options.withTag) + require.Equal(t, tc.ExpectedValue, opts.withTag) }) } } @@ -298,11 +298,11 @@ func TestOptions_WithSocketType(t *testing.T) { tc := tc t.Run(name, func(t *testing.T) { t.Parallel() - options := &options{} + opts := &options{} applyOption := WithSocketType(tc.Value) - err := applyOption(options) + err := applyOption(opts) require.NoError(t, err) - require.Equal(t, tc.ExpectedValue, options.withSocketType) + require.Equal(t, tc.ExpectedValue, opts.withSocketType) }) } } @@ -346,16 +346,16 @@ func TestOptions_WithMaxDuration(t *testing.T) { tc := tc t.Run(name, func(t *testing.T) { t.Parallel() - options := &options{} + opts := &options{} applyOption := WithMaxDuration(tc.Value) - err := applyOption(options) + err := applyOption(opts) switch { case tc.IsErrorExpected: require.Error(t, err) require.EqualError(t, err, tc.ExpectedErrorMessage) default: require.NoError(t, err) - require.Equal(t, tc.ExpectedValue, options.withMaxDuration) + require.Equal(t, tc.ExpectedValue, opts.withMaxDuration) } }) } @@ -402,9 +402,9 @@ func TestOptions_WithFileMode(t *testing.T) { tc := tc t.Run(name, func(t *testing.T) { t.Parallel() - options := &options{} + opts := &options{} applyOption := WithFileMode(tc.Value) - err := applyOption(options) + err := applyOption(opts) switch { case tc.IsErrorExpected: require.Error(t, err) @@ -414,10 +414,10 @@ func TestOptions_WithFileMode(t *testing.T) { switch { case tc.IsNilExpected: // Optional Option 'not supplied' (i.e. was whitespace/empty string) - require.Nil(t, options.withFileMode) + require.Nil(t, opts.withFileMode) default: // Dereference the pointer, so we can examine the file mode. - require.Equal(t, tc.ExpectedValue, *options.withFileMode) + require.Equal(t, tc.ExpectedValue, *opts.withFileMode) } } }) diff --git a/internal/observability/event/sink_file.go b/internal/observability/event/sink_file.go index d102a34d77..0a5e2b63eb 100644 --- a/internal/observability/event/sink_file.go +++ b/internal/observability/event/sink_file.go @@ -22,6 +22,8 @@ const ( devnull = "/dev/null" ) +var _ eventlogger.Node = (*FileSink)(nil) + // FileSink is a sink node which handles writing events to file. type FileSink struct { file *os.File @@ -82,7 +84,7 @@ func NewFileSink(path string, format string, opt ...Option) (*FileSink, error) { } // Process handles writing the event to the file sink. -func (f *FileSink) Process(ctx context.Context, e *eventlogger.Event) (*eventlogger.Event, error) { +func (s *FileSink) Process(ctx context.Context, e *eventlogger.Event) (*eventlogger.Event, error) { const op = "event.(FileSink).Process" select { @@ -96,16 +98,16 @@ func (f *FileSink) Process(ctx context.Context, e *eventlogger.Event) (*eventlog } // '/dev/null' path means we just do nothing and pretend we're done. - if f.path == devnull { + if s.path == devnull { return nil, nil } - formatted, found := e.Format(f.requiredFormat) + formatted, found := e.Format(s.requiredFormat) if !found { - return nil, fmt.Errorf("%s: unable to retrieve event formatted as %q", op, f.requiredFormat) + return nil, fmt.Errorf("%s: unable to retrieve event formatted as %q", op, s.requiredFormat) } - err := f.log(formatted) + err := s.log(formatted) if err != nil { return nil, fmt.Errorf("%s: error writing file for sink: %w", op, err) } @@ -115,33 +117,33 @@ func (f *FileSink) Process(ctx context.Context, e *eventlogger.Event) (*eventlog } // Reopen handles closing and reopening the file. -func (f *FileSink) Reopen() error { +func (s *FileSink) Reopen() error { const op = "event.(FileSink).Reopen" // '/dev/null' path means we just do nothing and pretend we're done. - if f.path == devnull { + if s.path == devnull { return nil } - f.fileLock.Lock() - defer f.fileLock.Unlock() + s.fileLock.Lock() + defer s.fileLock.Unlock() - if f.file == nil { - return f.open() + if s.file == nil { + return s.open() } - err := f.file.Close() + err := s.file.Close() // Set to nil here so that even if we error out, on the next access open() will be tried. - f.file = nil + s.file = nil if err != nil { return fmt.Errorf("%s: unable to close file for re-opening on sink: %w", op, err) } - return f.open() + return s.open() } // Type describes the type of this node (sink). -func (_ *FileSink) Type() eventlogger.NodeType { +func (s *FileSink) Type() eventlogger.NodeType { return eventlogger.NodeTypeSink } @@ -149,32 +151,32 @@ func (_ *FileSink) Type() eventlogger.NodeType { // if one is not already open. // It doesn't have any locking and relies on calling functions of FileSink to // handle this (e.g. log and Reopen methods). -func (f *FileSink) open() error { +func (s *FileSink) open() error { const op = "event.(FileSink).open" - if f.file != nil { + if s.file != nil { return nil } - if err := os.MkdirAll(filepath.Dir(f.path), f.fileMode); err != nil { - return fmt.Errorf("%s: unable to create file %q: %w", op, f.path, err) + if err := os.MkdirAll(filepath.Dir(s.path), s.fileMode); err != nil { + return fmt.Errorf("%s: unable to create file %q: %w", op, s.path, err) } var err error - f.file, err = os.OpenFile(f.path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, f.fileMode) + s.file, err = os.OpenFile(s.path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, s.fileMode) if err != nil { return fmt.Errorf("%s: unable to open file for sink: %w", op, err) } // Change the file mode in case the log file already existed. // We special case '/dev/null' since we can't chmod it, and bypass if the mode is zero. - switch f.path { + switch s.path { case devnull: default: - if f.fileMode != 0 { - err = os.Chmod(f.path, f.fileMode) + if s.fileMode != 0 { + err = os.Chmod(s.path, s.fileMode) if err != nil { - return fmt.Errorf("%s: unable to change file %q permissions '%v' for sink: %w", op, f.path, f.fileMode, err) + return fmt.Errorf("%s: unable to change file %q permissions '%v' for sink: %w", op, s.path, s.fileMode, err) } } } @@ -184,31 +186,31 @@ func (f *FileSink) open() error { // log writes the buffer to the file. // It acquires a lock on the file to do this. -func (f *FileSink) log(data []byte) error { +func (s *FileSink) log(data []byte) error { const op = "event.(FileSink).log" - f.fileLock.Lock() - defer f.fileLock.Unlock() + s.fileLock.Lock() + defer s.fileLock.Unlock() reader := bytes.NewReader(data) - if err := f.open(); err != nil { + if err := s.open(); err != nil { return fmt.Errorf("%s: unable to open file for sink: %w", op, err) } - if _, err := reader.WriteTo(f.file); err == nil { + if _, err := reader.WriteTo(s.file); err == nil { return nil } // Otherwise, opportunistically try to re-open the FD, once per call (1 retry attempt). - err := f.file.Close() + err := s.file.Close() if err != nil { return fmt.Errorf("%s: unable to close file for sink: %w", op, err) } - f.file = nil + s.file = nil - if err := f.open(); err != nil { + if err := s.open(); err != nil { return fmt.Errorf("%s: unable to re-open file for sink: %w", op, err) } @@ -217,7 +219,7 @@ func (f *FileSink) log(data []byte) error { return fmt.Errorf("%s: unable to seek to start of file for sink: %w", op, err) } - _, err = reader.WriteTo(f.file) + _, err = reader.WriteTo(s.file) if err != nil { return fmt.Errorf("%s: unable to re-write to file for sink: %w", op, err) } diff --git a/internal/observability/event/sink_noop.go b/internal/observability/event/sink_noop.go index be8a7d6594..e4899990d6 100644 --- a/internal/observability/event/sink_noop.go +++ b/internal/observability/event/sink_noop.go @@ -9,6 +9,8 @@ import ( "github.com/hashicorp/eventlogger" ) +var _ eventlogger.Node = (*NoopSink)(nil) + // NoopSink is a sink node which handles ignores everything. type NoopSink struct{} diff --git a/internal/observability/event/sink_socket.go b/internal/observability/event/sink_socket.go index 1a98b8258a..4a52db3438 100644 --- a/internal/observability/event/sink_socket.go +++ b/internal/observability/event/sink_socket.go @@ -15,6 +15,8 @@ import ( "github.com/hashicorp/eventlogger" ) +var _ eventlogger.Node = (*SocketSink)(nil) + // SocketSink is a sink node which handles writing events to socket. type SocketSink struct { requiredFormat string diff --git a/internal/observability/event/sink_stdout.go b/internal/observability/event/sink_stdout.go index 2c8a3323a7..695c4d9e7a 100644 --- a/internal/observability/event/sink_stdout.go +++ b/internal/observability/event/sink_stdout.go @@ -28,7 +28,7 @@ func NewStdoutSinkNode(format string) *StdoutSink { } // Process persists the provided eventlogger.Event to the standard output stream. -func (n *StdoutSink) Process(ctx context.Context, event *eventlogger.Event) (*eventlogger.Event, error) { +func (s *StdoutSink) Process(ctx context.Context, event *eventlogger.Event) (*eventlogger.Event, error) { const op = "event.(StdoutSink).Process" select { @@ -41,9 +41,9 @@ func (n *StdoutSink) Process(ctx context.Context, event *eventlogger.Event) (*ev return nil, fmt.Errorf("%s: event is nil: %w", op, ErrInvalidParameter) } - formattedBytes, found := event.Format(n.requiredFormat) + formattedBytes, found := event.Format(s.requiredFormat) if !found { - return nil, fmt.Errorf("%s: unable to retrieve event formatted as %q", op, n.requiredFormat) + return nil, fmt.Errorf("%s: unable to retrieve event formatted as %q", op, s.requiredFormat) } _, err := os.Stdout.Write(formattedBytes) @@ -56,11 +56,11 @@ func (n *StdoutSink) Process(ctx context.Context, event *eventlogger.Event) (*ev } // Reopen is a no-op for the StdoutSink type. -func (n *StdoutSink) Reopen() error { +func (s *StdoutSink) Reopen() error { return nil } // Type returns the eventlogger.NodeTypeSink constant. -func (n *StdoutSink) Type() eventlogger.NodeType { +func (s *StdoutSink) Type() eventlogger.NodeType { return eventlogger.NodeTypeSink } diff --git a/internal/observability/event/sink_syslog.go b/internal/observability/event/sink_syslog.go index ab04a808c8..586bedefab 100644 --- a/internal/observability/event/sink_syslog.go +++ b/internal/observability/event/sink_syslog.go @@ -12,6 +12,8 @@ import ( "github.com/hashicorp/eventlogger" ) +var _ eventlogger.Node = (*SyslogSink)(nil) + // SyslogSink is a sink node which handles writing events to syslog. type SyslogSink struct { requiredFormat string