forked from thanapolr/sdhook
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sdhook.go
390 lines (337 loc) · 10.8 KB
/
sdhook.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// Package sdhook provides a logrus compatible logging hook for Google
// Stackdriver logging.
package sdhook
import (
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/livesession/opentelemetry-go/api/trace"
"golang.org/x/time/rate"
"github.com/facebookgo/stack"
"github.com/fluent/fluent-logger-golang/fluent"
"github.com/sirupsen/logrus"
errorReporting "google.golang.org/api/clouderrorreporting/v1beta1"
logging "google.golang.org/api/logging/v2"
)
const (
// DefaultName is the default name passed to LogName when using service
// account credentials.
DefaultName = "default"
)
// TODO: batch, group and buffer message if limit exceeded
// StackdriverHook provides a logrus hook to Google Stackdriver logging.
type StackdriverHook struct {
// levels are the levels that logrus will hook to.
levels []logrus.Level
// projectID is the projectID
projectID string
// service is the logging service.
service *logging.EntriesService
// service is the error reporting service.
errorService *errorReporting.Service
// resource is the monitored resource.
resource *logging.MonitoredResource
// logName is the name of the log.
logName string
// labels are the labels to send with each log entry.
labels map[string]string
// partialSuccess allows partial writes of log entries if there is a badly
// formatted log.
partialSuccess bool
// agentClient defines the fluentd logger object that can send data to
// to the Google logging agent.
agentClient *fluent.Fluent
// errorReportingServiceName defines the value of the field <service>,
// required for a valid error reporting payload. If this value is set,
// messages where level/severity is higher than or equal to "error" will
// be sent to Stackdriver error reporting.
// See more at:
// https://cloud.google.com/error-reporting/docs/formatting-error-messages
errorReportingServiceName string
// errorReportingLogName is the name of the log for error reporting.
// It must contain the string "error"
// If not given, the string "<logName>_error" is used.
errorReportingLogName string
// waitGroup holds counters for each subroutine fired
waitGroup sync.WaitGroup
// limiterBurst is x events at once
limiterBurst int
// limiterLimit is x events per second
limiterLimit int
limiter *rate.Limiter
}
// New creates a StackdriverHook using the provided options that is suitible
// for using with logrus for logging to Google Stackdriver.
func New(opts ...Option) (*StackdriverHook, error) {
var err error
sh := &StackdriverHook{
levels: logrus.AllLevels,
limiterBurst: 100,
limiterLimit: 1000,
}
// apply opts
for _, o := range opts {
err = o(sh)
if err != nil {
return nil, err
}
}
// check service, resource, logName set
if sh.service == nil && sh.agentClient == nil {
return nil, errors.New("no stackdriver service was provided")
}
if sh.resource == nil && sh.agentClient == nil {
return nil, errors.New("the monitored resource was not provided")
}
if sh.projectID == "" && sh.agentClient == nil {
return nil, errors.New("the project id was not provided")
}
// set default project name
if sh.logName == "" {
err = LogName(DefaultName)(sh)
if err != nil {
return nil, err
}
}
// If error reporting log name not set, set it to log name
// plus string suffix
if sh.errorReportingLogName == "" {
sh.errorReportingLogName = sh.logName + "_errors"
}
sh.limiter = rate.NewLimiter(rate.Limit(sh.limiterLimit), sh.limiterBurst)
return sh, nil
}
func isError(entry *logrus.Entry) bool {
if entry != nil {
switch entry.Level {
case logrus.ErrorLevel:
return true
case logrus.FatalLevel:
return true
case logrus.PanicLevel:
return true
}
}
return false
}
// Levels returns the logrus levels that this hook is applied to. This can be
// set using the Levels Option.
func (sh *StackdriverHook) Levels() []logrus.Level {
return sh.levels
}
// Fire writes the message to the Stackdriver entry service.
func (sh *StackdriverHook) Fire(entry *logrus.Entry) error {
if !sh.limiter.Allow() {
return nil
}
sh.waitGroup.Add(1)
go func(entry *logrus.Entry) {
defer sh.waitGroup.Done()
var httpReq *logging.HttpRequest
// convert entry data to labels
labels := make(map[string]string, len(entry.Data))
for k, v := range entry.Data {
switch x := v.(type) {
case string:
labels[k] = x
case *http.Request:
httpReq = &logging.HttpRequest{
Referer: x.Referer(),
RemoteIp: x.RemoteAddr,
RequestMethod: x.Method,
RequestUrl: x.URL.String(),
UserAgent: x.UserAgent(),
}
case *logging.HttpRequest:
httpReq = x
default:
labels[k] = fmt.Sprintf("%v", v)
}
}
// write log entry
if sh.agentClient != nil {
sh.sendLogMessageViaAgent(entry, labels, httpReq)
} else {
sh.sendLogMessageViaAPI(entry, labels, httpReq)
}
}(sh.copyEntry(entry))
return nil
}
// Wait will return after all subroutines have returned.
// Use in conjunction with logrus return handling to ensure all of
// your logs are delivered before your program exits.
// `logrus.RegisterExitHandler(h.Wait)`
func (sh *StackdriverHook) Wait() {
sh.waitGroup.Wait()
}
func (sh *StackdriverHook) copyEntry(entry *logrus.Entry) *logrus.Entry {
e := *entry
e.Data = make(logrus.Fields, len(entry.Data))
for k, v := range entry.Data {
e.Data[k] = v
}
return &e
}
func (sh *StackdriverHook) sendLogMessageViaAgent(entry *logrus.Entry, labels map[string]string, httpReq *logging.HttpRequest) {
// The log entry payload schema is defined by the Google fluentd
// logging agent. See more at:
// https://github.com/GoogleCloudPlatform/fluent-plugin-google-cloud
logEntry := map[string]interface{}{
"severity": severityString(entry.Level),
"timestampSeconds": strconv.FormatInt(entry.Time.Unix(), 10),
"timestampNanos": strconv.FormatInt(entry.Time.UnixNano()-entry.Time.Unix()*1000000000, 10),
"message": entry.Message,
}
for k, v := range labels {
logEntry[k] = v
}
if httpReq != nil {
logEntry["httpRequest"] = httpReq
}
// The error reporting payload JSON schema is defined in:
// https://cloud.google.com/error-reporting/docs/formatting-error-messages
// Which reflects the structure of the ErrorEvent type in:
// https://godoc.org/google.golang.org/api/clouderrorreporting/v1beta1
if sh.errorReportingServiceName != "" && isError(entry) {
errorEvent := sh.buildErrorReportingEvent(entry, labels, httpReq)
errorStructPayload, err := json.Marshal(errorEvent)
if err != nil {
log.Printf("error marshaling error reporting data: %s", err.Error())
}
var errorJSONPayload map[string]interface{}
err = json.Unmarshal(errorStructPayload, &errorJSONPayload)
if err != nil {
log.Printf("error parsing error reporting data: %s", err.Error())
}
for k, v := range logEntry {
errorJSONPayload[k] = v
}
if err := sh.agentClient.Post(sh.errorReportingLogName, errorJSONPayload); err != nil {
log.Printf("error posting error reporting entries to logging agent: %s", err.Error())
}
} else {
if err := sh.agentClient.Post(sh.logName, logEntry); err != nil {
log.Printf("error posting log entries to logging agent: %s", err.Error())
}
}
}
func HexToString(id string) string {
bs, err := hex.DecodeString(id)
if err != nil {
return ""
}
return string(bs)
}
func (sh *StackdriverHook) sendLogMessageViaAPI(entry *logrus.Entry, labels map[string]string, httpReq *logging.HttpRequest) {
if sh.errorReportingServiceName != "" && isError(entry) {
errorEvent := sh.buildErrorReportingEvent(entry, labels, httpReq)
if sh != nil && sh.errorService != nil && sh.errorService.Projects != nil && sh.errorService.Projects.Events != nil {
_, err := sh.errorService.Projects.Events.Report("projects/"+sh.projectID, &errorEvent).Do()
if err != nil {
log.Println("cannot report event:", err)
}
} else {
log.Println("the error reporting service is not set")
}
} else {
logName := sh.logName
if sh.errorReportingLogName != "" && isError(entry) {
logName = sh.errorReportingLogName
}
ctx := entry.Context
var span trace.Span
if ctx != nil {
span = trace.SpanFromContext(ctx)
} else {
span = trace.SpanFromContext(context.Background())
}
var traceID string
if span.SpanContext().TraceID.IsValid() {
// TODO: what if we don't want parse to ascii
traceID = fmt.Sprintf("projects/%s/traces/%s", sh.projectID, HexToString(span.SpanContext().TraceID.String()))
}
var spanID string
if span.SpanContext().SpanID.IsValid() {
spanID = span.SpanContext().SpanID.String()
}
logEntry := &logging.LogEntry{
Severity: severityString(entry.Level),
Timestamp: entry.Time.Format(time.RFC3339),
HttpRequest: httpReq,
SpanId: spanID,
Trace: traceID,
}
entryWithMessage := entry.WithField("msg", entry.Message)
if jsonPayload, err := json.Marshal(entryWithMessage.Data); err == nil {
logEntry.JsonPayload = jsonPayload
} else {
logEntry.TextPayload = entry.Message
logEntry.Labels = labels
}
_, err := sh.service.Write(&logging.WriteLogEntriesRequest{
LogName: logName,
Resource: sh.resource,
Labels: sh.labels,
PartialSuccess: sh.partialSuccess,
Entries: []*logging.LogEntry{logEntry},
}).Do()
if err != nil {
log.Println("cannot deliver log entry:", err)
}
}
}
func (sh *StackdriverHook) buildErrorReportingEvent(entry *logrus.Entry, labels map[string]string, httpReq *logging.HttpRequest) errorReporting.ReportedErrorEvent {
errorEvent := errorReporting.ReportedErrorEvent{
EventTime: entry.Time.Format(time.RFC3339),
Message: entry.Message,
ServiceContext: &errorReporting.ServiceContext{
Service: sh.errorReportingServiceName,
Version: labels["version"],
},
Context: &errorReporting.ErrorContext{
User: labels["user"],
},
}
// Assumes that caller stack frame information of type
// github.com/facebookgo/stack.Frame has been added.
// Possibly via a library like github.com/Gurpartap/logrus-stack
if entry.Data["caller"] != nil {
caller := entry.Data["caller"].(stack.Frame)
errorEvent.Context.ReportLocation = &errorReporting.SourceLocation{
FilePath: caller.File,
FunctionName: caller.Name,
LineNumber: int64(caller.Line),
}
}
if httpReq != nil {
errRepHttpRequest := &errorReporting.HttpRequestContext{
Method: httpReq.RequestMethod,
Referrer: httpReq.Referer,
RemoteIp: httpReq.RemoteIp,
Url: httpReq.RequestUrl,
UserAgent: httpReq.UserAgent,
}
errorEvent.Context.HttpRequest = errRepHttpRequest
}
return errorEvent
}
func severityString(l logrus.Level) string {
switch l {
case logrus.FatalLevel:
return "critical"
case logrus.PanicLevel:
return "emergency"
case logrus.TraceLevel:
return "debug"
default:
return strings.ToUpper(l.String())
}
}