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
3d66ec81
Commit
3d66ec81
authored
May 27, 2016
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore(alerting): minor refactoring
parent
8ac635b6
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
56 additions
and
49 deletions
+56
-49
pkg/services/alerting/alerting.go
+2
-2
pkg/services/alerting/graphite_executor.go
+11
-47
pkg/services/alerting/rule_executor.go
+35
-0
pkg/services/alerting/types.go
+8
-0
No files found.
pkg/services/alerting/alerting.go
View file @
3d66ec81
...
@@ -50,7 +50,7 @@ func NewScheduler() *Scheduler {
...
@@ -50,7 +50,7 @@ func NewScheduler() *Scheduler {
func
(
this
*
Scheduler
)
heartBeat
()
{
func
(
this
*
Scheduler
)
heartBeat
()
{
//Lets cheat on this until we focus on clustering
//Lets cheat on this until we focus on clustering
log
.
Info
(
"Heartbeat: Sending heartbeat from "
+
this
.
serverId
)
//
log.Info("Heartbeat: Sending heartbeat from " + this.serverId)
this
.
clusterSize
=
1
this
.
clusterSize
=
1
this
.
serverPosition
=
1
this
.
serverPosition
=
1
...
@@ -119,7 +119,7 @@ func (this *Scheduler) queueJobs() {
...
@@ -119,7 +119,7 @@ func (this *Scheduler) queueJobs() {
func
(
this
*
Scheduler
)
Executor
(
executor
Executor
)
{
func
(
this
*
Scheduler
)
Executor
(
executor
Executor
)
{
for
job
:=
range
this
.
runQueue
{
for
job
:=
range
this
.
runQueue
{
log
.
Info
(
"Executor: queue length %d"
,
len
(
this
.
runQueue
))
//
log.Info("Executor: queue length %d", len(this.runQueue))
log
.
Info
(
"Executor: executing %s"
,
job
.
rule
.
Title
)
log
.
Info
(
"Executor: executing %s"
,
job
.
rule
.
Title
)
this
.
jobs
[
job
.
rule
.
Id
]
.
running
=
true
this
.
jobs
[
job
.
rule
.
Id
]
.
running
=
true
this
.
MeasureAndExecute
(
executor
,
job
)
this
.
MeasureAndExecute
(
executor
,
job
)
...
...
pkg/services/alerting/graphite_executor.go
View file @
3d66ec81
package
alerting
package
alerting
import
(
import
(
"encoding/json"
"fmt"
"fmt"
"github.com/franela/goreq"
"github.com/franela/goreq"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/bus"
...
@@ -14,13 +13,12 @@ import (
...
@@ -14,13 +13,12 @@ import (
type
GraphiteExecutor
struct
{}
type
GraphiteExecutor
struct
{}
type
Series
struct
{
type
GraphiteSerie
struct
{
Datapoints
[]
DataPoint
Datapoints
[]
[
2
]
float64
Target
string
Target
string
}
}
type
Response
[]
Series
type
GraphiteResponse
[]
GraphiteSerie
type
DataPoint
[]
json
.
Number
func
(
this
*
GraphiteExecutor
)
Execute
(
rule
m
.
AlertRule
,
responseQueue
chan
*
AlertResult
)
{
func
(
this
*
GraphiteExecutor
)
Execute
(
rule
m
.
AlertRule
,
responseQueue
chan
*
AlertResult
)
{
response
,
err
:=
this
.
getSeries
(
rule
)
response
,
err
:=
this
.
getSeries
(
rule
)
...
@@ -32,38 +30,7 @@ func (this *GraphiteExecutor) Execute(rule m.AlertRule, responseQueue chan *Aler
...
@@ -32,38 +30,7 @@ func (this *GraphiteExecutor) Execute(rule m.AlertRule, responseQueue chan *Aler
responseQueue
<-
this
.
executeRules
(
response
,
rule
)
responseQueue
<-
this
.
executeRules
(
response
,
rule
)
}
}
func
(
this
*
GraphiteExecutor
)
executeRules
(
series
[]
Series
,
rule
m
.
AlertRule
)
*
AlertResult
{
func
(
this
*
GraphiteExecutor
)
getSeries
(
rule
m
.
AlertRule
)
(
GraphiteResponse
,
error
)
{
for
_
,
v
:=
range
series
{
var
avg
float64
var
sum
float64
for
_
,
dp
:=
range
v
.
Datapoints
{
i
,
_
:=
dp
[
0
]
.
Float64
()
sum
+=
i
}
avg
=
sum
/
float64
(
len
(
v
.
Datapoints
))
if
float64
(
rule
.
CritLevel
)
<
avg
{
return
&
AlertResult
{
State
:
m
.
AlertStateCritical
,
Id
:
rule
.
Id
,
ActualValue
:
avg
}
}
if
float64
(
rule
.
WarnLevel
)
<
avg
{
return
&
AlertResult
{
State
:
m
.
AlertStateWarn
,
Id
:
rule
.
Id
,
ActualValue
:
avg
}
}
if
float64
(
rule
.
CritLevel
)
<
sum
{
return
&
AlertResult
{
State
:
m
.
AlertStateCritical
,
Id
:
rule
.
Id
,
ActualValue
:
sum
}
}
if
float64
(
rule
.
WarnLevel
)
<
sum
{
return
&
AlertResult
{
State
:
m
.
AlertStateWarn
,
Id
:
rule
.
Id
,
ActualValue
:
sum
}
}
}
return
&
AlertResult
{
State
:
m
.
AlertStateOk
,
Id
:
rule
.
Id
}
}
func
(
this
*
GraphiteExecutor
)
getSeries
(
rule
m
.
AlertRule
)
(
Response
,
error
)
{
query
:=
&
m
.
GetDataSourceByIdQuery
{
Id
:
rule
.
DatasourceId
,
OrgId
:
rule
.
OrgId
}
query
:=
&
m
.
GetDataSourceByIdQuery
{
Id
:
rule
.
DatasourceId
,
OrgId
:
rule
.
OrgId
}
if
err
:=
bus
.
Dispatch
(
query
);
err
!=
nil
{
if
err
:=
bus
.
Dispatch
(
query
);
err
!=
nil
{
return
nil
,
err
return
nil
,
err
...
@@ -71,22 +38,19 @@ func (this *GraphiteExecutor) getSeries(rule m.AlertRule) (Response, error) {
...
@@ -71,22 +38,19 @@ func (this *GraphiteExecutor) getSeries(rule m.AlertRule) (Response, error) {
v
:=
url
.
Values
{
v
:=
url
.
Values
{
"format"
:
[]
string
{
"json"
},
"format"
:
[]
string
{
"json"
},
"target"
:
[]
string
{
getTargetFromQuery
(
rule
)},
"target"
:
[]
string
{
getTargetFromRule
(
rule
)},
"until"
:
[]
string
{
"now"
},
"from"
:
[]
string
{
"-"
+
rule
.
QueryRange
},
}
}
v
.
Add
(
"from"
,
"-"
+
rule
.
QueryRange
)
res
,
err
:=
goreq
.
Request
{
v
.
Add
(
"until"
,
"now"
)
req
:=
goreq
.
Request
{
Method
:
"POST"
,
Method
:
"POST"
,
Uri
:
query
.
Result
.
Url
+
"/render"
,
Uri
:
query
.
Result
.
Url
+
"/render"
,
Body
:
v
.
Encode
(),
Body
:
v
.
Encode
(),
Timeout
:
500
*
time
.
Millisecond
,
Timeout
:
500
*
time
.
Millisecond
,
}
}
.
Do
()
res
,
err
:=
req
.
Do
()
response
:=
Response
{}
response
:=
Graphite
Response
{}
res
.
Body
.
FromJsonTo
(
&
response
)
res
.
Body
.
FromJsonTo
(
&
response
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -100,7 +64,7 @@ func (this *GraphiteExecutor) getSeries(rule m.AlertRule) (Response, error) {
...
@@ -100,7 +64,7 @@ func (this *GraphiteExecutor) getSeries(rule m.AlertRule) (Response, error) {
return
response
,
nil
return
response
,
nil
}
}
func
getTargetFrom
Query
(
rule
m
.
AlertRule
)
string
{
func
getTargetFrom
Rule
(
rule
m
.
AlertRule
)
string
{
json
,
_
:=
simplejson
.
NewJson
([]
byte
(
rule
.
Query
))
json
,
_
:=
simplejson
.
NewJson
([]
byte
(
rule
.
Query
))
return
json
.
Get
(
"target"
)
.
MustString
()
return
json
.
Get
(
"target"
)
.
MustString
()
...
...
pkg/services/alerting/rule_executor.go
0 → 100644
View file @
3d66ec81
package
alerting
import
(
m
"github.com/grafana/grafana/pkg/models"
)
func
(
this
*
GraphiteExecutor
)
executeRules
(
series
[]
GraphiteSerie
,
rule
m
.
AlertRule
)
*
AlertResult
{
for
_
,
v
:=
range
series
{
var
avg
float64
var
sum
float64
for
_
,
dp
:=
range
v
.
Datapoints
{
sum
+=
dp
[
0
]
}
avg
=
sum
/
float64
(
len
(
v
.
Datapoints
))
if
float64
(
rule
.
CritLevel
)
<
avg
{
return
&
AlertResult
{
State
:
m
.
AlertStateCritical
,
Id
:
rule
.
Id
,
ActualValue
:
avg
}
}
if
float64
(
rule
.
WarnLevel
)
<
avg
{
return
&
AlertResult
{
State
:
m
.
AlertStateWarn
,
Id
:
rule
.
Id
,
ActualValue
:
avg
}
}
if
float64
(
rule
.
CritLevel
)
<
sum
{
return
&
AlertResult
{
State
:
m
.
AlertStateCritical
,
Id
:
rule
.
Id
,
ActualValue
:
sum
}
}
if
float64
(
rule
.
WarnLevel
)
<
sum
{
return
&
AlertResult
{
State
:
m
.
AlertStateWarn
,
Id
:
rule
.
Id
,
ActualValue
:
sum
}
}
}
return
&
AlertResult
{
State
:
m
.
AlertStateOk
,
Id
:
rule
.
Id
}
}
pkg/services/alerting/types.go
0 → 100644
View file @
3d66ec81
package
alerting
type
TimeSeries
struct
{
Name
string
`json:"name"`
Points
[][
2
]
float64
`json:"points"`
}
type
TimeSeriesSlice
[]
*
TimeSeries
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