func TestExamplePrograms(t *testing.T) { if testing.Short() { t.Skip("skipping test in short mode") } for _, tc := range exampleProgramTests { w := watcher.NewFakeWatcher() o := mtail.Options{Progs: tc.programfile, W: w} mtail, err := mtail.New(o) if err != nil { t.Fatalf("create mtail failed: %s", err) } if err := mtail.OneShot(tc.logfile); err != nil { t.Errorf("Oneshot failed for %s: %s", tc.logfile, err) continue } // Dirty hack to create json files :) if false { j, err := os.Create(tc.jsonfile) if err != nil { t.Errorf("%s: could not open json file: %s", tc.jsonfile, err) continue } defer j.Close() if err := mtail.WriteMetrics(j); err != nil { t.Errorf("couldn't marshall metrics: %q", err) continue } } j, err := os.Open(tc.jsonfile) if err != nil { t.Fatalf("%s: could not open json file: %s", tc.jsonfile, err) } defer j.Close() var m, ex bytes.Buffer mtail.Close() mtail.WriteMetrics(&m) m.WriteString("\n") // Golden data has trailing newline. if _, err := ex.ReadFrom(j); err != nil { t.Fatalf("Coldn't read from json: %s", err) } diff := pretty.Compare(m.String(), ex.String()) if len(diff) > 0 { t.Errorf("%s: metrics don't match:\n%s", tc.programfile, diff) } } }
func main() { flag.Parse() if *progs == "" { glog.Exitf("No mtail program directory specified; use -progs") } if *logs == "" && *logFds == "" { glog.Exitf("No logs specified to tail; use -logs or -logfds") } var logPathnames []string for _, pathname := range strings.Split(*logs, ",") { if pathname != "" { logPathnames = append(logPathnames, pathname) } } var logDescriptors []int for _, fdStr := range strings.Split(*logFds, ",") { fdNum, err := strconv.Atoi(fdStr) if err == nil { logDescriptors = append(logDescriptors, fdNum) } } if len(logPathnames) == 0 && len(logDescriptors) == 0 { glog.Exit("No logs to tail.") } o := mtail.Options{ Progs: *progs, LogPaths: logPathnames, LogFds: logDescriptors, Port: *port, OneShot: *oneShot, OneShotMetrics: *oneShotMetrics, CompileOnly: *compileOnly, DumpBytecode: *dumpBytecode, SyslogUseCurrentYear: *syslogUseCurrentYear, } m, err := mtail.New(o) if err != nil { glog.Fatalf("couldn't start: %s", err) } m.Run() }
func benchmarkProgram(b *testing.B, programfile string, logfile string) { w := watcher.NewFakeWatcher() o := mtail.Options{Progs: programfile, W: w} mtail, err := mtail.New(o) if err != nil { b.Fatalf("Failed to create mtail: %s", err) } b.ResetTimer() for i := 0; i < b.N; i++ { if err := mtail.OneShot(logfile); err != nil { b.Errorf("OneShot log parse failed: %s", err) } } b.StopTimer() mtail.Close() l, err := strconv.ParseInt(vm.LineCount.String(), 10, 64) if err != nil { b.Fatalf("strconv.ParseInt failed: %s", err) return } b.SetBytes(l) }