Skip to content

Commit ab356f8

Browse files
authored
lint: fix nilaway warnings (part 7) (#3865)
* lint: fix nilaway warnings (part 7) * lint * tidy
1 parent a7fbedf commit ab356f8

File tree

4 files changed

+38
-20
lines changed

4 files changed

+38
-20
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require (
1313
github.com/aws/aws-lambda-go v1.47.0
1414
github.com/aws/aws-sdk-go v1.52.0
1515
github.com/beevik/etree v1.4.1
16-
github.com/blackfireio/osinfo v1.1.0
16+
github.com/blackfireio/osinfo v1.1.0 // indirect
1717
github.com/bluele/gcache v0.0.2
1818
github.com/buger/jsonparser v1.1.1
1919
github.com/cenkalti/backoff/v5 v5.0.2

pkg/acquisition/modules/loki/entry.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package loki
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"strconv"
67
"time"
78
)
@@ -17,6 +18,11 @@ func (e *Entry) UnmarshalJSON(b []byte) error {
1718
if err != nil {
1819
return err
1920
}
21+
22+
if len(values) < 2 {
23+
return fmt.Errorf("invalid Loki entry: expected [timestamp, line], got %v", values)
24+
}
25+
2026
t, err := strconv.Atoi(values[0])
2127
if err != nil {
2228
return err

pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type LokiClient struct {
2525

2626
config Config
2727
t *tomb.Tomb
28-
fail_start time.Time
28+
failStart time.Time
2929
currentTickerInterval time.Duration
3030
requestHeaders map[string]string
3131
}
@@ -48,42 +48,45 @@ type Config struct {
4848
Limit int
4949
}
5050

51-
func updateURI(uri string, lq LokiQueryRangeResponse, infinite bool) string {
52-
u, _ := url.Parse(uri)
51+
func updateURI(uri string, lq LokiQueryRangeResponse, infinite bool) (string, error) {
52+
u, err := url.Parse(uri)
53+
if err != nil {
54+
return "", fmt.Errorf("invalid Loki URL %q: %w", uri, err)
55+
}
5356
queryParams := u.Query()
5457

5558
if len(lq.Data.Result) > 0 {
56-
lastTs := lq.Data.Result[0].Entries[len(lq.Data.Result[0].Entries)-1].Timestamp
59+
lastTS := lq.Data.Result[0].Entries[len(lq.Data.Result[0].Entries)-1].Timestamp
5760
// +1 the last timestamp to avoid getting the same result again.
58-
queryParams.Set("start", strconv.Itoa(int(lastTs.UnixNano()+1)))
61+
queryParams.Set("start", strconv.Itoa(int(lastTS.UnixNano()+1)))
5962
}
6063

6164
if infinite {
6265
queryParams.Set("end", strconv.Itoa(int(time.Now().UnixNano())))
6366
}
6467

6568
u.RawQuery = queryParams.Encode()
66-
return u.String()
69+
return u.String(), nil
6770
}
6871

6972
func (lc *LokiClient) SetTomb(t *tomb.Tomb) {
7073
lc.t = t
7174
}
7275

7376
func (lc *LokiClient) resetFailStart() {
74-
if !lc.fail_start.IsZero() {
75-
log.Infof("loki is back after %s", time.Since(lc.fail_start))
77+
if !lc.failStart.IsZero() {
78+
log.Infof("loki is back after %s", time.Since(lc.failStart))
7679
}
77-
lc.fail_start = time.Time{}
80+
lc.failStart = time.Time{}
7881
}
7982

8083
func (lc *LokiClient) shouldRetry() bool {
81-
if lc.fail_start.IsZero() {
84+
if lc.failStart.IsZero() {
8285
lc.Logger.Warningf("loki is not available, will retry for %s", lc.config.FailMaxDuration)
83-
lc.fail_start = time.Now()
86+
lc.failStart = time.Now()
8487
return true
8588
}
86-
if time.Since(lc.fail_start) > lc.config.FailMaxDuration {
89+
if time.Since(lc.failStart) > lc.config.FailMaxDuration {
8790
lc.Logger.Errorf("loki didn't manage to recover after %s, giving up", lc.config.FailMaxDuration)
8891
return false
8992
}
@@ -171,7 +174,10 @@ func (lc *LokiClient) queryRange(ctx context.Context, uri string, c chan *LokiQu
171174
}
172175
}
173176

174-
uri = updateURI(uri, lq, infinite)
177+
uri, err = updateURI(uri, lq, infinite)
178+
if err != nil {
179+
return fmt.Errorf("querying range: %w", err)
180+
}
175181
}
176182
}
177183
}
@@ -301,7 +307,7 @@ func (lc *LokiClient) QueryRange(ctx context.Context, infinite bool) chan *LokiQ
301307
return c
302308
}
303309

304-
// Create a wrapper for http.Get to be able to set headers and auth
310+
// Get creates a wrapper for http.Get to be able to set headers and auth
305311
func (lc *LokiClient) Get(ctx context.Context, url string) (*http.Response, error) {
306312
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
307313
if err != nil {

pkg/acquisition/modules/loki/internal/lokiclient/types.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package lokiclient
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"strconv"
67
"time"
78
)
@@ -17,6 +18,11 @@ func (e *Entry) UnmarshalJSON(b []byte) error {
1718
if err != nil {
1819
return err
1920
}
21+
22+
if len(values) < 2 {
23+
return fmt.Errorf("invalid Loki entry: expected [timestamp, line], got %v", values)
24+
}
25+
2026
t, err := strconv.Atoi(values[0])
2127
if err != nil {
2228
return err
@@ -37,8 +43,8 @@ type DroppedEntry struct {
3743
}
3844

3945
type LokiResponse struct {
40-
Streams []Stream `json:"streams"`
41-
DroppedEntries []interface{} `json:"dropped_entries"` //We don't care about the actual content i think ?
46+
Streams []Stream `json:"streams"`
47+
DroppedEntries []any `json:"dropped_entries"` //We don't care about the actual content i think ?
4248
}
4349

4450
// LokiQuery GET response.
@@ -49,7 +55,7 @@ type LokiQueryRangeResponse struct {
4955
}
5056

5157
type Data struct {
52-
ResultType string `json:"resultType"`
53-
Result []Stream `json:"result"` // Warning, just stream value is handled
54-
Stats interface{} `json:"stats"` // Stats is boring, just ignore it
58+
ResultType string `json:"resultType"`
59+
Result []Stream `json:"result"` // Warning, just stream value is handled
60+
Stats any `json:"stats"` // Stats is boring, just ignore it
5561
}

0 commit comments

Comments
 (0)