Issue
I need to change event fields such as release or environment after the SDK is initialized. For example, I want all outgoing events to use a new environment after a button click.
Applies To
All SaaS Customers and Self-Hosted Users
JavaScript SDK
Event Processors (
Sentry.addEventProcessor/scope.addEventProcessor)
Resolution
Use an Event Processor to set event fields at send time. That overrides values on each event. It does not change the options you passed to Sentry.init.
let dynamicEnvironment = "development";Sentry.addEventProcessor((event) => {
event.environment = dynamicEnvironment;
return event;
});const handleClick = () => {
dynamicEnvironment = "production";
};
You can use the same pattern for other event fields such as release.
Sentry.addEventProcessor runs globally. Processors can run in an undetermined order, so another processor may change the event afterward. If you need the final value right before send, set the field in beforeSend instead. beforeSend is guaranteed to run last.
let dynamicEnvironment = "development";Sentry.init({
dsn: "https://[email protected]/0",
beforeSend(event) {
event.environment = dynamicEnvironment;
return event;
},
});
For changes that apply only while a scope is active, use scope.addEventProcessor inside Sentry.withScope. See Event Processors.
