Repository navigation
Add optional Sentry error monitoring - #154
Merged
Merged
Conversation
Port of MyTurnTag API's Sentry setup. sentry-ruby / sentry-rails 7.1, initialised only when SENTRY_DSN is set (config/initializers/sentry.rb), so development, test and any deploy without a DSN behave as before. - Events are sent only from production, errors only: no tracing, profiling or breadcrumbs. - Expected 4xx exceptions (RecordInvalid, RecordNotDestroyed, Pundit) are excluded on top of sentry-rails' defaults. - No personal data: send_default_pii off, the user is the shopkeeper's ID, the account ID is a tag (SetCurrentRequestDetails), and before_send drops the devise_token_auth headers, masks email addresses and applies filter_parameters. - Kamal: SENTRY_DSN is an optional env secret read from NATIVEAPPTEMPLATEAPI_SENTRY_DSN. - README documents turning it on, what is sent and the recommended Sentry privacy settings (no IP storage, scrub $user.geo.**). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01K5hZEFqXhBsUQwSWsgR6eH
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01K5hZEFqXhBsUQwSWsgR6eH
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Ports the optional Sentry error monitoring from the MyTurnTag API (sentry-ruby / sentry-rails 7.1.0, same gem versions) to the template.
SENTRY_DSNis set. Without itSentry.initis never called; development, test and any deploy without a DSN behave exactly as before.enabled_environments = %w[production]; tracing and profiling are off (nil, not0.0), and breadcrumbs are disabled.rescue_fromhandlers inApi::V1::Shopkeeper::BaseController, sentry-rails' defaults, plusActiveRecord::RecordInvalid,ActiveRecord::RecordNotDestroyedandPundit::NotAuthorizedError(NativeapptemplateSentry::EXPECTED_EXCEPTIONS).send_default_pii = false. The Sentry user is the shopkeeper's ID only, and the account ID is a tag (SetCurrentRequestDetails).before_senddrops the devise_token_auth headers, masks email addresses in messages, exceptions and the URL, and appliesfilter_parametersto request data and extras.SENTRY_DSNis an optionalenv.secret..kamal/secretsreads it fromNATIVEAPPTEMPLATEAPI_SENTRY_DSN; if that is unset, the container gets an empty value and Sentry stays off. Nothing was deployed.$user.geo.**), because the IP setting alone doesn't drop geo. AGENTS.md and CHANGELOG are updated too.No DSN or other secret is committed.
Tests
test/config/sentry_test.rb: no init without a DSN (checked at boot, independent of test order); with a DSN, production-only, no PII, no tracing, profiling or breadcrumbs; expected exceptions excluded and real bugs allowed;before_sendscrubbing.test/integration/sentry_context_test.rb: an unhandled error in a shopkeeper request is sent withuser = {id:}and theaccount_idtag, with no email, name or auth headers; a 401 and handled 404s are not sent.test/config/deploy_config_test.rb:SENTRY_DSNis inenv.secretand read from the app-prefixed variable.I broke the code for each new test and confirmed it failed (18 breakages, each one red), then restored the code.
Verification
bin/cipasses: rubocop, erb_lint, bundler-audit, brakeman, 614 tests / 1594 assertions, seeds.Sentry.initialized? == false, andcapture_exception/set_userare no-ops.RAILS_ENV=production) with a dummy DSN pointing at a local listener on 127.0.0.1: one envelope arrived. It had release =KAMAL_VERSION,user: {id}only, theaccount_idtag, only theAcceptheader, no query string,REMOTE_ADDRfiltered and the email masked. TheRecordInvalidcaptured in the same run was not sent.🤖 Generated with Claude Code
https://claude.ai/code/session_01K5hZEFqXhBsUQwSWsgR6eH