Skip to content

Commit

Permalink
Swap stvp rollbar library for official go rollbar lib
Browse files Browse the repository at this point in the history
The rollbar library we've been using previously has been archived in
favour of an official rollbar go library that rollbar maintains. This
updates our usage of the rollbar library to use the active library. This
also involved minor fixes to update library calls to match the api of
the new library.
  • Loading branch information
sdawson committed Dec 10, 2021
1 parent 8b90954 commit c2d7356
Show file tree
Hide file tree
Showing 1,854 changed files with 593,659 additions and 62 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ require (
github.com/philhofer/fwd v1.1.1 // indirect
github.com/pkg/errors v0.9.1
github.com/remind101/newrelic v0.0.0-20161025005755-2671e3bb51df
github.com/rollbar/rollbar-go v1.4.1
github.com/shirou/gopsutil v3.21.7+incompatible
github.com/stretchr/testify v1.7.0
github.com/stvp/rollbar v0.5.1
github.com/urfave/cli v1.22.5
gopkg.in/DataDog/dd-trace-go.v1 v1.31.1
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ github.com/remind101/newrelic v0.0.0-20161025005755-2671e3bb51df h1:XHD04U3QEA4A
github.com/remind101/newrelic v0.0.0-20161025005755-2671e3bb51df/go.mod h1:Yqm7hS5p6GFzFeXvUAR/WwH7ezcK+gSaI64SxK2amOU=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rollbar/rollbar-go v1.4.1 h1:9hc5EBeDDudrTJxoIKaEbsYt8ON/OLRWf6Ftxe/p590=
github.com/rollbar/rollbar-go v1.4.1/go.mod h1:kLQ9gP3WCRGrvJmF0ueO3wK9xWocej8GRX98D8sa39w=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shirou/gopsutil v3.21.7+incompatible h1:g/wcPHcuCQvHSePVofjQljd2vX4ty0+J6VoMB+NPcdk=
Expand All @@ -246,8 +248,6 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stvp/rollbar v0.5.1 h1:qvyWbd0RNL5V27MBumqCXlcU7ohmHeEtKX+Czc8oeuw=
github.com/stvp/rollbar v0.5.1/go.mod h1:/fyFC854GgkbHRz/rSsiYc6h84o0G5hxBezoQqRK7Ho=
github.com/tinylib/msgp v1.1.2 h1:gWmO7n0Ys2RBEb7GPYB9Ujq8Mk5p2U08lRnmMcGy6BQ=
github.com/tinylib/msgp v1.1.2/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE=
github.com/urfave/cli v1.22.5 h1:lNq9sAHXK2qfdI8W+GRItjCEkI+2oR4d+MEHy1CKXoU=
Expand Down
6 changes: 3 additions & 3 deletions reporter/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/remind101/pkg/reporter"
"github.com/remind101/pkg/reporter/hb2"
"github.com/stvp/rollbar"
"github.com/rollbar/rollbar-go"
)

