Paginate Loki query.
This commit is contained in:
parent
a81d52a1ff
commit
d7a1c6a938
|
@ -228,13 +228,21 @@ func (l *LokiSource) OneShotAcquisition(out chan types.Event, t *tomb.Tomb) erro
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "error while getting OneShotAcquisition")
|
return errors.Wrap(err, "error while getting OneShotAcquisition")
|
||||||
}
|
}
|
||||||
// FIXME: Paginate the responses with a loop
|
|
||||||
// See /usr/local/etc/grafana/grafana.ini
|
// See https://grafana.com/docs/loki/latest/api/#get-lokiapiv1query_range
|
||||||
params := &url.Values{}
|
params := &url.Values{}
|
||||||
params.Set("query", l.Config.Query)
|
params.Set("query", l.Config.Query)
|
||||||
params.Set("direction", "forward")
|
params.Set("direction", "forward") // FIXME
|
||||||
params.Set("since", time.Time(l.Config.Since).Format(time.RFC3339))
|
params.Set("limit", fmt.Sprintf("%d", lokiLimit))
|
||||||
params.Set("limit", "1000") // FIXME
|
params.Set("end", time.Now().Format(time.RFC3339))
|
||||||
|
start := time.Time(l.Config.Since)
|
||||||
|
|
||||||
|
var lq LokiQuery
|
||||||
|
defer t.Kill(nil)
|
||||||
|
defer l.logger.Info("Loki queried")
|
||||||
|
|
||||||
|
for {
|
||||||
|
params.Set("start", start.Format(time.RFC3339))
|
||||||
url := fmt.Sprintf("%s/loki/api/v1/query_range?%s",
|
url := fmt.Sprintf("%s/loki/api/v1/query_range?%s",
|
||||||
l.Config.URL,
|
l.Config.URL,
|
||||||
params.Encode())
|
params.Encode())
|
||||||
|
@ -253,25 +261,28 @@ func (l *LokiSource) OneShotAcquisition(out chan types.Event, t *tomb.Tomb) erro
|
||||||
logger.WithField("status", resp.StatusCode).Error(string(msg))
|
logger.WithField("status", resp.StatusCode).Error(string(msg))
|
||||||
return fmt.Errorf("Loki query return bad status : %d", resp.StatusCode)
|
return fmt.Errorf("Loki query return bad status : %d", resp.StatusCode)
|
||||||
}
|
}
|
||||||
var lq LokiQuery
|
|
||||||
/*
|
|
||||||
fmt.Println(url)
|
|
||||||
msg, _ := io.ReadAll(resp.Body)
|
|
||||||
fmt.Println(string(msg))
|
|
||||||
*/
|
|
||||||
decoder := json.NewDecoder(resp.Body)
|
decoder := json.NewDecoder(resp.Body)
|
||||||
err = decoder.Decode(&lq)
|
err = decoder.Decode(&lq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "can't parse JSON loki response")
|
return errors.Wrap(err, "can't parse JSON loki response")
|
||||||
}
|
}
|
||||||
|
if len(lq.Data.Result) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
for _, result := range lq.Data.Result {
|
for _, result := range lq.Data.Result {
|
||||||
|
if len(result.Values) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
start = result.Values[0].Timestamp
|
||||||
logger.WithField("stream", result.Stream).Debug("Results", len(result.Values))
|
logger.WithField("stream", result.Stream).Debug("Results", len(result.Values))
|
||||||
for _, entry := range result.Values {
|
for _, entry := range result.Values {
|
||||||
l.readOneEntry(entry, result.Stream, out)
|
l.readOneEntry(entry, result.Stream, out)
|
||||||
}
|
}
|
||||||
|
if len(result.Values) <= lokiLimit {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
logger.Info("Loki queried")
|
|
||||||
t.Kill(nil)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue