Set Up Feature Flags

With Feature Flags, Sentry tracks feature flag evaluations in your application, keeps an audit log of feature flag changes, and reports any suspicious updates that may have caused an error.

If you use a third-party SDK to evaluate feature flags, use the generic API to manually track feature flag evaluations. The SDK stores each evaluation on the current scope. If a span or transaction is active, the SDK also records the evaluation on that active span or transaction. Only boolean flag evaluations are supported.

Call SentrySDK.addFeatureFlag after evaluating a feature flag:

Copied
import Sentry

SentrySDK.addFeatureFlag(name: "test-flag", result: false)
SentrySDK.capture(error: error)

Go to your Sentry project and confirm that your error event has recorded the feature flag "test-flag" and its value "false".

If you use a custom hub, add the feature flag evaluation to that hub. This updates the custom hub's scope and active span or transaction, without changing the global SentrySDK scope:

Copied
hub.addFeatureFlag(name: "test-flag", result: false)
hub.capture(error: error)

Use a scope callback when the evaluation should apply to this capture's local scope instead of remaining on the current scope:

Copied
SentrySDK.capture(error: error) { scope in
    scope.addFeatureFlag(name: "test-flag", result: false)
}

Feature flags added in a scope callback don't persist on the current scope. If a span or transaction is active, the SDK still records the evaluation on that active span or transaction.

Clear feature flags when they no longer apply, such as after a user signs out or switches accounts:

Copied
SentrySDK.configureScope { scope in
    scope.clearFeatureFlags()
}

clearFeatureFlags() removes evaluations from the current scope. It doesn't remove evaluations already recorded on an active span or transaction.

Scope evaluations are attached to error and message events. The current scope keeps the latest evaluation for up to 100 flag names. Re-evaluating a flag updates its stored value and makes it the most recent evaluation.

Active spans and transactions record evaluations for up to 10 flag names as span attributes using the flag.evaluation.<name> key. Re-evaluating a recorded flag updates its value. After 10 flag names are recorded, evaluations for new flag names are ignored. Spans don't inherit evaluations from their parent span.

Change tracking requires registering a Sentry webhook with a feature flag provider. For set up instructions, visit the documentation for your provider:

Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").