Skip to content

Instantly share code, notes, and snippets.

@tanelmae
Created September 29, 2020 20:00
Show Gist options
  • Select an option

  • Save tanelmae/7801cfe4372f6571a5ddb058720c303a to your computer and use it in GitHub Desktop.

Select an option

Save tanelmae/7801cfe4372f6571a5ddb058720c303a to your computer and use it in GitHub Desktop.
Export from LogDNA with golang
package main
import (
"errors"
"flag"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"time"
)
const (
simpleDateFormat = "2006-01-02"
)
func main() {
from := flag.String("from", "", "Log period starting time")
to := flag.String("to", "", "Log period end time")
tag := flag.String("tag", "", "LogDNA tag")
apps := flag.String("apps", "", "List of apps")
flag.Parse()
apiKey := os.Getenv("LOGDNA_KEY")
fromTime, err := time.Parse(simpleDateFormat, *from)
if err != nil {
log.Fatalln(err)
}
toTime, err := time.Parse(simpleDateFormat, *to)
if err != nil {
log.Fatalln(err)
}
client := &http.Client{}
req, err := http.NewRequest("GET", "https://api.logdna.com/v1/export", nil)
if err != nil {
log.Fatalln(err)
}
req.SetBasicAuth(apiKey, ";")
params := req.URL.Query()
params.Add("from", strconv.FormatInt(fromTime.Unix(), 10))
params.Add("to", strconv.FormatInt(toTime.Unix(), 10))
params.Add("query", "tag:"+*tag)
params.Add("apps", *apps)
req.URL.RawQuery = params.Encode()
req.Header.Set("User-Agent", "logdna-client")
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
if resp.StatusCode != http.StatusOK {
log.Fatalln(errors.New(string(respBytes)))
}
log.Println(string(respBytes))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment