Skip to content

Commit 7cf4e13

Browse files
Fix linter errors
1 parent 763db4c commit 7cf4e13

File tree

6 files changed

+68
-41
lines changed

6 files changed

+68
-41
lines changed

extension/encoding/awslogsencodingextension/extension.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,8 @@ func (e *encodingExtension) getReaderFromFormat(buf []byte) (string, io.Reader,
131131
if len(buf) > 2 && buf[0] == 0x1f && buf[1] == 0x8b {
132132
reader, err := e.getGzipReader(buf)
133133
return gzipEncoding, reader, err
134-
} else {
135-
return bytesEncoding, bytes.NewReader(buf), nil
136134
}
135+
return bytesEncoding, bytes.NewReader(buf), nil
137136
case formatVPCFlowLog:
138137
switch e.vpcFormat {
139138
case fileFormatParquet:

extension/encoding/awslogsencodingextension/extension_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func TestNew_ELBAcessLog(t *testing.T) {
7171
require.NotNil(t, e)
7272

7373
_, err = e.UnmarshalLogs([]byte("invalid"))
74-
require.ErrorContains(t, err, `failed to get reader for "elb_access_log" logs`)
74+
require.ErrorContains(t, err, `failed to unmarshal logs as "elb_access_log" format`)
7575
}
7676

7777
func TestNew_Unimplemented(t *testing.T) {

extension/encoding/awslogsencodingextension/internal/unmarshaler/elb-access-log/benchmark_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func createELBAccessLogContent(b *testing.B, filename string, nLogs int) []byte
4343
for i := 0; i < nLogs; i++ {
4444
buf.Write(data)
4545
if i != nLogs-1 {
46-
buf.Write([]byte("\n"))
46+
buf.WriteString("\n")
4747
}
4848
}
4949

extension/encoding/awslogsencodingextension/internal/unmarshaler/elb-access-log/elb.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ const (
2828
type ClbAccessLogRecord struct {
2929
Time string // Timestamp the load balancer received the request from the client, in ISO 8601 format
3030
ELB string // The name of the load balancer
31-
ClientIp string // Client IP
31+
ClientIP string // Client IP
3232
ClientPort int64 // Client port
33-
BackendIpPort string // The IP address and port of the registered instance that processed this request, or -
33+
BackendIPPort string // The IP address and port of the registered instance that processed this request, or -
3434
RequestProcessingTime float64 // Time taken to process the request in seconds (HTTP/TCP)
3535
BackendProcessingTime float64 // Time taken for the registered instance to respond
3636
ResponseProcessingTime float64 // Time taken to send the response to the client
@@ -59,7 +59,7 @@ func convertTextToClbAccessLogRecord(fields []string) (ClbAccessLogRecord, error
5959
record := ClbAccessLogRecord{
6060
Time: fields[0], // Timestamp
6161
ELB: fields[1], // Load balancer name
62-
BackendIpPort: fields[3], // Backend IP:Port
62+
BackendIPPort: fields[3], // Backend IP:Port
6363
ELBStatusCode: 0, // Placeholder for ELB status code
6464
BackendStatusCode: 0, // Placeholder for Backend status code
6565
UserAgent: fields[12], // User-Agent
@@ -68,7 +68,7 @@ func convertTextToClbAccessLogRecord(fields []string) (ClbAccessLogRecord, error
6868
}
6969

7070
// Process the fields for numerical values (convenient to parse from string)
71-
record.ClientIp = strings.Split(fields[2], ":")[0]
71+
record.ClientIP = strings.Split(fields[2], ":")[0]
7272
if record.ClientPort, err = safeConvertStrToInt(strings.Split(fields[2], ":")[1]); err != nil {
7373
return record, fmt.Errorf("could not convert client port to integer: %w", err)
7474
}
@@ -119,9 +119,9 @@ type NlbAccessLogRecord struct {
119119
Time string // Timestamp the load balancer generated a response to the client in ISO 8601 format
120120
ELB string // Load balancer resource ID
121121
Listener string // Resource ID of the TLS listener for the connection
122-
ClientIp string // Client IP
122+
ClientIP string // Client IP
123123
ClientPort int64 // Client port
124-
DestinationIpPort string // The destination IP and port of the target
124+
DestinationIPPort string // The destination IP and port of the target
125125
ConnectionTime int64 // Total time for the connection to complete, in milliseconds
126126
TLSHandshakeTime int64 // Time for the TLS handshake to complete, in milliseconds, or -
127127
ReceivedBytes int64 // Count of bytes received by the load balancer from the client, after decryption
@@ -156,7 +156,7 @@ func convertTextToNlbAccessLogRecord(fields []string) (NlbAccessLogRecord, error
156156
Time: fields[2], // Timestamp
157157
ELB: fields[3], // Load balancer resource ID
158158
Listener: fields[4], // Listener ID
159-
DestinationIpPort: fields[6], // Destination IP and port
159+
DestinationIPPort: fields[6], // Destination IP and port
160160
TLSHandshakeTime: 0, // TLSHandshakeTime placeholder value
161161
IncomingTLSAlert: fields[11], // Incoming TLS alert
162162
ChosenCertARN: fields[12], // Chosen certificate ARN
@@ -172,7 +172,7 @@ func convertTextToNlbAccessLogRecord(fields []string) (NlbAccessLogRecord, error
172172
}
173173

174174
// Processing additional fields if applicable
175-
record.ClientIp = strings.Split(fields[5], ":")[0]
175+
record.ClientIP = strings.Split(fields[5], ":")[0]
176176
if record.ClientPort, err = safeConvertStrToInt(strings.Split(fields[5], ":")[1]); err != nil {
177177
return record, fmt.Errorf("could not convert client port to integer: %w", err)
178178
}
@@ -203,9 +203,9 @@ type AlbAccessLogRecord struct {
203203
Type string // Type of request (http, https, etc.)
204204
Time string // Timestamp the load balancer generated a response to the client in ISO 8601 format
205205
ELB string // Load balancer resource ID
206-
ClientIp string // Client IP
206+
ClientIP string // Client IP
207207
ClientPort int64 // Client port
208-
TargetIpPort string // Target IP and port
208+
TargetIPPort string // Target IP and port
209209
RequestProcessingTime string // Time taken to process the request in seconds
210210
TargetProcessingTime string // Time taken for the target to process the request in seconds
211211
ResponseProcessingTime string // Time taken to send the response to the client in seconds
@@ -246,7 +246,7 @@ func convertTextToAlbAccessLogRecord(fields []string) (AlbAccessLogRecord, error
246246
Type: fields[0],
247247
Time: fields[1],
248248
ELB: fields[2],
249-
TargetIpPort: fields[4],
249+
TargetIPPort: fields[4],
250250
RequestProcessingTime: fields[5],
251251
TargetProcessingTime: fields[6],
252252
ResponseProcessingTime: fields[7],
@@ -268,7 +268,7 @@ func convertTextToAlbAccessLogRecord(fields []string) (AlbAccessLogRecord, error
268268
Classification: fields[27],
269269
ClassificationReason: fields[28],
270270
}
271-
record.ClientIp = strings.Split(fields[3], ":")[0]
271+
record.ClientIP = strings.Split(fields[3], ":")[0]
272272
if record.ClientPort, err = safeConvertStrToInt(strings.Split(fields[3], ":")[1]); err != nil {
273273
return record, fmt.Errorf("could not convert client port to integer: %w", err)
274274
}
@@ -379,7 +379,7 @@ func scanField(logLine string) (string, string, error) {
379379
}
380380

381381
// Remove space after closing quote if present
382-
if len(remaining) > 0 && remaining[0] == ' ' {
382+
if remaining != "" && remaining[0] == ' ' {
383383
remaining = remaining[1:]
384384
}
385385

extension/encoding/awslogsencodingextension/internal/unmarshaler/elb-access-log/fields.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ package elbaccesslogs // import "github.com/open-telemetry/opentelemetry-collect
66
const (
77
AttributeELBStatusCode = "aws.elb.status.code" // int
88
AttributeELBBackendStatusCode = "aws.elb.backend.status.code" // int
9-
AttributeTlsListenerResourceID = "aws.elb.tls.listener.resource_id" // string
9+
AttributeTLSListenerResourceID = "aws.elb.tls.listener.resource_id" // string
1010
)

extension/encoding/awslogsencodingextension/internal/unmarshaler/elb-access-log/unmarshaler.go

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package elbaccesslogs // import "github.com/open-telemetry/opentelemetry-collect
55

66
import (
77
"bufio"
8+
"errors"
89
"fmt"
910
"io"
1011
"strings"
@@ -32,7 +33,7 @@ func NewELBAccessLogUnmarshaler(buildInfo component.BuildInfo, logger *zap.Logge
3233
}
3334

3435
type resourceAttributes struct {
35-
resourceId string
36+
resourceID string
3637
}
3738

3839
// UnmarshalAWSLogs processes a file containing ELB access logs.
@@ -47,7 +48,7 @@ func (f *elbAccessLogUnmarshaler) UnmarshalAWSLogs(reader io.Reader) (plog.Logs,
4748

4849
// Read first line to determine format
4950
if !scanner.Scan() {
50-
return plog.Logs{}, fmt.Errorf("no log lines found")
51+
return plog.Logs{}, errors.New("no log lines found")
5152
}
5253
line = scanner.Text()
5354

@@ -74,23 +75,17 @@ func (f *elbAccessLogUnmarshaler) UnmarshalAWSLogs(reader io.Reader) (plog.Logs,
7475
// Process lines based on determined syntax
7576
switch syntax {
7677
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
8080
}
81-
f.addToAlbAccessLogs(resourceAttr, scopeLogs, record)
8281
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
8684
}
87-
f.addToNlbAccessLogs(resourceAttr, scopeLogs, record)
8885
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
9288
}
93-
f.addToClbAccessLogs(resourceAttr, scopeLogs, record)
9489
default:
9590
return plog.Logs{}, fmt.Errorf("unsupported log syntax: %s", syntax)
9691
}
@@ -102,6 +97,9 @@ func (f *elbAccessLogUnmarshaler) UnmarshalAWSLogs(reader io.Reader) (plog.Logs,
10297

10398
line = scanner.Text()
10499
fields, err = extractFields(line)
100+
if err != nil {
101+
return plog.Logs{}, fmt.Errorf("failed to parse log line: %w", err)
102+
}
105103
if len(fields) == 0 {
106104
return plog.Logs{}, fmt.Errorf("log line has no fields: %s", line)
107105
}
@@ -127,10 +125,20 @@ func (f *elbAccessLogUnmarshaler) createLogs() (plog.Logs, plog.ResourceLogs, pl
127125
}
128126

129127
// setResourceAttributes based on the resourceAttributes
130-
func (f *elbAccessLogUnmarshaler) setResourceAttributes(r *resourceAttributes, logs plog.ResourceLogs) {
128+
func (*elbAccessLogUnmarshaler) setResourceAttributes(r *resourceAttributes, logs plog.ResourceLogs) {
131129
attr := logs.Resource().Attributes()
132130
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
134142
}
135143

136144
// addToClbAccessLogs adds clb record to provided logs based
@@ -146,9 +154,9 @@ func (f *elbAccessLogUnmarshaler) addToClbAccessLogs(resourceAttr *resourceAttri
146154
// Create record log
147155
recordLog := plog.NewLogRecord()
148156
// Set resource id
149-
resourceAttr.resourceId = clbRecord.ELB
157+
resourceAttr.resourceID = clbRecord.ELB
150158
// Populate record attributes
151-
recordLog.Attributes().PutStr(string(conventions.ClientAddressKey), clbRecord.ClientIp)
159+
recordLog.Attributes().PutStr(string(conventions.ClientAddressKey), clbRecord.ClientIP)
152160
recordLog.Attributes().PutStr(string(conventions.HTTPRequestMethodKey), clbRecord.RequestMethod)
153161
recordLog.Attributes().PutStr(string(conventions.URLFullKey), clbRecord.RequestURI)
154162
recordLog.Attributes().PutStr(string(conventions.NetworkProtocolNameKey), clbRecord.ProtocolName)
@@ -176,6 +184,16 @@ func (f *elbAccessLogUnmarshaler) addToClbAccessLogs(resourceAttr *resourceAttri
176184
recordLog.MoveTo(rScope)
177185
}
178186

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+
179197
// addToAlbAccessLogs adds alb record to provided logs based
180198
// on the extracted logs of each resource
181199
func (f *elbAccessLogUnmarshaler) addToAlbAccessLogs(resourceAttr *resourceAttributes, scopeLogs plog.ScopeLogs, albRecord AlbAccessLogRecord) {
@@ -189,11 +207,11 @@ func (f *elbAccessLogUnmarshaler) addToAlbAccessLogs(resourceAttr *resourceAttri
189207
// Create record log
190208
recordLog := plog.NewLogRecord()
191209
// Set resource id
192-
resourceAttr.resourceId = albRecord.ELB
210+
resourceAttr.resourceID = albRecord.ELB
193211
// Populate record attributes
194212
recordLog.Attributes().PutStr(string(conventions.NetworkProtocolNameKey), albRecord.Type)
195213
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)
197215
recordLog.Attributes().PutStr(string(conventions.HTTPRequestMethodKey), albRecord.RequestMethod)
198216
recordLog.Attributes().PutStr(string(conventions.URLFullKey), albRecord.RequestURI)
199217
recordLog.Attributes().PutInt(string(conventions.ClientPortKey), albRecord.ClientPort)
@@ -215,6 +233,16 @@ func (f *elbAccessLogUnmarshaler) addToAlbAccessLogs(resourceAttr *resourceAttri
215233
recordLog.MoveTo(rScope)
216234
}
217235

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+
218246
// addToNlbAccessLogs adds nlb record to provided logs based
219247
// on the extracted logs of each resource
220248
func (f *elbAccessLogUnmarshaler) addToNlbAccessLogs(resourceAttr *resourceAttributes, scopeLogs plog.ScopeLogs, nlbRecord NlbAccessLogRecord) {
@@ -228,15 +256,15 @@ func (f *elbAccessLogUnmarshaler) addToNlbAccessLogs(resourceAttr *resourceAttri
228256
// Create record log
229257
recordLog := plog.NewLogRecord()
230258
// Set resource id
231-
resourceAttr.resourceId = nlbRecord.ELB
259+
resourceAttr.resourceID = nlbRecord.ELB
232260
// Populate record attributes
233261
recordLog.Attributes().PutStr(string(conventions.NetworkProtocolNameKey), nlbRecord.Type)
234262
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)
236264
recordLog.Attributes().PutInt(string(conventions.ClientPortKey), nlbRecord.ClientPort)
237265
recordLog.Attributes().PutInt(string(conventions.HTTPRequestSizeKey), nlbRecord.ReceivedBytes)
238266
recordLog.Attributes().PutInt(string(conventions.HTTPResponseSizeKey), nlbRecord.SentBytes)
239-
recordLog.Attributes().PutStr(AttributeTlsListenerResourceID, nlbRecord.Listener)
267+
recordLog.Attributes().PutStr(AttributeTLSListenerResourceID, nlbRecord.Listener)
240268
recordLog.Attributes().PutStr(string(conventions.TLSProtocolVersionKey), nlbRecord.TLSProtocolVersion)
241269
recordLog.Attributes().PutStr(string(conventions.TLSCipherKey), nlbRecord.TLSCipher)
242270

0 commit comments

Comments
 (0)