-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgelflog.go
165 lines (150 loc) · 4.29 KB
/
gelflog.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
package traefik_gelf_plugin
import (
"context"
"encoding/json"
"fmt"
"github.com/kjk/betterguid"
"gopkg.in/Graylog2/go-gelf.v2/gelf"
"log"
"net/http"
"os"
"reflect"
"time"
)
// Config holds the plugin configuration.
type Config struct {
GelfEndpoint string `json:"gelfEndpoint,omitempty"`
GelfPort int `json:"gelfPort,omitempty"`
HostnameOverride string `json:"hostnameOverride"`
EmitTraceId bool `json:"emitTraceId"`
TraceIdHeader string `json:"traceIdHeader"`
EmitRequestStart bool `json:"emitRequestStart"`
RequestStartTimeHeader string `json:"requestStartTimeHeader"`
Debug bool `json:"debug,omitempty"`
}
// CreateConfig creates and initializes the plugin configuration.
func CreateConfig() *Config {
return &Config{
EmitTraceId: true,
TraceIdHeader: "X-TraceId",
EmitRequestStart: true,
RequestStartTimeHeader: "X-Request-Start",
Debug: false,
}
}
type GelfLog struct {
Name string
Next http.Handler
Config *Config
GelfHostname string
GelfWriter *gelf.UDPWriter
}
// New creates and returns a plugin instance.
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
log.Println("DEV MODE ENABLED!")
tLog := &GelfLog{
Name: name,
Next: next,
Config: config,
}
s, err := json.MarshalIndent(ctx, "", "\t")
log.Println("Plugin Context")
if err != nil {
log.Println("Error printing context", err)
}else {
log.Println(string(s))
}
if config == nil {
return nil, fmt.Errorf("config can not be empty")
}
if config.GelfEndpoint == "" {
return nil, fmt.Errorf("you must specify a GELF compatibile endpoint")
}
if config.GelfPort == 0 || config.GelfPort > 65353 {
return nil, fmt.Errorf("you must specify a valid port")
}
if config.HostnameOverride == "" {
tLog.GelfHostname, _ = os.Hostname()
} else {
tLog.GelfHostname = config.HostnameOverride
}
tLog.GelfWriter, _ = gelf.NewUDPWriter(fmt.Sprintf("%s:%d", config.GelfEndpoint, config.GelfPort))
if config.Debug {
log.Println(fmt.Sprintf("Debug Logging Enabled"), config)
var configMap = map[string]interface{}{}
v := reflect.ValueOf(config)
typeOfS := v.Elem().Type()
for i := 0; i< v.Elem().NumField(); i++ {
configMap[typeOfS.Field(i).Name] = v.Elem().Field(i).Interface()
}
tLog.GelfWriter.WriteMessage(wrapMessage("Logger Initialized", "Logger successfully initialized", tLog.GelfHostname, configMap))
log.Println("Sent message to GELF Endpoint")
}
return tLog, nil
}
func (h *GelfLog) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
id := betterguid.New()
if h.Config.EmitRequestStart {
req.Header.Set(h.Config.RequestStartTimeHeader, fmt.Sprint(makeTimestampMilli()))
}
if h.Config.EmitTraceId {
req.Header.Set(h.Config.TraceIdHeader, fmt.Sprint(id))
}
if h.GelfWriter != nil {
var headerMap = map[string]interface{}{}
for str, val := range req.Header {
for index, iVal := range val {
headerName := str
if index > 0 {
headerName = fmt.Sprintf("%s_%d", str, index)
}
headerMap[headerName] = iVal
}
}
headerMap["Host"] = req.Host
message := wrapMessage(fmt.Sprintf("Request to %s", req.Host), fmt.Sprintf("Request to %s", req.Host), h.GelfHostname, headerMap)
err := h.GelfWriter.WriteMessage(message)
if h.Config.Debug {
log.Println("Sent message to GELF Endpoint", err)
log.Println("Request Context")
s, err := json.MarshalIndent(req.Context(), "", "\t")
s1, err1 := json.MarshalIndent(context.Background(), "", "\t")
if err != nil {
log.Println("Error printing context", err)
}else {
log.Println(string(s))
}
log.Println("Background Context")
if err1 != nil {
log.Println("Error printing background context", err)
}else {
log.Println(string(s1))
}
}
}
h.Next.ServeHTTP(rw, req)
}
func wrapMessage(s string, f string, h string, ex map[string]interface{}) *gelf.Message {
/*
Level is a standard syslog level
Facility is deprecated
Line is deprecated
File is deprecated
*/
m := &gelf.Message{
Version: "1.1",
Host: h,
Short: s,
Full: f,
TimeUnix: float64(time.Now().Unix()),
Level: 5,
Extra: ex,
}
return m
}
func unixMilli(t time.Time) int64 {
return t.Round(time.Millisecond).UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond))
}
func makeTimestampMilli() int64 {
return unixMilli(time.Now())
}