-
Notifications
You must be signed in to change notification settings - Fork 0
/
loki.go
152 lines (117 loc) · 2.93 KB
/
loki.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package loki
import (
"crypto/md5"
"errors"
"fmt"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"go.uber.org/zap"
"io"
"net/url"
)
func init() {
caddy.RegisterModule(LokiLogger{})
}
type LokiLogger struct {
Endpoint string
Labels map[string]interface{}
Repl *caddy.Replacer
Logger *zap.Logger
}
func (l LokiLogger) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "caddy.logging.writers.loki",
New: func() caddy.Module {
return new(LokiLogger)
},
}
}
var (
ErrEmptyEndpoint = errors.New("endpoint url is required")
ErrInvalidEndpointPlaceholders = errors.New("invalid endpoint placeholders")
ErrInvalidEndpointUrl = errors.New("invalid endpoint url")
ErrInvalidLabelValReplacement = errors.New("invalid label value placeholder")
)
func (l *LokiLogger) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
if l.Labels == nil {
l.Labels = make(map[string]interface{})
}
for d.Next() {
if !d.NextArg() {
return d.ArgErr()
}
l.Endpoint = d.Val()
for nesting := d.Nesting(); d.NextBlock(nesting); {
if d.Val() == "label" {
if d.CountRemainingArgs() == 2 {
args := d.RemainingArgs()
label := args[0]
value := args[1]
l.Labels[label] = value
continue
}
for nesting := d.Nesting(); d.NextBlock(nesting); {
label := d.Val()
if !d.NextArg() {
return d.ArgErr()
}
l.Labels[label] = d.ScalarVal()
}
}
}
}
return nil
}
func (l LokiLogger) String() string {
u, _ := url.Parse(l.Endpoint)
return u.Redacted()
}
func (l LokiLogger) WriterKey() string {
h := md5.New()
io.WriteString(h, l.Endpoint)
io.WriteString(h, fmt.Sprintf("%s", l.Labels))
return fmt.Sprintf("loki:%x", h.Sum(nil))
}
func (l *LokiLogger) OpenWriter() (io.WriteCloser, error) {
return NewLokiWriter(l.Endpoint, l.Labels, l.Logger), nil
}
func (l *LokiLogger) Validate() error {
if l.Endpoint == "" {
return ErrEmptyEndpoint
}
replacedUrl, err := l.Repl.ReplaceOrErr(l.Endpoint, true, true)
if err != nil {
return errors.Join(ErrInvalidEndpointPlaceholders, err)
}
if _, err := url.Parse(replacedUrl); err != nil {
return errors.Join(ErrInvalidEndpointUrl, err)
}
for _, v := range l.Labels {
vStr, ok := v.(string)
if !ok {
continue
}
_, err := l.Repl.ReplaceOrErr(vStr, false, true)
if err != nil {
return errors.Join(ErrInvalidLabelValReplacement, err)
}
}
return nil
}
func (l *LokiLogger) Provision(context caddy.Context) error {
repl, ok := context.Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
if !ok {
repl = caddy.NewReplacer()
}
l.Repl = repl
l.Logger = context.Logger(l)
return nil
}
// Interface guards.
var (
_ caddy.Module = (*LokiLogger)(nil)
_ caddy.Provisioner = (*LokiLogger)(nil)
_ caddy.Validator = (*LokiLogger)(nil)
_ caddy.WriterOpener = (*LokiLogger)(nil)
_ caddyfile.Unmarshaler = (*LokiLogger)(nil)
)