9
9
"time"
10
10
11
11
"github.com/stretchr/testify/assert"
12
- "github.com/stretchr/testify/require"
13
12
semconv "go.opentelemetry.io/collector/semconv/v1.27.0"
14
13
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
15
14
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -91,7 +90,7 @@ func TestUnstructuredListToLogData(t *testing.T) {
91
90
assert .Equal (t , 3 , logRecords .Len ())
92
91
})
93
92
94
- t .Run ("Test event.name in watch events " , func (t * testing.T ) {
93
+ t .Run ("Test event observed timestamp is present " , func (t * testing.T ) {
95
94
config := & K8sObjectsConfig {
96
95
gvr : & schema.GroupVersionResource {
97
96
Group : "" ,
@@ -112,7 +111,8 @@ func TestUnstructuredListToLogData(t *testing.T) {
112
111
},
113
112
}
114
113
115
- logs , err := watchObjectsToLogData (event , time .Now (), config )
114
+ observedAt := time .Now ()
115
+ logs , err := watchObjectsToLogData (event , observedAt , config )
116
116
assert .NoError (t , err )
117
117
118
118
assert .Equal (t , 1 , logs .LogRecordCount ())
@@ -123,47 +123,105 @@ func TestUnstructuredListToLogData(t *testing.T) {
123
123
logRecords := rl .ScopeLogs ().At (0 ).LogRecords ()
124
124
assert .Equal (t , 1 , rl .ScopeLogs ().Len ())
125
125
assert .Equal (t , 1 , logRecords .Len ())
126
-
127
- attrs := logRecords .At (0 ).Attributes ()
128
- eventName , ok := attrs .Get ("event.name" )
129
- require .True (t , ok )
130
- assert .EqualValues (t , "generic-name" , eventName .AsRaw ())
126
+ assert .Positive (t , logRecords .At (0 ).ObservedTimestamp ().AsTime ().Unix ())
127
+ assert .Equal (t , logRecords .At (0 ).ObservedTimestamp ().AsTime ().Unix (), observedAt .Unix ())
131
128
})
132
129
133
- t .Run ("Test event observed timestamp is present" , func (t * testing.T ) {
130
+ t .Run ("Test pull and watch objects both contain k8s.namespace.name" , func (t * testing.T ) {
131
+ observedTimestamp := time .Now ()
134
132
config := & K8sObjectsConfig {
135
133
gvr : & schema.GroupVersionResource {
136
134
Group : "" ,
137
135
Version : "v1" ,
138
136
Resource : "events" ,
139
137
},
140
138
}
141
- event := & watch.Event {
139
+ watchedEvent := & watch.Event {
142
140
Type : watch .Added ,
143
141
Object : & unstructured.Unstructured {
144
142
Object : map [string ]any {
145
143
"kind" : "Event" ,
146
144
"apiVersion" : "v1" ,
147
145
"metadata" : map [string ]any {
148
- "name" : "generic-name" ,
146
+ "name" : "generic-name" ,
147
+ "namespace" : "my-namespace" ,
149
148
},
150
149
},
151
150
},
152
151
}
153
152
154
- observedAt := time .Now ()
155
- logs , err := watchObjectsToLogData (event , observedAt , config )
156
- assert .NoError (t , err )
157
-
158
- assert .Equal (t , 1 , logs .LogRecordCount ())
153
+ pulledEvent := & unstructured.UnstructuredList {
154
+ Items : []unstructured.Unstructured {{
155
+ Object : map [string ]any {
156
+ "kind" : "Event" ,
157
+ "apiVersion" : "v1" ,
158
+ "metadata" : map [string ]any {
159
+ "name" : "generic-name" ,
160
+ "namespace" : "my-namespace" ,
161
+ },
162
+ },
163
+ }},
164
+ }
159
165
160
- resourceLogs := logs .ResourceLogs ()
161
- assert .Equal (t , 1 , resourceLogs .Len ())
162
- rl := resourceLogs .At (0 )
163
- logRecords := rl .ScopeLogs ().At (0 ).LogRecords ()
164
- assert .Equal (t , 1 , rl .ScopeLogs ().Len ())
165
- assert .Equal (t , 1 , logRecords .Len ())
166
- assert .Positive (t , logRecords .At (0 ).ObservedTimestamp ().AsTime ().Unix ())
167
- assert .Equal (t , logRecords .At (0 ).ObservedTimestamp ().AsTime ().Unix (), observedAt .Unix ())
166
+ logEntryFromWatchEvent , err := watchObjectsToLogData (watchedEvent , observedTimestamp , config )
167
+ assert .NoError (t , err )
168
+ assert .NotNil (t , logEntryFromWatchEvent )
169
+
170
+ // verify the event.type, event.domain and k8s.resource.name attributes have been added
171
+
172
+ watchEventResourceAttrs := logEntryFromWatchEvent .ResourceLogs ().At (0 ).Resource ().Attributes ()
173
+ k8sNamespace , ok := watchEventResourceAttrs .Get (semconv .AttributeK8SNamespaceName )
174
+ assert .True (t , ok )
175
+ assert .Equal (t ,
176
+ "my-namespace" ,
177
+ k8sNamespace .Str (),
178
+ )
179
+
180
+ watchEvenLogRecordtAttrs := logEntryFromWatchEvent .ResourceLogs ().At (0 ).ScopeLogs ().At (0 ).LogRecords ().At (0 ).Attributes ()
181
+ eventType , ok := watchEvenLogRecordtAttrs .Get ("event.name" )
182
+ assert .True (t , ok )
183
+ assert .Equal (
184
+ t ,
185
+ "generic-name" ,
186
+ eventType .AsString (),
187
+ )
188
+
189
+ eventDomain , ok := watchEvenLogRecordtAttrs .Get ("event.domain" )
190
+ assert .True (t , ok )
191
+ assert .Equal (
192
+ t ,
193
+ "k8s" ,
194
+ eventDomain .AsString (),
195
+ )
196
+
197
+ k8sResourceName , ok := watchEvenLogRecordtAttrs .Get ("k8s.resource.name" )
198
+ assert .True (t , ok )
199
+ assert .Equal (
200
+ t ,
201
+ "events" ,
202
+ k8sResourceName .AsString (),
203
+ )
204
+
205
+ logEntryFromPulledEvent := unstructuredListToLogData (pulledEvent , observedTimestamp , config )
206
+ assert .NotNil (t , logEntryFromPulledEvent )
207
+
208
+ pullEventResourceAttrs := logEntryFromPulledEvent .ResourceLogs ().At (0 ).Resource ().Attributes ()
209
+ k8sNamespace , ok = pullEventResourceAttrs .Get (semconv .AttributeK8SNamespaceName )
210
+ assert .True (t , ok )
211
+ assert .Equal (
212
+ t ,
213
+ "my-namespace" ,
214
+ k8sNamespace .Str (),
215
+ )
216
+
217
+ pullEventLogRecordAttrs := logEntryFromPulledEvent .ResourceLogs ().At (0 ).ScopeLogs ().At (0 ).LogRecords ().At (0 ).Attributes ()
218
+
219
+ k8sResourceName , ok = pullEventLogRecordAttrs .Get ("k8s.resource.name" )
220
+ assert .True (t , ok )
221
+ assert .Equal (
222
+ t ,
223
+ "events" ,
224
+ k8sResourceName .AsString (),
225
+ )
168
226
})
169
227
}
0 commit comments