Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
b5a29b62
Commit
b5a29b62
authored
Jun 22, 2016
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test(alerting): add tests for when to send notifcations
parent
6edae37a
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
12 deletions
+46
-12
pkg/services/alerting/notifier.go
+18
-10
pkg/services/alerting/notifier_test.go
+28
-2
No files found.
pkg/services/alerting/notifier.go
View file @
b5a29b62
...
...
@@ -13,26 +13,32 @@ import (
type
NotifierImpl
struct
{
log
log
.
Logger
getNotifications
func
(
orgId
int64
,
notificationGroups
[]
int64
)
[]
*
Notification
}
func
NewNotifier
()
*
NotifierImpl
{
log
:=
log
.
New
(
"alerting.notifier"
)
return
&
NotifierImpl
{
log
:
log
.
New
(
"alerting.notifier"
),
log
:
log
,
getNotifications
:
buildGetNotifiers
(
log
),
}
}
func
(
n
NotifierImpl
)
ShouldDispath
(
alertResult
*
AlertResult
,
notifier
*
Notification
)
bool
{
warn
:=
alertResult
.
State
==
alertstates
.
Warn
&&
notifier
.
SendWarning
crit
:=
alertResult
.
State
==
alertstates
.
Critical
&&
notifier
.
SendCritical
return
(
warn
||
crit
)
||
alertResult
.
State
==
alertstates
.
Ok
}
func
(
n
*
NotifierImpl
)
Notify
(
alertResult
*
AlertResult
)
{
notifiers
:=
n
.
getNotifi
er
s
(
alertResult
.
AlertJob
.
Rule
.
OrgId
,
alertResult
.
AlertJob
.
Rule
.
NotificationGroups
)
notifiers
:=
n
.
getNotifi
cation
s
(
alertResult
.
AlertJob
.
Rule
.
OrgId
,
alertResult
.
AlertJob
.
Rule
.
NotificationGroups
)
for
_
,
notifier
:=
range
notifiers
{
warn
:=
alertResult
.
State
==
alertstates
.
Warn
&&
notifier
.
SendWarning
crit
:=
alertResult
.
State
==
alertstates
.
Critical
&&
notifier
.
SendCritical
if
(
warn
||
crit
)
||
alertResult
.
State
==
alertstates
.
Ok
{
if
n
.
ShouldDispath
(
alertResult
,
notifier
)
{
n
.
log
.
Info
(
"Sending notification"
,
"state"
,
alertResult
.
State
,
"type"
,
notifier
.
Type
)
go
notifier
.
Notifierr
.
Dispatch
(
alertResult
)
}
}
}
type
Notification
struct
{
...
...
@@ -107,7 +113,8 @@ type NotificationDispatcher interface {
Dispatch
(
alertResult
*
AlertResult
)
}
func
(
n
*
NotifierImpl
)
getNotifiers
(
orgId
int64
,
notificationGroups
[]
int64
)
[]
*
Notification
{
func
buildGetNotifiers
(
log
log
.
Logger
)
func
(
orgId
int64
,
notificationGroups
[]
int64
)
[]
*
Notification
{
return
func
(
orgId
int64
,
notificationGroups
[]
int64
)
[]
*
Notification
{
query
:=
&
m
.
GetAlertNotificationQuery
{
OrgID
:
orgId
,
Ids
:
notificationGroups
,
...
...
@@ -115,21 +122,22 @@ func (n *NotifierImpl) getNotifiers(orgId int64, notificationGroups []int64) []*
}
err
:=
bus
.
Dispatch
(
query
)
if
err
!=
nil
{
n
.
log
.
Error
(
"Failed to read notifications"
,
"error"
,
err
)
log
.
Error
(
"Failed to read notifications"
,
"error"
,
err
)
}
var
result
[]
*
Notification
n
.
log
.
Info
(
"notifiriring"
,
"count"
,
len
(
query
.
Result
),
"groups"
,
notificationGroups
)
log
.
Info
(
"notifiriring"
,
"count"
,
len
(
query
.
Result
),
"groups"
,
notificationGroups
)
for
_
,
notification
:=
range
query
.
Result
{
not
,
err
:=
NewNotificationFromDBModel
(
notification
)
if
err
==
nil
{
result
=
append
(
result
,
not
)
}
else
{
n
.
log
.
Error
(
"Failed to read notification model"
,
"error"
,
err
)
log
.
Error
(
"Failed to read notification model"
,
"error"
,
err
)
}
}
return
result
}
}
func
NewNotificationFromDBModel
(
model
*
m
.
AlertNotification
)
(
*
Notification
,
error
)
{
...
...
pkg/services/alerting/notifier_test.go
View file @
b5a29b62
...
...
@@ -7,12 +7,38 @@ import (
"github.com/grafana/grafana/pkg/components/simplejson"
m
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting/alertstates"
.
"github.com/smartystreets/goconvey/convey"
)
func
TestAlertNotificationExtraction
(
t
*
testing
.
T
)
{
Convey
(
"Notifier tests"
,
t
,
func
()
{
Convey
(
"rules for sending notifications"
,
func
()
{
dummieNotifier
:=
NotifierImpl
{}
Convey
(
"Parsing alert notification from settings"
,
t
,
func
()
{
result
:=
&
AlertResult
{
State
:
alertstates
.
Critical
,
}
notifier
:=
&
Notification
{
Name
:
"Test Notifier"
,
Type
:
"TestType"
,
SendCritical
:
true
,
SendWarning
:
true
,
}
Convey
(
"Should send notification"
,
func
()
{
So
(
dummieNotifier
.
ShouldDispath
(
result
,
notifier
),
ShouldBeTrue
)
})
Convey
(
"warn:false and state:warn should not send"
,
func
()
{
result
.
State
=
alertstates
.
Warn
notifier
.
SendWarning
=
false
So
(
dummieNotifier
.
ShouldDispath
(
result
,
notifier
),
ShouldBeFalse
)
})
})
Convey
(
"Parsing alert notification from settings"
,
func
()
{
Convey
(
"Parsing email"
,
func
()
{
Convey
(
"empty settings should return error"
,
func
()
{
json
:=
`{ }`
...
...
@@ -94,6 +120,6 @@ func TestAlertNotificationExtraction(t *testing.T) {
So
(
webhook
.
Url
,
ShouldEqual
,
"http://localhost:3000"
)
})
})
})
})
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment