Skip to content

Commit e615d5c

Browse files
authored
Update all the things #42
This is mostly just a routine update of nixpkgs and go modules. Except, for the rollbar-go module, which is an update to what should have been a new major version as a bunch of backwards incompatible changes were released. The new rollbar code plays better with stack trace containing errors and even has support github.com/pkg/errors in the module, which replaces much of what is in internal/rollbar, 🎉!
2 parents 3a3c5da + 9880714 commit e615d5c

9 files changed

Lines changed: 447 additions & 226 deletions

File tree

.drone.yml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
11
---
2-
32
workspace:
43
base: /go
54
path: src/github.com/packethost/pkg
65

76
pipeline:
87
fmt:
98
group: ci
10-
image: golang:1.13-alpine
9+
image: golang:1.17-alpine
1110
commands:
1211
- apk add --update --upgrade --no-cache git
1312
- go get golang.org/x/tools/cmd/goimports
1413
- goimports -d . | (! grep .)
1514

1615
lint:
1716
group: ci
18-
image: golangci/golangci-lint:v1.23.8
17+
image: golangci/golangci-lint:v1.42
1918
commands:
2019
- golangci-lint run -v -D errcheck
2120

2221
test:
2322
group: ci
24-
image: golang:1.13
23+
image: golang:1.17
2524
commands:
26-
- GO111MODULE=on go test -v -race -coverprofile=coverage.txt -covermode=atomic ./...
25+
- go test -v -race -coverprofile=coverage.txt -covermode=atomic ./...
2726

2827
build:
2928
group: ci
30-
image: golang:1.13-alpine
29+
image: golang:1.17-alpine
3130
commands:
32-
- GO111MODULE=on CGO_ENABLED=0 go build ./cmd/...
31+
- CGO_ENABLED=0 go build ./cmd/...
3332

3433
examplelog:
3534
group: examples

cmd/examplelog/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ func main() {
2828
ll.With("error", err).Info("just an info level message about an error")
2929
ll.Error(err, "this is an actual error! Will even go to rollbar where we can ignore it or not")
3030

31+
ll.Error(errors.WithMessage(err, "error with an extra WithMessage"))
32+
3133
helpfulWrapper(ll, "this is being called via helpfulWrapper")
3234
l.Close()
3335
}

go.mod

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,25 @@ module github.com/packethost/pkg
33
go 1.12
44

55
require (
6-
github.com/gogo/protobuf v1.2.1 // indirect
7-
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0
6+
github.com/cespare/xxhash/v2 v2.1.2 // indirect
7+
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
88
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
9-
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
109
github.com/pkg/errors v0.9.1
11-
github.com/prometheus/client_golang v1.0.0
12-
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4
13-
github.com/prometheus/common v0.6.0 // indirect
14-
github.com/prometheus/procfs v0.0.3 // indirect
15-
github.com/rollbar/rollbar-go v1.0.2
16-
github.com/sirupsen/logrus v1.4.2 // indirect
10+
github.com/prometheus/client_golang v1.11.0
11+
github.com/prometheus/client_model v0.2.0
12+
github.com/prometheus/common v0.31.1 // indirect
13+
github.com/prometheus/procfs v0.7.3 // indirect
14+
github.com/rollbar/rollbar-go v1.4.2
15+
github.com/rollbar/rollbar-go/errors v0.0.0-20210929193720-32947096267e
1716
github.com/stretchr/testify v1.7.0
18-
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.22.0
19-
go.uber.org/multierr v1.6.0 // indirect
20-
go.uber.org/zap v1.16.0
21-
golang.org/x/text v0.3.2 // indirect
22-
google.golang.org/grpc v1.39.0
17+
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.25.0
18+
go.uber.org/atomic v1.9.0 // indirect
19+
go.uber.org/multierr v1.7.0 // indirect
20+
go.uber.org/zap v1.19.1
21+
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f // indirect
22+
golang.org/x/sys v0.0.0-20211015200801-69063c4bb744 // indirect
23+
golang.org/x/text v0.3.7 // indirect
24+
google.golang.org/genproto v0.0.0-20211018162055-cf77aa76bad2 // indirect
25+
google.golang.org/grpc v1.41.0
2326
google.golang.org/grpc/examples v0.0.0-20210728214646-ad0a2a847cdf
2427
)

