@@ -25,7 +25,7 @@ type LokiClient struct {
25
25
26
26
config Config
27
27
t * tomb.Tomb
28
- fail_start time.Time
28
+ failStart time.Time
29
29
currentTickerInterval time.Duration
30
30
requestHeaders map [string ]string
31
31
}
@@ -48,42 +48,45 @@ type Config struct {
48
48
Limit int
49
49
}
50
50
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
+ }
53
56
queryParams := u .Query ()
54
57
55
58
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
57
60
// +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 )))
59
62
}
60
63
61
64
if infinite {
62
65
queryParams .Set ("end" , strconv .Itoa (int (time .Now ().UnixNano ())))
63
66
}
64
67
65
68
u .RawQuery = queryParams .Encode ()
66
- return u .String ()
69
+ return u .String (), nil
67
70
}
68
71
69
72
func (lc * LokiClient ) SetTomb (t * tomb.Tomb ) {
70
73
lc .t = t
71
74
}
72
75
73
76
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 ))
76
79
}
77
- lc .fail_start = time.Time {}
80
+ lc .failStart = time.Time {}
78
81
}
79
82
80
83
func (lc * LokiClient ) shouldRetry () bool {
81
- if lc .fail_start .IsZero () {
84
+ if lc .failStart .IsZero () {
82
85
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 ()
84
87
return true
85
88
}
86
- if time .Since (lc .fail_start ) > lc .config .FailMaxDuration {
89
+ if time .Since (lc .failStart ) > lc .config .FailMaxDuration {
87
90
lc .Logger .Errorf ("loki didn't manage to recover after %s, giving up" , lc .config .FailMaxDuration )
88
91
return false
89
92
}
@@ -171,7 +174,10 @@ func (lc *LokiClient) queryRange(ctx context.Context, uri string, c chan *LokiQu
171
174
}
172
175
}
173
176
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
+ }
175
181
}
176
182
}
177
183
}
@@ -301,7 +307,7 @@ func (lc *LokiClient) QueryRange(ctx context.Context, infinite bool) chan *LokiQ
301
307
return c
302
308
}
303
309
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
305
311
func (lc * LokiClient ) Get (ctx context.Context , url string ) (* http.Response , error ) {
306
312
request , err := http .NewRequestWithContext (ctx , http .MethodGet , url , http .NoBody )
307
313
if err != nil {
0 commit comments