From 430ada53b8c1e4d931d610d491265a66bc3e533e Mon Sep 17 00:00:00 2001 From: Shinichi Ishimura Date: Wed, 17 Jul 2019 06:44:47 +0900 Subject: [PATCH 1/3] Can display version even if the env vars are set. --- main.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 679536f..15a62a7 100644 --- a/main.go +++ b/main.go @@ -25,9 +25,6 @@ var ( 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("version", false, "Show version of taildog.") - - apiKey = getEnv("DD_API_KEY") - appKey = getEnv("DD_APP_KEY") ) type config struct { @@ -38,6 +35,8 @@ type config struct { tmpl *template.Template follow bool lastInfo *logsInfo + apiKey string + appKey string } type logsInfo struct { @@ -120,7 +119,7 @@ func showLogs(cfg *config) (*logsInfo, error) { } url := fmt.Sprintf( - "https://api.datadoghq.com/api/v1/logs-queries/list?api_key=%s&application_key=%s", apiKey, appKey) + "https://api.datadoghq.com/api/v1/logs-queries/list?api_key=%s&application_key=%s", cfg.apiKey, cfg.appKey) req, err := http.NewRequest("POST", url, bytes.NewBuffer(reqBodyJson)) if err != nil { return nil, err @@ -204,6 +203,9 @@ func (t myTime) Add(d time.Duration) myTime { } func newConfig() (*config, error) { + apiKey := getEnv("DD_API_KEY") + appKey := getEnv("DD_APP_KEY") + tmpl, err := template.New("logLine").Parse(*msgFormat + "\n") if err != nil { return nil, err @@ -243,6 +245,8 @@ func newConfig() (*config, error) { follow: follow, tmpl: tmpl, lastInfo: &logsInfo{}, + apiKey: apiKey, + appKey: appKey, }, nil } From 084578838f92701d52f1199a73daf868202af128 Mon Sep 17 00:00:00 2001 From: Shinichi Ishimura Date: Wed, 17 Jul 2019 07:11:49 +0900 Subject: [PATCH 2/3] Allow other formats of the "from" and "to" --- main.go | 73 ++++++++++++++------------------------------------------- 1 file changed, 18 insertions(+), 55 deletions(-) diff --git a/main.go b/main.go index 15a62a7..9c1787e 100644 --- a/main.go +++ b/main.go @@ -29,8 +29,8 @@ var ( type config struct { query string - from myTime - to myTime + from string + to string limit int tmpl *template.Template follow bool @@ -59,10 +59,6 @@ type logContent struct { Message string `json:"message"` } -type myTime struct { - time.Time -} - func main() { flag.Parse() if *version { @@ -96,14 +92,14 @@ func main() { } func showLogs(cfg *config) (*logsInfo, error) { - //println("$$$ " + cfg.String() + " ::: " + time.Now().String()) + //cfg.Debug() reqBody := map[string]interface{}{ "query": cfg.query, "limit": cfg.limit, - "time": map[string]int64{ - "from": cfg.from.UnixMillis(), - "to": cfg.to.UnixMillis(), + "time": map[string]string{ + "from": cfg.from, + "to": cfg.to, }, "sort": "asc", } @@ -181,25 +177,8 @@ func getEnv(key string) string { return "" } -func now() myTime { - return newTime(time.Now()) -} - -func newTime(t time.Time) myTime { - return myTime{t} -} - -func parseTime(str string) (myTime, error) { - t, err := time.Parse(time.RFC3339, str) - return newTime(t), err -} - -func (t myTime) UnixMillis() int64 { - return t.UnixNano() / 1000000 -} - -func (t myTime) Add(d time.Duration) myTime { - return myTime{t.Time.Add(d)} +func formatTime(t time.Time) string { + return t.Format(time.RFC3339Nano) } func newConfig() (*config, error) { @@ -211,30 +190,19 @@ func newConfig() (*config, error) { return nil, err } - var from, to myTime - if *fromStr != "" { - from, err = parseTime(*fromStr) - if err != nil { - return nil, err - } - } - if *toStr != "" { - to, err = parseTime(*toStr) - if err != nil { - return nil, err - } - } + from := *fromStr + to := *toStr - if (from.IsZero() && !to.IsZero()) || (!from.IsZero() && to.IsZero()) { + if (from == "" && to != "") || (from != "" && to == "") { return nil, fmt.Errorf("both 'from' and 'to' must be set") } follow := false - if from.IsZero() && to.IsZero() { + if from == "" && to == "" { follow = true // First attempt for the follow mode, retrieve logs from 30 seconds ago - from = now().Add(time.Duration(-30 * time.Second)) - to = now() + from = formatTime(time.Now().Add(time.Duration(-30 * time.Second))) + to = formatTime(time.Now()) } return &config{ @@ -258,18 +226,13 @@ func (cfg *config) update(info *logsInfo) error { } if len(info.Logs) != 0 { - ts := info.Logs[len(info.Logs)-1].Content.Timestamp - t, err := parseTime(ts) - if err != nil { - return err - } - cfg.from = t + cfg.from = info.Logs[len(info.Logs)-1].Content.Timestamp } - cfg.to = now() + cfg.to = formatTime(time.Now()) return nil } -func (cfg *config) String() string { - return fmt.Sprintf("%s %s %s", cfg.from.Format(time.RFC3339Nano), cfg.to.Format(time.RFC3339Nano), cfg.lastInfo.NextLogId) +func (cfg *config) Debug() { + println(fmt.Sprintf("[%s] from:%s, to:%s, nextLogId:%s", time.Now().Format(time.RFC3339), cfg.from, cfg.to, cfg.lastInfo.NextLogId)) } From 5430909a0d03e7c039f54613c9155302905c467c Mon Sep 17 00:00:00 2001 From: Shinichi Ishimura Date: Wed, 17 Jul 2019 07:24:03 +0900 Subject: [PATCH 3/3] Update help message --- main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 9c1787e..83b2a89 100644 --- a/main.go +++ b/main.go @@ -19,11 +19,11 @@ var ( msgFormat = flag.String("f", "{{.Timestamp}} {{.Host}} {{.Service}} {{.Message}}", "Message format of entries in Golang's template style.\n"+ "You can use any field in the \"content\" of the response of the Log Query API.\n"+ - "https://docs.datadoghq.com/api/?lang=bash#get-a-list-of-logs\n") + "https://docs.datadoghq.com/api/#get-a-list-of-logs\n") interval = flag.Int("i", 15, "Interval time in seconds until the next attempt.") 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.") + fromStr = flag.String("from", "", "Minimum timestamp for requested logs. See https://docs.datadoghq.com/api/#get-a-list-of-logs for more details of its format.") + toStr = flag.String("to", "", "Maximum timestamp for requested logs. See https://docs.datadoghq.com/api/#get-a-list-of-logs for more details of its format.") version = flag.Bool("version", false, "Show version of taildog.") )