Skip to content

Commit

Permalink
Add rollbar integration
Browse files Browse the repository at this point in the history
  • Loading branch information
benbjohnson committed Jan 2, 2021
1 parent b410824 commit b210961
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ jobs:
[github]
client-id = "${{ secrets.GH_CLIENT_ID }}"
client-secret = "${{ secrets.GH_CLIENT_SECRET }}"
[rollbar]
token = "${{ secrets.ROLLBAR_TOKEN }}"
EOF
- name: Copy file to server
Expand Down
37 changes: 37 additions & 0 deletions cmd/wtfd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/benbjohnson/wtf/inmem"
"github.com/benbjohnson/wtf/sqlite"
"github.com/pelletier/go-toml"
"github.com/rollbar/rollbar-go"
)

// Build version, injected during build.
Expand Down Expand Up @@ -146,6 +147,17 @@ func (m *Main) ParseFlags(ctx context.Context, args []string) error {
// Run executes the program. The configuration should already be set up before
// calling this function.
func (m *Main) Run(ctx context.Context) (err error) {
// Initialize error tracking.
if m.Config.Rollbar.Token != "" {
rollbar.SetToken(m.Config.Rollbar.Token)
rollbar.SetEnvironment("production")
rollbar.SetCodeVersion(version)
rollbar.SetServerRoot("github.com/benbjohnson/wtf")
wtf.ReportError = rollbarReportError
wtf.ReportPanic = rollbarReportPanic
log.Printf("rollbar error tracking enabled")
}

// Initialize event service for real-time events.
// We are using an in-memory implementation but this could be changed to
// a more robust service if we expanded out to multiple nodes.
Expand Down Expand Up @@ -240,6 +252,10 @@ type Config struct {
ClientID string `toml:"client-id"`
ClientSecret string `toml:"client-secret"`
} `toml:"github"`

Rollbar struct {
Token string `toml:"token"`
} `toml:"rollbar"`
}

// DefaultConfig returns a new instance of Config with defaults set.
Expand Down Expand Up @@ -289,3 +305,24 @@ func expandDSN(dsn string) (string, error) {
}
return expand(dsn)
}

// rollbarReportError reports internal errors to rollbar.
func rollbarReportError(ctx context.Context, err error, args ...interface{}) {
if wtf.ErrorCode(err) != wtf.EINTERNAL {
return
}

// Set user information for error, if available.
if u := wtf.UserFromContext(ctx); u != nil {
rollbar.SetPerson(fmt.Sprint(u.ID), u.Name, u.Email)
} else {
rollbar.ClearPerson()
}

rollbar.Error(append([]interface{}{err}, args)...)
}

// rollbarReportPanic reports panics to rollbar.
func rollbarReportPanic(err interface{}) {
rollbar.LogPanic(err, true)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/gorilla/websocket v1.4.2
github.com/mattn/go-sqlite3 v1.14.4
github.com/pelletier/go-toml v1.8.1
github.com/rollbar/rollbar-go v1.2.0
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/oauth2 v0.0.0-20201109201403-9fd604954f58
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrap
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rollbar/rollbar-go v1.2.0 h1:CUanFtVu0sa3QZ/fBlgevdGQGLWaE3D4HxoVSQohDfo=
github.com/rollbar/rollbar-go v1.2.0/go.mod h1:czC86b8U4xdUH7W2C6gomi2jutLm8qK0OtrF5WMvpcc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
3 changes: 2 additions & 1 deletion http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ func Error(w http.ResponseWriter, r *http.Request, err error) {
// Extract error code & message.
code, message := wtf.ErrorCode(err), wtf.ErrorMessage(err)

// Log internal errors.
// Log & report internal errors.
if code == wtf.EINTERNAL {
wtf.ReportError(r.Context(), err, r)
LogError(r, err)
}

Expand Down
17 changes: 17 additions & 0 deletions http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ func NewServer() *Server {
router: mux.NewRouter(),
}

// Report panics to external service.
s.router.Use(reportPanic)

// Our router is wrapped by another function handler to perform some
// middleware-like tasks that cannot be performed by actual middleware.
// This includes changing route paths for JSON endpoints & overridding methods.
Expand Down Expand Up @@ -346,6 +349,20 @@ func loadFlash(next http.Handler) http.Handler {
})
}

// reportPanic is middleware for catching panics and reporting them.
func reportPanic(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
wtf.ReportPanic(err)
}
}()

next.ServeHTTP(w, r)
})
}

// handleNotFound handles requests to routes that don't exist.
func (s *Server) handleNotFound(w http.ResponseWriter, r *http.Request) {
tmpl := html.ErrorTemplate{
Expand Down
10 changes: 10 additions & 0 deletions wtf.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
package wtf

import (
"context"
)

// Build version & commit SHA.
var (
Version string
Commit string
)

// ReportError notifies an external service of errors. No-op by default.
var ReportError = func(ctx context.Context, err error, args ...interface{}) {}

// ReportPanic notifies an external service of panics. No-op by default.
var ReportPanic = func(err interface{}) {}

0 comments on commit b210961

Please sign in to comment.