Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
jobily
Nightingale
提交
7161c1ac
N
Nightingale
项目概览
jobily
/
Nightingale
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
N
Nightingale
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
7161c1ac
编写于
1月 11, 2021
作者:
Y
yubo
提交者:
GitHub
1月 11, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
adjust some file, variable name for prober module (#503)
* move pulgins_config.go to config dir * add mongodb, redis yml
上级
a9cf307c
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
164 addition
and
166 deletion
+164
-166
etc/plugins/mysql.yml
etc/plugins/mysql.yml
+1
-9
src/models/mon_collect_rule.go
src/models/mon_collect_rule.go
+6
-2
src/modules/prober/cache/cache.go
src/modules/prober/cache/cache.go
+1
-1
src/modules/prober/cache/collectrule.go
src/modules/prober/cache/collectrule.go
+0
-0
src/modules/prober/config/plugin.go
src/modules/prober/config/plugin.go
+54
-51
src/modules/prober/expr/expr.go
src/modules/prober/expr/expr.go
+4
-3
src/modules/prober/manager/collectrule.go
src/modules/prober/manager/collectrule.go
+65
-61
src/modules/prober/manager/manager.go
src/modules/prober/manager/manager.go
+33
-39
未找到文件。
etc/plugins/mysql.yml
浏览文件 @
7161c1ac
mode
:
whitelist
# whitelist(default),
all
mode
:
whitelist
# whitelist(default),
overlay
metrics
:
-
name
:
mysql_queries
type
:
COUNTER
...
...
@@ -14,20 +14,12 @@ metrics:
-
name
:
mysql_variables_max_connections
type
:
GAUGE
comment
:
"
最大连接数"
-
name
:
mysql_connections_threshold
type
:
GAUGE
expr
:
mysql_threads_connected / mysql_variables_max_connections
comment
:
"
连接数阈值
<
0.8"
-
name
:
mysql_innodb_buffer_pool_read_requests
type
:
COUNTER
comment
:
"
innodb缓冲池查询总数"
-
name
:
mysql_innodb_buffer_pool_reads
type
:
COUNTER
comment
:
"
innodb从磁盘查询数"
-
name
:
mysql_innodb_buffer_read_threshold
type
:
COUNTER
expr
:
(mysql_innodb_buffer_pool_read_requests - mysql_innodb_buffer_pool_reads) / mysql_innodb_buffer_pool_read_requests
comment
:
"
磁盘查询报警阈值
<
0.95"
-
name
:
mysql_binary_files_count
type
:
COUNTER
-
name
:
mysql_binary_size_bytes
...
...
src/models/mon_collect_rule.go
浏览文件 @
7161c1ac
...
...
@@ -15,10 +15,10 @@ const (
type
CollectRule
struct
{
Id
int64
`json:"id"`
Nid
int64
`json:"nid"`
Step
int
`json:"step" description:"interval"`
Step
int
64
`json:"step" description:"interval"`
Timeout
int
`json:"timeout"`
CollectType
string
`json:"collect_type" description:"plugin name"`
Name
string
`json:"name"`
Name
string
`json:"name"
describes:"customize name"
`
Region
string
`json:"region"`
Comment
string
`json:"comment"`
Data
json
.
RawMessage
`json:"data"`
...
...
@@ -33,6 +33,10 @@ type validator interface {
Validate
()
error
}
func
(
p
CollectRule
)
PluginName
()
string
{
return
p
.
CollectType
}
func
(
p
*
CollectRule
)
Validate
(
v
...
interface
{})
error
{
if
p
.
Name
==
""
{
return
fmt
.
Errorf
(
"invalid collectRule.name"
)
...
...
src/modules/prober/cache/cache.go
浏览文件 @
7161c1ac
...
...
@@ -13,6 +13,6 @@ func Init(ctx context.Context) error {
CollectRule
=
NewCollectRuleCache
(
&
config
.
Config
.
CollectRule
)
CollectRule
.
start
(
ctx
)
MetricHistory
=
NewHistory
()
InitPluginsConfig
(
config
.
Config
)
config
.
InitPluginsConfig
(
config
.
Config
)
return
nil
}
src/modules/prober/cache/collect
_
rule.go
→
src/modules/prober/cache/collectrule.go
浏览文件 @
7161c1ac
文件已移动
src/modules/prober/c
ache/plugins_config
.go
→
src/modules/prober/c
onfig/plugin
.go
浏览文件 @
7161c1ac
package
c
ache
package
c
onfig
import
(
"fmt"
...
...
@@ -6,15 +6,24 @@ import (
"path/filepath"
"strings"
"github.com/didi/nightingale/src/common/dataobj"
"github.com/didi/nightingale/src/modules/monapi/collector"
"github.com/didi/nightingale/src/modules/prober/config"
"github.com/didi/nightingale/src/modules/prober/expr"
"github.com/influxdata/telegraf"
"github.com/toolkits/pkg/logger"
"gopkg.in/yaml.v2"
)
type
MetricConfig
struct
{
var
(
pluginConfigs
map
[
string
]
*
PluginConfig
)
const
(
PluginModeWhitelist
=
iota
PluginModeOverlay
)
type
Metric
struct
{
Name
string
`yaml:"name"`
Type
string
`yaml:"type"`
Comment
string
`yaml:"comment"`
...
...
@@ -23,23 +32,19 @@ type MetricConfig struct {
}
type
PluginConfig
struct
{
Metrics
[]
*
MetricConfig
`yaml:"metrics"`
Mode
string
`yaml:"mode"`
mode
int
`yaml:"-"`
Name
string
Mode
int
Metrics
map
[
string
]
*
Metric
ExprMetrics
map
[
string
]
*
Metric
}
type
CacheP
luginConfig
struct
{
Name
string
Mode
int
Metrics
map
[
string
]
*
MetricConfig
type
p
luginConfig
struct
{
Metrics
[]
*
Metric
`yaml:"metrics"`
Mode
string
`yaml:"mode"`
mode
int
`yaml:"-"`
}
const
(
PluginModeWhitelist
=
iota
PluginModeOverlay
)
func
(
p
*
PluginConfig
)
Validate
()
error
{
func
(
p
*
pluginConfig
)
Validate
()
error
{
switch
strings
.
ToLower
(
p
.
Mode
)
{
case
"whitelist"
:
p
.
mode
=
PluginModeWhitelist
...
...
@@ -51,19 +56,12 @@ func (p *PluginConfig) Validate() error {
return
nil
}
var
(
metricsConfig
map
[
string
]
*
MetricConfig
metricsExpr
map
[
string
]
*
CachePluginConfig
)
func
InitPluginsConfig
(
cf
*
config
.
ConfYaml
)
{
metricsConfig
=
make
(
map
[
string
]
*
MetricConfig
)
metricsExpr
=
make
(
map
[
string
]
*
CachePluginConfig
)
plugins
:=
collector
.
GetRemoteCollectors
()
for
_
,
plugin
:=
range
plugins
{
cacheConfig
:=
newCachePluginConfig
()
config
:=
PluginConfig
{}
metricsExpr
[
plugin
]
=
cacheConfig
func
InitPluginsConfig
(
cf
*
ConfYaml
)
{
pluginConfigs
=
make
(
map
[
string
]
*
PluginConfig
)
for
_
,
plugin
:=
range
collector
.
GetRemoteCollectors
()
{
c
:=
pluginConfig
{}
config
:=
newPluginConfig
()
pluginConfigs
[
plugin
]
=
config
file
:=
filepath
.
Join
(
cf
.
PluginsConfig
,
plugin
+
".yml"
)
b
,
err
:=
ioutil
.
ReadFile
(
file
)
...
...
@@ -72,49 +70,53 @@ func InitPluginsConfig(cf *config.ConfYaml) {
continue
}
if
err
:=
yaml
.
Unmarshal
(
b
,
&
c
onfig
);
err
!=
nil
{
if
err
:=
yaml
.
Unmarshal
(
b
,
&
c
);
err
!=
nil
{
logger
.
Warningf
(
"yaml.Unmarshal %s err %s"
,
plugin
,
err
)
continue
}
if
err
:=
c
onfig
.
Validate
();
err
!=
nil
{
if
err
:=
c
.
Validate
();
err
!=
nil
{
logger
.
Warningf
(
"%s Validate() err %s"
,
plugin
,
err
)
continue
}
c
acheC
onfig
.
Name
=
plugin
c
acheConfig
.
Mode
=
config
.
mode
config
.
Name
=
plugin
c
onfig
.
Mode
=
c
.
mode
for
_
,
v
:=
range
config
.
Metrics
{
if
_
,
ok
:=
metricsConfig
[
v
.
Name
];
ok
{
panic
(
fmt
.
Sprintf
(
"plugin %s metrics %s is already exists"
,
plugin
,
v
.
Name
))
}
if
v
.
Expr
==
""
{
// nomore
metricsConfig
[
v
.
Name
]
=
v
}
else
{
for
_
,
v
:=
range
c
.
Metrics
{
if
v
.
Expr
!=
""
{
err
:=
v
.
parse
()
if
err
!=
nil
{
panic
(
fmt
.
Sprintf
(
"plugin %s metrics %s expr %s parse err %s"
,
plugin
,
v
.
Name
,
v
.
Expr
,
err
))
}
cacheConfig
.
Metrics
[
v
.
Name
]
=
v
config
.
ExprMetrics
[
v
.
Name
]
=
v
}
else
{
config
.
Metrics
[
v
.
Name
]
=
v
}
}
logger
.
Infof
(
"loaded plugin config %s"
,
file
)
}
}
func
(
p
*
Metric
Config
)
parse
()
(
err
error
)
{
func
(
p
*
Metric
)
parse
()
(
err
error
)
{
p
.
notations
,
err
=
expr
.
NewNotations
([]
byte
(
p
.
Expr
))
return
}
func
(
p
*
Metric
Config
)
Calc
(
vars
map
[
string
]
float64
)
(
float64
,
error
)
{
func
(
p
*
Metric
)
Calc
(
vars
map
[
string
]
*
dataobj
.
MetricValue
)
(
float64
,
error
)
{
return
p
.
notations
.
Calc
(
vars
)
}
func
Metric
(
metric
string
,
typ
telegraf
.
ValueType
)
(
c
*
MetricConfig
,
ok
bool
)
{
c
,
ok
=
metricsConfig
[
metric
]
func
GetMetric
(
plugin
,
metric
string
,
typ
telegraf
.
ValueType
)
(
c
*
Metric
,
ok
bool
)
{
p
,
ok
:=
pluginConfigs
[
plugin
]
if
!
ok
{
return
}
c
,
ok
=
p
.
Metrics
[
metric
]
if
!
ok
{
c
,
ok
=
p
.
ExprMetrics
[
metric
]
}
if
!
ok
{
return
}
...
...
@@ -126,8 +128,8 @@ func Metric(metric string, typ telegraf.ValueType) (c *MetricConfig, ok bool) {
return
}
func
Get
MetricExprs
(
pluginName
string
)
(
c
*
Cache
PluginConfig
,
ok
bool
)
{
c
,
ok
=
metricsExpr
[
pluginName
]
func
Get
PluginConfig
(
pluginName
string
)
(
c
*
PluginConfig
,
ok
bool
)
{
c
,
ok
=
pluginConfigs
[
pluginName
]
return
}
...
...
@@ -148,8 +150,9 @@ func metricType(typ telegraf.ValueType) string {
}
}
func
newCachePluginConfig
()
*
CachePluginConfig
{
return
&
CachePluginConfig
{
Metrics
:
map
[
string
]
*
MetricConfig
{},
func
newPluginConfig
()
*
PluginConfig
{
return
&
PluginConfig
{
Metrics
:
map
[
string
]
*
Metric
{},
ExprMetrics
:
map
[
string
]
*
Metric
{},
}
}
src/modules/prober/expr/expr.go
浏览文件 @
7161c1ac
...
...
@@ -7,6 +7,7 @@ import (
"go/token"
"strconv"
"github.com/didi/nightingale/src/common/dataobj"
"github.com/toolkits/pkg/logger"
)
...
...
@@ -61,7 +62,7 @@ func (s *StackFloat) Push(f float64) { *s = append(*s, f) }
func
(
s
*
StackFloat
)
Pop
()
float64
{
n
:=
(
*
s
)[
len
(
*
s
)
-
1
];
*
s
=
(
*
s
)[
:
len
(
*
s
)
-
1
];
return
n
}
func
(
s
*
StackFloat
)
Len
()
int
{
return
len
(
*
s
)
}
func
(
rpn
Notations
)
Calc
(
vars
map
[
string
]
float64
)
(
float64
,
error
)
{
func
(
rpn
Notations
)
Calc
(
vars
map
[
string
]
*
dataobj
.
MetricValue
)
(
float64
,
error
)
{
var
s
StackFloat
for
i
:=
0
;
i
<
rpn
.
Len
();
i
++
{
tn
:=
rpn
[
i
]
...
...
@@ -70,8 +71,8 @@ func (rpn Notations) Calc(vars map[string]float64) (float64, error) {
if
v
,
ok
:=
vars
[
tn
.
tokenVariable
];
!
ok
{
return
0
,
fmt
.
Errorf
(
"variable %s is not set"
,
tn
.
tokenVariable
)
}
else
{
logger
.
Debugf
(
"get %s %f"
,
tn
.
tokenVariable
,
v
)
s
.
Push
(
v
)
logger
.
Debugf
(
"get %s %f"
,
tn
.
tokenVariable
,
v
.
Value
)
s
.
Push
(
v
.
Value
)
}
case
tokenConst
:
s
.
Push
(
tn
.
tokenConst
)
...
...
src/modules/prober/manager/
rule_entity
.go
→
src/modules/prober/manager/
collectrule
.go
浏览文件 @
7161c1ac
...
...
@@ -9,22 +9,22 @@ import (
"github.com/didi/nightingale/src/common/dataobj"
"github.com/didi/nightingale/src/models"
"github.com/didi/nightingale/src/modules/monapi/collector"
"github.com/didi/nightingale/src/modules/prober/c
ache
"
"github.com/didi/nightingale/src/modules/prober/c
onfig
"
"github.com/influxdata/telegraf"
"github.com/toolkits/pkg/logger"
)
// not thread-safe
type
ruleEntity
struct
{
type
collectRule
struct
{
sync
.
RWMutex
telegraf
.
Input
rule
*
models
.
CollectRule
*
models
.
CollectRule
tags
map
[
string
]
string
precision
time
.
Duration
metrics
[]
*
dataobj
.
MetricValue
}
func
new
RuleEntity
(
rule
*
models
.
CollectRule
)
(
*
ruleEntity
,
error
)
{
func
new
CollectRule
(
rule
*
models
.
CollectRule
)
(
*
collectRule
,
error
)
{
c
,
err
:=
collector
.
GetCollector
(
rule
.
CollectType
)
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -40,73 +40,87 @@ func newRuleEntity(rule *models.CollectRule) (*ruleEntity, error) {
return
nil
,
err
}
return
&
ruleEntity
{
Input
:
input
,
rule
:
rule
,
tags
:
tags
,
metrics
:
[]
*
dataobj
.
MetricValue
{},
precision
:
time
.
Second
,
return
&
collectRule
{
Input
:
input
,
CollectRule
:
rule
,
tags
:
tags
,
metrics
:
[]
*
dataobj
.
MetricValue
{},
precision
:
time
.
Second
,
},
nil
}
//
calc metrics with expression
func
(
p
*
ruleEntity
)
calc
()
error
{
//
prepareMetrics
func
(
p
*
collectRule
)
prepareMetrics
()
error
{
if
len
(
p
.
metrics
)
==
0
{
return
nil
}
sample
:=
p
.
metrics
[
0
]
ts
:=
p
.
metrics
[
0
]
.
Timestamp
nid
:=
strconv
.
FormatInt
(
p
.
Nid
,
10
)
configs
,
ok
:=
cache
.
GetMetricExprs
(
p
.
rule
.
CollectType
)
pluginConfig
,
ok
:=
config
.
GetPluginConfig
(
p
.
PluginName
()
)
if
!
ok
{
return
nil
}
vars
:=
map
[
string
]
float64
{}
vars
:=
map
[
string
]
*
dataobj
.
MetricValue
{}
for
_
,
v
:=
range
p
.
metrics
{
logger
.
Debugf
(
"get v[%s] %f"
,
v
.
Metric
,
v
.
Value
)
vars
[
v
.
Metric
]
=
v
.
Value
vars
[
v
.
Metric
]
=
v
}
for
_
,
config
:=
range
configs
.
Metrics
{
f
,
err
:=
config
.
Calc
(
vars
)
p
.
metrics
=
p
.
metrics
[
:
0
]
for
_
,
metric
:=
range
pluginConfig
.
ExprMetrics
{
f
,
err
:=
metric
.
Calc
(
vars
)
if
err
!=
nil
{
logger
.
Debugf
(
"calc err %s"
,
err
)
continue
}
p
.
metrics
=
append
(
p
.
metrics
,
&
dataobj
.
MetricValue
{
Nid
:
sample
.
N
id
,
Metric
:
config
.
Name
,
Timestamp
:
sample
.
Timestamp
,
Step
:
sample
.
Step
,
CounterType
:
config
.
Type
,
TagsMap
:
sample
.
TagsMap
,
Nid
:
n
id
,
Metric
:
metric
.
Name
,
Timestamp
:
ts
,
Step
:
p
.
Step
,
CounterType
:
metric
.
Type
,
TagsMap
:
p
.
tags
,
Value
:
f
,
ValueUntyped
:
f
,
})
}
if
configs
.
Mode
==
cache
.
PluginModeOverlay
{
for
k
,
v
:=
range
vars
{
if
_
,
ok
:=
configs
.
Metrics
[
k
];
ok
{
for
k
,
v
:=
range
vars
{
if
metric
,
ok
:=
pluginConfig
.
Metrics
[
k
];
ok
{
p
.
metrics
=
append
(
p
.
metrics
,
&
dataobj
.
MetricValue
{
Nid
:
nid
,
Metric
:
k
,
Timestamp
:
ts
,
Step
:
p
.
Step
,
CounterType
:
metric
.
Type
,
TagsMap
:
v
.
TagsMap
,
Value
:
v
.
Value
,
ValueUntyped
:
v
.
ValueUntyped
,
})
}
else
{
if
pluginConfig
.
Mode
==
config
.
PluginModeWhitelist
{
continue
}
p
.
metrics
=
append
(
p
.
metrics
,
&
dataobj
.
MetricValue
{
Nid
:
sample
.
N
id
,
Nid
:
n
id
,
Metric
:
k
,
Timestamp
:
sample
.
Timestamp
,
Step
:
sample
.
Step
,
Timestamp
:
ts
,
Step
:
p
.
Step
,
CounterType
:
"GAUGE"
,
TagsMap
:
sample
.
TagsMap
,
Value
:
v
,
ValueUntyped
:
v
,
TagsMap
:
v
.
TagsMap
,
Value
:
v
.
Value
,
ValueUntyped
:
v
.
ValueUntyped
,
})
}
}
return
nil
}
func
(
p
*
ruleEntity
)
update
(
rule
*
models
.
CollectRule
)
error
{
if
p
.
r
ule
.
LastUpdated
==
rule
.
LastUpdated
{
func
(
p
*
collectRule
)
update
(
rule
*
models
.
CollectRule
)
error
{
if
p
.
CollectR
ule
.
LastUpdated
==
rule
.
LastUpdated
{
return
nil
}
...
...
@@ -123,14 +137,14 @@ func (p *ruleEntity) update(rule *models.CollectRule) error {
}
p
.
Input
=
input
p
.
r
ule
=
rule
p
.
CollectR
ule
=
rule
p
.
tags
=
tags
return
nil
}
// https://docs.influxdata.com/telegraf/v1.14/data_formats/output/prometheus/
func
(
p
*
ruleEntity
)
MakeMetric
(
metric
telegraf
.
Metric
)
[]
*
dataobj
.
MetricValue
{
func
(
p
*
collectRule
)
MakeMetric
(
metric
telegraf
.
Metric
)
[]
*
dataobj
.
MetricValue
{
tags
:=
map
[
string
]
string
{}
for
_
,
v
:=
range
metric
.
TagList
()
{
tags
[
v
.
Key
]
=
v
.
Value
...
...
@@ -140,10 +154,8 @@ func (p *ruleEntity) MakeMetric(metric telegraf.Metric) []*dataobj.MetricValue {
tags
[
k
]
=
v
}
nid
:=
strconv
.
FormatInt
(
p
.
rule
.
Nid
,
10
)
name
:=
metric
.
Name
()
ts
:=
metric
.
Time
()
.
Unix
()
step
:=
int64
(
p
.
rule
.
Step
)
// deprecated
fields
:=
metric
.
Fields
()
ms
:=
make
([]
*
dataobj
.
MetricValue
,
0
,
len
(
fields
))
...
...
@@ -153,17 +165,9 @@ func (p *ruleEntity) MakeMetric(metric telegraf.Metric) []*dataobj.MetricValue {
continue
}
c
,
ok
:=
cache
.
Metric
(
name
+
"_"
+
k
,
metric
.
Type
())
if
!
ok
{
continue
}
ms
=
append
(
ms
,
&
dataobj
.
MetricValue
{
Nid
:
nid
,
Metric
:
c
.
Name
,
Metric
:
name
+
"_"
+
k
,
Timestamp
:
ts
,
Step
:
step
,
CounterType
:
c
.
Type
,
TagsMap
:
tags
,
Value
:
f
,
ValueUntyped
:
f
,
...
...
@@ -173,7 +177,7 @@ func (p *ruleEntity) MakeMetric(metric telegraf.Metric) []*dataobj.MetricValue {
return
ms
}
func
(
p
*
ruleEntity
)
AddFields
(
func
(
p
*
collectRule
)
AddFields
(
measurement
string
,
fields
map
[
string
]
interface
{},
tags
map
[
string
]
string
,
...
...
@@ -182,7 +186,7 @@ func (p *ruleEntity) AddFields(
p
.
addFields
(
measurement
,
tags
,
fields
,
telegraf
.
Untyped
,
t
...
)
}
func
(
p
*
ruleEntity
)
AddGauge
(
func
(
p
*
collectRule
)
AddGauge
(
measurement
string
,
fields
map
[
string
]
interface
{},
tags
map
[
string
]
string
,
...
...
@@ -191,7 +195,7 @@ func (p *ruleEntity) AddGauge(
p
.
addFields
(
measurement
,
tags
,
fields
,
telegraf
.
Gauge
,
t
...
)
}
func
(
p
*
ruleEntity
)
AddCounter
(
func
(
p
*
collectRule
)
AddCounter
(
measurement
string
,
fields
map
[
string
]
interface
{},
tags
map
[
string
]
string
,
...
...
@@ -200,7 +204,7 @@ func (p *ruleEntity) AddCounter(
p
.
addFields
(
measurement
,
tags
,
fields
,
telegraf
.
Counter
,
t
...
)
}
func
(
p
*
ruleEntity
)
AddSummary
(
func
(
p
*
collectRule
)
AddSummary
(
measurement
string
,
fields
map
[
string
]
interface
{},
tags
map
[
string
]
string
,
...
...
@@ -209,7 +213,7 @@ func (p *ruleEntity) AddSummary(
p
.
addFields
(
measurement
,
tags
,
fields
,
telegraf
.
Summary
,
t
...
)
}
func
(
p
*
ruleEntity
)
AddHistogram
(
func
(
p
*
collectRule
)
AddHistogram
(
measurement
string
,
fields
map
[
string
]
interface
{},
tags
map
[
string
]
string
,
...
...
@@ -218,20 +222,20 @@ func (p *ruleEntity) AddHistogram(
p
.
addFields
(
measurement
,
tags
,
fields
,
telegraf
.
Histogram
,
t
...
)
}
func
(
p
*
ruleEntity
)
AddMetric
(
m
telegraf
.
Metric
)
{
func
(
p
*
collectRule
)
AddMetric
(
m
telegraf
.
Metric
)
{
m
.
SetTime
(
m
.
Time
()
.
Round
(
p
.
precision
))
if
metrics
:=
p
.
MakeMetric
(
m
);
m
!=
nil
{
p
.
pushMetrics
(
metrics
)
}
}
func
(
p
*
ruleEntity
)
pushMetrics
(
metrics
[]
*
dataobj
.
MetricValue
)
{
func
(
p
*
collectRule
)
pushMetrics
(
metrics
[]
*
dataobj
.
MetricValue
)
{
p
.
Lock
()
defer
p
.
Unlock
()
p
.
metrics
=
append
(
p
.
metrics
,
metrics
...
)
}
func
(
p
*
ruleEntity
)
addFields
(
func
(
p
*
collectRule
)
addFields
(
measurement
string
,
tags
map
[
string
]
string
,
fields
map
[
string
]
interface
{},
...
...
@@ -249,18 +253,18 @@ func (p *ruleEntity) addFields(
// AddError passes a runtime error to the accumulator.
// The error will be tagged with the plugin name and written to the log.
func
(
p
*
ruleEntity
)
AddError
(
err
error
)
{
func
(
p
*
collectRule
)
AddError
(
err
error
)
{
if
err
==
nil
{
return
}
log
.
Print
f
(
"Error in plugin: %v"
,
err
)
log
ger
.
Debug
f
(
"Error in plugin: %v"
,
err
)
}
func
(
p
*
ruleEntity
)
SetPrecision
(
precision
time
.
Duration
)
{
func
(
p
*
collectRule
)
SetPrecision
(
precision
time
.
Duration
)
{
p
.
precision
=
precision
}
func
(
p
*
ruleEntity
)
getTime
(
t
[]
time
.
Time
)
time
.
Time
{
func
(
p
*
collectRule
)
getTime
(
t
[]
time
.
Time
)
time
.
Time
{
var
timestamp
time
.
Time
if
len
(
t
)
>
0
{
timestamp
=
t
[
0
]
...
...
@@ -270,6 +274,6 @@ func (p *ruleEntity) getTime(t []time.Time) time.Time {
return
timestamp
.
Round
(
p
.
precision
)
}
func
(
p
*
ruleEntity
)
WithTracking
(
maxTracked
int
)
telegraf
.
TrackingAccumulator
{
func
(
p
*
collectRule
)
WithTracking
(
maxTracked
int
)
telegraf
.
TrackingAccumulator
{
return
nil
}
src/modules/prober/manager/manager.go
浏览文件 @
7161c1ac
...
...
@@ -16,20 +16,20 @@ import (
)
type
manager
struct
{
ctx
context
.
Context
cache
*
cache
.
CollectRuleCache
config
*
config
.
ConfYaml
heap
ruleSummaryHeap
index
map
[
int64
]
*
ruleEntity
// add at cache.C , del at executeAt check
worker
[]
worker
tx
chan
*
ruleEntity
ctx
context
.
Context
cache
*
cache
.
CollectRuleCache
config
*
config
.
ConfYaml
heap
ruleSummaryHeap
index
map
[
int64
]
*
collectRule
// add at cache.C , del at executeAt check
worker
[]
worker
collectRuleCh
chan
*
collectRule
}
func
NewManager
(
cfg
*
config
.
ConfYaml
,
cache
*
cache
.
CollectRuleCache
)
*
manager
{
return
&
manager
{
cache
:
cache
,
config
:
cfg
,
index
:
make
(
map
[
int64
]
*
ruleEntity
),
index
:
make
(
map
[
int64
]
*
collectRule
),
}
}
...
...
@@ -37,15 +37,13 @@ func (p *manager) Start(ctx context.Context) error {
workerProcesses
:=
p
.
config
.
WorkerProcesses
p
.
ctx
=
ctx
p
.
tx
=
make
(
chan
*
ruleEntity
,
1
)
p
.
collectRuleCh
=
make
(
chan
*
collectRule
,
1
)
heap
.
Init
(
&
p
.
heap
)
p
.
worker
=
make
([]
worker
,
workerProcesses
)
for
i
:=
0
;
i
<
workerProcesses
;
i
++
{
p
.
worker
[
i
]
.
rx
=
p
.
tx
p
.
worker
[
i
]
.
collectRuleCh
=
p
.
collectRuleCh
p
.
worker
[
i
]
.
ctx
=
ctx
// p.worker[i].acc = p.acc
p
.
worker
[
i
]
.
loop
(
i
)
}
...
...
@@ -65,7 +63,7 @@ func (p *manager) loop() {
case
<-
p
.
ctx
.
Done
()
:
return
case
<-
p
.
cache
.
C
:
if
err
:=
p
.
Sync
Rules
();
err
!=
nil
{
if
err
:=
p
.
Add
Rules
();
err
!=
nil
{
log
.
Printf
(
"manager.SyncRules err %s"
,
err
)
}
case
<-
tick
.
C
:
...
...
@@ -89,36 +87,37 @@ func (p *manager) schedule() error {
}
summary
:=
heap
.
Pop
(
&
p
.
heap
)
.
(
*
ruleSummary
)
rule
,
ok
:=
p
.
cache
.
Get
(
summary
.
id
)
rule
Config
,
ok
:=
p
.
cache
.
Get
(
summary
.
id
)
if
!
ok
{
// drop it if not exist in cache
delete
(
p
.
index
,
summary
.
id
)
continue
}
entity
,
ok
:=
p
.
index
[
rule
.
Id
]
rule
,
ok
:=
p
.
index
[
ruleConfig
.
Id
]
if
!
ok
{
// impossible
log
.
Printf
(
"manager.index[%d] not exists"
,
rule
.
Id
)
// let's fix it
p
.
index
[
entity
.
rule
.
Id
]
=
entity
log
.
Printf
(
"manager.index[%d] not exists"
,
ruleConfig
.
Id
)
continue
}
// update rule
if
err
:=
entity
.
update
(
rule
);
err
!=
nil
{
if
err
:=
rule
.
update
(
ruleConfig
);
err
!=
nil
{
logger
.
Warningf
(
"ruleEntity update err %s"
,
err
)
}
p
.
tx
<-
entity
p
.
collectRuleCh
<-
rule
summary
.
executeAt
=
now
+
int64
(
rule
.
Step
)
summary
.
executeAt
=
now
+
int64
(
rule
Config
.
Step
)
heap
.
Push
(
&
p
.
heap
,
summary
)
continue
}
}
func
(
p
*
manager
)
SyncRules
()
error
{
// AddRules add new rule to p.index from cache
// update / cleanup will be done by p.schedule() -> ruleEntity.update()
func
(
p
*
manager
)
AddRules
()
error
{
for
_
,
v
:=
range
p
.
cache
.
GetAll
()
{
if
_
,
ok
:=
p
.
index
[
v
.
Id
];
!
ok
{
p
.
AddRule
(
v
)
...
...
@@ -128,7 +127,7 @@ func (p *manager) SyncRules() error {
}
func
(
p
*
manager
)
AddRule
(
rule
*
models
.
CollectRule
)
error
{
ruleEntity
,
err
:=
new
RuleEntity
(
rule
)
ruleEntity
,
err
:=
new
CollectRule
(
rule
)
if
err
!=
nil
{
return
err
}
...
...
@@ -141,11 +140,6 @@ func (p *manager) AddRule(rule *models.CollectRule) error {
return
nil
}
type
collectRule
interface
{
telegraf
.
Input
tags
()
map
[
string
]
string
}
func
telegrafInput
(
rule
*
models
.
CollectRule
)
(
telegraf
.
Input
,
error
)
{
c
,
err
:=
collector
.
GetCollector
(
rule
.
CollectType
)
if
err
!=
nil
{
...
...
@@ -155,9 +149,9 @@ func telegrafInput(rule *models.CollectRule) (telegraf.Input, error) {
}
type
worker
struct
{
ctx
context
.
Context
cache
*
cache
.
CollectRuleCache
rx
chan
*
ruleEntity
ctx
context
.
Context
cache
*
cache
.
CollectRuleCache
collectRuleCh
chan
*
collectRule
}
func
(
p
*
worker
)
loop
(
id
int
)
{
...
...
@@ -166,8 +160,8 @@ func (p *worker) loop(id int) {
select
{
case
<-
p
.
ctx
.
Done
()
:
return
case
entity
:=
<-
p
.
rx
:
if
err
:=
p
.
do
(
entity
);
err
!=
nil
{
case
rule
:=
<-
p
.
collectRuleCh
:
if
err
:=
p
.
do
(
rule
);
err
!=
nil
{
log
.
Printf
(
"work[%d].do err %s"
,
id
,
err
)
}
}
...
...
@@ -175,20 +169,20 @@ func (p *worker) loop(id int) {
}()
}
func
(
p
*
worker
)
do
(
entity
*
ruleEntity
)
error
{
entity
.
metrics
=
entity
.
metrics
[
:
0
]
func
(
p
*
worker
)
do
(
rule
*
collectRule
)
error
{
rule
.
metrics
=
rule
.
metrics
[
:
0
]
// telegraf
err
:=
entity
.
Input
.
Gather
(
entity
)
if
len
(
entity
.
metrics
)
==
0
{
err
:=
rule
.
Input
.
Gather
(
rule
)
if
len
(
rule
.
metrics
)
==
0
{
return
err
}
// eval expression metrics
entity
.
calc
()
rule
.
prepareMetrics
()
// send
core
.
Push
(
entity
.
metrics
)
core
.
Push
(
rule
.
metrics
)
return
err
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录