Skip to content

Commit 957461b

Browse files
committed
add a more fine-grained way to filter messages
1 parent f89912b commit 957461b

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

slackrus.go

+13
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@ const (
1414
VERISON = "0.0.3"
1515
)
1616

17+
// Filter is a filter applied to log entries to filter out any messages which are too noisy.
18+
// The filter should return true if the message should be included, and false if not.
19+
type Filter func(entry *logrus.Entry) bool
20+
1721
// SlackrusHook is a logrus Hook for dispatching messages to the specified
1822
// channel on Slack.
1923
type SlackrusHook struct {
2024
// Messages with a log level not contained in this array
2125
// will not be dispatched. If nil, all messages will be dispatched.
2226
AcceptedLevels []logrus.Level
27+
// Filters are applied to messages to determine if any entry should not be send out.
28+
Filters []Filter
2329
HookURL string
2430
IconURL string
2531
Channel string
@@ -64,6 +70,13 @@ func (sh *SlackrusHook) Fire(e *logrus.Entry) error {
6470
color = "warning"
6571
}
6672

73+
// If any of the filters rejects the message then we return early
74+
for _, filter := range sh.Filters {
75+
if !filter(e) {
76+
return nil
77+
}
78+
}
79+
6780
msg := &slack.Message{
6881
Username: sh.Username,
6982
Channel: sh.Channel,

slackrus_test.go

+68
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66
"net/http/httptest"
77
"testing"
8+
"time"
89

910
"github.com/johntdyer/slack-go"
1011
"github.com/sirupsen/logrus"
@@ -137,3 +138,70 @@ func TestFieldSortPriorities(t *testing.T) {
137138
t.Errorf("3rd field title not as expected: exp: %q, got: %q", exp, got)
138139
}
139140
}
141+
142+
type filterMock struct {
143+
called int
144+
rejectNext bool
145+
}
146+
147+
func (f *filterMock) Filter(e *logrus.Entry) bool {
148+
f.called++
149+
150+
if f.rejectNext {
151+
f.rejectNext = false
152+
return false
153+
}
154+
return true
155+
}
156+
157+
func TestFilters(t *testing.T) {
158+
f := NewFixture(t)
159+
defer f.Cleanup()
160+
161+
mock := filterMock{}
162+
163+
logger := logrus.New()
164+
logger.AddHook(&SlackrusHook{
165+
HookURL: f.URL(),
166+
Channel: "#slack-testing",
167+
Filters: []Filter{
168+
mock.Filter,
169+
},
170+
})
171+
172+
logger.Info("hi there")
173+
<-f.MsgRcvd
174+
175+
if exp, got := 1, len(f.Messages); exp != got {
176+
t.Fatalf("received unexpected number of messages: exp: %d, got: %d", exp, got)
177+
}
178+
if mock.called != 1 {
179+
t.Fatalf("filter should have been called")
180+
}
181+
182+
logger.Info("hi there")
183+
<-f.MsgRcvd
184+
185+
if exp, got := 2, len(f.Messages); exp != got {
186+
t.Fatalf("received unexpected number of messages: exp: %d, got: %d", exp, got)
187+
}
188+
if mock.called != 2 {
189+
t.Fatalf("filter should have been called")
190+
}
191+
192+
// set the mock to reject the next
193+
mock.rejectNext = true
194+
195+
logger.Info("hi there")
196+
197+
select {
198+
case <-f.MsgRcvd:
199+
t.Fatalf("did not expect a message to be send")
200+
case <-time.After(10 * time.Millisecond):
201+
// no message after 10ms, should be fine
202+
}
203+
204+
if exp, got := 2, len(f.Messages); exp != got {
205+
t.Fatalf("received unexpected number of messages: exp: %d, got: %d", exp, got)
206+
}
207+
}

0 commit comments

Comments
 (0)