mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-15 22:04:03 +02:00
* Add support for loki datasource --------- Co-authored-by: Mathieu Lecarme <mathieu@garambrogne.net> Co-authored-by: Sebastien Blot <sebastien@crowdsec.net> Co-authored-by: Thibault "bui" Koechlin <thibault@crowdsec.net>
60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
package loki
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
type Entry struct {
|
|
Timestamp time.Time
|
|
Line string
|
|
}
|
|
|
|
func (e *Entry) UnmarshalJSON(b []byte) error {
|
|
var values []string
|
|
err := json.Unmarshal(b, &values)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
t, err := strconv.Atoi(values[0])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
e.Timestamp = time.Unix(int64(t), 0)
|
|
e.Line = values[1]
|
|
return nil
|
|
}
|
|
|
|
type Stream struct {
|
|
Stream map[string]string `json:"stream"`
|
|
Entries []Entry `json:"values"`
|
|
}
|
|
|
|
type DroppedEntry struct {
|
|
Labels map[string]string `json:"labels"`
|
|
Timestamp time.Time `json:"timestamp"`
|
|
}
|
|
|
|
type Tail struct {
|
|
Streams []Stream `json:"streams"`
|
|
DroppedEntries []DroppedEntry `json:"dropped_entries"`
|
|
}
|
|
|
|
// LokiQuery GET response.
|
|
// See https://grafana.com/docs/loki/latest/api/#get-lokiapiv1query
|
|
type LokiQuery struct {
|
|
Status string `json:"status"`
|
|
Data Data `json:"data"`
|
|
}
|
|
|
|
type Data struct {
|
|
ResultType string `json:"resultType"`
|
|
Result []StreamResult `json:"result"` // Warning, just stream value is handled
|
|
Stats interface{} `json:"stats"` // Stats is boring, just ignore it
|
|
}
|
|
|
|
type StreamResult struct {
|
|
Stream map[string]string `json:"stream"`
|
|
Values []Entry `json:"values"`
|
|
}
|