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
57018738
Unverified
Commit
57018738
authored
Mar 08, 2019
by
Torkel Ödegaard
Committed by
GitHub
Mar 08, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #13825 from athurg/optimize_dingding_alert_message
Optimize dingding alert message
parents
d104ee1c
70b23ab7
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
68 additions
and
19 deletions
+68
-19
pkg/services/alerting/notifiers/dingding.go
+68
-19
No files found.
pkg/services/alerting/notifiers/dingding.go
View file @
57018738
package
notifiers
package
notifiers
import
(
import
(
"fmt"
"net/url"
"strings"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/log"
...
@@ -8,19 +12,26 @@ import (
...
@@ -8,19 +12,26 @@ import (
"github.com/grafana/grafana/pkg/services/alerting"
"github.com/grafana/grafana/pkg/services/alerting"
)
)
func
init
()
{
const
DefaultDingdingMsgType
=
"link"
alerting
.
RegisterNotifier
(
&
alerting
.
NotifierPlugin
{
const
DingdingOptionsTemplate
=
`
Type
:
"dingding"
,
Name
:
"DingDing"
,
Description
:
"Sends HTTP POST request to DingDing"
,
Factory
:
NewDingDingNotifier
,
OptionsTemplate
:
`
<h3 class="page-heading">DingDing settings</h3>
<h3 class="page-heading">DingDing settings</h3>
<div class="gf-form">
<div class="gf-form">
<span class="gf-form-label width-10">Url</span>
<span class="gf-form-label width-10">Url</span>
<input type="text" required class="gf-form-input max-width-26" ng-model="ctrl.model.settings.url" placeholder="https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxx"></input>
<input type="text" required class="gf-form-input max-width-70" ng-model="ctrl.model.settings.url" placeholder="https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxx"></input>
</div>
<div class="gf-form">
<span class="gf-form-label width-10">MessageType</span>
<select class="gf-form-input max-width-14" ng-model="ctrl.model.settings.msgType" ng-options="s for s in ['link','actionCard']" ng-init="ctrl.model.settings.msgType=ctrl.model.settings.msgType || '`
+
DefaultDingdingMsgType
+
`'"></select>
</div>
</div>
`
,
`
func
init
()
{
alerting
.
RegisterNotifier
(
&
alerting
.
NotifierPlugin
{
Type
:
"dingding"
,
Name
:
"DingDing"
,
Description
:
"Sends HTTP POST request to DingDing"
,
Factory
:
NewDingDingNotifier
,
OptionsTemplate
:
DingdingOptionsTemplate
,
})
})
}
}
...
@@ -31,8 +42,11 @@ func NewDingDingNotifier(model *m.AlertNotification) (alerting.Notifier, error)
...
@@ -31,8 +42,11 @@ func NewDingDingNotifier(model *m.AlertNotification) (alerting.Notifier, error)
return
nil
,
alerting
.
ValidationError
{
Reason
:
"Could not find url property in settings"
}
return
nil
,
alerting
.
ValidationError
{
Reason
:
"Could not find url property in settings"
}
}
}
msgType
:=
model
.
Settings
.
Get
(
"msgType"
)
.
MustString
(
DefaultDingdingMsgType
)
return
&
DingDingNotifier
{
return
&
DingDingNotifier
{
NotifierBase
:
NewNotifierBase
(
model
),
NotifierBase
:
NewNotifierBase
(
model
),
MsgType
:
msgType
,
Url
:
url
,
Url
:
url
,
log
:
log
.
New
(
"alerting.notifier.dingding"
),
log
:
log
.
New
(
"alerting.notifier.dingding"
),
},
nil
},
nil
...
@@ -40,8 +54,9 @@ func NewDingDingNotifier(model *m.AlertNotification) (alerting.Notifier, error)
...
@@ -40,8 +54,9 @@ func NewDingDingNotifier(model *m.AlertNotification) (alerting.Notifier, error)
type
DingDingNotifier
struct
{
type
DingDingNotifier
struct
{
NotifierBase
NotifierBase
Url
string
MsgType
string
log
log
.
Logger
Url
string
log
log
.
Logger
}
}
func
(
this
*
DingDingNotifier
)
Notify
(
evalContext
*
alerting
.
EvalContext
)
error
{
func
(
this
*
DingDingNotifier
)
Notify
(
evalContext
*
alerting
.
EvalContext
)
error
{
...
@@ -52,6 +67,16 @@ func (this *DingDingNotifier) Notify(evalContext *alerting.EvalContext) error {
...
@@ -52,6 +67,16 @@ func (this *DingDingNotifier) Notify(evalContext *alerting.EvalContext) error {
this
.
log
.
Error
(
"Failed to get messageUrl"
,
"error"
,
err
,
"dingding"
,
this
.
Name
)
this
.
log
.
Error
(
"Failed to get messageUrl"
,
"error"
,
err
,
"dingding"
,
this
.
Name
)
messageUrl
=
""
messageUrl
=
""
}
}
q
:=
url
.
Values
{
"pc_slide"
:
{
"false"
},
"url"
:
{
messageUrl
},
}
// Use special link to auto open the message url outside of Dingding
// Refer: https://open-doc.dingtalk.com/docs/doc.htm?treeId=385&articleId=104972&docType=1#s9
messageUrl
=
"dingtalk://dingtalkclient/page/link?"
+
q
.
Encode
()
this
.
log
.
Info
(
"messageUrl:"
+
messageUrl
)
this
.
log
.
Info
(
"messageUrl:"
+
messageUrl
)
message
:=
evalContext
.
Rule
.
Message
message
:=
evalContext
.
Rule
.
Message
...
@@ -61,15 +86,39 @@ func (this *DingDingNotifier) Notify(evalContext *alerting.EvalContext) error {
...
@@ -61,15 +86,39 @@ func (this *DingDingNotifier) Notify(evalContext *alerting.EvalContext) error {
message
=
title
message
=
title
}
}
bodyJSON
,
err
:=
simplejson
.
NewJson
([]
byte
(
`{
for
i
,
match
:=
range
evalContext
.
EvalMatches
{
"msgtype": "link",
message
+=
fmt
.
Sprintf
(
"
\\
n%2d. %s: %s"
,
i
+
1
,
match
.
Metric
,
match
.
Value
)
"link": {
}
"text": "`
+
message
+
`",
"title": "`
+
title
+
`",
var
bodyStr
string
"picUrl": "`
+
picUrl
+
`",
if
this
.
MsgType
==
"actionCard"
{
"messageUrl": "`
+
messageUrl
+
`"
// Embed the pic into the markdown directly because actionCard doesn't have a picUrl field
if
picUrl
!=
""
{
message
=
"![]("
+
picUrl
+
")
\\
n
\\
n"
+
message
}
}
}`
))
bodyStr
=
`{
"msgtype": "actionCard",
"actionCard": {
"text": "`
+
strings
.
Replace
(
message
,
`"`
,
"'"
,
-
1
)
+
`",
"title": "`
+
strings
.
Replace
(
title
,
`"`
,
"'"
,
-
1
)
+
`",
"singleTitle": "More",
"singleURL": "`
+
messageUrl
+
`"
}
}`
}
else
{
bodyStr
=
`{
"msgtype": "link",
"link": {
"text": "`
+
message
+
`",
"title": "`
+
title
+
`",
"picUrl": "`
+
picUrl
+
`",
"messageUrl": "`
+
messageUrl
+
`"
}
}`
}
bodyJSON
,
err
:=
simplejson
.
NewJson
([]
byte
(
bodyStr
))
if
err
!=
nil
{
if
err
!=
nil
{
this
.
log
.
Error
(
"Failed to create Json data"
,
"error"
,
err
,
"dingding"
,
this
.
Name
)
this
.
log
.
Error
(
"Failed to create Json data"
,
"error"
,
err
,
"dingding"
,
this
.
Name
)
...
...
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