func TestCollectorPrefix(t *testing.T) { logrus.SetLevel(logrus.ErrorLevel) c := make(map[string]interface{}) c["interval"] = 1 c["prefix"] = "px." collector := collector.New("Test") collector.SetInterval(1) collector.Configure(c) collectorChannel := map[string]chan metric.Metric{ "Test": make(chan metric.Metric), } testHandler := handler.New("Log") testHandler.SetCollectorChannels(collectorChannel) var wg sync.WaitGroup wg.Add(2) go func() { defer wg.Done() collector.Channel() <- metric.New("hello") close(collector.Channel()) }() go func() { defer wg.Done() testMetric := <-collectorChannel["Test"] assert.Equal(t, "px.hello", testMetric.Name) }() readFromCollector(collector, []handler.Handler{testHandler}) wg.Wait() }
func TestReadFromCollector(t *testing.T) { logrus.SetLevel(logrus.ErrorLevel) c := make(map[string]interface{}) c["interval"] = 1 collector := collector.New("Test") collector.SetInterval(1) collector.Configure(c) var wg sync.WaitGroup wg.Add(2) collectorStatChannel := make(chan metric.CollectorEmission) go func() { defer wg.Done() collector.Channel() <- metric.New("hello") time.Sleep(time.Duration(2) * time.Second) m2 := metric.New("world") m2.AddDimension("collectorCanonicalName", "Foobar") collector.Channel() <- m2 time.Sleep(time.Duration(2) * time.Second) m2.AddDimension("collectorCanonicalName", "Foobar") collector.Channel() <- m2 close(collector.Channel()) }() collectorMetrics := map[string]uint64{} go func() { defer wg.Done() for collectorMetric := range collectorStatChannel { collectorMetrics[collectorMetric.Name] = collectorMetric.EmissionCount } }() readFromCollector(collector, []handler.Handler{}, collectorStatChannel) wg.Wait() assert.Equal(t, uint64(1), collectorMetrics["Test"]) assert.Equal(t, uint64(2), collectorMetrics["Foobar"]) }