Skip to content

Commit 35a9d89

Browse files
authored
Merge pull request #498 from rollbar/added/session-id-forwarding
Added session forwarding via baggage headers to httpx and requests
2 parents 0683f40 + 83b861d commit 35a9d89

17 files changed

Lines changed: 2015 additions & 1 deletion

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ classifiers = [
4040
]
4141
requires-python = ">=3.10"
4242
dependencies = [
43-
"requests>=0.12.1",
43+
"requests>=2.0.0",
4444
"typing_extensions; python_version < \"3.11\""
4545
]
4646

rollbar/__init__.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from rollbar.lib.session import get_current_session, set_current_session, parse_session_request_baggage_headers
3535

3636
if 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():
281282
IgnorableLevel = 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+
284300
class 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+
611670
def lambda_function(f):
612671
"""
613672
Decorator for making error handling on AWS Lambda easier

0 commit comments

Comments
 (0)