-
Notifications
You must be signed in to change notification settings - Fork 0
feat(observability): add Sentry error tracking across backend, web, and mobile #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| --- | ||
| topic: auth | ||
| last_verified: 2026-06-15 | ||
| sources: | ||
| - internal/usecase/auth_usecase.go | ||
| - internal/transport/middleware/auth.go | ||
| - internal/transport/handlers/auth_handler.go | ||
| - pkg/firebase/admin.go | ||
| - internal/bootstrap/bootstrap.go | ||
| --- | ||
|
|
||
| # Firebase Auth | ||
|
|
||
| ## Domain type | ||
|
|
||
| `FirebaseToken` is defined in `internal/usecase/auth_usecase.go` and holds the claims extracted from a verified Firebase ID token: | ||
|
|
||
| ```go | ||
| type FirebaseToken struct { | ||
| UID string `json:"uid"` | ||
| Email string `json:"email"` | ||
| Name string `json:"name"` | ||
| PhotoURL string `json:"photoUrl"` | ||
| Claims map[string]any `json:"claims"` | ||
| } | ||
| ``` | ||
|
|
||
| `UID`, `Email`, `Name`, and `PhotoURL` are promoted from the raw Firebase JWT claims (`uid`, `email`, `name`, `picture`). `Claims` contains the full unmodified payload. | ||
|
|
||
| ## Usecase interfaces | ||
|
|
||
| Both interfaces live in `internal/usecase/auth_usecase.go`. | ||
|
|
||
| ```go | ||
| // FirebaseTokenVerifier can verify a raw Firebase ID token string. | ||
| type FirebaseTokenVerifier interface { | ||
| VerifyIDToken(ctx context.Context, idToken string) (*FirebaseToken, error) | ||
| } | ||
|
|
||
| // FirebaseAdminClient extends FirebaseTokenVerifier with user-management operations. | ||
| type FirebaseAdminClient interface { | ||
| FirebaseTokenVerifier | ||
| GetUserByEmail(ctx context.Context, email string) (string, error) | ||
| UpdateUserPassword(ctx context.Context, uid, newPassword string) error | ||
| } | ||
| ``` | ||
|
|
||
| `FirebaseTokenVerifier` is the narrow interface used by the `FirebaseAuth` middleware. `FirebaseAdminClient` is the broader interface stored on `bootstrap.App` and wired into the server. | ||
|
|
||
| ## pkg/firebase/admin.go | ||
|
|
||
| `NewAuthClient` initialises the Firebase Admin SDK and returns a `usecase.FirebaseAdminClient`: | ||
|
|
||
| ```go | ||
| func NewAuthClient(ctx context.Context, projectID, credentialsJSON string) (usecase.FirebaseAdminClient, error) | ||
| ``` | ||
|
|
||
| - When `credentialsJSON` is non-empty the SDK is initialised with `option.WithCredentialsJSON` (service account key). | ||
| - When `credentialsJSON` is empty the SDK falls back to Application Default Credentials (ADC) — used on GCP. | ||
|
|
||
| The returned value is `*authClientAdapter`, a private type that wraps `*auth.Client` from the Firebase Admin SDK. This adapter satisfies both `FirebaseTokenVerifier` and `FirebaseAdminClient` without leaking the SDK type into the application layer. | ||
|
|
||
| ## FirebaseAuth middleware | ||
|
|
||
| Defined in `internal/transport/middleware/auth.go`. | ||
|
|
||
| ```go | ||
| const FirebaseClaimsKey = "firebase_claims" | ||
|
|
||
| func FirebaseAuth(verifier usecase.FirebaseTokenVerifier) gin.HandlerFunc | ||
| ``` | ||
|
|
||
| For every request the middleware: | ||
| 1. Reads the `Authorization` header; aborts with `401` if the header is missing or does not start with `Bearer `. | ||
| 2. Calls `verifier.VerifyIDToken(ctx, idToken)`. | ||
| 3. On success: stores `*usecase.FirebaseToken` in the Gin context under `FirebaseClaimsKey` and calls `c.Next()`. | ||
| 4. On error: aborts with `401` and `{"error": "invalid or expired token"}`. | ||
|
|
||
| Retrieve claims inside a handler: | ||
| ```go | ||
| val, _ := c.Get(middleware.FirebaseClaimsKey) | ||
| token, ok := val.(*usecase.FirebaseToken) | ||
| ``` | ||
|
|
||
| ## MeHandler (GET /api/v1/me) | ||
|
|
||
| Defined in `internal/transport/handlers/auth_handler.go`. | ||
|
|
||
| ```go | ||
| func (h *Handler) MeHandler(c *gin.Context) | ||
| ``` | ||
|
|
||
| - Reads `*usecase.FirebaseToken` from the Gin context (`FirebaseClaimsKey`). | ||
| - Returns `200 OK` with the token struct serialised as JSON. | ||
| - Returns `401 Unauthorized` with `{"error": "unauthorized"}` if the context value is missing or of the wrong type (should not happen when `FirebaseAuth` is applied to the group). | ||
|
|
||
| The handler is registered on the `/api/v1` group in `RegisterRoutes`: | ||
| ```go | ||
| api := r.Group("/api/v1") | ||
| if verifier != nil { | ||
| api.Use(middleware.FirebaseAuth(verifier)) | ||
| } | ||
| api.GET("/me", h.MeHandler) | ||
| ``` | ||
|
|
||
| ## Disabling auth in development | ||
|
|
||
| When `FIREBASE_PROJECT_ID` is not set `bootstrap.Run` skips Firebase initialisation and `app.Firebase` is `nil`. `server.NewServer` passes `app.Firebase` directly to `RegisterRoutes` as the `verifier` argument. When `verifier` is `nil` the `if verifier != nil` guard in `RegisterRoutes` skips `api.Use(middleware.FirebaseAuth(...))`, so `/api/v1/me` is reachable without a token. | ||
|
|
||
| To enable auth locally set both `FIREBASE_PROJECT_ID` and `FIREBASE_SERVICE_ACCOUNT_JSON` in `backend/.env`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| --- | ||
| topic: observability | ||
| last_verified: 2026-06-15 | ||
| sources: | ||
| - internal/transport/middleware/sentry.go | ||
| - internal/bootstrap/bootstrap.go | ||
| - internal/transport/handlers/routes.go | ||
| --- | ||
|
|
||
| # Observability | ||
|
|
||
| ## Sentry SDK | ||
|
|
||
| | Package | Version | | ||
| |---|---| | ||
| | `github.com/getsentry/sentry-go` | v0.46.2 | | ||
| | `github.com/getsentry/sentry-go/gin` | v0.46.2 | | ||
|
|
||
| ## SentryMiddleware | ||
|
|
||
| `internal/transport/middleware/sentry.go` exports a single function: | ||
|
|
||
| ```go | ||
| func SentryMiddleware(dsn string) gin.HandlerFunc | ||
| ``` | ||
|
|
||
| Behavior: | ||
| - When `dsn` is empty, returns `func(c *gin.Context) { c.Next() }` — a no-op that adds no overhead. | ||
| - When `dsn` is non-empty, calls `sentry.Init(sentry.ClientOptions{Dsn: dsn})` and returns `sentrygin.New(sentrygin.Options{Repanic: true})`. | ||
| - `Repanic: true` means the middleware re-panics after capturing, allowing Gin's `Recovery()` to handle the panic response normally. | ||
|
|
||
| ## Middleware registration order | ||
|
|
||
| `RegisterRoutes` in `internal/transport/handlers/routes.go` registers middleware in this order: | ||
|
|
||
| 1. `SentryMiddleware(sentryDSN)` — first, so it wraps all subsequent handlers | ||
| 2. `gin.Recovery()` + `gin.Logger()` (debug) or `gin.Recovery()` + `middleware.Logger()` (non-debug) | ||
| 3. `middleware.RateLimit(rps, burst)` | ||
| 4. CORS | ||
|
|
||
| `RegisterRoutes` signature: | ||
|
|
||
| ```go | ||
| func (h *Handler) RegisterRoutes(rps float64, burst int, verifier usecase.FirebaseTokenVerifier, sentryDSN string) http.Handler | ||
| ``` | ||
|
|
||
| The `sentryDSN` parameter is forwarded directly from `Config.SentryDSN`. | ||
|
|
||
| ## Environment variable | ||
|
|
||
| | Variable | Required | Default | | ||
| |---|---|---| | ||
| | `SENTRY_DSN` | No | `""` (Sentry disabled) | | ||
|
|
||
| Loaded in `loadConfig()` in `internal/bootstrap/bootstrap.go`: | ||
|
|
||
| ```go | ||
| SentryDSN: os.Getenv("SENTRY_DSN"), | ||
| ``` | ||
|
|
||
| Stored on `Config.SentryDSN`. Not validated — an empty value disables Sentry without error. | ||
|
|
||
| ## Supplying the DSN | ||
|
|
||
| **Local development** — add to `backend/.env`: | ||
| ```dotenv | ||
| SENTRY_DSN=https://<key>@o<org>.ingest.sentry.io/<project> | ||
| ``` | ||
|
|
||
| **Production** — set `SENTRY_DSN` as an environment variable in your deployment platform. The app reads it at startup via `godotenv/autoload` (dev) or the process environment (production). | ||
|
|
||
| Leave `SENTRY_DSN` empty (or omit it) to run without Sentry. The app starts and serves normally in both cases. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.