Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

agent 加入根据正则表达式排除 metrics 的能力 #58

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions cfg.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,14 @@
"collector": {
"ifacePrefix": ["eth", "em"]
},
"addTags":{
"region": "cn"
},
"ignore": {
"cpu.busy": true,
"df.bytes.free": true,
"df.bytes.total": true,
"df.bytes.used": true,
"df.bytes.used.percent": true,
"df.inodes.total": true,
"df.inodes.free": true,
"df.inodes.used": true,
"df.inodes.used.percent": true,
"mem.memtotal": true,
"mem.memused": true,
"mem.memused.percent": true,
"mem.memfree": true,
"mem.swaptotal": true,
"mem.swapused": true,
"mem.swapfree": true
"^cpu\\.busy": "",
"^df\\.(bytes|inodes)\\.(total|free|used(\\.percent)?)": "",
"^mem\\.(swap|mem)(total|free|used(\\.percent)?)": "",

"^df\\.": "^.*mount=.*/docker/.*$",
}
}
25 changes: 12 additions & 13 deletions cron/collector.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cron

import (
"log"
"time"

"github.com/open-falcon/agent/funcs"
Expand Down Expand Up @@ -42,7 +43,7 @@ func collect(sec int64, fns []func() []*model.MetricValue) {
}

mvs := []*model.MetricValue{}
ignoreMetrics := g.Config().IgnoreMetrics
debug := g.Config().Debug

for _, fn := range fns {
items := fn()
Expand All @@ -54,23 +55,21 @@ func collect(sec int64, fns []func() []*model.MetricValue) {
continue
}

for _, mv := range items {
if b, ok := ignoreMetrics[mv.Metric]; ok && b {
continue
} else {
mvs = append(mvs, mv)
}
if debug {
log.Println(" -> collect ", len(items), " metrics\n")
}
mvs = append(mvs, items...)
}

filtered := *g.FilterMetrics(&mvs)

now := time.Now().Unix()
for j := 0; j < len(mvs); j++ {
mvs[j].Step = sec
mvs[j].Endpoint = hostname
mvs[j].Timestamp = now
for j := 0; j < len(filtered); j++ {
filtered[j].Step = sec
filtered[j].Endpoint = hostname
filtered[j].Timestamp = now
}

g.SendToTransfer(mvs)

g.SendToTransfer(filtered)
}
}
25 changes: 16 additions & 9 deletions g/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"log"
"os"
"regexp"
"sync"

"github.com/toolkits/file"
Expand Down Expand Up @@ -41,15 +42,16 @@ type CollectorConfig struct {
}

type GlobalConfig struct {
Debug bool `json:"debug"`
Hostname string `json:"hostname"`
IP string `json:"ip"`
Plugin *PluginConfig `json:"plugin"`
Heartbeat *HeartbeatConfig `json:"heartbeat"`
Transfer *TransferConfig `json:"transfer"`
Http *HttpConfig `json:"http"`
Collector *CollectorConfig `json:"collector"`
IgnoreMetrics map[string]bool `json:"ignore"`
Debug bool `json:"debug"`
Hostname string `json:"hostname"`
IP string `json:"ip"`
Plugin *PluginConfig `json:"plugin"`
Heartbeat *HeartbeatConfig `json:"heartbeat"`
Transfer *TransferConfig `json:"transfer"`
Http *HttpConfig `json:"http"`
Collector *CollectorConfig `json:"collector"`
Ignore map[string]string `json:"ignore"`
AddTags map[string]string `json:"addTags"`
}

var (
Expand Down Expand Up @@ -113,6 +115,11 @@ func ParseConfig(cfg string) {
log.Fatalln("parse config file:", cfg, "fail:", err)
}

for k, v := range c.Ignore {
regexp.MustCompile(k)
regexp.MustCompile(v)
}

lock.Lock()
defer lock.Unlock()

Expand Down
52 changes: 52 additions & 0 deletions g/metric.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package g

import (
"log"
"regexp"

"github.com/open-falcon/common/model"
"github.com/open-falcon/common/utils"
)

var cachedRegexp map[string]*regexp.Regexp = make(map[string]*regexp.Regexp)

func cachedMatch(re string, tags string) bool {
r, ok := cachedRegexp[re]
if !ok {
r = regexp.MustCompile(re)
cachedRegexp[re] = r
}
return r.MatchString(tags)
}

func FilterMetrics(metrics *[]*model.MetricValue) *[]*model.MetricValue {
addTags := Config().AddTags
ignore := Config().Ignore
debug := Config().Debug

filtered := make([]*model.MetricValue, 0)

metricsLoop:
for _, mv := range *metrics {
for metricRe, tagsRe := range ignore {
if cachedMatch(metricRe, mv.Metric) && cachedMatch(tagsRe, mv.Tags) {
if debug {
log.Println("=> Filtered metric", mv.Metric, "/", mv.Tags, "by rule", metricRe, "=>", tagsRe)
}
continue metricsLoop
}
}

if addTags != nil {
tags := utils.DictedTagstring(mv.Tags)
for k, v := range addTags {
if _, ok := tags[k]; !ok {
tags[k] = v
}
}
mv.Tags = utils.SortedTags(tags)
}
filtered = append(filtered, mv)
}
return &filtered
}
3 changes: 2 additions & 1 deletion http/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ func configPushRoutes() {
return
}

g.SendToTransfer(metrics)
filtered := *g.FilterMetrics(&metrics)
g.SendToTransfer(filtered)
w.Write([]byte("success"))
})
}
3 changes: 2 additions & 1 deletion plugins/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,6 @@ func PluginRun(plugin *Plugin) {
return
}

g.SendToTransfer(metrics)
filtered := *g.FilterMetrics(&metrics)
g.SendToTransfer(filtered)
}