go.sum

Lines changed: 380 additions & 57 deletions
Large diffs are not rendered by default.

log/internal/rollbar/rollbar.go

Lines changed: 4 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44
package rollbar
55

66
import (
7-
"fmt"
87
"os"
9-
"runtime"
108

119
"github.com/packethost/pkg/env"
12-
"github.com/pkg/errors"
1310
rollbar "github.com/rollbar/rollbar-go"
11+
rollbarerrors "github.com/rollbar/rollbar-go/errors"
1412
"go.uber.org/zap"
1513
)
1614

@@ -37,96 +35,13 @@ func Setup(l *zap.SugaredLogger, service string) func() {
3735
enable = false
3836
}
3937
rollbar.SetEnabled(enable)
38+
rollbar.SetStackTracer(rollbarerrors.StackTracer)
4039

4140
return rollbar.Wait
4241
}
4342

44-
// rError exists to implement rollbar.CauseStacker so that rollbar can have stack info.
45-
// see https://github.com/rollbar/rollbar-go/blob/v1.0.2/doc.go#L64
46-
type rError struct {
47-
err error
48-
}
49-
50-
func (e rError) Error() string {
51-
return e.err.Error()
52-
}
53-
54-
func (e rError) Cause() error {
55-
return e.err
56-
}
57-
58-
// logInternalError is a helper to log errors through zap and to rollbar if we run into an error while logging a client's error.
59-
// We can use rollbar.ErrorWithExtras here because the stack trace rollbar collects will be of where error is.
60-
// This handles the so called "error while logging error" case.
61-
func logInternalError(err error, ctx map[string]interface{}) {
62-
l := log.With("error", err)
63-
if len(ctx) != 0 {
64-
fields := make([]interface{}, 0, len(ctx)*2)
65-
for k, v := range ctx {
66-
fields = append(fields, k)
67-
fields = append(fields, v)
68-
}
69-
l = l.With(fields...)
70-
}
71-
ctx["errorVerbose"] = fmt.Sprintf("%+v", err)
72-
l.Error(err)
73-
// 1 level of stack frames are skipped, because we don't want care to have logInternalError show up
74-
rollbar.ErrorWithStackSkipWithExtras(rollbar.ERR, err, 1, ctx)
75-
}
76-
77-
// Stack converts a github.com/pkg/errors Error stack into a rollbar stack
78-
func (e rError) Stack() rollbar.Stack {
79-
type stackTracer interface {
80-
StackTrace() errors.StackTrace
81-
}
82-
type causer interface {
83-
Cause() error
84-
}
85-
ctx := map[string]interface{}{}
86-
87-
err := e.Cause()
88-
var st stackTracer
89-
var ok bool
90-
// try to find if there's a stackTracer in the stack of errors
91-
// WithMessage does not add a stack so New->WithMessage->WM->...->WM means we need to unwrap until we get to the
92-
// New'd one.
93-
for {
94-
st, ok = err.(stackTracer)
95-
if ok {
96-
break
97-
}
98-
cause, ok := err.(causer)
99-
if !ok {
100-
ctx["cause"] = e.Cause()
101-
logInternalError(errors.New("cause does not implement StackTracer"), ctx)
102-
return nil
103-
}
104-
err = cause.Cause()
105-
}
106-
107-
stack := st.StackTrace()
108-
rStack := rollbar.Stack(make([]rollbar.Frame, len(stack)))
109-
110-
for i, frame := range stack {
111-
// From pkg/error's docs
112-
//
113-
// Frame represents a program counter inside a stack frame.
114-
// For historical reasons if Frame is interpreted as a uintptr
115-
// its value represents the program counter + 1.
116-
// type Frame uintptr
117-
frame -= 1
118-
119-
f := runtime.FuncForPC(uintptr(frame))
120-
rStack[i].Method = f.Name()
121-
rStack[i].Filename, rStack[i].Line = f.FileLine(uintptr(frame))
122-
}
123-
124-
return rStack
125-
}
126-
127-
func Notify(err error, args ...interface{}) {
128-
rErr := rError{err: err}
129-
rollbar.Error(rErr)
43+
func Notify(err error) {
44+
rollbar.Error(err)
13045
}
13146

13247
func getEnvironment() string {

log/internal/rollbar/rollbar_test.go

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import (
99
"testing"
1010

1111
"github.com/pkg/errors"
12-
rollbar "github.com/rollbar/rollbar-go"
13-
"go.uber.org/zap"
14-
"go.uber.org/zap/zaptest"
12+
rollbarerr "github.com/rollbar/rollbar-go/errors"
1513
)
1614

1715
func TestStack(t *testing.T) {
@@ -20,9 +18,9 @@ func TestStack(t *testing.T) {
2018
err := errors.New("hi")
2119
n := runtime.Callers(1, pc)
2220

23-
stack := []rollbar.Frame(rError{err: err}.Stack())
24-
if n == 0 {
25-
t.Fatal("unable to recover stack information")
21+
stack, ok := rollbarerr.StackTracer(err)
22+
if !ok {
23+
t.Fatalf("rollbarerr.StackTracer returned false")
2624
}
2725
pc = pc[:n]
2826
frames := runtime.CallersFrames(pc)
@@ -41,12 +39,12 @@ func TestStack(t *testing.T) {
4139
}
4240

4341
t.Logf("want: method=%v %v:%v", want.Function, want.File, want.Line)
44-
t.Logf(" got: method=%v %v:%v", got.Method, got.Filename, got.Line)
45-
if want.File != got.Filename {
46-
t.Fatalf("filename mismatch: i=%d\nwant=%s\n got=%s\n", i, want.File, got.Filename)
42+
t.Logf(" got: method=%v %v:%v", got.Function, got.File, got.Line)
43+
if want.File != got.File {
44+
t.Fatalf("filename mismatch: i=%d\nwant=%s\n got=%s\n", i, want.File, got.File)
4745
}
48-
if want.Func.Name() != got.Method {
49-
t.Fatalf("func name mismatch: i=%d\nwant=%s\n got=%s\n", i, want.Func.Name(), got.Method)
46+
if want.Func.Name() != got.Function {
47+
t.Fatalf("func name mismatch: i=%d\nwant=%s\n got=%s\n", i, want.Func.Name(), got.Function)
5048
}
5149
if want.Line != got.Line {
5250
t.Fatalf("line number mismatch: i=%d\nwant=%d\n got=%d\n", i, want.Line, got.Line)
@@ -63,36 +61,23 @@ func TestStack(t *testing.T) {
6361
}
6462
}
6563

66-
func TestError(t *testing.T) {
67-
err := errors.New("hi")
68-
rErr := rError{err: err}
69-
if rErr.Error() != err.Error() {
70-
t.Fatalf("Error() mismatch:\nwant=%s\n got=%s\n", err.Error(), rErr.Error())
71-
}
72-
}
73-
74-
func TestCause(t *testing.T) {
75-
err := errors.New("hi")
76-
rErr := rError{err: err}
77-
if rErr.Cause() != err {
78-
t.Fatalf("Cause() mismatch:\nwant=%v\n got=%v\n", err, rErr.Cause())
79-
}
80-
}
81-
8264
func TestPkgErrorsCompat(t *testing.T) {
83-
log = zaptest.NewLogger(t, zaptest.Level(zap.WarnLevel)).Sugar()
84-
for _, err := range []error{
85-
errors.Errorf("Errorf"),
86-
errors.New("New"),
87-
errors.WithMessage(errors.New("New"), "WithMessage errors.New"),
88-
errors.WithStack(fmt.Errorf("fmt.Errorf")),
89-
errors.WithStack(errors.New("New")),
90-
errors.Wrap(fmt.Errorf("fmt.Errorf"), "Wrap fmt.Errrof"),
91-
errors.Wrap(errors.New("New"), "Wrap errors.New"),
65+
for name, err := range map[string]error{
66+
"Errorf": errors.Errorf("Errorf"),
67+
"New": errors.New("New"),
68+
"WithStack(fmt.Errorf)": errors.WithStack(fmt.Errorf("fmt.Errorf")),
69+
"WithStack(errors.New)": errors.WithStack(errors.New("New")),
70+
"Wrap(fmt.Errorf)": errors.Wrap(fmt.Errorf("fmt.Errorf"), "Wrap fmt.Errrof"),
71+
"Wrap(errors.New)": errors.Wrap(errors.New("New"), "Wrap errors.New"),
9272
} {
93-
rErr := rError{err: err}
94-
if rErr.Stack() == nil {
95-
t.Fatalf("expected err to implement stackTracer but does not: %v", err)
96-
}
73+
t.Run(name, func(t *testing.T) {
74+
stack, ok := rollbarerr.StackTracer(err)
75+
if !ok {
76+
t.Fatal("rollbarerr.StackTracer returned false")
77+
}
78+
if stack == nil {
79+
t.Fatalf("expected err to implement rollbar.StackTracer but does not: %v", err)
80+
}
81+
})
9782
}
9883
}

log/log.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,14 @@ func (l Logger) Close() {
100100
}
101101

102102
// Error is used to log an error, the error will be forwared to rollbar and/or other external services.
103-
// All the values of arg are stringified and concatenated without any strings.
103+
// All the values of arg are stringified and concatenated without any space.
104104
// If no args are provided err.Error() is used as the log message.
105105
func (l Logger) Error(err error, args ...interface{}) {
106106
if err == nil {
107107
return
108108
}
109109
if ok := os.Getenv("ROLLBAR_TOKEN"); ok != "" {
110-
rollbar.Notify(err, args)
110+
rollbar.Notify(err)
111111
}
112112
if len(args) == 0 {
113113
args = append(args, err)

prometheus/promdb/promdb_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ func TestNewDatabaseCollectorWithNoLabels(t *testing.T) {
5858
collector := NewDatabaseCollector(db)
5959

6060
if collector == nil {
61-
t.Error("got nil collector want DatabaseCollector")
61+
t.Fatal("got nil collector want DatabaseCollector")
6262
}
6363

6464
if collector.db != db {
65-
t.Errorf("got %+v database want %+v", collector.db, db)
65+
t.Fatalf("got %+v database want %+v", collector.db, db)
6666
}
6767

6868
if collector.labels != nil {
69-
t.Errorf("got %q labels want nil", collector.labels)
69+
t.Fatalf("got %q labels want nil", collector.labels)
7070
}
7171
}
7272

@@ -77,15 +77,15 @@ func TestNewDatabaseCollectorWithLabels(t *testing.T) {
7777
collector := NewDatabaseCollectorWithLabels(db, labels)
7878

7979
if collector == nil {
80-
t.Error("got nil collector want DatabaseCollector")
80+
t.Fatal("got nil collector want DatabaseCollector")
8181
}
8282

8383
if collector.db != db {
84-
t.Errorf("got %+v database want %+v", collector.db, db)
84+
t.Fatalf("got %+v database want %+v", collector.db, db)
8585
}
8686

8787
if !labelsMatch(collector.labels, labels) {
88-
t.Errorf("got %q labels want %q", collector.labels, labels)
88+
t.Fatalf("got %q labels want %q", collector.labels, labels)
8989
}
9090
}
9191

shell.nix

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
let _pkgs = import <nixpkgs> { };
22
in { pkgs ? import (_pkgs.fetchFromGitHub {
33
owner = "NixOS";
4-
repo = "nixpkgs-channels";
5-
#branch@date: nixpkgs-unstable@2020-02-01
6-
rev = "e3a9318b6fdb2b022c0bda66d399e1e481b24b5c";
7-
sha256 = "1hlblna9j0afvcm20p15f5is7cmwl96mc4vavc99ydc4yc9df62a";
4+
repo = "nixpkgs";
5+
#branch@date: nixpkgs-unstable@2021-10-11
6+
rev = "2cdd608fab0af07647da29634627a42852a8c97f";
7+
sha256 = "1szv364xr25yqlljrlclv8z2lm2n1qva56ad9vd02zcmn2pimdih";
88
}) { } }:
99

1010
with pkgs;
1111

12-
mkShell {
13-
buildInputs = [
14-
go
15-
goimports
16-
golangci-lint
17-
];
18-
}
12+
mkShell { buildInputs = [ go goimports golangci-lint nixfmt ]; }

0 commit comments

Comments
 (0)