func TestNewReporterFromConfigString(t *testing.T) {
Expand All @@ -31,10 +31,10 @@ func TestNewReporterFromConfigString(t *testing.T) {
t.Errorf("got %#v, but wanted %#v", got, want)
}

if got, want := rollbar.Token, "rollbarkey"; got != want {
if got, want := rollbar.Token(), "rollbarkey"; got != want {
t.Errorf("got %#v, but wanted %#v", got, want)
}
if got, want := rollbar.Environment, "rollbarenv"; got != want {
if got, want := rollbar.Environment(), "rollbarenv"; got != want {
t.Errorf("got %#v, but wanted %#v", got, want)
}
}
76 changes: 22 additions & 54 deletions reporter/rollbar/rollbar.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package rollbar

import (
"fmt"
"net/http"
"os"
"strconv"

"context"

"github.com/pkg/errors"
"github.com/remind101/pkg/reporter/util"
"github.com/stvp/rollbar"
"github.com/rollbar/rollbar-go"
)

const ErrorLevel = "error"
Expand All @@ -21,100 +19,70 @@ const (
EnvEndpoint = "ROLLBAR_ENDPOINT"
)

// TODO (sophied): A type temporarily stolen from github.com/stvp/rollbar
// to allow us to migrate all the places that use this reporting package
// away from a custom stack trace type before we switch over to go's default
// error type across the board.
type Frame struct {
Filename string `json:"filename"`
Method string `json:"method"`
Line int `json:"lineno"`
}

type rollbarReporter struct{}

// The stvp/rollbar package is implemented as a global, so let's not fool our
// callers by generating an instance of a reporter. Rollbar config is actually
// global, so we'll have the Rollbar reporter be a global too.
var Reporter = &rollbarReporter{}

func ConfigureReporter(token, environment string) {
rollbar.Token = token
rollbar.Environment = environment
rollbar.SetToken(token)
rollbar.SetEnvironment(environment)
}

func ConfigureFromEnvironment() {
if token := os.Getenv(EnvAccessToken); token != "" {
rollbar.Token = token
rollbar.SetToken(token)
}
if env := os.Getenv(EnvEnvironment); env != "" {
rollbar.Environment = env
rollbar.SetEnvironment(env)
}

if endpoint := os.Getenv(EnvEndpoint); endpoint != "" {
rollbar.Endpoint = endpoint
rollbar.SetEndpoint(endpoint)
}
}

func (r *rollbarReporter) ReportWithLevel(ctx context.Context, level string, err error) error {
var request *http.Request
extraFields := []*rollbar.Field{}
var stackTrace rollbar.Stack = nil
var extraFields map[string]interface{}

if e, ok := err.(util.Contexter); ok {
extraFields = getContextData(e)
extraFields = e.ContextData()
}

if e, ok := err.(util.Requester); ok {
request = e.Request()
}

if e, ok := err.(util.StackTracer); ok {
stackTrace = makeRollbarStack(e.StackTrace())
}

if e, ok := err.(util.Causer); ok {
err = e.Cause() // Report the actual cause of the error.
}

reportToRollbar(level, request, err, stackTrace, extraFields)
reportToRollbar(level, request, err, extraFields)
return nil
}

func (r *rollbarReporter) Flush() {
rollbar.Wait()
}

func reportToRollbar(level string, request *http.Request, err error, stack rollbar.Stack, extraFields []*rollbar.Field) {
func reportToRollbar(level string, request *http.Request, err error, extraFields map[string]interface{}) {
if request != nil {
if stack != nil {
rollbar.RequestErrorWithStack(level, request, err, stack, extraFields...)
} else {
rollbar.RequestError(level, request, err, extraFields...)
}
rollbar.Error(level, request, err, extraFields)
} else {
if stack != nil {
rollbar.ErrorWithStack(level, err, stack, extraFields...)
} else {
rollbar.Error(level, err, extraFields...)
}
rollbar.Error(level, err, extraFields)
}
}

func makeRollbarStack(stack errors.StackTrace) rollbar.Stack {
length := len(stack)
rollbarStack := make(rollbar.Stack, length)
for index, frame := range stack[:length] {
// Rollbar's website has a "most recent call last" header. We need to
// reverse the order of the stack frames we send it, so our stack traces
// are shown in that order.
rollbarStack[length-index-1] = rollbar.Frame{
Line: parseInt(fmt.Sprintf("%d", frame)),
Filename: fmt.Sprintf("%s", frame),
Method: fmt.Sprintf("%n", frame)}
}
return rollbarStack
}

func parseInt(s string) int {
i, _ := strconv.Atoi(s)
return i
}

func getContextData(err util.Contexter) []*rollbar.Field {
fields := []*rollbar.Field{}
for key, value := range err.ContextData() {
fields = append(fields, &rollbar.Field{Name: key, Data: value})
}
return fields
}
4 changes: 2 additions & 2 deletions reporter/rollbar/rollbar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

"github.com/remind101/pkg/httpx/errors"
"github.com/remind101/pkg/reporter"
"github.com/stvp/rollbar"
"github.com/rollbar/rollbar-go"
)

func TestIsAReporter(t *testing.T) {
Expand Down Expand Up @@ -42,7 +42,7 @@ func TestReportsThingsToRollbar(t *testing.T) {
err := errors.New(ctx, boom, 0)

ConfigureReporter("token", "test")
rollbar.Endpoint = ts.URL + "/"
rollbar.SetEndpoint(ts.URL + "/")
fmt.Println(ts.URL)
ctx = reporter.WithReporter(ctx, Reporter)
reporter.Report(ctx, err)
Expand Down
Loading

0 comments on commit c2d7356

Please sign in to comment.