@@ -8,58 +8,9 @@ import (
88
99 "github.com/stretchr/testify/assert"
1010 "github.com/stretchr/testify/require"
11- "go.opentelemetry.io/collector/confmap"
1211 "go.opentelemetry.io/collector/pipeline"
13-
14- translatorconfig "github.com/aws/amazon-cloudwatch-agent/translator/config"
15- translatorcontext "github.com/aws/amazon-cloudwatch-agent/translator/context"
1612)
1713
18- func TestNewTranslators_Disabled (t * testing.T ) {
19- conf := confmap .NewFromStringMap (map [string ]any {
20- "opentelemetry" : map [string ]any {"collect" : map [string ]any {}},
21- })
22- translators := NewTranslators (conf )
23- assert .Equal (t , 0 , translators .Len ())
24- }
25-
26- func TestNewTranslators_TwoEntries (t * testing.T ) {
27- translatorcontext .CurrentContext ().SetOs (translatorconfig .OS_TYPE_WINDOWS )
28- defer translatorcontext .CurrentContext ().SetOs ("" )
29- conf := confmap .NewFromStringMap (map [string ]any {
30- "opentelemetry" : map [string ]any {
31- "collect" : map [string ]any {
32- "windows_events" : map [string ]any {
33- "collect_list" : []any {
34- map [string ]any {"event_name" : "System" },
35- map [string ]any {"event_name" : "Application" },
36- },
37- },
38- },
39- },
40- })
41- translators := NewTranslators (conf )
42- assert .Equal (t , 2 , translators .Len ())
43- }
44-
45- func TestNewTranslators_NonWindows (t * testing.T ) {
46- translatorcontext .CurrentContext ().SetOs (translatorconfig .OS_TYPE_LINUX )
47- defer translatorcontext .CurrentContext ().SetOs ("" )
48- conf := confmap .NewFromStringMap (map [string ]any {
49- "opentelemetry" : map [string ]any {
50- "collect" : map [string ]any {
51- "windows_events" : map [string ]any {
52- "collect_list" : []any {
53- map [string ]any {"event_name" : "System" },
54- },
55- },
56- },
57- },
58- })
59- translators := NewTranslators (conf )
60- assert .Equal (t , 0 , translators .Len ())
61- }
62-
6314func TestPipelineTranslator_ID (t * testing.T ) {
6415 pt := & windowsEventsPipelineTranslator {entry : eventEntry {name : "system_0" }}
6516 assert .Equal (t , pipeline .NewIDWithName (pipeline .SignalLogs , "windows_events_system_0" ), pt .ID ())
@@ -101,37 +52,19 @@ func TestPipelineTranslator_Translate_WithFilter(t *testing.T) {
10152 assert .Equal (t , 2 , result .Processors .Len ())
10253}
10354
104- func TestParseEntries (t * testing.T ) {
105- conf := confmap .NewFromStringMap (map [string ]any {
106- "opentelemetry" : map [string ]any {
107- "collect" : map [string ]any {
108- "windows_events" : map [string ]any {
109- "collect_list" : []any {
110- map [string ]any {"event_name" : "System" , "event_levels" : []any {"ERROR" }, "event_format" : "xml" , "log_group_name" : "/custom/system" },
111- map [string ]any {"event_name" : "Application" , "event_ids" : []any {float64 (1001 )}},
112- map [string ]any {"event_name" : "Microsoft-Windows-PowerShell/Operational" , "event_levels" : []any {"WARNING" }},
113- },
114- },
115- },
116- },
117- })
118- entries := parseEntries (conf )
119- require .Len (t , entries , 3 )
120-
121- assert .Equal (t , "system" , entries [0 ].name )
122- assert .Equal (t , "System" , entries [0 ].channel )
123- assert .True (t , entries [0 ].raw )
124- assert .Equal (t , "/custom/system" , entries [0 ].resource ["aws.log.group.name" ])
125- assert .Equal (t , []string {"ERROR" }, entries [0 ].eventLevels )
126-
127- assert .Equal (t , "application" , entries [1 ].name )
128- assert .Equal (t , "Application" , entries [1 ].channel )
129- assert .False (t , entries [1 ].raw )
130- assert .Equal (t , []int {1001 }, entries [1 ].eventIDs )
131-
132- assert .Equal (t , "microsoft-windows-powershell_operational" , entries [2 ].name )
133- assert .Equal (t , "Microsoft-Windows-PowerShell/Operational" , entries [2 ].channel )
134- assert .Equal (t , []string {"WARNING" }, entries [2 ].eventLevels )
55+ func TestPipelineTranslator_Translate_WithRoutingAttrs (t * testing.T ) {
56+ pt := & windowsEventsPipelineTranslator {entry : eventEntry {
57+ name : "system" ,
58+ receiverName : "system" ,
59+ channel : "System" ,
60+ resource : map [string ]string {"aws.log.source" : "windows_events" , "aws.log.channel" : "System" },
61+ logGroupName : "/custom/group" ,
62+ }}
63+ result , err := pt .Translate (nil )
64+ require .NoError (t , err )
65+
66+ // resource processor + scope transform = 2 processors
67+ assert .Equal (t , 2 , result .Processors .Len ())
13568}
13669
13770func TestPipelineTranslator_DuplicateChannels_SharedReceiver (t * testing.T ) {
@@ -162,32 +95,6 @@ func TestPipelineTranslator_DuplicateChannels_SharedReceiver(t *testing.T) {
16295 assert .Equal (t , r1 .Receivers .Keys (), r2 .Receivers .Keys ())
16396}
16497
165- func TestParseEntries_DuplicateChannels (t * testing.T ) {
166- conf := confmap .NewFromStringMap (map [string ]any {
167- "opentelemetry" : map [string ]any {
168- "collect" : map [string ]any {
169- "windows_events" : map [string ]any {
170- "collect_list" : []any {
171- map [string ]any {"event_name" : "System" , "event_levels" : []any {"ERROR" }},
172- map [string ]any {"event_name" : "System" , "event_levels" : []any {"WARNING" }},
173- map [string ]any {"event_name" : "System" , "event_ids" : []any {float64 (1001 )}},
174- },
175- },
176- },
177- },
178- })
179- entries := parseEntries (conf )
180- require .Len (t , entries , 3 )
181- assert .Equal (t , "system" , entries [0 ].name )
182- assert .Equal (t , "system_1" , entries [1 ].name )
183- assert .Equal (t , "system_2" , entries [2 ].name )
184-
185- // All entries share the same receiver (same channel = same checkpoint)
186- assert .Equal (t , "system" , entries [0 ].receiverName )
187- assert .Equal (t , "system" , entries [1 ].receiverName )
188- assert .Equal (t , "system" , entries [2 ].receiverName )
189- }
190-
19198func TestPipelineTranslator_Translate_XmlWithEventIDs_Error (t * testing.T ) {
19299 pt := & windowsEventsPipelineTranslator {entry : eventEntry {
193100 name : "security" ,
@@ -237,3 +144,32 @@ func TestBuildFilterCondition(t *testing.T) {
237144 })
238145 }
239146}
147+
148+ func TestRoutingAttributes (t * testing.T ) {
149+ tests := []struct {
150+ name string
151+ entry eventEntry
152+ expected map [string ]string
153+ }{
154+ {
155+ name : "no routing attrs" ,
156+ entry : eventEntry {name : "system" },
157+ expected : map [string ]string {},
158+ },
159+ {
160+ name : "log group only" ,
161+ entry : eventEntry {name : "system" , logGroupName : "/custom/group" },
162+ expected : map [string ]string {"aws.log.group.name" : "/custom/group" },
163+ },
164+ {
165+ name : "both" ,
166+ entry : eventEntry {name : "system" , logGroupName : "/custom/group" , logStreamName : "my-stream" },
167+ expected : map [string ]string {"aws.log.group.name" : "/custom/group" , "aws.log.stream.name" : "my-stream" },
168+ },
169+ }
170+ for _ , tt := range tests {
171+ t .Run (tt .name , func (t * testing.T ) {
172+ assert .Equal (t , tt .expected , tt .entry .routingAttributes ())
173+ })
174+ }
175+ }
0 commit comments