func TestMetricToStatsd(t *testing.T) { ts, terr := time.Parse("2006/01/02 15:04:05", "2012/07/24 10:14:00") if terr != nil { t.Errorf("time parse error: %s", terr) } scalarMetric := metrics.NewMetric("foo", "prog", metrics.Counter) d, _ := scalarMetric.GetDatum() d.Set(37, ts) r := FakeSocketWrite(metricToStatsd, scalarMetric) expected := []string{"prog.foo:37|c"} if !reflect.DeepEqual(expected, r) { t.Errorf("String didn't match:\n\texpected: %v\n\treceived: %v", expected, r) } dimensionedMetric := metrics.NewMetric("bar", "prog", metrics.Gauge, "l") d, _ = dimensionedMetric.GetDatum("quux") d.Set(37, ts) d, _ = dimensionedMetric.GetDatum("snuh") d.Set(42, ts) r = FakeSocketWrite(metricToStatsd, dimensionedMetric) expected = []string{ "prog.bar.l.quux:37|c", "prog.bar.l.snuh:42|c"} if !reflect.DeepEqual(expected, r) { t.Errorf("String didn't match:\n\texpected: %v\n\treceived: %v", expected, r) } }
func TestMetricToGraphite(t *testing.T) { ts, terr := time.Parse("2006/01/02 15:04:05", "2012/07/24 10:14:00") if terr != nil { t.Errorf("time parse error: %s", terr) } scalarMetric := metrics.NewMetric("foo", "prog", metrics.Counter) d, _ := scalarMetric.GetDatum() d.Set(37, ts) r := FakeSocketWrite(metricToGraphite, scalarMetric) expected := []string{"prog.foo 37 1343124840\n"} diff := pretty.Compare(r, expected) if len(diff) > 0 { t.Errorf("String didn't match:\n%s", diff) } dimensionedMetric := metrics.NewMetric("bar", "prog", metrics.Gauge, "l") d, _ = dimensionedMetric.GetDatum("quux") d.Set(37, ts) d, _ = dimensionedMetric.GetDatum("snuh") d.Set(37, ts) r = FakeSocketWrite(metricToGraphite, dimensionedMetric) expected = []string{ "prog.bar.l.quux 37 1343124840\n", "prog.bar.l.snuh 37 1343124840\n"} diff = pretty.Compare(r, expected) if len(diff) > 0 { t.Errorf("String didn't match:\n%s", diff) } }
// TestInstrs tests that each instruction behaves as expected through one // instruction cycle. func TestInstrs(t *testing.T) { for _, tc := range instructions { var m []*metrics.Metric m = append(m, metrics.NewMetric("foo", "test", metrics.Counter), metrics.NewMetric("bar", "test", metrics.Counter)) v := New(tc.name, tc.re, tc.str, m, []instr{tc.i}, true) v.t = new(thread) v.t.stack = make([]interface{}, 0) for _, item := range tc.reversedStack { v.t.Push(item) } v.t.matches = make(map[int][]string, 0) v.input = "aaaab" v.execute(v.t, tc.i) diff := pretty.Compare(v.t.stack, tc.expectedStack) if len(diff) > 0 { t.Errorf("%s: unexpected virtual machine stack state.\n%s", tc.name, diff) } // patch in the thread stack because otherwise the test table is huge tc.expectedThread.stack = tc.expectedStack if diff = pretty.Compare(v.t, &tc.expectedThread); len(diff) > 0 { t.Errorf("%s: unexpected virtual machine thread state.\n%s", tc.name, diff) } } }
func TestMetricToCollectd(t *testing.T) { ts, terr := time.Parse("2006/01/02 15:04:05", "2012/07/24 10:14:00") if terr != nil { t.Errorf("time parse error: %s", terr) } ms := metrics.Store{} scalarMetric := metrics.NewMetric("foo", "prog", metrics.Counter) d, _ := scalarMetric.GetDatum() d.Set(37, ts) ms.Add(scalarMetric) r := FakeSocketWrite(metricToCollectd, scalarMetric) expected := []string{"PUTVAL \"gunstar/mtail-prog/counter-foo\" interval=60 1343124840:37\n"} diff := pretty.Compare(r, expected) if len(diff) > 0 { t.Errorf("String didn't match:\n%s", diff) } dimensionedMetric := metrics.NewMetric("bar", "prog", metrics.Gauge, "label") d, _ = dimensionedMetric.GetDatum("quux") d.Set(37, ts) d, _ = dimensionedMetric.GetDatum("snuh") d.Set(37, ts) ms.ClearMetrics() ms.Add(dimensionedMetric) r = FakeSocketWrite(metricToCollectd, dimensionedMetric) expected = []string{ "PUTVAL \"gunstar/mtail-prog/gauge-bar-label-quux\" interval=60 1343124840:37\n", "PUTVAL \"gunstar/mtail-prog/gauge-bar-label-snuh\" interval=60 1343124840:37\n"} diff = pretty.Compare(r, expected) if len(diff) > 0 { t.Errorf("String didn't match:\n%s", diff) } }