-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
helper.go
172 lines (145 loc) · 4.35 KB
/
helper.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
package logrusx
import (
"context"
"errors"
"fmt"
"net/http"
"reflect"
"strings"
"github.com/gobuffalo/pop/v5/logging"
"github.com/sirupsen/logrus"
"go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace"
"go.opentelemetry.io/otel/propagation"
"github.com/ory/x/errorsx"
)
type Logger struct {
*logrus.Entry
leakSensitive bool
opts []Option
name string
version string
}
var opts = otelhttptrace.WithPropagators(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
func (l *Logger) LeakSensitiveData() bool {
return l.leakSensitive
}
func (l *Logger) Logrus() *logrus.Logger {
return l.Entry.Logger
}
func (l *Logger) NewEntry() *Logger {
ll := *l
ll.Entry = logrus.NewEntry(l.Logger)
return &ll
}
func (l *Logger) WithContext(ctx context.Context) *Logger {
ll := *l
ll.Entry = l.Logger.WithContext(ctx)
return &ll
}
func (l *Logger) WithRequest(r *http.Request) *Logger {
headers := map[string]interface{}{}
if ua := r.UserAgent(); len(ua) > 0 {
headers["user-agent"] = ua
}
if cookie := l.maybeRedact(r.Header.Get("Cookie")); cookie != nil {
headers["cookie"] = cookie
}
if auth := l.maybeRedact(r.Header.Get("Authorization")); auth != nil {
headers["authorization"] = auth
}
for key, value := range r.Header {
if strings.ToLower(key) == "cookie" ||
strings.ToLower(key) == "authorization" {
continue
}
headers[strings.ToLower(key)] = value
}
scheme := "https"
if r.TLS == nil {
scheme = "http"
}
ll := l.WithField("http_request", map[string]interface{}{
"remote": r.RemoteAddr,
"method": r.Method,
"path": r.URL.EscapedPath(),
"query": l.maybeRedact(r.URL.RawQuery),
"scheme": scheme,
"host": r.Host,
"headers": headers,
})
if _, _, spanCtx := otelhttptrace.Extract(r.Context(), r, opts); spanCtx.IsValid() {
traces := map[string]string{}
if spanCtx.HasTraceID() {
traces["trace_id"] = spanCtx.TraceID().String()
}
if spanCtx.HasSpanID() {
traces["span_id"] = spanCtx.SpanID().String()
}
ll = ll.WithField("otel", traces)
}
return ll
}
func (l *Logger) WithFields(f logrus.Fields) *Logger {
ll := *l
ll.Entry = l.Entry.WithFields(f)
return &ll
}
func (l *Logger) WithField(key string, value interface{}) *Logger {
ll := *l
ll.Entry = l.Entry.WithField(key, value)
return &ll
}
func (l *Logger) maybeRedact(value interface{}) interface{} {
if fmt.Sprintf("%v", value) == "" || value == nil {
return nil
}
if !l.leakSensitive {
return `Value is sensitive and has been redacted. To see the value set config key "log.leak_sensitive_values = true" or environment variable "LOG_LEAK_SENSITIVE_VALUES=true".`
}
return value
}
func (l *Logger) WithSensitiveField(key string, value interface{}) *Logger {
return l.WithField(key, l.maybeRedact(value))
}
func (l *Logger) WithError(err error) *Logger {
ctx := map[string]interface{}{"message": err.Error()}
if l.Entry.Logger.IsLevelEnabled(logrus.TraceLevel) {
if e, ok := err.(errorsx.StackTracer); ok {
ctx["trace"] = fmt.Sprintf("%+v", e.StackTrace())
} else {
ctx["trace"] = fmt.Sprintf("stack trace could not be recovered from error type %s", reflect.TypeOf(err))
}
}
if c := errorsx.ReasonCarrier(nil); errors.As(err, &c) {
ctx["reason"] = c.Reason()
}
if c := errorsx.RequestIDCarrier(nil); errors.As(err, &c) && c.RequestID() != "" {
ctx["request_id"] = c.RequestID()
}
if c := errorsx.DetailsCarrier(nil); errors.As(err, &c) && c.Details() != nil {
ctx["details"] = c.Details()
}
if c := errorsx.StatusCarrier(nil); errors.As(err, &c) && c.Status() != "" {
ctx["status"] = c.Status()
}
if c := errorsx.StatusCodeCarrier(nil); errors.As(err, &c) && c.StatusCode() != 0 {
ctx["status_code"] = c.StatusCode()
}
if c := errorsx.DebugCarrier(nil); errors.As(err, &c) {
ctx["debug"] = c.Debug()
}
return l.WithField("error", ctx)
}
var popLevelTranslations = map[logging.Level]logrus.Level{
// logging.SQL: logrus.TraceLevel, we never want to log SQL statements, see https://github.com/ory/keto/issues/454
logging.Debug: logrus.DebugLevel,
logging.Info: logrus.InfoLevel,
logging.Warn: logrus.WarnLevel,
logging.Error: logrus.ErrorLevel,
}
func (l *Logger) PopLogger(lvl logging.Level, s string, args ...interface{}) {
level, ok := popLevelTranslations[lvl]
if ok {
l.WithField("source", "pop").Logf(level, s, args...)
}
}