forked from mongodb/grip
/
slack.go
277 lines (228 loc) · 6.88 KB
/
slack.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
package send
import (
"errors"
"fmt"
"log"
"os"
"strings"
"sync"
"github.com/bluele/slack"
"github.com/mongodb/grip/level"
"github.com/mongodb/grip/message"
)
const (
slackClientToken = "GRIP_SLACK_CLIENT_TOKEN"
)
type slackJournal struct {
opts *SlackOptions
*Base
}
// NewSlackLogger constructs a Sender that posts messages to a slack,
// given a slack API token. Configure the slack sender using a SlackOptions struct.
func NewSlackLogger(opts *SlackOptions, token string, l LevelInfo) (Sender, error) {
if err := opts.Validate(); err != nil {
return nil, err
}
s := &slackJournal{
opts: opts,
Base: NewBase(opts.Name),
}
s.opts.client.Create(token)
if err := s.SetLevel(l); err != nil {
return nil, err
}
if _, err := s.opts.client.AuthTest(); err != nil {
return nil, fmt.Errorf("slack authentication error: %v", err)
}
fallback := log.New(os.Stdout, "", log.LstdFlags)
if err := s.SetErrorHandler(ErrorHandlerFromLogger(fallback)); err != nil {
return nil, err
}
s.reset = func() {
fallback.SetPrefix(fmt.Sprintf("[%s] ", s.Name()))
}
s.SetName(opts.Name)
return s, nil
}
// MakeSlackLogger is equivalent to NewSlackLogger, but constructs a
// Sender reading the slack token from the environment variable
// "GRIP_SLACK_CLIENT_TOKEN".
func MakeSlackLogger(opts *SlackOptions) (Sender, error) {
token := os.Getenv(slackClientToken)
if token == "" {
return nil, fmt.Errorf("environment variable %s not defined, cannot create slack client",
slackClientToken)
}
return NewSlackLogger(opts, token, LevelInfo{level.Trace, level.Trace})
}
func (s *slackJournal) Send(m message.Composer) {
if s.Level().ShouldLog(m) {
s.Base.mutex.RLock()
defer s.Base.mutex.RUnlock()
msg, params := s.opts.produceMessage(m)
if err := s.opts.client.ChatPostMessage(s.opts.Channel, msg, params); err != nil {
s.ErrorHandler(err, message.NewFormattedMessage(m.Priority(),
"%s: %s\n", params.Attachments[0].Fallback, msg))
}
}
}
// SlackOptions configures the behavior for constructing messages sent
// to slack.
type SlackOptions struct {
// Channel, Name, and Hostname are basic configurations
// options for the sender, and control where the messages are
// sent, which hostname the sender reports itself as, and the
// name of the journaler.
Channel string `bson:"channel" json:"channel" yaml:"channel"`
Hostname string `bson:"hostname" json:"hostname" yaml:"hostname"`
Name string `bson:"name" json:"name" yaml:"name"`
// Configuration options for appending structured data to the
// message sent to slack. The BasicMetadata option appends
// priority and host information to the message. The Fields
// option appends structured data if the Raw method of a
// Composer returns a message.Fields map. If you specify a set
// of fields in the FieldsSet value, only those fields will be
// attached to the message.
BasicMetadata bool `bson:"add_basic_metadata" json:"add_basic_metadata" yaml:"add_basic_metadata"`
Fields bool `bson:"use_fields" json:"use_fields" yaml:"use_fields"`
AllFields bool `bson:"all_fields" json:"all_fields" yaml:"all_fields"`
FieldsSet map[string]bool `bson:"fields" json:"fields" yaml:"fields"`
client slackClient
mutex sync.RWMutex
}
func (o *SlackOptions) fieldSetShouldInclude(name string) bool {
if name == "time" || name == "metadata" {
return false
}
o.mutex.RLock()
defer o.mutex.RUnlock()
if o.AllFields || o.FieldsSet == nil {
return true
}
return o.FieldsSet[name]
}
// Validate inspects the contents SlackOptions struct and returns an
// error that reports on missing required fields (e.g. Channel and
// Name); if the hostname is missing and the os.Hostname() syscall
// fails (but supplies the Hostname as reported by Hostname if there is
// no Hostname is specified). Validate also prepends a missing "#" to
// the channel setting if the "#" character is not set.
func (o *SlackOptions) Validate() error {
if o == nil {
return errors.New("slack options cannot be nil")
}
errs := []string{}
if o.Channel == "" {
errs = append(errs, "no channel specified")
}
if o.Name == "" {
errs = append(errs, "no logger/journal name specified")
}
if o.FieldsSet == nil {
o.FieldsSet = map[string]bool{}
}
if o.client == nil {
o.client = &slackClientImpl{}
}
if o.Hostname == "" {
hostname, err := os.Hostname()
if err != nil {
errs = append(errs, err.Error())
} else {
o.Hostname = hostname
}
}
if !strings.HasPrefix(o.Channel, "#") {
o.Channel = "#" + o.Channel
}
if len(errs) > 0 {
return errors.New(strings.Join(errs, "; "))
}
return nil
}
func (o *SlackOptions) produceMessage(m message.Composer) (string, *slack.ChatPostMessageOpt) {
var msg string
o.mutex.RLock()
defer o.mutex.RUnlock()
attachment := slack.Attachment{}
fallbacks := []string{}
p := m.Priority()
if o.BasicMetadata {
if o.Name != "" {
fallbacks = append(fallbacks, fmt.Sprintf("journal=%s", o.Name))
attachment.Fields = append(attachment.Fields,
&slack.AttachmentField{
Title: "Journal",
Value: o.Name,
Short: true,
})
}
if o.Hostname != "!" && o.Hostname != "" {
fallbacks = append(fallbacks, fmt.Sprintf("host=%s", o.Hostname))
attachment.Fields = append(attachment.Fields,
&slack.AttachmentField{
Title: "Host",
Value: o.Hostname,
Short: true,
})
}
fallbacks = append(fallbacks, fmt.Sprintf("priority=%s", p))
attachment.Fields = append(attachment.Fields,
&slack.AttachmentField{
Title: "priority",
Value: p.String(),
Short: true,
})
}
if o.Fields {
fields, ok := m.Raw().(message.Fields)
if ok {
for k, v := range fields {
if !o.fieldSetShouldInclude(k) {
continue
}
fallbacks = append(fallbacks, fmt.Sprintf("%s=%v", k, v))
attachment.Fields = append(attachment.Fields,
&slack.AttachmentField{
Title: k,
Value: fmt.Sprintf("%v", v),
Short: true,
})
}
} else {
msg = m.String()
}
} else {
msg = m.String()
}
if len(fallbacks) > 0 {
attachment.Fallback = fmt.Sprintf("[%s]", strings.Join(fallbacks, ", "))
}
switch p {
case level.Emergency, level.Alert, level.Critical:
attachment.Color = "danger"
case level.Warning, level.Notice:
attachment.Color = "warning"
default: // includes info/debug
attachment.Color = "good"
}
return msg, &slack.ChatPostMessageOpt{
Attachments: []*slack.Attachment{&attachment},
}
}
////////////////////////////////////////////////////////////////////////
//
// interface wrapper for the slack client so that we can mock things out
//
////////////////////////////////////////////////////////////////////////
type slackClient interface {
Create(string)
AuthTest() (*slack.AuthTestApiResponse, error)
ChatPostMessage(string, string, *slack.ChatPostMessageOpt) error
}
type slackClientImpl struct {
*slack.Slack
}
func (c *slackClientImpl) Create(token string) {
c.Slack = slack.New(token)
}