Skip to content

feat: add WithMechanism capture option - #1472

Open
sharmis-tp wants to merge 6 commits into
getsentry:masterfrom
sharmis-tp:issue-1102-with-mechanism
Open

sharmis-tp wants to merge 6 commits into
getsentry:masterfrom
sharmis-tp:issue-1102-with-mechanism

Conversation

@sharmis-tp

Copy link
Copy Markdown

Adds a sentry.WithMechanism(*sentry.Mechanism) capture option, so the exception mechanism (type, handled flag, data) can be set for a single capture through the existing CaptureOption API. This works for CaptureException, Recover and CaptureEvent, both the package-level functions and the Client methods:

sentry.Recover(ctx, recovered, sentry.WithMechanism(&sentry.Mechanism{
    Type:    "auto.http.go.gin.middleware",
    Handled: new(false),
}))

Until now every SDK capture reported a generic mechanism (or none for a single error), so integrations could not report their own mechanism type, as the exception mechanism spec asks for, and there was no public way to mark an exception as unhandled.

How it behaves:

  • The mechanism goes on the outermost exception, which is the error passed to CaptureException or Recover. Exceptions deeper in the error chain keep their chained mechanism.
  • ExceptionID, ParentID, Source and IsExceptionGroup link the exception chain together, so they are kept from the mechanism the SDK generated rather than taken from the option. An empty Type keeps the generated type, or falls back to generic.
  • It is applied in prepareEvent, before event processors and BeforeSend, so both see the final mechanism.
  • The supplied *Mechanism is copied, including Handled and Data, so integrations can share one package-level value across captures without callbacks changing it.
  • Events without exceptions (messages, and panics with a non-error value) are not changed.

As agreed in the issue, the integrations will switch to their own mechanism types in a follow-up PR.

Issues


## Checks run locally

Go is not installed on the machine, so everything ran in Docker:

| Check | Command | Result |
|---|---|---|
| gofmt | `golang:1.26`: `gofmt -l -s .` | no files listed |
| build | `golang:1.26`: `go build work` | ok |
| vet | `golang:1.26`: `go vet work` (same as `make vet`) | ok |
| tests, whole workspace, race detector | `golang:1.26`: `go test -count=1 -timeout 300s -race work` (same as `make test-race`) | all packages ok |
| root package tests on Go 1.27 | `golangci/golangci-lint:v2.14` image (Go 1.27.1): `go test -count=1 -race .` | ok |
| lint | `golangci/golangci-lint:v2.14`: `golangci-lint run --timeout=10m ./...` (root module) | 0 issues |
| new tests are real | removed the `applyMechanism` call and ran `TestWithMechanism` again | 5 of the 7 cases fail, as they should (the nil-mechanism and message cases are no-ops by design) |

Not run:

- `golangci-lint` on the integration sub-modules (CI lints every module in `go.work`). No integration code was changed.
- `make mod-tidy` (the Makefile needs `jq`). No dependencies or `go.mod` files changed.
- Windows and macOS (CI runs those).
- Nothing was sent to a real Sentry instance, so how Sentry's UI shows the mechanism was not checked.

## Files changed

- `capture_options.go`: `mechanism` field on `captureOptions` and the exported `WithMechanism` option with its doc comment.
- `exception.go`: `(*Event).applyMechanism`, which copies the mechanism onto the outermost exception and keeps the chain-link fields.
- `client.go`: one line in `prepareEvent` that calls `event.applyMechanism(opts.mechanism)` after the level is resolved.
- `capture_options_test.go`: `TestWithMechanism`, a table test using the `sentrytest` fixture. It covers CaptureException, Recover, a wrapped error chain, an `errors.Join` group, an empty type, a nil mechanism and a message, and checks what reaches `BeforeSend`. Also `TestWithMechanismCopiesSharedMechanism`.

No CHANGELOG edit: since the 1.0 work, `CHANGELOG.md` is only changed in `release:` commits. Craft builds the entry from the PR title, or from a `### Changelog Entry` section in the PR body.

@giortzisg giortzisg left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The PR still needs to follow the defaults for user and sdk related captures (see the spec). We essentially need to set the default to {type: "generic", handled: true} on exception.go

Comment thread capture_options.go Resolved
@sharmis-tp

Copy link
Copy Markdown
Author

Thanks for the review! Updated in 994ea63:

  1. Simplified WithMechanism to WithMechanism(mechanismType string, handled bool) — the SDK-owned fields (ExceptionID, ParentID, Source, IsExceptionGroup, Data) are no longer user-settable.

  2. Default mechanism is now {type: "generic", handled: true} on every exception produced by convertErrorToExceptions:

    • Single errors get a clean {type: "generic", handled: true} mechanism (was nil)
    • Root exceptions in chains also get handled: true on their generated mechanism

All root-package tests pass with -race.

@giortzisg giortzisg left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

looks good, left some small recommendations

Comment thread capture_options.go Resolved
Comment thread capture_options_test.go Resolved
Comment thread capture_options_test.go Resolved
Comment thread exception.go Resolved
Comment thread exception.go Resolved
@sharmis-tp

Copy link
Copy Markdown
Author

Pushed — removed the "message is unaffected" test case as suggested. All remaining tests pass.

@sharmis-tp
sharmis-tp force-pushed the issue-1102-with-mechanism branch from 2ed04e2 to bd5c921 Compare November 14, 2026 02:48
@sharmis-tp
sharmis-tp requested a review from a team as a code owner November 14, 2026 02:48
@sharmis-tp

Copy link
Copy Markdown
Author

Simplified TestWithMechanism to a single wrapped-group test case as suggested. All the other review suggestions (simplified doc comment, cmp.Or, removed nil-mechanism code) were already addressed in 994ea63.

@giortzisg giortzisg left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good. Let's do the integrations followup before merging though.

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.

Set mechanism type to origin

2 participants