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: 0 additions & 7 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@ linters:
- third_party$
- builtin$
- examples$
settings:
forbidigo:
analyze-types: true
forbid:
- 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
58 changes: 28 additions & 30 deletions cmd/vroom/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"google.golang.org/api/googleapi"

"github.com/getsentry/vroom/internal/chunk"
"github.com/getsentry/vroom/internal/sentryutil"
"github.com/getsentry/vroom/internal/storageutil"
)

Expand Down Expand Up @@ -41,41 +42,40 @@ type postProfileFromChunkIDsResponse struct {
// This is more of a GET method, but since we're receiving a list of chunk IDs as part of a
// body request, we use a POST method instead (similarly to the flamegraph endpoint).
func (env *environment) postProfileFromChunkIDs(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
hub := sentry.GetHubFromContext(ctx)
ctx, scope := sentryutil.EnsureScope(r.Context())

ps := httprouter.ParamsFromContext(ctx)
rawOrganizationID := ps.ByName("organization_id")
organizationID, err := strconv.ParseUint(rawOrganizationID, 10, 64)
if err != nil {
hub.CaptureException(err)
sentry.CaptureException(ctx, err)
w.WriteHeader(http.StatusBadRequest)
return
}

hub.Scope().SetTag("organization_id", rawOrganizationID)
scope.SetTag("organization_id", rawOrganizationID)

rawProjectID := ps.ByName("project_id")
projectID, err := strconv.ParseUint(rawProjectID, 10, 64)
if err != nil {
hub.CaptureException(err)
sentry.CaptureException(ctx, err)
w.WriteHeader(http.StatusBadRequest)
return
}
hub.Scope().SetTag("project_id", rawProjectID)
scope.SetTag("project_id", rawProjectID)

var requestBody postProfileFromChunkIDsRequest
s := sentry.StartSpan(ctx, "processing")
s.Description = "Decoding data"
err = json.NewDecoder(r.Body).Decode(&requestBody)
s.Finish()
if err != nil {
hub.CaptureException(err)
sentry.CaptureException(ctx, err)
w.WriteHeader(http.StatusBadRequest)
return
}
r.Body.Close()

hub.Scope().SetTag("num_chunks", fmt.Sprintf("%d", len(requestBody.ChunkIDs)))
scope.SetTag("num_chunks", fmt.Sprintf("%d", len(requestBody.ChunkIDs)))
s = sentry.StartSpan(ctx, "chunks.read")
s.Description = "Read profile chunks from GCS"

Expand Down Expand Up @@ -128,14 +128,14 @@ func (env *environment) postProfileFromChunkIDs(w http.ResponseWriter, r *http.R
}
var e *googleapi.Error
if ok := errors.As(err, &e); ok {
hub.Scope().SetContext("Google Cloud Storage Error", map[string]interface{}{
scope.SetContext("Google Cloud Storage Error", map[string]interface{}{
"body": e.Body,
"code": e.Code,
"details": e.Details,
"message": e.Message,
})
}
hub.CaptureException(err)
sentry.CaptureException(ctx, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
Expand Down Expand Up @@ -174,7 +174,7 @@ func (env *environment) postProfileFromChunkIDs(w http.ResponseWriter, r *http.R
mergedChunk, err := chunk.MergeSampleChunks(sampleChunks, requestBody.Start, requestBody.End)
s.Finish()
if err != nil {
hub.CaptureException(err)
sentry.CaptureException(ctx, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
Expand All @@ -185,7 +185,7 @@ func (env *environment) postProfileFromChunkIDs(w http.ResponseWriter, r *http.R
})
s.Finish()
if err != nil {
hub.CaptureException(err)
sentry.CaptureException(ctx, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
Expand All @@ -205,7 +205,7 @@ func (env *environment) postProfileFromChunkIDs(w http.ResponseWriter, r *http.R
sp, err := chunk.SpeedscopeFromAndroidChunks(androidChunks, requestBody.Start, requestBody.End)
s.Finish()
if err != nil {
hub.CaptureException(err)
sentry.CaptureException(ctx, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
Expand All @@ -216,7 +216,7 @@ func (env *environment) postProfileFromChunkIDs(w http.ResponseWriter, r *http.R
})
s.Finish()
if err != nil {
hub.CaptureException(err)
sentry.CaptureException(ctx, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
Expand All @@ -232,50 +232,48 @@ func (env *environment) postProfileFromChunkIDs(w http.ResponseWriter, r *http.R
}

func (env *environment) getRawChunk(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
hub := sentry.GetHubFromContext(ctx)
ctx, scope := sentryutil.EnsureScope(r.Context())

ps := httprouter.ParamsFromContext(ctx)
rawOrganizationID := ps.ByName("organization_id")
organizationID, err := strconv.ParseUint(rawOrganizationID, 10, 64)
if err != nil {
hub.CaptureException(err)
sentry.CaptureException(ctx, err)
w.WriteHeader(http.StatusBadRequest)
return
}

hub.Scope().SetTag("organization_id", rawOrganizationID)
scope.SetTag("organization_id", rawOrganizationID)

rawProjectID := ps.ByName("project_id")
projectID, err := strconv.ParseUint(rawProjectID, 10, 64)
if err != nil {
hub.CaptureException(err)
sentry.CaptureException(ctx, err)
w.WriteHeader(http.StatusBadRequest)
return
}

hub.Scope().SetTag("project_id", rawProjectID)
scope.SetTag("project_id", rawProjectID)

profilerID := ps.ByName("profiler_id")
_, err = uuid.Parse(profilerID)
if err != nil {
hub.CaptureException(err)
sentry.CaptureException(ctx, err)
w.WriteHeader(http.StatusBadRequest)
return
}

hub.Scope().SetTag("profiler_id", profilerID)

hub.Scope().SetTag("project_id", rawProjectID)
scope.SetTag("profiler_id", profilerID)

chunkID := ps.ByName("chunk_id")
_, err = uuid.Parse(chunkID)
if err != nil {
hub.CaptureException(err)
sentry.CaptureException(ctx, err)
w.WriteHeader(http.StatusBadRequest)
return
}

hub.Scope().SetTag("chunk_id", chunkID)
scope.SetTag("chunk_id", chunkID)

s := sentry.StartSpan(ctx, "chunk.read")
s.Description = "Read chunk from GCS"
Expand All @@ -295,14 +293,14 @@ func (env *environment) getRawChunk(w http.ResponseWriter, r *http.Request) {
}
var e *googleapi.Error
if ok := errors.As(err, &e); ok {
hub.Scope().SetContext("Google Cloud Storage Error", map[string]interface{}{
scope.SetContext("Google Cloud Storage Error", map[string]interface{}{
"body": e.Body,
"code": e.Code,
"details": e.Details,
"message": e.Message,
})
}
hub.CaptureException(err)
sentry.CaptureException(ctx, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
Expand All @@ -311,7 +309,7 @@ func (env *environment) getRawChunk(w http.ResponseWriter, r *http.Request) {
defer s.Finish()
b, err := json.Marshal(c)
if err != nil {
hub.CaptureException(err)
sentry.CaptureException(ctx, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
Expand Down
23 changes: 8 additions & 15 deletions cmd/vroom/flamegraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/getsentry/vroom/internal/examples"
"github.com/getsentry/vroom/internal/flamegraph"
"github.com/getsentry/vroom/internal/metrics"
"github.com/getsentry/vroom/internal/sentryutil"
)

type (
Expand All @@ -24,32 +25,28 @@ type (
)

func (env *environment) postFlamegraph(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctx, scope := sentryutil.EnsureScope(r.Context())
downloadContext, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()
hub := sentry.GetHubFromContext(ctx)

ps := httprouter.ParamsFromContext(ctx)
rawOrganizationID := ps.ByName("organization_id")
organizationID, err := strconv.ParseUint(rawOrganizationID, 10, 64)
if err != nil {
if hub != nil {
hub.CaptureException(err)
}
sentry.CaptureException(ctx, err)
w.WriteHeader(http.StatusBadRequest)
return
}

hub.Scope().SetTag("organization_id", rawOrganizationID)
scope.SetTag("organization_id", rawOrganizationID)

var body postFlamegraphBody
s := sentry.StartSpan(ctx, "processing")
s.Description = "Decoding data"
err = json.NewDecoder(r.Body).Decode(&body)
s.Finish()
if err != nil {
if hub != nil {
hub.CaptureException(err)
}
sentry.CaptureException(ctx, err)
w.WriteHeader(http.StatusBadRequest)
return
}
Expand All @@ -71,9 +68,7 @@ func (env *environment) postFlamegraph(w http.ResponseWriter, r *http.Request) {
)
s.Finish()
if err != nil {
if hub != nil {
hub.CaptureException(err)
}
sentry.CaptureException(ctx, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
Expand All @@ -82,9 +77,7 @@ func (env *environment) postFlamegraph(w http.ResponseWriter, r *http.Request) {
defer s.Finish()
b, err := json.Marshal(speedscope)
if err != nil {
if hub != nil {
hub.CaptureException(err)
}
sentry.CaptureException(ctx, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
Expand Down
25 changes: 9 additions & 16 deletions cmd/vroom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,15 @@ func newEnvironment(ctx context.Context) (*environment, error) {
}

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

func (e *environment) newRouter() (*httprouter.Router, error) {
Expand Down Expand Up @@ -177,15 +176,9 @@ 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 {
rootHub.CaptureException(err)
sentry.CaptureException(context.Background(), err)
log.Fatal("error setting up the router", err)
}

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

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

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

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

slog.Info("vroom started")

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

err = server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
rootHub.CaptureException(err)
sentry.CaptureException(context.Background(), err)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This seems unexpected - would we not use the same context for these calls vs making a new empty one?

slog.Error("server failed", "err", err)
}

<-waitForShutdown

// Shutdown the rest of the environment after the HTTP connections are closed
close(readJobs)
env.shutdown(rootCtx)
env.shutdown(context.Background())
slog.Info("vroom graceful shutdown")
Comment thread
mjq marked this conversation as resolved.
Resolved
}

Expand Down
Loading
Loading