Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ linters:
- third_party$
- builtin$
- examples$
settings:
forbidigo:
analyze-types: true
forbid:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neat

- pkg: github.com/getsentry/sentry-go
pattern: '^sentry\.(AddBreadcrumb|CaptureCheckIn|CaptureEvent|CaptureException|CaptureMessage|ConfigureScope|CurrentHub|Flush|FlushWithContext|LastEventID|PopScope|PushScope|Recover|RecoverWithContext|WithMonitor|WithScope)$'
msg: "Do not use the global Hub. Always use the local Hub from context."
formatters:
enable:
- gofmt
Expand Down
4 changes: 2 additions & 2 deletions cmd/vroom/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (env *environment) postProfileFromChunkIDs(w http.ResponseWriter, r *http.R
rawProjectID := ps.ByName("project_id")
projectID, err := strconv.ParseUint(rawProjectID, 10, 64)
if err != nil {
sentry.CaptureException(err)
hub.CaptureException(err)
w.WriteHeader(http.StatusBadRequest)
return
}
Expand Down Expand Up @@ -248,7 +248,7 @@ func (env *environment) getRawChunk(w http.ResponseWriter, r *http.Request) {
rawProjectID := ps.ByName("project_id")
projectID, err := strconv.ParseUint(rawProjectID, 10, 64)
if err != nil {
sentry.CaptureException(err)
hub.CaptureException(err)
w.WriteHeader(http.StatusBadRequest)
return
}
Expand Down
32 changes: 19 additions & 13 deletions cmd/vroom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@ var (
readJobs chan storageutil.ReadJob
)

func newEnvironment() (*environment, error) {
func newEnvironment(ctx context.Context) (*environment, error) {
var e environment
err := cleanenv.ReadEnv(&e.config)
if err != nil {
return nil, err
}

ctx := context.Background()
e.storage, err = blob.OpenBucket(ctx, e.config.BucketURL)
if err != nil {
return nil, err
Expand All @@ -68,16 +67,17 @@ func newEnvironment() (*environment, error) {
return &e, nil
}

func (e *environment) shutdown() {
func (e *environment) shutdown(ctx context.Context) {
hub := sentry.GetHubFromContext(ctx)
err := e.storage.Close()
if err != nil {
sentry.CaptureException(err)
hub.CaptureException(err)
}
err = e.occurrencesWriter.Close()
if err != nil {
sentry.CaptureException(err)
hub.CaptureException(err)
}
sentry.Flush(5 * time.Second)
hub.Flush(5 * time.Second)
}

func (e *environment) newRouter() (*httprouter.Router, error) {
Expand Down Expand Up @@ -146,7 +146,7 @@ func main() {
logutil.ConfigureLogger()
clearDownFile()

env, err := newEnvironment()
env, err := newEnvironment(context.Background())
if err != nil {
log.Fatal("error setting up environment", err)
}
Expand Down Expand Up @@ -177,9 +177,15 @@ func main() {
log.Fatal("can't initialize sentry", err)
}

// This should be the only place we use the SDK's global Hub.
// Every goroutine should get its own clone instead.
// See: https://docs.sentry.io/platforms/go/usage/concurrency/
rootHub := sentry.CurrentHub() //nolint:forbidigo
rootCtx := sentry.SetHubOnContext(context.Background(), rootHub.Clone())

router, err := env.newRouter()
if err != nil {
sentry.CaptureException(err)
rootHub.CaptureException(err)
log.Fatal("error setting up the router", err)
}

Expand All @@ -190,7 +196,7 @@ func main() {
}

waitForShutdown := make(chan os.Signal)
go func() {
go func(hub *sentry.Hub) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
Expand All @@ -199,12 +205,12 @@ func main() {
defer cancel()

if err := server.Shutdown(cctx); err != nil {
sentry.CaptureException(err)
hub.CaptureException(err)
slog.Error("error shutting down server", "err", err)
}

close(waitForShutdown)
}()
}(rootHub.Clone())

slog.Info("vroom started")

Expand All @@ -215,15 +221,15 @@ func main() {

err = server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
sentry.CaptureException(err)
rootHub.CaptureException(err)
slog.Error("server failed", "err", err)
}

<-waitForShutdown

// Shutdown the rest of the environment after the HTTP connections are closed
close(readJobs)
env.shutdown()
env.shutdown(rootCtx)
slog.Info("vroom graceful shutdown")
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/vroom/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (env *environment) getRawProfile(w http.ResponseWriter, r *http.Request) {
rawProjectID := ps.ByName("project_id")
projectID, err := strconv.ParseUint(rawProjectID, 10, 64)
if err != nil {
sentry.CaptureException(err)
hub.CaptureException(err)
w.WriteHeader(http.StatusBadRequest)
return
}
Expand Down Expand Up @@ -116,7 +116,7 @@ func (env *environment) getProfile(w http.ResponseWriter, r *http.Request) {
rawProjectID := ps.ByName("project_id")
projectID, err := strconv.ParseUint(rawProjectID, 10, 64)
if err != nil {
sentry.CaptureException(err)
hub.CaptureException(err)
w.WriteHeader(http.StatusBadRequest)
return
}
Expand Down
Loading