From fc2f2c8566322fc00e21a1cfb7962d99366491ea Mon Sep 17 00:00:00 2001 From: Paulo Janotti Date: Thu, 4 Jan 2024 12:27:29 -0800 Subject: [PATCH 1/7] Add GOOS=windows to lint-matrix step --- .github/workflows/build-and-test.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 8644ecf4ade2..421a7c1adabd 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -104,6 +104,8 @@ jobs: key: go-lint-build-${{ matrix.group }}-${{ runner.os }}-${{ hashFiles('**/go.sum') }} - name: Lint run: make -j2 golint GROUP=${{ matrix.group }} + - name: Lint with GOOS=windows + run: GOOS=windows make -j2 golint GROUP=${{ matrix.group }} lint: if: ${{ github.actor != 'dependabot[bot]' && always() }} runs-on: ubuntu-latest From f39ccc113a7505afea508d419f3c2931ea898784 Mon Sep 17 00:00:00 2001 From: Paulo Janotti Date: Thu, 4 Jan 2024 16:21:06 -0800 Subject: [PATCH 2/7] [chore][pkg/stanza] Fix lint on Windows --- pkg/stanza/fileconsumer/file_windows.go | 5 ++-- .../input/journald/journald_nonlinux.go | 2 +- .../input/namedpipe/namedpipe_nonlinux.go | 5 ++-- pkg/stanza/operator/input/windows/api.go | 23 ++++++++++--------- pkg/stanza/operator/input/windows/bookmark.go | 5 ++-- pkg/stanza/operator/input/windows/event.go | 10 ++++---- pkg/stanza/operator/input/windows/operator.go | 5 ++-- .../input/windows/publishercache_test.go | 12 +++++++--- .../operator/input/windows/subscription.go | 6 +++-- 9 files changed, 42 insertions(+), 31 deletions(-) diff --git a/pkg/stanza/fileconsumer/file_windows.go b/pkg/stanza/fileconsumer/file_windows.go index e67c61ab2e2a..926d6a5697f3 100644 --- a/pkg/stanza/fileconsumer/file_windows.go +++ b/pkg/stanza/fileconsumer/file_windows.go @@ -12,11 +12,10 @@ import ( "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/reader" ) -func (m *Manager) preConsume(ctx context.Context, newReaders []*reader.Reader) { - return +func (m *Manager) preConsume(_ context.Context, _ []*reader.Reader) { } -// On windows, we close files immediately after reading becauase they cannot be moved while open. +// On windows, we close files immediately after reading because they cannot be moved while open. func (m *Manager) postConsume(readers []*reader.Reader) { m.previousPollFiles = readers m.closePreviousFiles() diff --git a/pkg/stanza/operator/input/journald/journald_nonlinux.go b/pkg/stanza/operator/input/journald/journald_nonlinux.go index 072e5aba69f4..feff3959bea9 100644 --- a/pkg/stanza/operator/input/journald/journald_nonlinux.go +++ b/pkg/stanza/operator/input/journald/journald_nonlinux.go @@ -14,6 +14,6 @@ import ( "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator" ) -func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) { +func (c Config) Build(_ *zap.SugaredLogger) (operator.Operator, error) { return nil, errors.New("journald input operator is only supported on linux") } diff --git a/pkg/stanza/operator/input/namedpipe/namedpipe_nonlinux.go b/pkg/stanza/operator/input/namedpipe/namedpipe_nonlinux.go index a99bb1eaec41..a5b8879d9f4c 100644 --- a/pkg/stanza/operator/input/namedpipe/namedpipe_nonlinux.go +++ b/pkg/stanza/operator/input/namedpipe/namedpipe_nonlinux.go @@ -9,10 +9,11 @@ package namedpipe // import "github.com/open-telemetry/opentelemetry-collector-c import ( "errors" - "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator" "go.uber.org/zap" + + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator" ) -func (c *Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) { +func (c *Config) Build(_ *zap.SugaredLogger) (operator.Operator, error) { return nil, errors.New("namedpipe input operator is only supported on linux") } diff --git a/pkg/stanza/operator/input/windows/api.go b/pkg/stanza/operator/input/windows/api.go index c04e142d4e00..a7f249ea8c3c 100644 --- a/pkg/stanza/operator/input/windows/api.go +++ b/pkg/stanza/operator/input/windows/api.go @@ -7,6 +7,7 @@ package windows // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/windows" import ( + "errors" "syscall" "unsafe" @@ -68,7 +69,7 @@ const ( // evtSubscribe is the direct syscall implementation of EvtSubscribe (https://docs.microsoft.com/en-us/windows/win32/api/winevt/nf-winevt-evtsubscribe) func evtSubscribe(session uintptr, signalEvent windows.Handle, channelPath *uint16, query *uint16, bookmark uintptr, context uintptr, callback uintptr, flags uint32) (uintptr, error) { handle, _, err := subscribeProc.Call(session, uintptr(signalEvent), uintptr(unsafe.Pointer(channelPath)), uintptr(unsafe.Pointer(query)), bookmark, context, callback, uintptr(flags)) - if err != ErrorSuccess { + if !errors.Is(err, ErrorSuccess) { return 0, err } @@ -78,7 +79,7 @@ func evtSubscribe(session uintptr, signalEvent windows.Handle, channelPath *uint // evtNext is the direct syscall implementation of EvtNext (https://docs.microsoft.com/en-us/windows/win32/api/winevt/nf-winevt-evtnext) func evtNext(resultSet uintptr, eventsSize uint32, events *uintptr, timeout uint32, flags uint32, returned *uint32) error { _, _, err := nextProc.Call(resultSet, uintptr(eventsSize), uintptr(unsafe.Pointer(events)), uintptr(timeout), uintptr(flags), uintptr(unsafe.Pointer(returned))) - if err != ErrorSuccess { + if !errors.Is(err, ErrorSuccess) { return err } @@ -86,21 +87,21 @@ func evtNext(resultSet uintptr, eventsSize uint32, events *uintptr, timeout uint } // evtRender is the direct syscall implementation of EvtRender (https://docs.microsoft.com/en-us/windows/win32/api/winevt/nf-winevt-evtrender) -func evtRender(context uintptr, fragment uintptr, flags uint32, bufferSize uint32, buffer *byte) (*uint32, *uint32, error) { +func evtRender(context uintptr, fragment uintptr, flags uint32, bufferSize uint32, buffer *byte) (*uint32, error) { bufferUsed := new(uint32) propertyCount := new(uint32) _, _, err := renderProc.Call(context, fragment, uintptr(flags), uintptr(bufferSize), uintptr(unsafe.Pointer(buffer)), uintptr(unsafe.Pointer(bufferUsed)), uintptr(unsafe.Pointer(propertyCount))) - if err != ErrorSuccess { - return bufferUsed, propertyCount, err + if !errors.Is(err, ErrorSuccess) { + return bufferUsed, err } - return bufferUsed, propertyCount, nil + return bufferUsed, nil } // evtClose is the direct syscall implementation of EvtClose (https://docs.microsoft.com/en-us/windows/win32/api/winevt/nf-winevt-evtclose) func evtClose(handle uintptr) error { _, _, err := closeProc.Call(handle) - if err != ErrorSuccess { + if !errors.Is(err, ErrorSuccess) { return err } @@ -110,7 +111,7 @@ func evtClose(handle uintptr) error { // evtCreateBookmark is the direct syscall implementation of EvtCreateBookmark (https://docs.microsoft.com/en-us/windows/win32/api/winevt/nf-winevt-evtcreatebookmark) func evtCreateBookmark(bookmarkXML *uint16) (uintptr, error) { handle, _, err := createBookmarkProc.Call(uintptr(unsafe.Pointer(bookmarkXML))) - if err != ErrorSuccess { + if !errors.Is(err, ErrorSuccess) { return 0, err } @@ -120,7 +121,7 @@ func evtCreateBookmark(bookmarkXML *uint16) (uintptr, error) { // evtUpdateBookmark is the direct syscall implementation of EvtUpdateBookmark (https://docs.microsoft.com/en-us/windows/win32/api/winevt/nf-winevt-evtcreatebookmark) func evtUpdateBookmark(bookmark uintptr, event uintptr) error { _, _, err := updateBookmarkProc.Call(bookmark, event) - if err != ErrorSuccess { + if !errors.Is(err, ErrorSuccess) { return err } @@ -130,7 +131,7 @@ func evtUpdateBookmark(bookmark uintptr, event uintptr) error { // evtOpenPublisherMetadata is the direct syscall implementation of EvtOpenPublisherMetadata (https://docs.microsoft.com/en-us/windows/win32/api/winevt/nf-winevt-evtopenpublishermetadata) func evtOpenPublisherMetadata(session uintptr, publisherIdentity *uint16, logFilePath *uint16, locale uint32, flags uint32) (uintptr, error) { handle, _, err := openPublisherMetadataProc.Call(session, uintptr(unsafe.Pointer(publisherIdentity)), uintptr(unsafe.Pointer(logFilePath)), uintptr(locale), uintptr(flags)) - if err != ErrorSuccess { + if !errors.Is(err, ErrorSuccess) { return 0, err } @@ -141,7 +142,7 @@ func evtOpenPublisherMetadata(session uintptr, publisherIdentity *uint16, logFil func evtFormatMessage(publisherMetadata uintptr, event uintptr, messageID uint32, valueCount uint32, values uintptr, flags uint32, bufferSize uint32, buffer *byte) (*uint32, error) { bufferUsed := new(uint32) _, _, err := formatMessageProc.Call(publisherMetadata, event, uintptr(messageID), uintptr(valueCount), values, uintptr(flags), uintptr(bufferSize), uintptr(unsafe.Pointer(buffer)), uintptr(unsafe.Pointer(bufferUsed))) - if err != ErrorSuccess { + if !errors.Is(err, ErrorSuccess) { return bufferUsed, err } diff --git a/pkg/stanza/operator/input/windows/bookmark.go b/pkg/stanza/operator/input/windows/bookmark.go index 1dc90b3d0e7d..5efedff0c3b1 100644 --- a/pkg/stanza/operator/input/windows/bookmark.go +++ b/pkg/stanza/operator/input/windows/bookmark.go @@ -7,6 +7,7 @@ package windows // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/windows" import ( + "errors" "fmt" "syscall" ) @@ -59,8 +60,8 @@ func (b *Bookmark) Render(buffer Buffer) (string, error) { return "", fmt.Errorf("bookmark handle is not open") } - bufferUsed, _, err := evtRender(0, b.handle, EvtRenderBookmark, buffer.SizeBytes(), buffer.FirstByte()) - if err == ErrorInsufficientBuffer { + bufferUsed, err := evtRender(0, b.handle, EvtRenderBookmark, buffer.SizeBytes(), buffer.FirstByte()) + if errors.Is(err, ErrorInsufficientBuffer) { buffer.UpdateSizeBytes(*bufferUsed) return b.Render(buffer) } diff --git a/pkg/stanza/operator/input/windows/event.go b/pkg/stanza/operator/input/windows/event.go index d549e16de540..aa085be4c649 100644 --- a/pkg/stanza/operator/input/windows/event.go +++ b/pkg/stanza/operator/input/windows/event.go @@ -25,8 +25,8 @@ func (e *Event) RenderSimple(buffer Buffer) (EventXML, error) { return EventXML{}, fmt.Errorf("event handle does not exist") } - bufferUsed, _, err := evtRender(0, e.handle, EvtRenderEventXML, buffer.SizeBytes(), buffer.FirstByte()) - if err == ErrorInsufficientBuffer { + bufferUsed, err := evtRender(0, e.handle, EvtRenderEventXML, buffer.SizeBytes(), buffer.FirstByte()) + if errors.Is(err, ErrorInsufficientBuffer) { buffer.UpdateSizeBytes(*bufferUsed) return e.RenderSimple(buffer) } @@ -50,7 +50,7 @@ func (e *Event) RenderFormatted(buffer Buffer, publisher Publisher) (EventXML, e } bufferUsed, err := evtFormatMessage(publisher.handle, e.handle, 0, 0, 0, EvtFormatMessageXML, buffer.SizeWide(), buffer.FirstByte()) - if err == ErrorInsufficientBuffer { + if errors.Is(err, ErrorInsufficientBuffer) { // If the bufferUsed is 0 return an error as we don't want to make a recursive call with no buffer if *bufferUsed == 0 { return EventXML{}, errUnknownNextFrame @@ -91,8 +91,8 @@ func (e *Event) RenderRaw(buffer Buffer) (EventRaw, error) { return EventRaw{}, fmt.Errorf("event handle does not exist") } - bufferUsed, _, err := evtRender(0, e.handle, EvtRenderEventXML, buffer.SizeBytes(), buffer.FirstByte()) - if err == ErrorInsufficientBuffer { + bufferUsed, err := evtRender(0, e.handle, EvtRenderEventXML, buffer.SizeBytes(), buffer.FirstByte()) + if errors.Is(err, ErrorInsufficientBuffer) { // If the bufferUsed is 0 return an error as we don't want to make a recursive call with no buffer if *bufferUsed == 0 { return EventRaw{}, errUnknownNextFrame diff --git a/pkg/stanza/operator/input/windows/operator.go b/pkg/stanza/operator/input/windows/operator.go index 8900862eef50..512383d65eff 100644 --- a/pkg/stanza/operator/input/windows/operator.go +++ b/pkg/stanza/operator/input/windows/operator.go @@ -12,9 +12,10 @@ import ( "sync" "time" + "go.uber.org/zap" + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper" - "go.uber.org/zap" ) func init() { @@ -81,7 +82,7 @@ func (e *Input) Start(persister operator.Persister) error { offsetXML, err := e.getBookmarkOffset(ctx) if err != nil { e.Errorf("Failed to open bookmark, continuing without previous bookmark: %s", err) - e.persister.Delete(ctx, e.channel) + _ = e.persister.Delete(ctx, e.channel) } if offsetXML != "" { diff --git a/pkg/stanza/operator/input/windows/publishercache_test.go b/pkg/stanza/operator/input/windows/publishercache_test.go index 63f29cb68865..bf0ad5f02863 100644 --- a/pkg/stanza/operator/input/windows/publishercache_test.go +++ b/pkg/stanza/operator/input/windows/publishercache_test.go @@ -14,7 +14,9 @@ import ( func TestGetValidPublisher(t *testing.T) { publisherCache := newPublisherCache() - defer publisherCache.evictAll() + defer func() { + require.NoError(t, publisherCache.evictAll()) + }() // Provider "Application" exists in all Windows versions. publisher, openPublisherErr := publisherCache.get("Application") @@ -29,7 +31,9 @@ func TestGetValidPublisher(t *testing.T) { func TestGetInvalidPublisher(t *testing.T) { publisherCache := newPublisherCache() - defer publisherCache.evictAll() + defer func() { + require.NoError(t, publisherCache.evictAll()) + }() // Provider "InvalidProvider" does not exist in any Windows version. publisher, openPublisherErr := publisherCache.get("InvalidProvider") @@ -44,7 +48,9 @@ func TestGetInvalidPublisher(t *testing.T) { func TestValidAndInvalidPublishers(t *testing.T) { publisherCache := newPublisherCache() - defer publisherCache.evictAll() + defer func() { + require.NoError(t, publisherCache.evictAll()) + }() // Provider "Application" exists in all Windows versions. publisher, openPublisherErr := publisherCache.get("Application") diff --git a/pkg/stanza/operator/input/windows/subscription.go b/pkg/stanza/operator/input/windows/subscription.go index a08aa1d51ed4..9ade42c52fc2 100644 --- a/pkg/stanza/operator/input/windows/subscription.go +++ b/pkg/stanza/operator/input/windows/subscription.go @@ -29,7 +29,9 @@ func (s *Subscription) Open(channel string, startAt string, bookmark Bookmark) e if err != nil { return fmt.Errorf("failed to create signal handle: %w", err) } - defer windows.CloseHandle(signalEvent) + defer func() { + _ = windows.CloseHandle(signalEvent) + }() channelPtr, err := syscall.UTF16PtrFromString(channel) if err != nil { @@ -74,7 +76,7 @@ func (s *Subscription) Read(maxReads int) ([]Event, error) { var eventsRead uint32 err := evtNext(s.handle, uint32(maxReads), &eventHandles[0], 0, 0, &eventsRead) - if err == ErrorInvalidOperation && eventsRead == 0 { + if errors.Is(err, ErrorInvalidOperation) && eventsRead == 0 { return nil, nil } From 92f78f311d1d39676b765d1502179dc9ad6c6edb Mon Sep 17 00:00:00 2001 From: pjanotti Date: Fri, 5 Jan 2024 06:37:15 -0800 Subject: [PATCH 3/7] [chore][pkg/winperfcounters] Fix lint on Windows --- pkg/winperfcounters/watcher.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/winperfcounters/watcher.go b/pkg/winperfcounters/watcher.go index 727d35ccd30c..6a1e11d04ba7 100644 --- a/pkg/winperfcounters/watcher.go +++ b/pkg/winperfcounters/watcher.go @@ -7,6 +7,7 @@ package winperfcounters // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/winperfcounters" import ( + "errors" "fmt" "time" @@ -104,8 +105,8 @@ func (pc *perfCounter) Path() string { func (pc *perfCounter) ScrapeData() ([]CounterValue, error) { if err := pc.query.CollectData(); err != nil { - pdhErr, ok := err.(*win_perf_counters.PdhError) - if !ok || pdhErr.ErrorCode != win_perf_counters.PDH_CALC_NEGATIVE_DENOMINATOR { + var pdhErr *win_perf_counters.PdhError + if !errors.As(err, &pdhErr) || pdhErr.ErrorCode != win_perf_counters.PDH_CALC_NEGATIVE_DENOMINATOR { return nil, fmt.Errorf("failed to collect data for performance counter '%s': %w", pc.path, err) } From 05871f59c7eb1dfffd1c1a52d049af2b3205e411 Mon Sep 17 00:00:00 2001 From: pjanotti Date: Fri, 5 Jan 2024 08:57:11 -0800 Subject: [PATCH 4/7] [chore][receiver/podman] Fix lint on Windows --- receiver/podmanreceiver/factory.go | 7 +------ receiver/podmanreceiver/receiver_windows.go | 8 ++++---- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/receiver/podmanreceiver/factory.go b/receiver/podmanreceiver/factory.go index 7d27c6d05bca..4c9ebaff350e 100644 --- a/receiver/podmanreceiver/factory.go +++ b/receiver/podmanreceiver/factory.go @@ -49,10 +49,5 @@ func createMetricsReceiver( consumer consumer.Metrics, ) (receiver.Metrics, error) { podmanConfig := config.(*Config) - dsr, err := newMetricsReceiver(ctx, params, podmanConfig, consumer, nil) - if err != nil { - return nil, err - } - - return dsr, nil + return newMetricsReceiver(ctx, params, podmanConfig, consumer, nil) } diff --git a/receiver/podmanreceiver/receiver_windows.go b/receiver/podmanreceiver/receiver_windows.go index 15a741ed82e3..a2811ef4b345 100644 --- a/receiver/podmanreceiver/receiver_windows.go +++ b/receiver/podmanreceiver/receiver_windows.go @@ -13,10 +13,10 @@ import ( func newMetricsReceiver( _ context.Context, - settings receiver.CreateSettings, - config *Config, - nextConsumer consumer.Metrics, - clientFactory any, + _ receiver.CreateSettings, + _ *Config, + _ consumer.Metrics, + _ any, ) (receiver.Metrics, error) { return nil, fmt.Errorf("podman receiver is not supported on windows") } From ae2b4883649ca893a7d087ef77e7aac6722a0089 Mon Sep 17 00:00:00 2001 From: pjanotti Date: Fri, 5 Jan 2024 09:18:17 -0800 Subject: [PATCH 5/7] [chore][receiver/sqlserver] Fix lint on Windows --- receiver/sqlserverreceiver/factory_windows.go | 6 ++--- .../sqlserverreceiver/factory_windows_test.go | 2 +- receiver/sqlserverreceiver/scraper.go | 10 +++---- receiver/sqlserverreceiver/scraper_test.go | 26 ++++++++----------- 4 files changed, 20 insertions(+), 24 deletions(-) diff --git a/receiver/sqlserverreceiver/factory_windows.go b/receiver/sqlserverreceiver/factory_windows.go index bb59a1a3b0f9..f11c466a2f24 100644 --- a/receiver/sqlserverreceiver/factory_windows.go +++ b/receiver/sqlserverreceiver/factory_windows.go @@ -18,7 +18,7 @@ import ( "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlserverreceiver/internal/metadata" ) -var errConfigNotSqlServer = errors.New("config was not a sqlserver receiver config") +var errConfigNotSQLServer = errors.New("config was not a sqlserver receiver config") // createMetricsReceiver creates a metrics receiver based on provided config. func createMetricsReceiver( @@ -29,9 +29,9 @@ func createMetricsReceiver( ) (receiver.Metrics, error) { cfg, ok := receiverCfg.(*Config) if !ok { - return nil, errConfigNotSqlServer + return nil, errConfigNotSQLServer } - sqlServerScraper := newSqlServerScraper(params, cfg) + sqlServerScraper := newSQLServerScraper(params, cfg) scraper, err := scraperhelper.NewScraper(metadata.Type, sqlServerScraper.scrape, scraperhelper.WithStart(sqlServerScraper.start), diff --git a/receiver/sqlserverreceiver/factory_windows_test.go b/receiver/sqlserverreceiver/factory_windows_test.go index b26c1bcf53f1..0f2eef62c7b7 100644 --- a/receiver/sqlserverreceiver/factory_windows_test.go +++ b/receiver/sqlserverreceiver/factory_windows_test.go @@ -47,7 +47,7 @@ func TestCreateMetricsReceiver(t *testing.T) { nil, consumertest.NewNop(), ) - require.ErrorIs(t, err, errConfigNotSqlServer) + require.ErrorIs(t, err, errConfigNotSQLServer) }, }, } diff --git a/receiver/sqlserverreceiver/scraper.go b/receiver/sqlserverreceiver/scraper.go index f5dcf543a5e8..8e57f3e7044d 100644 --- a/receiver/sqlserverreceiver/scraper.go +++ b/receiver/sqlserverreceiver/scraper.go @@ -38,8 +38,8 @@ type watcherRecorder struct { // it needs metadata.MetricsBuilder and timestamp as arguments. type curriedRecorder func(*metadata.MetricsBuilder, pcommon.Timestamp) -// newSqlServerScraper returns a new sqlServerScraper. -func newSqlServerScraper(params receiver.CreateSettings, cfg *Config) *sqlServerScraper { +// newSQLServerScraper returns a new sqlServerScraper. +func newSQLServerScraper(params receiver.CreateSettings, cfg *Config) *sqlServerScraper { return &sqlServerScraper{ logger: params.Logger, config: cfg, @@ -48,7 +48,7 @@ func newSqlServerScraper(params receiver.CreateSettings, cfg *Config) *sqlServer } // start creates and sets the watchers for the scraper. -func (s *sqlServerScraper) start(ctx context.Context, host component.Host) error { +func (s *sqlServerScraper) start(_ context.Context, _ component.Host) error { s.watcherRecorders = []watcherRecorder{} for _, pcr := range perfCounterRecorders { @@ -72,7 +72,7 @@ func (s *sqlServerScraper) start(ctx context.Context, host component.Host) error } // scrape collects windows performance counter data from all watchers and then records/emits it using the metricBuilder -func (s *sqlServerScraper) scrape(ctx context.Context) (pmetric.Metrics, error) { +func (s *sqlServerScraper) scrape(_ context.Context) (pmetric.Metrics, error) { recordersByDatabase, errs := recordersPerDatabase(s.watcherRecorders) for dbName, recorders := range recordersByDatabase { @@ -133,7 +133,7 @@ func (s *sqlServerScraper) emitMetricGroup(recorders []curriedRecorder, database } // shutdown stops all of the watchers for the scraper. -func (s sqlServerScraper) shutdown(ctx context.Context) error { +func (s sqlServerScraper) shutdown(_ context.Context) error { var errs error for _, wr := range s.watcherRecorders { err := wr.watcher.Close() diff --git a/receiver/sqlserverreceiver/scraper_test.go b/receiver/sqlserverreceiver/scraper_test.go index 8050f5338072..b262564fb6ec 100644 --- a/receiver/sqlserverreceiver/scraper_test.go +++ b/receiver/sqlserverreceiver/scraper_test.go @@ -30,7 +30,7 @@ type mockPerfCounterWatcher struct { } // Close provides a mock function with given fields: -func (_m mockPerfCounterWatcher) Close() error { +func (_m *mockPerfCounterWatcher) Close() error { ret := _m.Called() var r0 error @@ -44,7 +44,7 @@ func (_m mockPerfCounterWatcher) Close() error { } // Path provides a mock function with given fields: -func (_m mockPerfCounterWatcher) Path() string { +func (_m *mockPerfCounterWatcher) Path() string { ret := _m.Called() var r0 string @@ -58,16 +58,14 @@ func (_m mockPerfCounterWatcher) Path() string { } // ScrapeData provides a mock function with given fields: -func (_m mockPerfCounterWatcher) ScrapeData() ([]winperfcounters.CounterValue, error) { +func (_m *mockPerfCounterWatcher) ScrapeData() ([]winperfcounters.CounterValue, error) { ret := _m.Called() var r0 []winperfcounters.CounterValue if rf, ok := ret.Get(0).(func() []winperfcounters.CounterValue); ok { r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]winperfcounters.CounterValue) - } + } else if ret.Get(0) != nil { + r0 = ret.Get(0).([]winperfcounters.CounterValue) } var r1 error @@ -86,9 +84,9 @@ func TestSqlServerScraper(t *testing.T) { logger, obsLogs := observer.New(zap.WarnLevel) settings := receivertest.NewNopCreateSettings() settings.Logger = zap.New(logger) - s := newSqlServerScraper(settings, cfg) + s := newSQLServerScraper(settings, cfg) - s.start(context.Background(), nil) + assert.NoError(t, s.start(context.Background(), nil)) assert.Equal(t, 0, len(s.watcherRecorders)) assert.Equal(t, 21, obsLogs.Len()) assert.Equal(t, 21, obsLogs.FilterMessageSnippet("failed to create perf counter with path \\SQLServer:").Len()) @@ -117,15 +115,14 @@ func TestScrape(t *testing.T) { factory := NewFactory() cfg := factory.CreateDefaultConfig().(*Config) settings := receivertest.NewNopCreateSettings() - scraper := newSqlServerScraper(settings, cfg) + scraper := newSQLServerScraper(settings, cfg) for i, rec := range perfCounterRecorders { perfCounterWatcher := &mockPerfCounterWatcher{} perfCounterWatcher.On("ScrapeData").Return([]winperfcounters.CounterValue{{InstanceName: dbInstance, Value: float64(i)}}, nil) - i++ for _, recorder := range rec.recorders { scraper.watcherRecorders = append(scraper.watcherRecorders, watcherRecorder{ - watcher: *perfCounterWatcher, + watcher: perfCounterWatcher, recorder: recorder, }) } @@ -162,15 +159,14 @@ func TestScrape(t *testing.T) { cfg.InstanceName = "CustomInstance" settings := receivertest.NewNopCreateSettings() - scraper := newSqlServerScraper(settings, cfg) + scraper := newSQLServerScraper(settings, cfg) for i, rec := range perfCounterRecorders { perfCounterWatcher := &mockPerfCounterWatcher{} perfCounterWatcher.On("ScrapeData").Return([]winperfcounters.CounterValue{{InstanceName: dbInstance, Value: float64(i)}}, nil) - i++ for _, recorder := range rec.recorders { scraper.watcherRecorders = append(scraper.watcherRecorders, watcherRecorder{ - watcher: *perfCounterWatcher, + watcher: perfCounterWatcher, recorder: recorder, }) } From afbe1ec135e97c56d431e83940d5f9e293fe8a66 Mon Sep 17 00:00:00 2001 From: pjanotti Date: Fri, 5 Jan 2024 09:41:52 -0800 Subject: [PATCH 6/7] [chore][receiver/windowseventlog] Fix lint on Windows --- .../receiver_windows_test.go | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/receiver/windowseventlogreceiver/receiver_windows_test.go b/receiver/windowseventlogreceiver/receiver_windows_test.go index 7b7c7e289a29..bb88c7b9aa6e 100644 --- a/receiver/windowseventlogreceiver/receiver_windows_test.go +++ b/receiver/windowseventlogreceiver/receiver_windows_test.go @@ -83,12 +83,16 @@ func TestReadWindowsEventLogger(t *testing.T) { err = receiver.Start(ctx, componenttest.NewNopHost()) require.NoError(t, err) - defer receiver.Shutdown(ctx) + defer func() { + require.NoError(t, receiver.Shutdown(ctx)) + }() src := "otel" err = eventlog.InstallAsEventCreate(src, eventlog.Info|eventlog.Warning|eventlog.Error) require.NoError(t, err) - defer eventlog.Remove(src) + defer func() { + require.NoError(t, eventlog.Remove(src)) + }() logger, err := eventlog.Open(src) require.NoError(t, err) @@ -144,11 +148,15 @@ func TestReadWindowsEventLoggerRaw(t *testing.T) { err = receiver.Start(ctx, componenttest.NewNopHost()) require.NoError(t, err) - defer receiver.Shutdown(ctx) + defer func() { + require.NoError(t, receiver.Shutdown(ctx)) + }() src := "otel" err = eventlog.InstallAsEventCreate(src, eventlog.Info|eventlog.Warning|eventlog.Error) - defer eventlog.Remove(src) + defer func() { + require.NoError(t, eventlog.Remove(src)) + }() require.NoError(t, err) logger, err := eventlog.Open(src) @@ -197,11 +205,15 @@ func TestReadWindowsEventLoggerWithExcludeProvider(t *testing.T) { err = receiver.Start(ctx, componenttest.NewNopHost()) require.NoError(t, err) - defer receiver.Shutdown(ctx) + defer func() { + require.NoError(t, receiver.Shutdown(ctx)) + }() err = eventlog.InstallAsEventCreate(src, eventlog.Info|eventlog.Warning|eventlog.Error) require.NoError(t, err) - defer eventlog.Remove(src) + defer func() { + require.NoError(t, eventlog.Remove(src)) + }() logger, err := eventlog.Open(src) require.NoError(t, err) @@ -237,10 +249,14 @@ func TestReadWindowsEventLoggerRawWithExcludeProvider(t *testing.T) { err = receiver.Start(ctx, componenttest.NewNopHost()) require.NoError(t, err) - defer receiver.Shutdown(ctx) + defer func() { + require.NoError(t, receiver.Shutdown(ctx)) + }() err = eventlog.InstallAsEventCreate(src, eventlog.Info|eventlog.Warning|eventlog.Error) - defer eventlog.Remove(src) + defer func() { + require.NoError(t, eventlog.Remove(src)) + }() require.NoError(t, err) logger, err := eventlog.Open(src) From eca967c53e209ea8f9e41260002b903705ef815c Mon Sep 17 00:00:00 2001 From: pjanotti Date: Fri, 5 Jan 2024 10:15:54 -0800 Subject: [PATCH 7/7] [chore][receiver/windowsperfcounters] Fix lint on Windows --- .../windowsperfcountersreceiver/factory_windows.go | 2 +- .../windowsperfcounters_scraper.go | 7 +++---- .../windowsperfcounters_scraper_test.go | 12 ++++++------ 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/receiver/windowsperfcountersreceiver/factory_windows.go b/receiver/windowsperfcountersreceiver/factory_windows.go index 973609b87cc7..3433d9ebe798 100644 --- a/receiver/windowsperfcountersreceiver/factory_windows.go +++ b/receiver/windowsperfcountersreceiver/factory_windows.go @@ -17,7 +17,7 @@ import ( // createMetricsReceiver creates a metrics receiver based on provided config. func createMetricsReceiver( - ctx context.Context, + _ context.Context, params receiver.CreateSettings, cfg component.Config, consumer consumer.Metrics, diff --git a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go index 962dc72f941b..40d7c5a511b3 100644 --- a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go +++ b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper.go @@ -163,10 +163,9 @@ func initializeMetricDps(metric pmetric.Metric, now pcommon.Timestamp, counterVa if counterValue.InstanceName != "" { dp.Attributes().PutStr(instanceLabelName, counterValue.InstanceName) } - if attributes != nil { - for attKey, attVal := range attributes { - dp.Attributes().PutStr(attKey, attVal) - } + + for attKey, attVal := range attributes { + dp.Attributes().PutStr(attKey, attVal) } dp.SetTimestamp(now) diff --git a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go index b054e96fae6e..0d19f1b42072 100644 --- a/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go +++ b/receiver/windowsperfcountersreceiver/windowsperfcounters_scraper_test.go @@ -29,7 +29,6 @@ import ( type mockPerfCounter struct { counterValues []winperfcounters.CounterValue - metricRep MetricRep path string scrapeErr error closeErr error @@ -58,9 +57,8 @@ func Test_WindowsPerfCounterScraper(t *testing.T) { name string cfg *Config - mockCounterPath string - startMessage string - startErr string + startMessage string + startErr string expectedMetricPath string } @@ -176,7 +174,8 @@ func Test_WindowsPerfCounterScraper(t *testing.T) { require.NoError(t, err) expectedMetrics, err := golden.ReadMetrics(test.expectedMetricPath) - pmetrictest.CompareMetrics(expectedMetrics, actualMetrics, pmetrictest.IgnoreMetricValues()) + require.NoError(t, err) + err = pmetrictest.CompareMetrics(expectedMetrics, actualMetrics, pmetrictest.IgnoreMetricValues()) require.NoError(t, err) }) } @@ -341,7 +340,8 @@ func TestScrape(t *testing.T) { for _, test := range testCases { t.Run(test.name, func(t *testing.T) { mpc := mockPerfCounter{counterValues: test.mockCounterValues} - s := &scraper{cfg: &test.cfg, newWatcher: mockPerfCounterFactory(mpc)} + testConfig := test.cfg + s := &scraper{cfg: &testConfig, newWatcher: mockPerfCounterFactory(mpc)} errs := s.start(context.Background(), componenttest.NewNopHost()) require.NoError(t, errs)