Skip to content

Commit 90c0151

Browse files
authored
Refactor Windows Filter flags (#1926)
Co-authored-by: Akansha Agarwal <[email protected]>
1 parent 9f0d902 commit 90c0151

File tree

6 files changed

+155
-60
lines changed

6 files changed

+155
-60
lines changed

extension/agenthealth/handler/useragent/useragent.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ const (
3333
flagEnhancedContainerInsights = "enhanced_container_insights"
3434
flagSELinux = "selinux"
3535
flagROSA = "rosa"
36-
FlagWindowsEventIDs = "win_event_ids"
37-
FlagWindowsEventFilters = "win_event_filters"
38-
FlagWindowsEventLevels = "win_event_levels"
3936
separator = " "
4037

4138
typeInputs = "inputs"
@@ -85,6 +82,7 @@ func (ua *userAgent) SetComponents(otelCfg *otelcol.Config, telegrafCfg *telegra
8582

8683
for _, input := range telegrafCfg.Inputs {
8784
ua.inputs.Add(input.Config.Name)
85+
ua.setWindowsEventLogFeatureFlags(input)
8886
}
8987

9088
for _, output := range telegrafCfg.Outputs {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//go:build !windows
2+
3+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
// SPDX-License-Identifier: MIT
5+
6+
package useragent
7+
8+
import (
9+
"github.com/influxdata/telegraf/models"
10+
)
11+
12+
func (ua *userAgent) setWindowsEventLogFeatureFlags(_ *models.RunningInput) {
13+
// No-op on non-Windows platforms
14+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//go:build windows
2+
3+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
// SPDX-License-Identifier: MIT
5+
6+
package useragent
7+
8+
import (
9+
"github.com/influxdata/telegraf/models"
10+
11+
"github.com/aws/amazon-cloudwatch-agent/plugins/inputs/windows_event_log"
12+
)
13+
14+
const (
15+
flagWindowsEventIDs = "win_event_ids"
16+
flagWindowsEventFilters = "win_event_filters"
17+
flagWindowsEventLevels = "win_event_levels"
18+
pluginWindowsEventLog = "windows_event_log"
19+
)
20+
21+
func (ua *userAgent) setWindowsEventLogFeatureFlags(input *models.RunningInput) {
22+
if input.Config.Name == pluginWindowsEventLog {
23+
if plugin, ok := input.Input.(*windows_event_log.Plugin); ok {
24+
for _, eventConfig := range plugin.Events {
25+
if len(eventConfig.EventIDs) > 0 {
26+
ua.feature.Add(flagWindowsEventIDs)
27+
}
28+
if len(eventConfig.Filters) > 0 {
29+
ua.feature.Add(flagWindowsEventFilters)
30+
}
31+
if len(eventConfig.Levels) > 0 {
32+
ua.feature.Add(flagWindowsEventLevels)
33+
}
34+
}
35+
}
36+
}
37+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//go:build windows
2+
3+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
// SPDX-License-Identifier: MIT
5+
6+
package useragent
7+
8+
import (
9+
"testing"
10+
11+
"github.com/influxdata/telegraf/models"
12+
"github.com/stretchr/testify/assert"
13+
14+
"github.com/aws/amazon-cloudwatch-agent/plugins/inputs/windows_event_log"
15+
"github.com/aws/amazon-cloudwatch-agent/plugins/inputs/windows_event_log/wineventlog"
16+
)
17+
18+
func TestSetWindowsEventLogFeatureFlags(t *testing.T) {
19+
tests := []struct {
20+
name string
21+
inputName string
22+
plugin *windows_event_log.Plugin
23+
expectedFlags []string
24+
}{
25+
{
26+
name: "non-windows input",
27+
inputName: "cpu",
28+
plugin: &windows_event_log.Plugin{},
29+
expectedFlags: []string{},
30+
},
31+
{
32+
name: "no features",
33+
inputName: pluginWindowsEventLog,
34+
plugin: &windows_event_log.Plugin{
35+
Events: []windows_event_log.EventConfig{{Name: "System"}},
36+
},
37+
expectedFlags: []string{},
38+
},
39+
{
40+
name: "win_event_ids",
41+
inputName: pluginWindowsEventLog,
42+
plugin: &windows_event_log.Plugin{
43+
Events: []windows_event_log.EventConfig{{
44+
Name: "System",
45+
EventIDs: []int{1000, 1001},
46+
}},
47+
},
48+
expectedFlags: []string{flagWindowsEventIDs},
49+
},
50+
{
51+
name: "win_event_filters",
52+
inputName: pluginWindowsEventLog,
53+
plugin: &windows_event_log.Plugin{
54+
Events: []windows_event_log.EventConfig{{
55+
Name: "System",
56+
Filters: []*wineventlog.EventFilter{{Expression: "test"}},
57+
}},
58+
},
59+
expectedFlags: []string{flagWindowsEventFilters},
60+
},
61+
{
62+
name: "win_event_levels",
63+
inputName: pluginWindowsEventLog,
64+
plugin: &windows_event_log.Plugin{
65+
Events: []windows_event_log.EventConfig{{
66+
Name: "System",
67+
Levels: []string{"ERROR", "WARNING"},
68+
}},
69+
},
70+
expectedFlags: []string{flagWindowsEventLevels},
71+
},
72+
{
73+
name: "all flags",
74+
inputName: pluginWindowsEventLog,
75+
plugin: &windows_event_log.Plugin{
76+
Events: []windows_event_log.EventConfig{{
77+
Name: "System",
78+
EventIDs: []int{1000},
79+
Filters: []*wineventlog.EventFilter{{Expression: "test"}},
80+
Levels: []string{"ERROR"},
81+
}},
82+
},
83+
expectedFlags: []string{flagWindowsEventIDs, flagWindowsEventFilters, flagWindowsEventLevels},
84+
},
85+
}
86+
87+
for _, tt := range tests {
88+
t.Run(tt.name, func(t *testing.T) {
89+
ua := newUserAgent()
90+
input := &models.RunningInput{
91+
Config: &models.InputConfig{Name: tt.inputName},
92+
Input: tt.plugin,
93+
}
94+
95+
ua.setWindowsEventLogFeatureFlags(input)
96+
97+
for _, flag := range tt.expectedFlags {
98+
assert.Contains(t, ua.feature, flag)
99+
}
100+
assert.Len(t, ua.feature, len(tt.expectedFlags))
101+
})
102+
}
103+
}

plugins/inputs/windows_event_log/windows_event_log.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/influxdata/telegraf"
1616
"github.com/influxdata/telegraf/plugins/inputs"
1717

18-
"github.com/aws/amazon-cloudwatch-agent/extension/agenthealth/handler/useragent"
1918
"github.com/aws/amazon-cloudwatch-agent/internal/logscommon"
2019
"github.com/aws/amazon-cloudwatch-agent/internal/state"
2120
"github.com/aws/amazon-cloudwatch-agent/logs"
@@ -93,7 +92,6 @@ func (s *Plugin) Start(acc telegraf.Accumulator) error {
9392
return nil
9493
}
9594

96-
s.detectFeatures()
9795
monitor := newServiceMonitor()
9896
for _, eventConfig := range s.Events {
9997
// Assume no 2 EventConfigs have the same combination of:
@@ -158,18 +156,3 @@ func (s *Plugin) Stop() {
158156
func init() {
159157
inputs.Add("windows_event_log", func() telegraf.Input { return &Plugin{} })
160158
}
161-
func (s *Plugin) detectFeatures() {
162-
if ua := useragent.Get(); ua != nil {
163-
for _, eventConfig := range s.Events {
164-
if len(eventConfig.EventIDs) > 0 {
165-
ua.AddFeatureFlags(useragent.FlagWindowsEventIDs)
166-
}
167-
if len(eventConfig.Filters) > 0 {
168-
ua.AddFeatureFlags(useragent.FlagWindowsEventFilters)
169-
}
170-
if len(eventConfig.Levels) > 0 {
171-
ua.AddFeatureFlags(useragent.FlagWindowsEventLevels)
172-
}
173-
}
174-
}
175-
}

plugins/inputs/windows_event_log/windows_event_log_test.go

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ import (
1313

1414
"github.com/stretchr/testify/assert"
1515
"github.com/stretchr/testify/require"
16-
17-
"github.com/aws/amazon-cloudwatch-agent/extension/agenthealth/handler/useragent"
18-
"github.com/aws/amazon-cloudwatch-agent/plugins/inputs/windows_event_log/wineventlog"
1916
)
2017

2118
// TestGetStateFilePathGood tests getStateFilePath with good input.
@@ -108,40 +105,3 @@ func TestWindowsDuplicateStart(t *testing.T) {
108105
plugin.Start(nil)
109106
require.Equal(t, 1, len(plugin.newEvents), "Start should be ran only once so there should be only 1 new event")
110107
}
111-
112-
func TestDetectFeatures(t *testing.T) {
113-
plugin := &Plugin{
114-
Events: []EventConfig{
115-
{
116-
EventIDs: []int{1000, 1001},
117-
},
118-
{
119-
Filters: []*wineventlog.EventFilter{{Expression: "test"}},
120-
Levels: []string{"ERROR"},
121-
},
122-
},
123-
}
124-
125-
ua := useragent.Get()
126-
plugin.detectFeatures()
127-
128-
header := ua.Header(true)
129-
assert.Contains(t, header, useragent.FlagWindowsEventIDs)
130-
assert.Contains(t, header, useragent.FlagWindowsEventFilters)
131-
assert.Contains(t, header, useragent.FlagWindowsEventLevels)
132-
133-
// Test that only configured features are detected
134-
plugin = &Plugin{
135-
Events: []EventConfig{{
136-
EventIDs: []int{1000},
137-
}},
138-
}
139-
ua = useragent.Get()
140-
ua.Reset()
141-
plugin.detectFeatures()
142-
143-
header = ua.Header(true)
144-
assert.Contains(t, header, useragent.FlagWindowsEventIDs)
145-
assert.NotContains(t, header, useragent.FlagWindowsEventFilters)
146-
assert.NotContains(t, header, useragent.FlagWindowsEventLevels)
147-
}

0 commit comments

Comments
 (0)