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) } var lines int64 b.ResetTimer() for i := 0; i < b.N; i++ { count, err := mtail.OneShot(logfile, false) if err != nil { b.Errorf("OneShot log parse failed: %s", err) } lines += count } b.StopTimer() mtail.Close() if err != nil { b.Fatalf("strconv.ParseInt failed: %s", err) return } b.SetBytes(lines) }
func TestExamplePrograms(t *testing.T) { if testing.Short() { t.Skip("skipping test in short mode") } for _, tc := range exampleProgramTests { w := watcher.NewFakeWatcher() store := &metrics.Store{} o := mtail.Options{Progs: tc.programfile, W: w, Store: store} 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 } if err := mtail.WriteMetrics(j); err != nil { t.Errorf("couldn't marshall metrics: %q", err) continue } j.Close() } 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 ex bytes.Buffer if _, err := ex.ReadFrom(j); err != nil { t.Fatalf("Couldn't read from json: %s", err) } mtail.Close() var m bytes.Buffer mtail.WriteMetrics(&m) m.WriteString("\n") // Golden data has trailing newline. diff := pretty.Compare( strings.Split(ex.String(), "\n"), // want strings.Split(m.String(), "\n")) // got if len(diff) > 0 { t.Errorf("%s: metrics don't match:\n%s", tc.programfile, diff) t.Errorf("Store metrics: %#v", store.Metrics) } } }
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 TestExamplePrograms(t *testing.T) { if testing.Short() { t.Skip("skipping test in short mode") } for _, tc := range exampleProgramTests { w := watcher.NewFakeWatcher() store := metrics.NewStore() o := mtail.Options{Progs: tc.programfile, W: w, Store: store} mtail, err := mtail.New(o) if err != nil { t.Fatalf("create mtail failed: %s", err) } if _, err := mtail.OneShot(tc.logfile, false); err != nil { t.Errorf("Oneshot failed for %s: %s", tc.logfile, 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() golden_store := metrics.NewStore() testdata.ReadTestData(j, tc.programfile, golden_store) mtail.Close() diff := pretty.Compare(golden_store, store) if len(diff) > 0 { t.Errorf("%s: metrics don't match:\n%s", tc.programfile, diff) t.Errorf("Store metrics: %#v", store.Metrics) } } }
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) }
func main() { flag.Parse() if *progs == "" { glog.Exitf("No mtail program directory specified; use -progs") } if *logs == "" { glog.Exitf("No logs specified to tail; use -logs") } var logPathnames []string for _, pathname := range strings.Split(*logs, ",") { if pathname != "" { logPathnames = append(logPathnames, pathname) } } if len(logPathnames) == 0 { glog.Exit("No logs to tail.") } o := mtail.Options{ Progs: *progs, LogPaths: logPathnames, Port: *port, } m, err := mtail.New(o) if err != nil { glog.Fatalf("couldn't start: %s", err) } c := newMtailCollector(m) prometheus.MustRegister(c) go monitor() http.Handle("/metrics", prometheus.Handler()) log.Fatal(http.ListenAndServe(":"+*port, nil)) }