Skip to content

Commit

Permalink
feat: configure sentry (pathwar#534)
Browse files Browse the repository at this point in the history
Signed-off-by: Manfred Touron <[email protected]>
  • Loading branch information
moul committed Jun 11, 2020
1 parent 86b841a commit b3da2ca
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 10 deletions.
2 changes: 2 additions & 0 deletions AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion deployments/agent-dev/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ services:
- PATHWAR_AGENT_NGINX_DOMAIN_SUFFIX
- PATHWAR_AGENT_NAME
- PATHWAR_AGENT_NGINX_PORT
- PATHWAR_SENTRY_DSN
labels:
com.centurylinklabs.watchtower.enable: "true"
working_dir: /data/
command:
- --debug
- --sentry-dsn=$PATHWAR_SENTRY_DSN
- agent
- --salt=uns3cur3
- --moderator-password=uns3cur3
- --nginx-domain-suffix=$PATHWAR_AGENT_NGINX_DOMAIN_SUFFIX
- --agent-name=$PATHWAR_AGENT_NAME
- --port=$PATHWAR_AGENT_NGINX_PORT

# -agent-name localhost Agent Name
# -clean false remove all pathwar instances before executing
# -delay 10s delay between each loop iteration
Expand Down
3 changes: 3 additions & 0 deletions deployments/platform-dev/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ FOREST_AUTH_SECRET=CHANGEME
API_DEV_VIRTUAL_HOST=api-dev.pathwar.land
API_UNSAFE_VIRTUAL_HOST=api-unsafe.pathwar.land
ADMIN_DEV_VIRTUAL_HOST=admin-dev.pathwar.land

# sentry
SENTRY_DSN=CHANGEME
4 changes: 4 additions & 0 deletions deployments/platform-dev/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ services:
- VIRTUAL_PORT=8000
# loaded from .env
- API_URN
- SENTRY_DSN
command:
- pathwar
- --debug
- --sentry-dsn=$SENTRY_DSN
- api
- --urn=$API_URN
- server
Expand Down Expand Up @@ -44,9 +46,11 @@ services:
- VIRTUAL_PORT=8000
# loaded from .env
- API_URN
- SENTRY_DSN
command:
- pathwar
- --debug
- --sentry-dsn=$SENTRY_DSN
- api
- --urn=$API_URN
- --sso-unsafe
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ require (
github.com/docker/go-connections v0.4.0
github.com/docker/go-units v0.4.0 // indirect
github.com/dustin/go-humanize v1.0.0
github.com/getsentry/sentry-go v0.6.1
github.com/go-chi/chi v4.1.1+incompatible
github.com/go-sql-driver/mysql v1.5.0
github.com/gobuffalo/packr/v2 v2.8.0
github.com/gogo/gateway v1.1.0
github.com/gogo/protobuf v1.3.1
github.com/golang/protobuf v1.3.2
github.com/google/go-cmp v0.4.0 // indirect
github.com/google/go-querystring v1.0.0
github.com/grpc-ecosystem/go-grpc-middleware v1.2.0
github.com/grpc-ecosystem/grpc-gateway v1.14.5
Expand Down
108 changes: 108 additions & 0 deletions go.sum

Large diffs are not rendered by default.

69 changes: 61 additions & 8 deletions go/cmd/pathwar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
bearer "github.com/Bearer/bearer-go"
"github.com/bwmarrin/snowflake"
"github.com/docker/docker/client"
sentry "github.com/getsentry/sentry-go"
_ "github.com/go-sql-driver/mysql" // required by gorm
"github.com/jinzhu/gorm"
"github.com/oklog/run"
Expand Down Expand Up @@ -98,6 +99,7 @@ var (
composeUpForceRecreate bool
composeUpInstanceKey string
globalDebug bool
globalSentryDSN string
httpAPIAddr string
serverBind string
serverCORSAllowedOrigins string
Expand Down Expand Up @@ -148,6 +150,7 @@ func main() {
globalFlags.BoolVar(&globalDebug, "debug", false, "debug mode")
globalFlags.StringVar(&zipkinEndpoint, "zipkin-endpoint", "", "optional opentracing server")
globalFlags.StringVar(&bearerSecretKey, "bearer-secretkey", "", "bearer.sh secret key")
globalFlags.StringVar(&globalSentryDSN, "sentry-dsn", "", "Sentry DSN")

agentFlags.BoolVar(&agentClean, "clean", false, "remove all pathwar instances before executing")
agentFlags.BoolVar(&agentRunOnce, "once", false, "run once and don't start daemon loop")
Expand Down Expand Up @@ -252,9 +255,15 @@ func main() {
FlagSet: serverFlags,
Options: []ff.Option{ff.WithEnvVarNoPrefix()},
Exec: func(args []string) error {
if err := globalPreRun(); err != nil {
err := globalPreRun()
if err != nil {
return err
}
cleanup, err := initSentryFromEnv("starting API")
if err != nil {
return err
}
defer cleanup()

// init svc
svc, _, closer, err := svcFromFlags()
Expand Down Expand Up @@ -303,7 +312,8 @@ func main() {
Name: "sqlinfo",
Usage: "pathwar [global flags] api [api flags] sqlinfo",
Exec: func([]string) error {
if err := globalPreRun(); err != nil {
err := globalPreRun()
if err != nil {
return err
}

Expand All @@ -327,7 +337,8 @@ func main() {
Name: "sqldump",
Usage: "pathwar [global flags] api [api flags] sqldump",
Exec: func([]string) error {
if err := globalPreRun(); err != nil {
err := globalPreRun()
if err != nil {
return err
}

Expand Down Expand Up @@ -408,9 +419,11 @@ func main() {
if len(args) < 1 {
return flag.ErrHelp
}
if err := globalPreRun(); err != nil {
err := globalPreRun()
if err != nil {
return err
}

sso, err := ssoFromFlags()
if err != nil {
return errcode.ErrGetSSOClientFromFlags.Wrap(err)
Expand All @@ -429,9 +442,11 @@ func main() {
if len(args) < 1 {
return flag.ErrHelp
}
if err := globalPreRun(); err != nil {
err := globalPreRun()
if err != nil {
return err
}

sso, err := ssoFromFlags()
if err != nil {
return errcode.ErrGetSSOClientFromFlags.Wrap(err)
Expand Down Expand Up @@ -462,9 +477,11 @@ func main() {
FlagSet: composeUpFlags,
Options: []ff.Option{ff.WithEnvVarNoPrefix()},
Exec: func(args []string) error {
if err := globalPreRun(); err != nil {
err := globalPreRun()
if err != nil {
return err
}

if len(args) < 1 {
return flag.ErrHelp
}
Expand Down Expand Up @@ -506,7 +523,8 @@ func main() {
return flag.ErrHelp
}
path := args[0]
if err := globalPreRun(); err != nil {
err := globalPreRun()
if err != nil {
return err
}

Expand All @@ -528,7 +546,8 @@ func main() {
FlagSet: composePSFlags,
Options: []ff.Option{ff.WithEnvVarNoPrefix()},
Exec: func(args []string) error {
if err := globalPreRun(); err != nil {
err := globalPreRun()
if err != nil {
return err
}

Expand Down Expand Up @@ -803,6 +822,12 @@ func main() {
if err := globalPreRun(); err != nil {
return err
}
cleanup, err := initSentryFromEnv("starting agent")
if err != nil {
return err
}
defer cleanup()

ctx := context.Background()
dockerCli, err := client.NewEnvClient()
if err != nil {
Expand Down Expand Up @@ -1014,3 +1039,31 @@ func httpClientFromEnv(ctx context.Context) (*pwapi.HTTPClient, error) {

return pwapi.NewHTTPClient(oauth2.NewClient(ctx, ts), httpAPIAddr), nil
}

func initSentryFromEnv(startMessage string) (func(), error) {
cleanup := func() {}
if globalSentryDSN != "" {
// doc here: https://docs.sentry.io/platforms/go/config/
err := sentry.Init(sentry.ClientOptions{
Dsn: globalSentryDSN,
Release: pwversion.Version,
AttachStacktrace: true,
Debug: false,
// List of regexp strings that will be used to match against event's message
// and if applicable, caught errors type and value.
// If the match is found, then a whole event will be dropped.
// IgnoreErrors: []string{},
})
if err != nil {
return nil, err
}
cleanup = func() {
sentry.Flush(2 * time.Second)
sentry.Recover()
}
if startMessage != "" {
sentry.CaptureMessage(startMessage)
}
}
return cleanup, nil
}
1 change: 1 addition & 0 deletions go/cmd/pathwar/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ func Example() {
// FLAGS
// -bearer-secretkey ... bearer.sh secret key
// -debug false debug mode
// -sentry-dsn ... Sentry DSN
// -zipkin-endpoint ... optional opentracing server
}
5 changes: 5 additions & 0 deletions go/pkg/pwapi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"time"

sentryhttp "github.com/getsentry/sentry-go/http"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/gogo/gateway"
Expand Down Expand Up @@ -251,6 +252,10 @@ func httpServer(ctx context.Context, serverListenerAddr string, opts ServerOpts)
r.Use(middleware.Timeout(opts.RequestTimeout))
r.Use(middleware.RealIP)
r.Use(middleware.RequestID)
sentryMiddleware := sentryhttp.New(sentryhttp.Options{
Repanic: true,
})
r.Use(sentryMiddleware.Handle)

runtimeMux := runtime.NewServeMux(
runtime.WithMarshalerOption(runtime.MIMEWildcard, &gateway.JSONPb{
Expand Down

0 comments on commit b3da2ca

Please sign in to comment.