Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pkg/stanza] return raw Windows events as strings #22863

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .chloggen/feat_stanza_windowsinput_raweventbody.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use this changelog template to create an entry for release notes.
# If your change doesn't affect end users, such as a test fix or a tooling change,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: breaking

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: windowseventlogreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Emit raw Windows events as strings instead of byte arrays

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [22704]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
3 changes: 0 additions & 3 deletions pkg/stanza/operator/input/windows/buffer.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:build windows
// +build windows

package windows // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/windows"

import (
Expand Down
15 changes: 3 additions & 12 deletions pkg/stanza/operator/input/windows/buffer_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:build windows
// +build windows

package windows

import (
Expand All @@ -17,9 +14,7 @@ func TestBufferReadBytes(t *testing.T) {
buffer := NewBuffer()
utf8 := []byte("test")
utf16, _ := unicode.UTF16(unicode.LittleEndian, unicode.UseBOM).NewEncoder().Bytes(utf8)
for i, b := range utf16 {
buffer.buffer[i] = b
}
copy(buffer.buffer, utf16)
offset := uint32(len(utf16))
bytes, err := buffer.ReadBytes(offset)
require.NoError(t, err)
Expand All @@ -30,9 +25,7 @@ func TestBufferReadWideBytes(t *testing.T) {
buffer := NewBuffer()
utf8 := []byte("test")
utf16, _ := unicode.UTF16(unicode.LittleEndian, unicode.UseBOM).NewEncoder().Bytes(utf8)
for i, b := range utf16 {
buffer.buffer[i] = b
}
copy(buffer.buffer, utf16)
offset := uint32(len(utf16) / 2)
bytes, err := buffer.ReadWideChars(offset)
require.NoError(t, err)
Expand All @@ -43,9 +36,7 @@ func TestBufferReadString(t *testing.T) {
buffer := NewBuffer()
utf8 := []byte("test")
utf16, _ := unicode.UTF16(unicode.LittleEndian, unicode.UseBOM).NewEncoder().Bytes(utf8)
for i, b := range utf16 {
buffer.buffer[i] = b
}
copy(buffer.buffer, utf16)
offset := uint32(len(utf16))
result, err := buffer.ReadString(offset)
require.NoError(t, err)
Expand Down
11 changes: 4 additions & 7 deletions pkg/stanza/operator/input/windows/raw.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:build windows
// +build windows

package windows // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/windows"

import (
Expand All @@ -19,7 +16,7 @@ type EventRaw struct {
TimeCreated TimeCreated `xml:"System>TimeCreated"`
RenderedLevel string `xml:"RenderingInfo>Level"`
Level string `xml:"System>Level"`
bytes []byte
Body string `xml:"-"`
}

// parseTimestamp will parse the timestamp of the event.
Expand Down Expand Up @@ -65,8 +62,8 @@ func (e *EventRaw) parseSeverity() entry.Severity {
}

// parseBody will parse a body from the event.
func (e *EventRaw) parseBody() []byte {
return e.bytes
func (e *EventRaw) parseBody() string {
return e.Body
}

// unmarshalEventRaw will unmarshal EventRaw from xml bytes.
Expand All @@ -75,6 +72,6 @@ func unmarshalEventRaw(bytes []byte) (EventRaw, error) {
if err := xml.Unmarshal(bytes, &eventRaw); err != nil {
return EventRaw{}, fmt.Errorf("failed to unmarshal xml bytes into event: %w (%s)", err, string(bytes))
}
eventRaw.bytes = bytes
eventRaw.Body = string(bytes)
return eventRaw, nil
}
9 changes: 3 additions & 6 deletions pkg/stanza/operator/input/windows/raw_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:build windows
// +build windows

package windows

import (
Expand Down Expand Up @@ -65,10 +62,10 @@ func TestParseSeverityRaw(t *testing.T) {

func TestParseBodyRaw(t *testing.T) {
raw := EventRaw{
bytes: []byte("foo"),
Body: "foo",
}

require.Equal(t, []byte("foo"), raw.parseBody())
require.Equal(t, "foo", raw.parseBody())
}

func TestInvalidUnmarshalRaw(t *testing.T) {
Expand All @@ -89,7 +86,7 @@ func TestUnmarshalRaw(t *testing.T) {
SystemTime: "2022-04-22T10:20:52.3778625Z",
},
Level: "4",
bytes: data,
Body: string(data),
}

require.Equal(t, raw, event)
Expand Down
3 changes: 0 additions & 3 deletions pkg/stanza/operator/input/windows/xml.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:build windows
// +build windows

package windows // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/windows"

import (
Expand Down
3 changes: 0 additions & 3 deletions pkg/stanza/operator/input/windows/xml_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:build windows
// +build windows

package windows

import (
Expand Down