3434from rollbar .lib .session import get_current_session , set_current_session , parse_session_request_baggage_headers
3535
3636if TYPE_CHECKING :
37+ import re
3738 from rollbar .lib .payload import Attribute
3839 from rollbar .lib .type_info import KeyType
3940
@@ -281,6 +282,21 @@ def _get_fastapi_request():
281282IgnorableLevel = Level | Literal ['ignored' ]
282283
283284
285+ class PropagationSettings (TypedDict , total = False ):
286+ # List of headers to propagate session data on outgoing requests. Default: `['baggage']`. Only headers that are in
287+ # this list will be injected with session data. Header names are case-insensitive.
288+ enabled_headers : list [str ]
289+ # List of URLs to propagate session data on outgoing requests. Default: `[]`. This can be a list of strings or
290+ # compiled regex patterns. If a string is provided, it must match the full URL exactly. If a regex pattern is
291+ # provided, it will be matched against the full URL. Only URLs that match one of the provided patterns will have
292+ # session data propagated.
293+ enabled_urls : list [str | re .Pattern [str ]]
294+
295+
296+ class TracingSettings (TypedDict , total = False ):
297+ propagation : PropagationSettings
298+
299+
284300class NotifierSettings (TypedDict , total = False ):
285301 name : str
286302 version : str
@@ -316,6 +332,7 @@ class SettingsParams(TypedDict, total=False):
316332 verify_https : bool
317333 shortener_keys : list [tuple [str , ...]]
318334 suppress_reinit_warning : bool
335+ tracing : TracingSettings
319336 capture_email : bool
320337 capture_username : bool
321338 capture_ip : bool | Literal ['anonymize' ]
@@ -399,6 +416,12 @@ class Settings(TypedDict, SettingsParams, SettingsIrregular):
399416 'verify_https' : True ,
400417 'shortener_keys' : [],
401418 'suppress_reinit_warning' : False ,
419+ 'tracing' : {
420+ 'propagation' : {
421+ 'enabled_headers' : ['baggage' ],
422+ 'enabled_urls' : [],
423+ },
424+ },
402425 'capture_email' : False ,
403426 'capture_username' : False ,
404427 'capture_ip' : True ,
@@ -502,6 +525,7 @@ def init(
502525 :param suppress_reinit_warning: If `True`, suppresses the warning normally shown when `rollbar.init()` is called
503526 multiple times.
504527 :param timeout: Timeout for any HTTP requests made to the Rollbar API (in seconds).
528+ :param tracing: Configuration for tracing errors across services.
505529 :param verify_https: If `True`, network requests will fail unless encountering a valid certificate. Default `True`.
506530 """
507531 global SETTINGS , agent_log , _initialized , _transforms , _serialize_transform , _scrub_redact_transform , _threads
@@ -590,6 +614,7 @@ def init(
590614 _threads = queue .Queue ()
591615 events .reset ()
592616 filters .add_builtin_filters (SETTINGS )
617+ _init_tracing_propagation ()
593618
594619 _initialized = True
595620
@@ -608,6 +633,40 @@ def _requests_configuration(**kw):
608633 return {keys [k ]: kw .get (k , None ) for k in keys }
609634
610635
636+ def _init_tracing_propagation ():
637+ headers = SETTINGS ['tracing' ]['propagation' ]['enabled_headers' ]
638+ urls = SETTINGS ['tracing' ]['propagation' ]['enabled_urls' ]
639+
640+ if len (urls ) == 0 or not any (header .lower () == 'baggage' for header in headers ):
641+ return
642+ _init_httpx_propagation ()
643+ _init_requests_propagation ()
644+
645+
646+ def _init_httpx_propagation ():
647+ try :
648+ import httpx
649+ except ImportError :
650+ return
651+
652+ from rollbar .contrib .httpx import HTTPXContextPropagationManager
653+ HTTPXContextPropagationManager .instrument (enabled_urls = SETTINGS ['tracing' ]['propagation' ]['enabled_urls' ],
654+ enabled_headers = SETTINGS ['tracing' ]['propagation' ]['enabled_headers' ])
655+
656+
657+ def _init_requests_propagation ():
658+ try :
659+ import requests
660+ from rollbar .contrib .requests import RequestsContextPropagationManager
661+ except ImportError :
662+ return
663+
664+ RequestsContextPropagationManager .instrument (
665+ enabled_urls = SETTINGS ['tracing' ]['propagation' ]['enabled_urls' ],
666+ enabled_headers = SETTINGS ['tracing' ]['propagation' ]['enabled_headers' ],
667+ )
668+
669+
611670def lambda_function (f ):
612671 """
613672 Decorator for making error handling on AWS Lambda easier
0 commit comments