@@ -5,6 +5,7 @@ package elbaccesslogs // import "github.com/open-telemetry/opentelemetry-collect
5
5
6
6
import (
7
7
"bufio"
8
+ "errors"
8
9
"fmt"
9
10
"io"
10
11
"strings"
@@ -32,7 +33,7 @@ func NewELBAccessLogUnmarshaler(buildInfo component.BuildInfo, logger *zap.Logge
32
33
}
33
34
34
35
type resourceAttributes struct {
35
- resourceId string
36
+ resourceID string
36
37
}
37
38
38
39
// UnmarshalAWSLogs processes a file containing ELB access logs.
@@ -47,7 +48,7 @@ func (f *elbAccessLogUnmarshaler) UnmarshalAWSLogs(reader io.Reader) (plog.Logs,
47
48
48
49
// Read first line to determine format
49
50
if ! scanner .Scan () {
50
- return plog.Logs {}, fmt . Errorf ("no log lines found" )
51
+ return plog.Logs {}, errors . New ("no log lines found" )
51
52
}
52
53
line = scanner .Text ()
53
54
@@ -74,23 +75,17 @@ func (f *elbAccessLogUnmarshaler) UnmarshalAWSLogs(reader io.Reader) (plog.Logs,
74
75
// Process lines based on determined syntax
75
76
switch syntax {
76
77
case albAccessLogs :
77
- record , err := convertTextToAlbAccessLogRecord (fields )
78
- if err != nil {
79
- return plog.Logs {}, fmt .Errorf ("unable to convert log line to ALB record: %w" , err )
78
+ if err := f .handleAlbAccessLogs (fields , resourceAttr , scopeLogs ); err != nil {
79
+ return plog.Logs {}, err
80
80
}
81
- f .addToAlbAccessLogs (resourceAttr , scopeLogs , record )
82
81
case nlbAccessLogs :
83
- record , err := convertTextToNlbAccessLogRecord (fields )
84
- if err != nil {
85
- return plog.Logs {}, fmt .Errorf ("unable to convert log line to NLB record: %w" , err )
82
+ if err := f .handleNlbAccessLogs (fields , resourceAttr , scopeLogs ); err != nil {
83
+ return plog.Logs {}, err
86
84
}
87
- f .addToNlbAccessLogs (resourceAttr , scopeLogs , record )
88
85
case clbAccessLogs :
89
- record , err := convertTextToClbAccessLogRecord (fields )
90
- if err != nil {
91
- return plog.Logs {}, fmt .Errorf ("unable to convert log line to NLB record: %w" , err )
86
+ if err := f .handleClbAccessLogs (fields , resourceAttr , scopeLogs ); err != nil {
87
+ return plog.Logs {}, err
92
88
}
93
- f .addToClbAccessLogs (resourceAttr , scopeLogs , record )
94
89
default :
95
90
return plog.Logs {}, fmt .Errorf ("unsupported log syntax: %s" , syntax )
96
91
}
@@ -102,6 +97,9 @@ func (f *elbAccessLogUnmarshaler) UnmarshalAWSLogs(reader io.Reader) (plog.Logs,
102
97
103
98
line = scanner .Text ()
104
99
fields , err = extractFields (line )
100
+ if err != nil {
101
+ return plog.Logs {}, fmt .Errorf ("failed to parse log line: %w" , err )
102
+ }
105
103
if len (fields ) == 0 {
106
104
return plog.Logs {}, fmt .Errorf ("log line has no fields: %s" , line )
107
105
}
@@ -127,10 +125,20 @@ func (f *elbAccessLogUnmarshaler) createLogs() (plog.Logs, plog.ResourceLogs, pl
127
125
}
128
126
129
127
// setResourceAttributes based on the resourceAttributes
130
- func (f * elbAccessLogUnmarshaler ) setResourceAttributes (r * resourceAttributes , logs plog.ResourceLogs ) {
128
+ func (* elbAccessLogUnmarshaler ) setResourceAttributes (r * resourceAttributes , logs plog.ResourceLogs ) {
131
129
attr := logs .Resource ().Attributes ()
132
130
attr .PutStr (string (conventions .CloudProviderKey ), conventions .CloudProviderAWS .Value .AsString ())
133
- attr .PutStr (string (conventions .CloudResourceIDKey ), r .resourceId )
131
+ attr .PutStr (string (conventions .CloudResourceIDKey ), r .resourceID )
132
+ }
133
+
134
+ // handleClbAccessLogs handles clb access logs
135
+ func (f * elbAccessLogUnmarshaler ) handleClbAccessLogs (fields []string , resourceAttr * resourceAttributes , scopeLogs plog.ScopeLogs ) error {
136
+ record , err := convertTextToClbAccessLogRecord (fields )
137
+ if err != nil {
138
+ return fmt .Errorf ("unable to convert log line to CLB record: %w" , err )
139
+ }
140
+ f .addToClbAccessLogs (resourceAttr , scopeLogs , record )
141
+ return nil
134
142
}
135
143
136
144
// addToClbAccessLogs adds clb record to provided logs based
@@ -146,9 +154,9 @@ func (f *elbAccessLogUnmarshaler) addToClbAccessLogs(resourceAttr *resourceAttri
146
154
// Create record log
147
155
recordLog := plog .NewLogRecord ()
148
156
// Set resource id
149
- resourceAttr .resourceId = clbRecord .ELB
157
+ resourceAttr .resourceID = clbRecord .ELB
150
158
// Populate record attributes
151
- recordLog .Attributes ().PutStr (string (conventions .ClientAddressKey ), clbRecord .ClientIp )
159
+ recordLog .Attributes ().PutStr (string (conventions .ClientAddressKey ), clbRecord .ClientIP )
152
160
recordLog .Attributes ().PutStr (string (conventions .HTTPRequestMethodKey ), clbRecord .RequestMethod )
153
161
recordLog .Attributes ().PutStr (string (conventions .URLFullKey ), clbRecord .RequestURI )
154
162
recordLog .Attributes ().PutStr (string (conventions .NetworkProtocolNameKey ), clbRecord .ProtocolName )
@@ -176,6 +184,16 @@ func (f *elbAccessLogUnmarshaler) addToClbAccessLogs(resourceAttr *resourceAttri
176
184
recordLog .MoveTo (rScope )
177
185
}
178
186
187
+ // handleAlbAccessLogs handles alb access logs
188
+ func (f * elbAccessLogUnmarshaler ) handleAlbAccessLogs (fields []string , resourceAttr * resourceAttributes , scopeLogs plog.ScopeLogs ) error {
189
+ record , err := convertTextToAlbAccessLogRecord (fields )
190
+ if err != nil {
191
+ return fmt .Errorf ("unable to convert log line to ALB record: %w" , err )
192
+ }
193
+ f .addToAlbAccessLogs (resourceAttr , scopeLogs , record )
194
+ return nil
195
+ }
196
+
179
197
// addToAlbAccessLogs adds alb record to provided logs based
180
198
// on the extracted logs of each resource
181
199
func (f * elbAccessLogUnmarshaler ) addToAlbAccessLogs (resourceAttr * resourceAttributes , scopeLogs plog.ScopeLogs , albRecord AlbAccessLogRecord ) {
@@ -189,11 +207,11 @@ func (f *elbAccessLogUnmarshaler) addToAlbAccessLogs(resourceAttr *resourceAttri
189
207
// Create record log
190
208
recordLog := plog .NewLogRecord ()
191
209
// Set resource id
192
- resourceAttr .resourceId = albRecord .ELB
210
+ resourceAttr .resourceID = albRecord .ELB
193
211
// Populate record attributes
194
212
recordLog .Attributes ().PutStr (string (conventions .NetworkProtocolNameKey ), albRecord .Type )
195
213
recordLog .Attributes ().PutStr (string (conventions .NetworkProtocolVersionKey ), albRecord .ProtocolVersion )
196
- recordLog .Attributes ().PutStr (string (conventions .ClientAddressKey ), albRecord .ClientIp )
214
+ recordLog .Attributes ().PutStr (string (conventions .ClientAddressKey ), albRecord .ClientIP )
197
215
recordLog .Attributes ().PutStr (string (conventions .HTTPRequestMethodKey ), albRecord .RequestMethod )
198
216
recordLog .Attributes ().PutStr (string (conventions .URLFullKey ), albRecord .RequestURI )
199
217
recordLog .Attributes ().PutInt (string (conventions .ClientPortKey ), albRecord .ClientPort )
@@ -215,6 +233,16 @@ func (f *elbAccessLogUnmarshaler) addToAlbAccessLogs(resourceAttr *resourceAttri
215
233
recordLog .MoveTo (rScope )
216
234
}
217
235
236
+ // handleNlbAccessLogs handles nlb access logs
237
+ func (f * elbAccessLogUnmarshaler ) handleNlbAccessLogs (fields []string , resourceAttr * resourceAttributes , scopeLogs plog.ScopeLogs ) error {
238
+ record , err := convertTextToNlbAccessLogRecord (fields )
239
+ if err != nil {
240
+ return fmt .Errorf ("unable to convert log line to ALB record: %w" , err )
241
+ }
242
+ f .addToNlbAccessLogs (resourceAttr , scopeLogs , record )
243
+ return nil
244
+ }
245
+
218
246
// addToNlbAccessLogs adds nlb record to provided logs based
219
247
// on the extracted logs of each resource
220
248
func (f * elbAccessLogUnmarshaler ) addToNlbAccessLogs (resourceAttr * resourceAttributes , scopeLogs plog.ScopeLogs , nlbRecord NlbAccessLogRecord ) {
@@ -228,15 +256,15 @@ func (f *elbAccessLogUnmarshaler) addToNlbAccessLogs(resourceAttr *resourceAttri
228
256
// Create record log
229
257
recordLog := plog .NewLogRecord ()
230
258
// Set resource id
231
- resourceAttr .resourceId = nlbRecord .ELB
259
+ resourceAttr .resourceID = nlbRecord .ELB
232
260
// Populate record attributes
233
261
recordLog .Attributes ().PutStr (string (conventions .NetworkProtocolNameKey ), nlbRecord .Type )
234
262
recordLog .Attributes ().PutStr (string (conventions .NetworkProtocolVersionKey ), nlbRecord .Version )
235
- recordLog .Attributes ().PutStr (string (conventions .ClientAddressKey ), nlbRecord .ClientIp )
263
+ recordLog .Attributes ().PutStr (string (conventions .ClientAddressKey ), nlbRecord .ClientIP )
236
264
recordLog .Attributes ().PutInt (string (conventions .ClientPortKey ), nlbRecord .ClientPort )
237
265
recordLog .Attributes ().PutInt (string (conventions .HTTPRequestSizeKey ), nlbRecord .ReceivedBytes )
238
266
recordLog .Attributes ().PutInt (string (conventions .HTTPResponseSizeKey ), nlbRecord .SentBytes )
239
- recordLog .Attributes ().PutStr (AttributeTlsListenerResourceID , nlbRecord .Listener )
267
+ recordLog .Attributes ().PutStr (AttributeTLSListenerResourceID , nlbRecord .Listener )
240
268
recordLog .Attributes ().PutStr (string (conventions .TLSProtocolVersionKey ), nlbRecord .TLSProtocolVersion )
241
269
recordLog .Attributes ().PutStr (string (conventions .TLSCipherKey ), nlbRecord .TLSCipher )
242
270
0 commit comments