func TestBlacklisted(t *testing.T) { // Setup Filter config := coco.FilterConfig{ Blacklist: "/(vmem|irq|entropy|users)/", } raw := make(chan collectd.Packet) filtered := make(chan collectd.Packet) items := make(chan coco.BlacklistItem, 1000000) go coco.Filter(config, raw, filtered, items) // Setup blacklist blacklisted := map[string]map[string]int64{} go coco.Blacklist(items, &blacklisted) // Setup Api apiConfig := coco.ApiConfig{ Bind: "127.0.0.1:26082", } var tiers []coco.Tier go coco.Api(apiConfig, &tiers, &blacklisted) poll(t, apiConfig.Bind) // Push 10 metrics through that should be blacklisted for i := 0; i < 10; i++ { raw <- collectd.Packet{ Hostname: "foo", Plugin: "irq", Type: "irq", TypeInstance: strconv.Itoa(i), } } // Fetch blacklisted metrics resp, err := http.Get("http://127.0.0.1:26082/blacklisted") if err != nil { t.Fatalf("HTTP GET failed: %s", err) } body, err := ioutil.ReadAll(resp.Body) var result map[string]interface{} err = json.Unmarshal(body, &result) if err != nil { t.Errorf("Error when decoding JSON %+v.", err) t.Errorf("Response body: %s", string(body)) t.FailNow() } // Test the metrics have been blacklisted count := len(result["foo"].(map[string]interface{})) expected := 10 if count != expected { t.Errorf("Expected %d blacklisted metrics, got %d", count, expected) } }
func main() { kingpin.Version("1.0.0") kingpin.Parse() var config coco.Config if _, err := toml.DecodeFile(*configPath, &config); err != nil { log.Fatalln("fatal:", err) return } // Setup data structures to be shared across components blacklisted := map[string]map[string]int64{} raw := make(chan collectd.Packet, 1000000) filtered := make(chan collectd.Packet, 1000000) items := make(chan coco.BlacklistItem, 1000000) var tiers []coco.Tier for k, v := range config.Tiers { tier := coco.Tier{Name: k, Targets: v.Targets} tiers = append(tiers, tier) } if len(tiers) == 0 { log.Fatal("No tiers configured. Exiting.") } chans := map[string]chan collectd.Packet{ "raw": raw, "filtered": filtered, //"blacklist_items": items, } go coco.Measure(config.Measure, chans, &tiers) // Launch components to do the work go coco.Listen(config.Listen, raw) for i := 0; i < 4; i++ { go coco.Filter(config.Filter, raw, filtered, items) } go coco.Blacklist(items, &blacklisted) go coco.Send(&tiers, filtered) coco.Api(config.Api, &tiers, &blacklisted) }
/* Filter - Generate metric name - increment counter */ func TestFilterBlacklistsSamples(t *testing.T) { // Setup config := coco.FilterConfig{ Blacklist: "/(vmem|irq|entropy|users)/", } raw := make(chan collectd.Packet) filtered := make(chan collectd.Packet) blacklist := make(chan coco.BlacklistItem, 1000) go coco.Filter(config, raw, filtered, blacklist) count := 0 go func() { for { <-filtered count += 1 } }() // Test types := []string{"free", "used", "shared", "cached"} for _, name := range types { raw <- collectd.Packet{ Hostname: "foo", Plugin: "memory", Type: "memory", TypeInstance: name, } } for i := 0; i < 10; i++ { raw <- collectd.Packet{ Hostname: "foo", Plugin: "irq", Type: "irq", TypeInstance: "7", } } if count != len(types) { t.Errorf("Expected %d packets, got %d", len(types), count) } }