Skip to content

Commit

Permalink
[chore][pkg/stanza] Fix lint on Windows (#30339)
Browse files Browse the repository at this point in the history
**Description:**
Fixing lint issues when GOOS=windows. This is in preparation to
eventually include lint with GOOS=windows as part of CI.

**Link to tracking Issue:**
N/A

**Testing:**
`make` on component folder on a Windows box

**Documentation:**
N/A
  • Loading branch information
pjanotti authored Jan 9, 2024
1 parent 4226c58 commit 8249950
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 31 deletions.
5 changes: 2 additions & 3 deletions pkg/stanza/fileconsumer/file_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion pkg/stanza/operator/input/journald/journald_nonlinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
5 changes: 3 additions & 2 deletions pkg/stanza/operator/input/namedpipe/namedpipe_nonlinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
23 changes: 12 additions & 11 deletions pkg/stanza/operator/input/windows/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package windows // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/windows"

import (
"errors"
"syscall"
"unsafe"

Expand Down Expand Up @@ -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
}

Expand All @@ -78,29 +79,29 @@ 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
}

return nil
}

// 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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/stanza/operator/input/windows/bookmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package windows // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/windows"

import (
"errors"
"fmt"
"syscall"
)
Expand Down Expand Up @@ -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)
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/stanza/operator/input/windows/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions pkg/stanza/operator/input/windows/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 != "" {
Expand Down
12 changes: 9 additions & 3 deletions pkg/stanza/operator/input/windows/publishercache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand Down
6 changes: 4 additions & 2 deletions pkg/stanza/operator/input/windows/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 8249950

Please sign in to comment.