From c0fd651a5c2680067b400037a6cbdcffff198853 Mon Sep 17 00:00:00 2001 From: Shinichi Ishimura Date: Mon, 15 Jul 2019 05:59:48 +0900 Subject: [PATCH 1/3] Change option name of the version command. --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 0954054..540a118 100644 --- a/main.go +++ b/main.go @@ -24,7 +24,7 @@ var ( limit = flag.Int("l", 1000, "Number of logs fetched at once.") fromStr = flag.String("from", "", "Minimum timestamp for requested logs, should be an ISO-8601 string.") toStr = flag.String("to", "", "Maximum timestamp for requested logs, should be an ISO-8601 string.") - version = flag.Bool("V", false, "Show version of taildog") + version = flag.Bool("version", false, "Show version of taildog.") apiKey = getEnv("DD_API_KEY") appKey = getEnv("DD_APP_KEY") From c279e4e62bf1df1620bab9c6fedde27e21e75ee1 Mon Sep 17 00:00:00 2001 From: Shinichi Ishimura Date: Tue, 16 Jul 2019 11:40:38 +0900 Subject: [PATCH 2/3] Bump up minor version --- version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.go b/version.go index dd2482c..add2721 100644 --- a/version.go +++ b/version.go @@ -1,3 +1,3 @@ package main -var VERSION = "0.1.2" +var VERSION = "0.2.0" From aef072d48faf2ec2fe25454fd96279827f51f035 Mon Sep 17 00:00:00 2001 From: Shinichi Ishimura Date: Tue, 16 Jul 2019 11:42:51 +0900 Subject: [PATCH 3/3] - Change time to update config object - Add a filter to remove duplicate logs and set the next "to" as the same time of the last log. --- main.go | 72 +++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 25 deletions(-) diff --git a/main.go b/main.go index 540a118..679536f 100644 --- a/main.go +++ b/main.go @@ -31,13 +31,13 @@ var ( ) type config struct { - query string - from myTime - to myTime - limit int - tmpl *template.Template - nextLogId string - follow bool + query string + from myTime + to myTime + limit int + tmpl *template.Template + follow bool + lastInfo *logsInfo } type logsInfo struct { @@ -76,7 +76,7 @@ func main() { log.Fatal(err) } - cfg, err = showLogs(cfg) + logs, err := showLogs(cfg) if err != nil { log.Fatal(err) } @@ -84,14 +84,21 @@ func main() { for cfg.follow { time.Sleep(time.Duration(*interval) * time.Second) - cfg, err = showLogs(cfg) + err = cfg.update(logs) + if err != nil { + log.Fatal(err) + } + + logs, err = showLogs(cfg) if err != nil { log.Fatal(err) } } } -func showLogs(cfg *config) (*config, error) { +func showLogs(cfg *config) (*logsInfo, error) { + //println("$$$ " + cfg.String() + " ::: " + time.Now().String()) + reqBody := map[string]interface{}{ "query": cfg.query, "limit": cfg.limit, @@ -101,8 +108,10 @@ func showLogs(cfg *config) (*config, error) { }, "sort": "asc", } - if cfg.nextLogId != "" { - reqBody["startAt"] = cfg.nextLogId + + lastInfo := cfg.lastInfo + if lastInfo.NextLogId != "" { + reqBody["startAt"] = lastInfo.NextLogId } reqBodyJson, err := json.Marshal(reqBody) @@ -141,15 +150,28 @@ func showLogs(cfg *config) (*config, error) { return nil, err } + // Remove duplicate logs + var logsToDisplay []logInfo +FilterLoop: for _, l := range logsInfo.Logs { + if lastInfo != nil { + for _, lastLog := range lastInfo.Logs { + if l.Id == lastLog.Id { + continue FilterLoop + } + } + logsToDisplay = append(logsToDisplay, l) + } + } + + for _, l := range logsToDisplay { err := cfg.tmpl.Execute(os.Stdout, l.Content) if err != nil { return nil, err } } - err = cfg.update(logsInfo) - return cfg, err + return logsInfo, err } func getEnv(key string) string { @@ -214,18 +236,19 @@ func newConfig() (*config, error) { } return &config{ - query: *query, - from: from, - to: to, - limit: *limit, - follow: follow, - tmpl: tmpl, + query: *query, + from: from, + to: to, + limit: *limit, + follow: follow, + tmpl: tmpl, + lastInfo: &logsInfo{}, }, nil } func (cfg *config) update(info *logsInfo) error { - cfg.nextLogId = info.NextLogId - if cfg.nextLogId != "" { + cfg.lastInfo = info + if info.NextLogId != "" { // There are remaining logs (keep last condition) return nil } @@ -236,8 +259,7 @@ func (cfg *config) update(info *logsInfo) error { if err != nil { return err } - // (Timestamp of the last log) + 1ms, to avoid to show duplicate logs - cfg.from = t.Add(time.Duration(1 * time.Millisecond)) + cfg.from = t } cfg.to = now() @@ -245,5 +267,5 @@ func (cfg *config) update(info *logsInfo) error { } func (cfg *config) String() string { - return fmt.Sprintf("%s %s %s", cfg.from.Format(time.RFC3339Nano), cfg.to.Format(time.RFC3339Nano), cfg.nextLogId) + return fmt.Sprintf("%s %s %s", cfg.from.Format(time.RFC3339Nano), cfg.to.Format(time.RFC3339Nano), cfg.lastInfo.NextLogId) }