예제 #1
0
/*
Send
 - Hash lookup
 - Encode a packet
*/
func TestSend(t *testing.T) {
	// Setup listener
	listenConfig := coco.ListenConfig{
		Bind:    "127.0.0.1:25887",
		Typesdb: "../types.db",
	}
	samples := make(chan collectd.Packet)
	go coco.Listen(listenConfig, samples)

	var receive collectd.Packet
	done := make(chan bool)
	go func() {
		receive = <-samples
		// https://ariejan.net/2014/08/29/synchronize-goroutines-in-your-tests/
		done <- true
	}()

	// Setup sender
	tierConfig := make(map[string]coco.TierConfig)
	tierConfig["a"] = coco.TierConfig{Targets: []string{listenConfig.Bind}}

	var tiers []coco.Tier
	for k, v := range tierConfig {
		tier := coco.Tier{Name: k, Targets: v.Targets}
		tiers = append(tiers, tier)
	}
	t.Logf("tiers: %+v\n", tiers)

	filtered := make(chan collectd.Packet)
	go coco.Send(&tiers, filtered)

	// Test dispatch
	send := collectd.Packet{
		Hostname: "foo",
		Plugin:   "load",
		Type:     "load",
	}

	filtered <- send
	<-done

	if send.Hostname != receive.Hostname {
		t.Errorf("Expected %s got %s", send.Hostname, receive.Hostname)
	}
	if send.Plugin != receive.Plugin {
		t.Errorf("Expected %s got %s", send.Plugin, receive.Plugin)
	}
	if send.Type != receive.Type {
		t.Errorf("Expected %s got %s", send.Type, receive.Type)
	}
}
예제 #2
0
func TestSendTiers(t *testing.T) {
	// Setup listen
	listenConfig := coco.ListenConfig{
		Bind:    "127.0.0.1:25888",
		Typesdb: "../types.db",
	}
	raw := make(chan collectd.Packet)
	go coco.Listen(listenConfig, raw)

	count := 0
	go func() {
		for {
			<-raw
			count += 1
		}
	}()

	// Setup sender
	tierConfig := make(map[string]coco.TierConfig)
	tierConfig["a"] = coco.TierConfig{Targets: []string{"127.0.0.1:25888"}}
	tierConfig["b"] = coco.TierConfig{Targets: []string{"127.0.0.1:25888"}}
	tierConfig["c"] = coco.TierConfig{Targets: []string{"127.0.0.1:25888"}}

	var tiers []coco.Tier
	for k, v := range tierConfig {
		tier := coco.Tier{Name: k, Targets: v.Targets}
		tiers = append(tiers, tier)
	}

	filtered := make(chan collectd.Packet)
	go coco.Send(&tiers, filtered)

	// Test dispatch
	send := collectd.Packet{
		Hostname: "foo",
		Plugin:   "load",
		Type:     "load",
	}

	filtered <- send

	// Breathe a moment so packet works its way through
	time.Sleep(100 * time.Millisecond)
	if count != len(tierConfig) {
		t.Errorf("Expected %d packets, got %d", len(tierConfig), count)
	}
}
예제 #3
0
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)
}
예제 #4
0
func TestEncodeGauge(t *testing.T) {
	// Setup listen
	listenConfig := coco.ListenConfig{
		Bind:    "127.0.0.1:25961",
		Typesdb: "../types.db",
	}
	raw := make(chan collectd.Packet, 500)
	go coco.Listen(listenConfig, raw)

	// Setup sender
	tierConfig := make(map[string]coco.TierConfig)
	tierConfig["a"] = coco.TierConfig{Targets: []string{listenConfig.Bind}}

	var tiers []coco.Tier
	for k, v := range tierConfig {
		tier := coco.Tier{Name: k, Targets: v.Targets}
		tiers = append(tiers, tier)
	}

	filtered := make(chan collectd.Packet)
	go coco.Send(&tiers, filtered)

	// Dispatch a sample
	value := collectd.Value{
		Name:     "shortterm",
		Type:     uint8(1),
		TypeName: "gauge",
		Value:    0.5,
	}
	sample := collectd.Packet{
		Hostname: "foo",
		Plugin:   "load",
		Type:     "load",
		Interval: 10,
		Time:     uint64(time.Now().Unix()),
		Values:   []collectd.Value{value},
	}
	filtered <- sample

	// Breathe a moment so packet works its way through
	time.Sleep(100 * time.Millisecond)
	if len(raw) != 1 {
		t.Errorf("Expected %d packets, got %d\n", 1, len(raw))
		t.FailNow()
	}

	// Grab a sample
	s := <-raw
	if len(s.Values) != 1 {
		t.Errorf("Expected %d values in sample, got %d\n", 1, len(s.Values))
		t.FailNow()
	}

	v := s.Values[0]
	t.Logf("before: %+v\n", value)
	t.Logf("after:  %+v\n", v)

	if value.Value != v.Value {
		t.Errorf("Expected value to be %d, got %d\n", value.Value, v.Value)
		t.FailNow()
	}
}