Skip to content

Commit

Permalink
[chore] Consolidate event code in WEL receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
djaglowski committed Sep 5, 2024
1 parent fa75b6e commit 9036a1d
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 213 deletions.
23 changes: 0 additions & 23 deletions pkg/stanza/operator/input/windows/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,29 +139,6 @@ func (e *Event) Close() error {
return nil
}

func (e *Event) RenderRaw(buffer Buffer) (EventRaw, error) {
if e.handle == 0 {
return EventRaw{}, fmt.Errorf("event handle does not exist")
}

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
}

buffer.UpdateSizeBytes(*bufferUsed)
return e.RenderRaw(buffer)
}
bytes, err := buffer.ReadBytes(*bufferUsed)
if err != nil {
return EventRaw{}, fmt.Errorf("failed to read bytes from buffer: %w", err)
}

return unmarshalEventRaw(bytes)
}

// NewEvent will create a new event from an event handle.
func NewEvent(handle uintptr) Event {
return Event{
Expand Down
12 changes: 3 additions & 9 deletions pkg/stanza/operator/input/windows/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,6 @@ func (i *Input) read(ctx context.Context) int {

// processEvent will process and send an event retrieved from windows event log.
func (i *Input) processEvent(ctx context.Context, event Event) {
remoteServer := i.remote.Server

var providerName string // The provider name is only retrieved if needed.
if !i.raw || len(i.excludeProviders) > 0 {
var err error
Expand All @@ -253,13 +251,12 @@ func (i *Input) processEvent(ctx context.Context, event Event) {
}

if i.raw {
rawEvent, err := event.RenderRaw(i.buffer)
rawEvent, err := event.RenderSimple(i.buffer)
if err != nil {
i.Logger().Error("Failed to render raw event", zap.Error(err))
return
}

rawEvent.RemoteServer = remoteServer
i.sendEventRaw(ctx, rawEvent)
return
}
Expand All @@ -275,7 +272,6 @@ func (i *Input) processEvent(ctx context.Context, event Event) {
if publisher.Valid() {
formattedEvent, err := event.RenderFormatted(i.buffer, publisher)
if err == nil {
formattedEvent.RemoteServer = remoteServer
i.sendEvent(ctx, formattedEvent)
return
}
Expand All @@ -290,7 +286,6 @@ func (i *Input) processEvent(ctx context.Context, event Event) {
return
}

simpleEvent.RemoteServer = remoteServer
i.sendEvent(ctx, simpleEvent)
}

Expand All @@ -309,9 +304,8 @@ func (i *Input) sendEvent(ctx context.Context, eventXML EventXML) {
}

// sendEventRaw will send EventRaw as an entry to the operator's output.
func (i *Input) sendEventRaw(ctx context.Context, eventRaw EventRaw) {
body := eventRaw.parseBody()
entry, err := i.NewEntry(body)
func (i *Input) sendEventRaw(ctx context.Context, eventRaw EventXML) {
entry, err := i.NewEntry(eventRaw.Original)
if err != nil {
i.Logger().Error("Failed to create entry", zap.Error(err))
return
Expand Down
78 changes: 0 additions & 78 deletions pkg/stanza/operator/input/windows/raw.go

This file was deleted.

93 changes: 0 additions & 93 deletions pkg/stanza/operator/input/windows/raw_test.go

This file was deleted.

19 changes: 10 additions & 9 deletions pkg/stanza/operator/input/windows/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

// EventXML is the rendered xml of an event.
type EventXML struct {
Original string `xml:"-"`
EventID EventID `xml:"System>EventID"`
Provider Provider `xml:"System>Provider"`
Computer string `xml:"System>Computer"`
Expand Down Expand Up @@ -180,15 +181,6 @@ func parseEventData(eventData EventData) map[string]any {
return outputMap
}

// unmarshalEventXML will unmarshal EventXML from xml bytes.
func unmarshalEventXML(bytes []byte) (EventXML, error) {
var eventXML EventXML
if err := xml.Unmarshal(bytes, &eventXML); err != nil {
return EventXML{}, fmt.Errorf("failed to unmarshal xml bytes into event: %w (%s)", err, string(bytes))
}
return eventXML, nil
}

// EventID is the identifier of the event.
type EventID struct {
Qualifiers uint16 `xml:"Qualifiers,attr"`
Expand Down Expand Up @@ -267,3 +259,12 @@ func (e Execution) asMap() map[string]any {

return result
}

// unmarshalEventXML will unmarshal EventXML from xml bytes.
func unmarshalEventXML(bytes []byte) (EventXML, error) {
var eventXML EventXML
if err := xml.Unmarshal(bytes, &eventXML); err != nil {
return EventXML{}, fmt.Errorf("failed to unmarshal xml bytes into event: %w (%s)", err, string(bytes))
}
return eventXML, nil
}
2 changes: 1 addition & 1 deletion receiver/windowseventlogreceiver/receiver_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func requireExpectedLogRecords(t *testing.T, sink *consumertest.LogsSink, expect
// logs sometimes take a while to be written, so a substantial wait buffer is needed
require.EventuallyWithT(t, func(c *assert.CollectT) {
actualLogRecords = filterAllLogRecordsBySource(t, sink, expectedEventSrc)
assert.Len(c, actualLogRecords, expectedEventCount)
require.Len(c, actualLogRecords, expectedEventCount)
}, 10*time.Second, 250*time.Millisecond)

return actualLogRecords
Expand Down

0 comments on commit 9036a1d

Please sign in to comment.