You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am having an issue where it appears the only clean option to initialize Rollbar event handlers (e.g. rollbar.events.add_payload_handler) is within the RollbarNotifierMiddleware as a subclass of __init__. This does work, but it's not very clean.
When I configure it like this, the RollbarNotifierMiddleware sees that there is an access token in the Rollbar settings and then unconditionally calls rollbar.init:
But when this happens, the rollbar.init() calls rollbar.events.reset(). This clears out my previously-added payload handler since it was added prior to Django initializing middleware and thus the RollbarNotifierMiddleware invoking rollbar.init().
I see a few options, none of which I'm particularly thrilled about:
Subclass RollbarNotifierMiddleware, call super.__init__(), and then call rollbar.events.add_payload_handler() (this is the one I went with):
classTracedRollbarNotifierMiddleware(RollbarNotifierMiddleware):
""" The default RollbarNotifierMiddleware unconditionally does a rollbar.init() itself with the given access token """def__init__(self, get_response=None):
super().__init__(get_response)
importrollbarfromdjango.confimportsettingsdeftransform_rollbar_payload(
payload: Dict[str, Any], **kwargs: Dict[str, Any]
) ->Dict[str, Any]:
data=payload["data"]
custom=data.get("custom", {})
custom['service'] =settings.SERVICEcustom['env'] =settings.ENVIRONMENTcustom['version'] =settings.VERSIONspan=trace.get_current_span()
ifspan!=trace.span.INVALID_SPAN:
ctx=span.get_span_context()
ifctx!=trace.span.INVALID_SPAN_CONTEXT:
custom['trace_id'] =ctx.trace_idcustom['span_id'] =ctx.span_iddata["custom"] =customreturnpayloadrollbar.events.add_payload_handler(transform_rollbar_payload)
Move the event handler registration to within a Django AppConfig.ready(), I presume this happens after middleware instantiation
Call rollbar.init() myself, but still set a ROLLBAR = {'access_token': 'not-used'} in settings.py so that the middleware will still be activated (RollbarNotifierMiddleware is skipped unless this is set)
So what is the intended use of event handlers with the RollbarNotifierMiddleware? Where in the lifecycle should I be adding these?
The text was updated successfully, but these errors were encountered:
phillipuniverse
changed the title
Event handlers awkward interaction with the RollbarNotifierMiddleware
Event handlers awkward interaction with the DJango RollbarNotifierMiddlewareOct 25, 2021
phillipuniverse
changed the title
Event handlers awkward interaction with the DJango RollbarNotifierMiddleware
Event handlers awkward interaction with the Django RollbarNotifierMiddlewareOct 25, 2021
I am having an issue where it appears the only clean option to initialize Rollbar event handlers (e.g.
rollbar.events.add_payload_handler
) is within theRollbarNotifierMiddleware
as a subclass of__init__
. This does work, but it's not very clean.Here is my
ROLLBAR
variable insettings.py
:Then at the end of
settings.py
I want to add a payload handler to add trace information from OpenTelemetry:When I configure it like this, the
RollbarNotifierMiddleware
sees that there is an access token in the Rollbar settings and then unconditionally callsrollbar.init
:pyrollbar/rollbar/contrib/django/middleware.py
Line 202 in 205d904
But when this happens, the
rollbar.init()
callsrollbar.events.reset()
. This clears out my previously-added payload handler since it was added prior to Django initializing middleware and thus theRollbarNotifierMiddleware
invokingrollbar.init()
.I see a few options, none of which I'm particularly thrilled about:
RollbarNotifierMiddleware
, callsuper.__init__()
, and then callrollbar.events.add_payload_handler()
(this is the one I went with):AppConfig.ready()
, I presume this happens after middleware instantiationrollbar.init()
myself, but still set aROLLBAR = {'access_token': 'not-used'}
insettings.py
so that the middleware will still be activated (RollbarNotifierMiddleware
is skipped unless this is set)So what is the intended use of event handlers with the
RollbarNotifierMiddleware
? Where in the lifecycle should I be adding these?The text was updated successfully, but these errors were encountered: