Skip to content

Commit

Permalink
add log to influx
Browse files Browse the repository at this point in the history
  • Loading branch information
eraac committed Dec 14, 2017
1 parent 995a244 commit aece500
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 12 deletions.
4 changes: 2 additions & 2 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"image"

"github.com/fogleman/gg"
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
)

var cacheImages = map[int]image.Image{}
Expand All @@ -15,7 +15,7 @@ func getBaseImage(week Days) image.Image {
if !ok || refreshCache {
img = generateBaseImage(week)

log.Infof("miss cache")
logrus.Infof("miss cache")

cacheImages[int(week)] = img
}
Expand Down
7 changes: 7 additions & 0 deletions config.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,12 @@
},
"http": {
"port": 9090
},
"influxdb": {
"host": "",
"username": "",
"password": "",
"database": "",
"measurement": "cards-generator"
}
}
24 changes: 23 additions & 1 deletion generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import (
"strconv"
"sync"

"time"

"github.com/fogleman/gg"
"github.com/sirupsen/logrus"
)

var mutex = &sync.Mutex{}

func generateStoptimeCards(w http.ResponseWriter, r *http.Request) {
t := time.Now()
v := r.URL.Query()

mission := v.Get("mission")
Expand All @@ -20,9 +24,17 @@ func generateStoptimeCards(w http.ResponseWriter, r *http.Request) {
schedule := v.Get("schedule")
days, err := strconv.Atoi(v.Get("days"))

logger := logrus.WithFields(logrus.Fields{
"action": "generate",
"type": "stop_time",
"query": r.URL.RawQuery,
})

if mission == "" || origin == "" || terminus == "" || schedule == "" || days == 0 || days > 127 || err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("{\"status\": \"bad request\"}"))

logger.WithField("status", "bad_request").Warn("bad_request")
return
}

Expand All @@ -41,7 +53,17 @@ func generateStoptimeCards(w http.ResponseWriter, r *http.Request) {
dc.DrawStringAnchored(origin, xOrigin, yOrigin, 0, 1)
dc.DrawStringAnchored(terminus, xTerminus, yTerminus, 0, 1)

dc.EncodePNG(w)
if err := dc.EncodePNG(w); err != nil {
w.WriteHeader(http.StatusInternalServerError)

logrus.WithField("status", "fail").Error(err.Error())
return
}

logger.WithFields(logrus.Fields{
"status": "success",
"generate_time": time.Since(t).Seconds(),
}).Info("success")
}

func formatStation(name string) string {
Expand Down
34 changes: 30 additions & 4 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"flag"
"image/color"

"github.com/Abramovic/logrus_influxdb"
"github.com/fogleman/gg"
log "github.com/sirupsen/logrus"
client "github.com/influxdata/influxdb/client/v2"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"golang.org/x/image/font"
)
Expand Down Expand Up @@ -42,6 +44,7 @@ var (

func init() {
initConfig()
initLog()
initFont()
initVar()
initDays()
Expand All @@ -56,7 +59,7 @@ func initConfig() {

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err != nil {
log.Fatal(err)
logrus.Fatal(err)
}
}

Expand All @@ -65,12 +68,12 @@ func initFont() {

fontRegular, err = gg.LoadFontFace(viper.GetString("font.regular.path"), viper.GetFloat64("font.regular.size"))
if err != nil {
log.Fatal(err)
logrus.Fatal(err)
}

fontBold, err = gg.LoadFontFace(viper.GetString("font.bold.path"), viper.GetFloat64("font.bold.size"))
if err != nil {
log.Fatal(err)
logrus.Fatal(err)
}
}

Expand Down Expand Up @@ -139,3 +142,26 @@ func initDays() {
{Sunday, "D", widthPerCase*6 + widthPerCase/2},
}
}

func initLog() {
config := &logrus_influxdb.Config{
Database: viper.GetString("influxdb.database"),
Measurement: viper.GetString("influxdb.measurement"),
Tags: []string{"action", "status", "type"},
}

// Connect to InfluxDB using the standard client.
influxClient, _ := client.NewHTTPClient(client.HTTPConfig{
Addr: viper.GetString("influxdb.host"),
Username: viper.GetString("influxdb.username"),
Password: viper.GetString("influxdb.password"),
})

hook, err := logrus_influxdb.NewInfluxDB(config, influxClient)

if err != nil {
logrus.Fatal(err)
}

logrus.AddHook(hook)
}
10 changes: 5 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"os/signal"
"time"

log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)

Expand All @@ -25,24 +25,24 @@ func main() {
}

go func() {
log.Infof("HTTP Server listening on %s", h.Addr)
logrus.Infof("HTTP Server listening on %s", h.Addr)

if err := h.ListenAndServe(); err != nil {
log.Fatal(err.Error())
logrus.Fatal(err.Error())
}
}()

<-stop

log.Info("Graceful shutdown...")
logrus.Info("Graceful shutdown...")

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

err := h.Shutdown(ctx)

if err != nil {
log.Fatal(err.Error())
logrus.Fatal(err.Error())
}
}

Expand Down

0 comments on commit aece500

Please sign in to comment.