forked from go-extras/elogrus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook.go
283 lines (250 loc) · 8.3 KB
/
hook.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package elogrus
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
"sync"
"time"
"github.com/elastic/go-elasticsearch/v7/esapi"
"github.com/mugambikimathi/elogrus/internal/bulk"
"github.com/opensearch-project/opensearch-go/v2"
"github.com/sirupsen/logrus"
)
var (
// ErrCannotCreateIndex Fired if the index is not created
ErrCannotCreateIndex = fmt.Errorf("cannot create index")
)
// IndexNameFunc get index name
type IndexNameFunc func() string
type fireFunc func(entry *logrus.Entry, hook *ElasticHook) error
// ElasticHook is a logrus
// hook for ElasticSearch
type ElasticHook struct {
client *opensearch.Client
host string
index IndexNameFunc
levels []logrus.Level
ctx context.Context
ctxCancel context.CancelFunc
fireFunc fireFunc
}
type message struct {
Host string `json:"host"`
Timestamp string `json:"@timestamp"`
Message string `json:"message"`
Data logrus.Fields `json:"data"`
Level string `json:"level"`
}
// NewElasticHook creates new hook.
// client - ElasticSearch client with specific es version (v5/v6/v7/...)
// host - host of system
// level - log level
// index - name of the index in ElasticSearch
func NewElasticHook(client *opensearch.Client, host string, level logrus.Level, index string) (*ElasticHook, error) {
return NewElasticHookWithFunc(client, host, level, func() string { return index })
}
// NewAsyncElasticHook creates new hook with asynchronous log.
// client - ElasticSearch client with specific es version (v5/v6/v7/...)
// host - host of system
// level - log level
// index - name of the index in ElasticSearch
func NewAsyncElasticHook(client *opensearch.Client, host string, level logrus.Level, index string) (*ElasticHook, error) {
return NewAsyncElasticHookWithFunc(client, host, level, func() string { return index })
}
// NewBulkProcessorElasticHook creates new hook that uses a bulk processor for indexing.
// client - ElasticSearch client with specific es version (v5/v6/v7/...)
// host - host of system
// level - log level
// index - name of the index in ElasticSearch
func NewBulkProcessorElasticHook(client *opensearch.Client, host string, level logrus.Level, index string) (*ElasticHook, error) {
return NewBulkProcessorElasticHookWithFunc(client, host, level, func() string { return index })
}
// NewElasticHookWithFunc creates new hook with
// function that provides the index name. This is useful if the index name is
// somehow dynamic especially based on time.
// client - ElasticSearch client with specific es version (v5/v6/v7/...)
// host - host of system
// level - log level
// indexFunc - function providing the name of index
func NewElasticHookWithFunc(client *opensearch.Client, host string, level logrus.Level, indexFunc IndexNameFunc) (*ElasticHook, error) {
return newHookFuncAndFireFunc(client, host, level, indexFunc, syncFireFunc)
}
// NewAsyncElasticHookWithFunc creates new asynchronous hook with
// function that provides the index name. This is useful if the index name is
// somehow dynamic especially based on time.
// client - ElasticSearch client with specific es version (v5/v6/v7/...)
// host - host of system
// level - log level
// indexFunc - function providing the name of index
func NewAsyncElasticHookWithFunc(client *opensearch.Client, host string, level logrus.Level, indexFunc IndexNameFunc) (*ElasticHook, error) {
return newHookFuncAndFireFunc(client, host, level, indexFunc, asyncFireFunc)
}
// NewBulkProcessorElasticHookWithFunc creates new hook with
// function that provides the index name. This is useful if the index name is
// somehow dynamic especially based on time that uses a bulk processor for
// indexing.
// client - ElasticSearch client with specific es version (v5/v6/v7/...)
// host - host of system
// level - log level
// indexFunc - function providing the name of index
func NewBulkProcessorElasticHookWithFunc(client *opensearch.Client, host string, level logrus.Level, indexFunc IndexNameFunc) (*ElasticHook, error) {
fireFunc, err := makeBulkFireFunc(client)
if err != nil {
return nil, err
}
return newHookFuncAndFireFunc(client, host, level, indexFunc, fireFunc)
}
func newHookFuncAndFireFunc(client *opensearch.Client, host string, level logrus.Level, indexFunc IndexNameFunc, fireFunc fireFunc) (*ElasticHook, error) {
var levels []logrus.Level
for _, l := range []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
logrus.InfoLevel,
logrus.DebugLevel,
logrus.TraceLevel,
} {
if l <= level {
levels = append(levels, l)
}
}
ctx, cancel := context.WithCancel(context.TODO())
// Use the IndexExists service to check if a specified index exists.
indexExistsResp, err := client.Indices.Exists([]string{indexFunc()})
if err != nil {
// Handle error
cancel()
return nil, err
}
if indexExistsResp.StatusCode == http.StatusNotFound {
createIndexResp, err := client.Indices.Create(indexFunc())
if err != nil || createIndexResp.IsError() {
cancel()
return nil, ErrCannotCreateIndex
}
}
return &ElasticHook{
client: client,
host: host,
index: indexFunc,
levels: levels,
ctx: ctx,
ctxCancel: cancel,
fireFunc: fireFunc,
}, nil
}
// Fire is required to implement
// Logrus hook
func (hook *ElasticHook) Fire(entry *logrus.Entry) error {
return hook.fireFunc(entry, hook)
}
func asyncFireFunc(entry *logrus.Entry, hook *ElasticHook) error {
go func() {
_ = syncFireFunc(entry, hook) // TODO: how can we handle the error?
}()
return nil
}
func createMessage(entry *logrus.Entry, hook *ElasticHook) *message {
level := entry.Level.String()
if e, ok := entry.Data[logrus.ErrorKey]; ok && e != nil {
if err, ok := e.(error); ok {
entry.Data[logrus.ErrorKey] = err.Error()
}
}
return &message{
hook.host,
entry.Time.UTC().Format(time.RFC3339Nano),
entry.Message,
entry.Data,
strings.ToUpper(level),
}
}
func syncFireFunc(entry *logrus.Entry, hook *ElasticHook) error {
data, err := json.Marshal(createMessage(entry, hook))
if err != nil {
return err
}
req := esapi.IndexRequest{
Index: hook.index(),
Body: bytes.NewReader(data),
}
// Perform the request with the client.
res, err := req.Do(context.Background(), hook.client)
if err != nil {
return err
}
defer res.Body.Close()
return err
}
// Create closure with bulk processor tied to fireFunc.
// Note: garbage collector will never be able to free memory allocated for bulkWriters
func makeBulkFireFunc(client *opensearch.Client) (fireFunc, error) {
bulkWriters := make(map[*ElasticHook]*bulk.Writer)
var lock sync.RWMutex
getWriter := func(hook *ElasticHook) *bulk.Writer {
lock.RLock()
writer := bulkWriters[hook]
lock.RUnlock()
if writer != nil {
return writer
}
lock.Lock()
writer = bulkWriters[hook]
if writer != nil { // this is a second check to avoid sequential writes
lock.Unlock()
return writer
}
// long path, create a new writer
writer = bulk.NewBulkWriterWithErrorHandler(time.Second, func(data []byte) error {
res, err := client.Bulk(bytes.NewReader(data),
client.Bulk.WithIndex(hook.index()),
)
if err != nil {
return err
}
defer res.Body.Close()
if res.IsError() {
raw := make(map[string]interface{})
if err := json.NewDecoder(res.Body).Decode(&raw); err != nil {
return fmt.Errorf("failure to to parse response body: %s", err.Error())
} else {
return fmt.Errorf("error: [%d] %s: %s",
res.StatusCode,
raw["error"].(map[string]interface{})["type"],
raw["error"].(map[string]interface{})["reason"],
)
}
// A successful response might still contain errors for particular documents...
//
}
return nil
}, func(data []byte, err error) {
// TODO: how to handle the error??
// panic(fmt.Sprintf("error: %s", err))
})
bulkWriters[hook] = writer
lock.Unlock()
return writer
}
return func(entry *logrus.Entry, hook *ElasticHook) error {
data, err := json.Marshal(createMessage(entry, hook))
if err != nil {
return err
}
data = append([]byte(`{"index":{}}`+"\n"), data...)
_, _ = getWriter(hook).Write(append(data, '\n'))
return nil
}, nil
}
// Levels Required for logrus hook implementation
func (hook *ElasticHook) Levels() []logrus.Level {
return hook.levels
}
// Cancel all calls to elastic
func (hook *ElasticHook) Cancel() {
hook.ctxCancel()
}