forked from smartcontractkit/wasp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loki_client.go
114 lines (102 loc) · 2.81 KB
/
loki_client.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package wasp
import (
"encoding/json"
"fmt"
"os"
"time"
"github.com/cortexproject/cortex/pkg/util/flagext"
lokiClient "github.com/grafana/loki/pkg/promtail/client"
"github.com/prometheus/common/config"
"github.com/prometheus/common/model"
"github.com/rs/zerolog/log"
)
type LocalLogger struct{}
func (m *LocalLogger) Log(kvars ...interface{}) error {
for _, v := range kvars {
log.Debug().Interface("Key", v).Msg("Loki client internal log")
}
return nil
}
// LokiClient is a Loki/Promtail client wrapper
type LokiClient struct {
lokiClient.Client
}
// Handle handles adding a new label set and a message to the batch
func (m *LokiClient) Handle(ls model.LabelSet, t time.Time, s string) error {
log.Debug().
Interface("Labels", ls).
Time("Time", t).
Str("Data", s).
Msg("Sending data to Loki")
return m.Client.Handle(ls, t, s)
}
// HandleStruct handles adding a new label set and a message to the batch, marshalling JSON from struct
func (m *LokiClient) HandleStruct(ls model.LabelSet, t time.Time, st interface{}) error {
d, err := json.Marshal(st)
if err != nil {
return fmt.Errorf("failed to marshal struct in response: %v", st)
}
log.Debug().
Interface("Labels", ls).
Time("Time", t).
Str("Data", string(d)).
Msg("Sending data to Loki")
return m.Client.Handle(ls, t, string(d))
}
// Stop stops the client goroutine
func (m *LokiClient) Stop() {
m.Client.Stop()
}
// LokiConfig Loki/Promtail client configuration
type LokiConfig struct {
// URL url to Loki endpoint
URL string `yaml:"url"`
// Token is Loki authorization token
Token string `yaml:"token"`
// BatchWait max time to wait until sending a new batch
BatchWait time.Duration `yaml:"batch_wait"`
// BatchSize size of a messages batch
BatchSize int `yaml:"batch_size"`
// Timeout is a batch send timeout
Timeout time.Duration `yaml:"timeout"`
}
func NewDefaultLokiConfig(url string, token string) *LokiConfig {
return &LokiConfig{
URL: url,
Token: token,
BatchWait: 5 * time.Second,
BatchSize: 500 * 1024,
Timeout: 20 * time.Second,
}
}
func NewEnvLokiConfig() *LokiConfig {
return &LokiConfig{
URL: os.Getenv("LOKI_URL"),
Token: os.Getenv("LOKI_TOKEN"),
BatchWait: 5 * time.Second,
BatchSize: 500 * 1024,
Timeout: 20 * time.Second,
}
}
// NewLokiClient creates a new Loki/Promtail client
func NewLokiClient(extCfg *LokiConfig) (*LokiClient, error) {
serverURL := flagext.URLValue{}
err := serverURL.Set(extCfg.URL)
if err != nil {
return nil, err
}
cfg := lokiClient.Config{
URL: serverURL,
BatchWait: extCfg.BatchWait,
BatchSize: extCfg.BatchSize,
Timeout: extCfg.Timeout,
Client: config.HTTPClientConfig{BearerToken: config.Secret(extCfg.Token)},
}
c, err := lokiClient.New(cfg, &LocalLogger{})
if err != nil {
return nil, err
}
return &LokiClient{
Client: c,
}, nil
}