/
logrusloki.go
94 lines (77 loc) · 1.85 KB
/
logrusloki.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package logging
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"time"
"github.com/ethersphere/beekeeper/pkg/logging/loki"
"github.com/sirupsen/logrus"
)
type LokiHook struct {
hostname string
lokiEndpoint string
httpclient *http.Client
}
func newLoki(lokiEndpoint string) LokiHook {
hostname, err := os.Hostname()
if err != nil {
hostname = "unknown"
}
return LokiHook{
hostname: hostname,
lokiEndpoint: lokiEndpoint,
httpclient: &http.Client{Timeout: 5 * time.Second},
}
}
func (l LokiHook) Levels() []logrus.Level {
return []logrus.Level{
logrus.ErrorLevel,
logrus.WarnLevel,
logrus.InfoLevel,
logrus.DebugLevel,
logrus.TraceLevel,
}
}
func (l LokiHook) Fire(entry *logrus.Entry) error {
stream := loki.NewStream()
msg, err := entry.Logger.Formatter.Format(entry)
if err != nil {
return fmt.Errorf("loki format failed: %s", err.Error())
}
stream.AddEntry(entry.Time, string(msg))
stream.AddLabel("hostname", l.hostname)
batch := loki.NewBatch()
batch.AddStream(stream)
err = l.executeHTTPRequest(batch)
if err != nil {
return fmt.Errorf("loki request failed: %s", err.Error())
}
return nil
}
func (l LokiHook) executeHTTPRequest(batch *loki.Batch) error {
data, err := json.Marshal(batch)
if err != nil {
return err
}
req, err := http.NewRequest("POST", l.lokiEndpoint, bytes.NewReader(data))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := l.httpclient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
data, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("error reading response (%s): %s", resp.Status, err.Error())
}
return fmt.Errorf("error posting loki batch (%s): %s", resp.Status, string(data))
}
return err
}