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
d60339a9
Commit
d60339a9
authored
Oct 09, 2017
by
utkarshcmu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Kafka REST Proxy works with Grafana
parent
68829a82
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
121 additions
and
0 deletions
+121
-0
pkg/services/alerting/notifiers/kafka.go
+120
-0
public/app/features/alerting/alert_tab_ctrl.ts
+1
-0
No files found.
pkg/services/alerting/notifiers/kafka.go
0 → 100644
View file @
d60339a9
package
notifiers
import
(
"strconv"
"fmt"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/log"
m
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
)
func
init
()
{
alerting
.
RegisterNotifier
(
&
alerting
.
NotifierPlugin
{
Type
:
"kafka"
,
Name
:
"Kafka REST Proxy"
,
Description
:
"Sends notifications to Kafka Rest Proxy"
,
Factory
:
NewKafkaNotifier
,
OptionsTemplate
:
`
<h3 class="page-heading">Kafka settings</h3>
<div class="gf-form">
<span class="gf-form-label width-14">Kafka REST Proxy</span>
<input type="text" required class="gf-form-input max-width-22" ng-model="ctrl.model.settings.kafkaRestProxy" placeholder="http://localhost:8082"></input>
</div>
<div class="gf-form">
<span class="gf-form-label width-14">Topic</span>
<input type="text" required class="gf-form-input max-width-22" ng-model="ctrl.model.settings.kafkaTopic" placeholder="topic1"></input>
</div>
`
,
})
}
func
NewKafkaNotifier
(
model
*
m
.
AlertNotification
)
(
alerting
.
Notifier
,
error
)
{
endpoint
:=
model
.
Settings
.
Get
(
"kafkaRestProxy"
)
.
MustString
()
if
endpoint
==
""
{
return
nil
,
alerting
.
ValidationError
{
Reason
:
"Could not find kafka rest proxy endpoint property in settings"
}
}
topic
:=
model
.
Settings
.
Get
(
"kafkaTopic"
)
.
MustString
()
if
topic
==
""
{
return
nil
,
alerting
.
ValidationError
{
Reason
:
"Could not find kafka topic property in settings"
}
}
return
&
KafkaNotifier
{
NotifierBase
:
NewNotifierBase
(
model
.
Id
,
model
.
IsDefault
,
model
.
Name
,
model
.
Type
,
model
.
Settings
),
Endpoint
:
endpoint
,
Topic
:
topic
,
log
:
log
.
New
(
"alerting.notifier.kafka"
),
},
nil
}
type
KafkaNotifier
struct
{
NotifierBase
Endpoint
string
Topic
string
log
log
.
Logger
}
func
(
this
*
KafkaNotifier
)
Notify
(
evalContext
*
alerting
.
EvalContext
)
error
{
state
:=
evalContext
.
Rule
.
State
customData
:=
"Triggered metrics:
\n\n
"
for
_
,
evt
:=
range
evalContext
.
EvalMatches
{
customData
=
customData
+
fmt
.
Sprintf
(
"%s: %v
\n
"
,
evt
.
Metric
,
evt
.
Value
)
}
this
.
log
.
Info
(
"Notifying Kafka"
,
"alert_state"
,
state
)
recordJSON
:=
simplejson
.
New
()
records
:=
make
([]
interface
{},
1
)
bodyJSON
:=
simplejson
.
New
()
bodyJSON
.
Set
(
"description"
,
evalContext
.
Rule
.
Name
+
" - "
+
evalContext
.
Rule
.
Message
)
bodyJSON
.
Set
(
"client"
,
"Grafana"
)
bodyJSON
.
Set
(
"details"
,
customData
)
bodyJSON
.
Set
(
"incident_key"
,
"alertId-"
+
strconv
.
FormatInt
(
evalContext
.
Rule
.
Id
,
10
))
ruleUrl
,
err
:=
evalContext
.
GetRuleUrl
()
if
err
!=
nil
{
this
.
log
.
Error
(
"Failed get rule link"
,
"error"
,
err
)
return
err
}
bodyJSON
.
Set
(
"client_url"
,
ruleUrl
)
if
evalContext
.
ImagePublicUrl
!=
""
{
contexts
:=
make
([]
interface
{},
1
)
imageJSON
:=
simplejson
.
New
()
imageJSON
.
Set
(
"type"
,
"image"
)
imageJSON
.
Set
(
"src"
,
evalContext
.
ImagePublicUrl
)
contexts
[
0
]
=
imageJSON
bodyJSON
.
Set
(
"contexts"
,
contexts
)
}
valueJSON
:=
simplejson
.
New
()
valueJSON
.
Set
(
"value"
,
bodyJSON
)
records
[
0
]
=
valueJSON
recordJSON
.
Set
(
"records"
,
records
)
body
,
_
:=
recordJSON
.
MarshalJSON
()
topicUrl
:=
this
.
Endpoint
+
"/topics/"
+
this
.
Topic
cmd
:=
&
m
.
SendWebhookSync
{
Url
:
topicUrl
,
Body
:
string
(
body
),
HttpMethod
:
"POST"
,
HttpHeader
:
map
[
string
]
string
{
"Content-Type"
:
"application/vnd.kafka.json.v2+json"
,
"Accept"
:
"application/vnd.kafka.v2+json"
,
},
}
if
err
:=
bus
.
DispatchCtx
(
evalContext
.
Ctx
,
cmd
);
err
!=
nil
{
this
.
log
.
Error
(
"Failed to send notification to Kafka"
,
"error"
,
err
,
"body"
,
string
(
body
))
return
err
}
return
nil
}
public/app/features/alerting/alert_tab_ctrl.ts
View file @
d60339a9
...
@@ -94,6 +94,7 @@ export class AlertTabCtrl {
...
@@ -94,6 +94,7 @@ export class AlertTabCtrl {
case
"opsgenie"
:
return
"fa fa-bell"
;
case
"opsgenie"
:
return
"fa fa-bell"
;
case
"hipchat"
:
return
"fa fa-mail-forward"
;
case
"hipchat"
:
return
"fa fa-mail-forward"
;
case
"pushover"
:
return
"fa fa-mobile"
;
case
"pushover"
:
return
"fa fa-mobile"
;
case
"kafka"
:
return
"fa fa-random"
;
}
}
return
'fa fa-bell'
;
return
'fa fa-bell'
;
}
}
...
...
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