Skip to content

Commit

Permalink
Adding sentry for error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
kilgaloon committed Mar 25, 2019
1 parent dd38aa6 commit c22c348
Show file tree
Hide file tree
Showing 17 changed files with 71 additions and 21 deletions.
3 changes: 3 additions & 0 deletions agent/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"time"

"github.com/getsentry/raven-go"
"github.com/kilgaloon/leprechaun/api"
)

Expand All @@ -25,6 +26,7 @@ func (d *Default) WorkersList(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
j, err := json.Marshal(resp)
if err != nil {
raven.CaptureError(err, nil)
log.Fatal(err)
}

Expand All @@ -47,6 +49,7 @@ func (d *Default) KillWorker(w http.ResponseWriter, r *http.Request) {

j, err := json.Marshal(resp)
if err != nil {
raven.CaptureError(err, nil)
log.Fatal(err)
}

Expand Down
9 changes: 9 additions & 0 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"time"

"github.com/getsentry/raven-go"
"github.com/olekukonko/tablewriter"
)

Expand Down Expand Up @@ -87,6 +88,7 @@ func RevealEndpoint(e string, c Cmd) string {
func Info(c Cmd) {
r, err := HTTPClient.Get(RevealEndpoint(infoEndpoint, c))
if err != nil {
raven.CaptureError(err, nil)
log.Fatal(err)
}

Expand All @@ -100,6 +102,7 @@ func Info(c Cmd) {
resp := &InfoResponse{}
err = json.NewDecoder(r.Body).Decode(resp)
if err != nil {
raven.CaptureError(err, nil)
log.Fatal(err)
}

Expand All @@ -113,6 +116,7 @@ func Info(c Cmd) {
func Process(c Cmd) {
r, err := HTTPClient.Get(RevealEndpoint(processCmds[c.Command()], c))
if err != nil {
raven.CaptureError(err, nil)
log.Fatal(err)
}

Expand All @@ -126,6 +130,7 @@ func Process(c Cmd) {
resp := &WorkersResponse{}
err = json.NewDecoder(r.Body).Decode(resp)
if err != nil {
raven.CaptureError(err, nil)
log.Fatal(err)
}

Expand All @@ -137,6 +142,7 @@ func Process(c Cmd) {
func WorkersList(c Cmd) {
r, err := HTTPClient.Get(RevealEndpoint(workersListEndpoint, c))
if err != nil {
raven.CaptureError(err, nil)
log.Fatal(err)
}

Expand All @@ -150,6 +156,7 @@ func WorkersList(c Cmd) {
resp := &WorkersResponse{}
err = json.NewDecoder(r.Body).Decode(resp)
if err != nil {
raven.CaptureError(err, nil)
log.Fatal(err)
}

Expand All @@ -169,6 +176,7 @@ func WorkersList(c Cmd) {
func WorkersKill(c Cmd) {
r, err := HTTPClient.Get(RevealEndpoint(workersKillEndpoint, c) + "?name=" + c.Args()[0])
if err != nil {
raven.CaptureError(err, nil)
log.Fatal(err)
}

Expand All @@ -182,6 +190,7 @@ func WorkersKill(c Cmd) {
resp := &WorkersResponse{}
err = json.NewDecoder(r.Body).Decode(resp)
if err != nil {
raven.CaptureError(err, nil)
log.Fatal(err)
}

Expand Down
3 changes: 3 additions & 0 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package api

import (
"net/http"

"github.com/getsentry/raven-go"
)

var mux = http.NewServeMux()
Expand Down Expand Up @@ -40,6 +42,7 @@ func (a *API) RegisterHandle(e string, h func(w http.ResponseWriter, r *http.Req
func (a *API) Start() {
a.HTTP.Handler = mux
if err := a.HTTP.ListenAndServe(); err != nil {
raven.CaptureError(err, nil)
panic(err)
}
}
Expand Down
14 changes: 5 additions & 9 deletions client/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"strconv"

"github.com/getsentry/raven-go"
"github.com/kilgaloon/leprechaun/api"
"github.com/kilgaloon/leprechaun/daemon"
)
Expand All @@ -24,6 +25,7 @@ func (client *Client) cmdinfo(w http.ResponseWriter, r *http.Request) {

j, err := json.Marshal(resp)
if err != nil {
raven.CaptureError(err, nil)
log.Fatal(err)
}

Expand All @@ -41,13 +43,11 @@ func (client *Client) cmdpause(w http.ResponseWriter, r *http.Request) {
if client.GetStatus() == daemon.Paused {
w.WriteHeader(http.StatusOK)
resp.Message = "Client paused"
} else {
w.WriteHeader(http.StatusExpectationFailed)
resp.Message = "Client failed to pause"
}

j, err := json.Marshal(resp)
if err != nil {
raven.CaptureError(err, nil)
log.Fatal(err)
}

Expand All @@ -68,13 +68,11 @@ func (client *Client) cmdstart(w http.ResponseWriter, r *http.Request) {
if client.GetStatus() == daemon.Started {
w.WriteHeader(http.StatusOK)
resp.Message = "Client started"
} else {
w.WriteHeader(http.StatusExpectationFailed)
resp.Message = "Client failed to start"
}

j, err := json.Marshal(resp)
if err != nil {
raven.CaptureError(err, nil)
log.Fatal(err)
}

Expand All @@ -88,13 +86,11 @@ func (client *Client) cmdstop(w http.ResponseWriter, r *http.Request) {
if client.GetStatus() == daemon.Stopped {
w.WriteHeader(http.StatusOK)
resp.Message = "Client stopped"
} else {
w.WriteHeader(http.StatusExpectationFailed)
resp.Message = "Client failed to stop"
}

j, err := json.Marshal(resp)
if err != nil {
raven.CaptureError(err, nil)
log.Fatal(err)
}

Expand Down
2 changes: 2 additions & 0 deletions client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/fsnotify/fsnotify"
"github.com/getsentry/raven-go"
"github.com/kilgaloon/leprechaun/agent"
"github.com/kilgaloon/leprechaun/daemon"

Expand Down Expand Up @@ -48,6 +49,7 @@ func (client *Client) Start() {
// watch for new recipes
watcher, err := fsnotify.NewWatcher()
if err != nil {
raven.CaptureError(err, nil)
panic("Failed to create watcher")
}

Expand Down
2 changes: 2 additions & 0 deletions client/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path/filepath"
"time"

"github.com/getsentry/raven-go"
"github.com/kilgaloon/leprechaun/daemon"
"github.com/kilgaloon/leprechaun/recipe"
schedule "github.com/kilgaloon/leprechaun/recipe/schedule"
Expand All @@ -22,6 +23,7 @@ func (client *Client) BuildQueue() {

files, err := ioutil.ReadDir(client.GetConfig().GetRecipesPath())
if err != nil {
raven.CaptureError(err, nil)
panic(err)
}

Expand Down
12 changes: 11 additions & 1 deletion config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const (
SMTPHost = ""
SMTPUsername = ""
SMTPPassword = ""
ServerDomain = ""
ServerDomain = "localhost"
ErrorReporting = true
)

// Configs for different agents
Expand Down Expand Up @@ -63,6 +64,7 @@ type AgentConfig struct {
SMTPUsername string
SMTPPassword string
Domain string
ErrorReporting bool
}

// GetPath returns path of config file
Expand Down Expand Up @@ -172,6 +174,11 @@ func (ac AgentConfig) GetServerDomain() []string {
return []string{domain, wwwdomain}
}

// GetErrorReporting returns flag to decide is remote reporting enabled or not
func (ac AgentConfig) GetErrorReporting() bool {
return ac.ErrorReporting
}

// New Create new config
func (c *Configs) New(name string, path string) *AgentConfig {
cfg, err := ini.Load(path)
Expand Down Expand Up @@ -232,6 +239,9 @@ func (c *Configs) New(name string, path string) *AgentConfig {

ac.Domain = cfg.Section("").Key(name + ".domain").MustString(ServerDomain)

gErrorReporting := cfg.Section("").Key("error_reporting").MustBool(ErrorReporting)
ac.ErrorReporting = cfg.Section("").Key(name + ".error_reporting").MustBool(gErrorReporting)

c.cfgs[name] = ac
return ac
}
4 changes: 2 additions & 2 deletions config/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestBuildWithoutSettings(t *testing.T) {
assert.Equal(t, "", cfg.GetSMTPHost())
assert.Equal(t, "", cfg.GetSMTPUsername())
assert.Equal(t, "", cfg.GetSMTPPassword())
assert.Equal(t, []string([]string{"", "www."}), cfg.GetServerDomain())
assert.Equal(t, []string([]string{"localhost", "www.localhost"}), cfg.GetServerDomain())
}

func TestBuildGlobalFallback(t *testing.T) {
Expand Down Expand Up @@ -88,7 +88,7 @@ func TestBuildWithInvalidValues(t *testing.T) {
assert.Equal(t, LockFile, cfg.GetLockFile())
assert.Equal(t, MaxAllowedWorkers, cfg.GetMaxAllowedWorkers())
assert.Equal(t, MaxAllowedQueueWorkers, cfg.GetMaxAllowedQueueWorkers())
assert.Equal(t, []string([]string{"", "www."}), cfg.GetServerDomain())
assert.Equal(t, []string([]string{"localhost", "www.localhost"}), cfg.GetServerDomain())
}

func TestGettingNotExistingConfig(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions cron/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"strconv"

"github.com/getsentry/raven-go"
"github.com/kilgaloon/leprechaun/api"
"github.com/kilgaloon/leprechaun/daemon"
)
Expand Down Expand Up @@ -72,6 +73,7 @@ func (c *Cron) cmdstart(w http.ResponseWriter, r *http.Request) {

j, err := json.Marshal(resp)
if err != nil {
raven.CaptureError(err, nil)
log.Fatal(err)
}

Expand All @@ -89,6 +91,7 @@ func (c *Cron) cmdstop(w http.ResponseWriter, r *http.Request) {

j, err := json.Marshal(resp)
if err != nil {
raven.CaptureError(err, nil)
log.Fatal(err)
}

Expand Down
6 changes: 6 additions & 0 deletions daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strconv"
"strings"

"github.com/getsentry/raven-go"
"github.com/kilgaloon/leprechaun/api"
"github.com/kilgaloon/leprechaun/config"
)
Expand Down Expand Up @@ -218,5 +219,10 @@ func init() {
d.API = api.New()
d.shutdownChan = make(chan bool, 1)

cfg := d.Configs.New("daemon", d.ConfigPath)
if cfg.GetErrorReporting() && os.Getenv("RUN_MODE") != "test" {
raven.SetDSN("https://63f6916e9a4f4ae08853f5b1fe5eabda:a2abd1e7a4f944dca632f875279197f7@sentry.io/1422644")
}

Srv = d
}
5 changes: 5 additions & 0 deletions dist/configs/config.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# error reporting to our sentry
# we are using this just to fix bugs in advance
# if you don't want to share this data
# set it to true
daemon.error_reporting = true
# we want to log our errors, and specify where
error_log = var/log/leprechaun/error.log
# we want to log our info about app to know whats going on
Expand Down
1 change: 1 addition & 0 deletions dist/configs/debug_config.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
daemon.error_reporting = false
# we want to log our errors, and specify where
error_log = ../../var/log/leprechaun/error.log
# we want to log our info about app to know whats going on
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
module github.com/kilgaloon/leprechaun

require (
github.com/certifi/gocertifi v0.0.0-20190105021004-abcd57078448 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.4.7
github.com/getsentry/raven-go v0.2.0
github.com/gopherjs/gopherjs v0.0.0-20181004151105-1babbf986f6f // indirect
github.com/jtolds/gls v4.2.1+incompatible // indirect
github.com/klauspost/cpuid v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.3 // indirect
github.com/mholt/certmagic v0.0.0-20190122014809-c1d472b46046
github.com/miekg/dns v1.1.3 // indirect
github.com/olekukonko/tablewriter v0.0.0-20180912035003-be2c049b30cc
github.com/pkg/errors v0.8.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/robfig/cron v0.0.0-20180505203441-b41be1df6967
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
github.com/certifi/gocertifi v0.0.0-20190105021004-abcd57078448 h1:8tNk6SPXzLDnATTrWoI5Bgw9s/x4uf0kmBpk21NZgI4=
github.com/certifi/gocertifi v0.0.0-20190105021004-abcd57078448/go.mod h1:GJKEexRPVJrBSOjoqN5VNOIKJ5Q3RViH6eu3puDRwx4=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/getsentry/raven-go v0.2.0 h1:no+xWJRb5ZI7eE8TWgIq1jLulQiIoLG0IfYxv5JYMGs=
github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ=
github.com/gopherjs/gopherjs v0.0.0-20181004151105-1babbf986f6f h1:JJ2EP5vV3LAD2U1CxQtD7PTOO15Y96kXmKDz7TjxGHs=
github.com/gopherjs/gopherjs v0.0.0-20181004151105-1babbf986f6f/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/jtolds/gls v4.2.1+incompatible h1:fSuqC+Gmlu6l/ZYAoZzx2pyucC8Xza35fpRVWLVmUEE=
Expand All @@ -16,6 +20,8 @@ github.com/miekg/dns v1.1.3 h1:1g0r1IvskvgL8rR+AcHzUA+oFmGcQlaIm4IqakufeMM=
github.com/miekg/dns v1.1.3/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/olekukonko/tablewriter v0.0.0-20180912035003-be2c049b30cc h1:rQ1O4ZLYR2xXHXgBCCfIIGnuZ0lidMQw2S5n1oOv+Wg=
github.com/olekukonko/tablewriter v0.0.0-20180912035003-be2c049b30cc/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robfig/cron v0.0.0-20180505203441-b41be1df6967 h1:x7xEyJDP7Hv3LVgvWhzioQqbC/KtuUhTigKlH/8ehhE=
Expand Down
4 changes: 4 additions & 0 deletions log/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"log"
"os"

"github.com/getsentry/raven-go"
)

// Logs struct holds path to different logs
Expand All @@ -18,6 +20,7 @@ func (l Logs) Error(message string, v ...interface{}) {
if len(l.ErrorLog) > 0 {
file, err := os.OpenFile(l.ErrorLog, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
raven.CaptureErrorAndWait(err, nil)
panic(err)
}

Expand All @@ -26,6 +29,7 @@ func (l Logs) Error(message string, v ...interface{}) {
log.SetOutput(file)
}

raven.CaptureError(fmt.Errorf(message, v...), nil)
log.Printf(message, v...)

if l.Debug {
Expand Down
Loading

0 comments on commit c22c348

Please sign in to comment.