forked from nyaruka/courier
-
Notifications
You must be signed in to change notification settings - Fork 1
/
channel_log.go
222 lines (176 loc) · 6.3 KB
/
channel_log.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
package courier
import (
"fmt"
"time"
"github.com/nyaruka/gocommon/dates"
"github.com/nyaruka/gocommon/httpx"
"github.com/nyaruka/gocommon/stringsx"
"github.com/nyaruka/gocommon/uuids"
)
// ChannelLogUUID is our type for a channel log UUID
type ChannelLogUUID uuids.UUID
// ChannelLogType is the type of channel interaction we are logging
type ChannelLogType string
const (
ChannelLogTypeUnknown ChannelLogType = "unknown"
ChannelLogTypeMsgSend ChannelLogType = "msg_send"
ChannelLogTypeMsgStatus ChannelLogType = "msg_status"
ChannelLogTypeMsgReceive ChannelLogType = "msg_receive"
ChannelLogTypeEventReceive ChannelLogType = "event_receive"
ChannelLogTypeMultiReceive ChannelLogType = "multi_receive"
ChannelLogTypeAttachmentFetch ChannelLogType = "attachment_fetch"
ChannelLogTypeTokenRefresh ChannelLogType = "token_refresh"
ChannelLogTypePageSubscribe ChannelLogType = "page_subscribe"
ChannelLogTypeWebhookVerify ChannelLogType = "webhook_verify"
)
type ChannelError struct {
code string
extCode string
message string
}
func NewChannelError(code, extCode, message string, args ...any) *ChannelError {
return &ChannelError{code: code, extCode: extCode, message: fmt.Sprintf(message, args...)}
}
func ErrorResponseStatusCode() *ChannelError {
return NewChannelError("response_status_code", "", "Unexpected response status code.")
}
func ErrorResponseUnparseable(format string) *ChannelError {
return NewChannelError("response_unparseable", "", "Unable to parse response as %s.", format)
}
func ErrorResponseUnexpected(expected string) *ChannelError {
return NewChannelError("response_unexpected", "", "Expected response to be '%s'.", expected)
}
func ErrorResponseValueMissing(key string) *ChannelError {
return NewChannelError("response_value_missing", "", "Unable to find '%s' response.", key)
}
func ErrorMediaUnsupported(contentType string) *ChannelError {
return NewChannelError("media_unsupported", "", "Unsupported attachment media type: %s.", contentType)
}
// ErrorMediaUnresolveable is used when media is unresolveable due to the channel's specific requirements
func ErrorMediaUnresolveable(contentType string) *ChannelError {
return NewChannelError("media_unresolveable", "", "Unable to find version of %s attachment compatible with channel.", contentType)
}
func ErrorAttachmentNotDecodable() *ChannelError {
return NewChannelError("attachment_not_decodable", "", "Unable to decode embedded attachment data.")
}
func ErrorExternal(code, message string) *ChannelError {
if message == "" {
message = fmt.Sprintf("Service specific error: %s.", code)
}
return NewChannelError("external", code, message)
}
func (e *ChannelError) Redact(r stringsx.Redactor) *ChannelError {
return &ChannelError{code: e.code, extCode: e.extCode, message: r(e.message)}
}
func (e *ChannelError) Message() string {
return e.message
}
func (e *ChannelError) Code() string {
return e.code
}
func (e *ChannelError) ExtCode() string {
return e.extCode
}
// ChannelLog stores the HTTP traces and errors generated by an interaction with a channel.
type ChannelLog struct {
uuid ChannelLogUUID
type_ ChannelLogType
channel Channel
httpLogs []*httpx.Log
errors []*ChannelError
createdOn time.Time
elapsed time.Duration
attached bool
recorder *httpx.Recorder
redactor stringsx.Redactor
}
// NewChannelLogForIncoming creates a new channel log for an incoming request, the type of which won't be known
// until the handler completes.
func NewChannelLogForIncoming(logType ChannelLogType, ch Channel, r *httpx.Recorder, redactVals []string) *ChannelLog {
return newChannelLog(logType, ch, r, false, redactVals)
}
// NewChannelLogForSend creates a new channel log for a message send
func NewChannelLogForSend(msg MsgOut, redactVals []string) *ChannelLog {
return newChannelLog(ChannelLogTypeMsgSend, msg.Channel(), nil, true, redactVals)
}
// NewChannelLogForSend creates a new channel log for an attachment fetch
func NewChannelLogForAttachmentFetch(ch Channel, redactVals []string) *ChannelLog {
return newChannelLog(ChannelLogTypeAttachmentFetch, ch, nil, true, redactVals)
}
// NewChannelLog creates a new channel log with the given type and channel
func NewChannelLog(t ChannelLogType, ch Channel, redactVals []string) *ChannelLog {
return newChannelLog(t, ch, nil, false, redactVals)
}
func newChannelLog(t ChannelLogType, ch Channel, r *httpx.Recorder, attached bool, redactVals []string) *ChannelLog {
return &ChannelLog{
uuid: ChannelLogUUID(uuids.New()),
type_: t,
channel: ch,
recorder: r,
createdOn: dates.Now(),
redactor: stringsx.NewRedactor("**********", redactVals...),
}
}
// HTTP logs an outgoing HTTP request and response
func (l *ChannelLog) HTTP(t *httpx.Trace) {
l.httpLogs = append(l.httpLogs, l.traceToLog(t))
}
func (l *ChannelLog) Error(e *ChannelError) {
l.errors = append(l.errors, e.Redact(l.redactor))
}
// Deprecated: channel handlers should add user-facing error messages via .Error() instead
func (l *ChannelLog) RawError(err error) {
l.Error(NewChannelError("", "", err.Error()))
}
func (l *ChannelLog) End() {
if l.recorder != nil {
// prepend so it's the first HTTP request in the log
l.httpLogs = append([]*httpx.Log{l.traceToLog(l.recorder.Trace)}, l.httpLogs...)
}
l.elapsed = time.Since(l.createdOn)
}
func (l *ChannelLog) UUID() ChannelLogUUID {
return l.uuid
}
func (l *ChannelLog) Type() ChannelLogType {
return l.type_
}
func (l *ChannelLog) SetType(t ChannelLogType) {
l.type_ = t
}
func (l *ChannelLog) Channel() Channel {
return l.channel
}
func (l *ChannelLog) Attached() bool {
return l.attached
}
func (l *ChannelLog) SetAttached(a bool) {
l.attached = a
}
func (l *ChannelLog) HTTPLogs() []*httpx.Log {
return l.httpLogs
}
func (l *ChannelLog) Errors() []*ChannelError {
return l.errors
}
func (l *ChannelLog) CreatedOn() time.Time {
return l.createdOn
}
func (l *ChannelLog) Elapsed() time.Duration {
return l.elapsed
}
// if we have an error or a non 2XX/3XX http response then log is considered an error
func (l *ChannelLog) IsError() bool {
if len(l.errors) > 0 {
return true
}
for _, l := range l.httpLogs {
if l.StatusCode < 200 || l.StatusCode >= 400 {
return true
}
}
return false
}
func (l *ChannelLog) traceToLog(t *httpx.Trace) *httpx.Log {
return httpx.NewLog(t, 2048, 50000, l.redactor)
}