Skip to content

Comments

Go backend#1089

Open
colinkawai wants to merge 4 commits intomasterfrom
go
Open

Go backend#1089
colinkawai wants to merge 4 commits intomasterfrom
go

Conversation

@colinkawai
Copy link

Two main functions to review are getProducts() and getInventory()

@vercel
Copy link
Contributor

vercel bot commented Nov 10, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
empower Ignored Ignored Nov 10, 2025 4:31am

for k, v := range m {
if f, ok := v.(float64); ok {
if id, _ := strconv.Atoi(k); id > 0 {
quantities[id] = int(f) // Go panic: assignment to entry in nil map
Copy link
Contributor

Choose a reason for hiding this comment

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

Bug: The quantities map is declared but not initialized before assignment, causing a panic during checkout processing.
Severity: CRITICAL | Confidence: 1.00

🔍 Detailed Analysis

The Go server will panic when processing a normal checkout request due to an assignment to an uninitialized map. The quantities map is declared as var quantities map[int]int but not initialized using make before values are assigned to its entries at go/cmd/server/main.go:514. This occurs when o.ValidateInventory is "true" and o.Cart contains items, which are standard conditions for a user proceeding through checkout with items in their cart. The panic will crash the server's request handler.

💡 Suggested Fix

Initialize the quantities map using quantities = make(map[int]int) before attempting to assign values to its entries.

🤖 Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: go/cmd/server/main.go#L514

Potential issue: The Go server will panic when processing a normal checkout request due
to an assignment to an uninitialized map. The `quantities` map is declared as `var
quantities map[int]int` but not initialized using `make` before values are assigned to
its entries at `go/cmd/server/main.go:514`. This occurs when `o.ValidateInventory` is
"true" and `o.Cart` contains items, which are standard conditions for a user proceeding
through checkout with items in their cart. The panic will crash the server's request
handler.

Did we get this right? 👍 / 👎 to inform future reviews.

@codecov
Copy link

codecov bot commented Nov 10, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 24.62%. Comparing base (da05e8e) to head (eb43652).
✅ All tests successful. No failed tests found.

❌ Your project check has failed because the head coverage (24.62%) is below the target coverage (35.00%). You can increase the head coverage or adjust the target coverage.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1089   +/-   ##
=======================================
  Coverage   24.62%   24.62%           
=======================================
  Files          41       41           
  Lines        1202     1202           
  Branches      137      137           
=======================================
  Hits          296      296           
  Misses        881      881           
  Partials       25       25           
Flag Coverage Δ
api 5.42% <ø> (ø)
frontend 40.98% <ø> (ø)
Components Coverage Δ
checkout_module 5.42% <ø> (ø)
product_component 37.69% <ø> (ø)
Files with missing lines Coverage Δ
react/src/utils/backendrouter.js 92.30% <ø> (ø)

@cstavitsky
Copy link
Contributor

@sentry review

Copy link
Collaborator

@giortzisg giortzisg left a comment

Choose a reason for hiding this comment

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

Had a look at the go server implementation and left some comments

Comment on lines +281 to +288
// wrap mux with a 404 logger
logged := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rw := &statusRecorder{ResponseWriter: w, status: 200}
mux.ServeHTTP(rw, r)
if rw.status == http.StatusNotFound {
log.Printf("404 Not Found: %s %s", r.Method, r.URL.Path)
}
})
Copy link
Collaborator

Choose a reason for hiding this comment

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

I noticed that we also trace 404 requests on the dashboard. We should probably skip these by setting the status code correctly to 404 (Just included 404 ignores with version 0.38.0, so the SDK version also needs to be bumped)

mux.HandleFunc("/product/0/info", func(w http.ResponseWriter, r *http.Request) {
// Let Sentry HTTP middleware handle transaction creation automatically
// This is crucial for N+1 detection to work properly
logger := sentry.NewLogger(r.Context())
Copy link
Collaborator

Choose a reason for hiding this comment

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

for the purpose of the demo I would imagine it makes sense to use slog, rather than our own logger, since most people would default to that.

querySpan := sentry.StartSpan(ctx, "get_inventory", sentry.WithDescription("db.query"))
defer querySpan.Finish()

sqlSpan := sentry.StartSpan(querySpan.Context(), "db", sentry.WithDescription("SELECT * FROM inventory WHERE productId"))
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't really know if it makes sense to showcase sql transactions for Go, since we currently don't support a db integration (although we will very soon).

On the same note, is the flask app creating fake spans or is it actually integrating with a db?

Copy link
Author

@colinkawai colinkawai Nov 13, 2025

Choose a reason for hiding this comment

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

Flask uses the Sql integration. Yeah the purpose here is to showcase the n+1 performance capability. Are go devs not tracking db queries right now in Sentry?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not yet, we have an sql integration PR that's almost ready, but currently it's getting blocked by a core refactoring we are doing. I guess it's ok for now, but we should use the actual integration when we release it.

@giortzisg
Copy link
Collaborator

Also another consideration to showcase the product even more, we introduced exception groups from v0.36.0 so it would be nice if we could use a third party library like https://github.com/go-errors/errors, so that our nested errors would have their own unique stack traces.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants