Skip to content

Added session forwarding via baggage headers to httpx and requests - #498

Merged
danielmorell merged 2 commits into
masterfrom
added/session-id-forwarding
Aug 27, 2026
Merged

Added session forwarding via baggage headers to httpx and requests#498
danielmorell merged 2 commits into
masterfrom
added/session-id-forwarding

Conversation

@danielmorell

@danielmorell danielmorell commented Aug 26, 2026

Copy link
Copy Markdown
Collaborator

Description of the change

This PR adds the ability to instrument outgoing request made via HTTPX and requests with the Rollbar session IDs and execution scope IDs in the baggage header.

Propagation of the session and execution scope is controlled with two parameters enabled_urls and enabled_headers. By default enabled_headers is ["baggage"].

Only requests to URLs included in the enabled_urls will include the propagated session and execution scope IDs.

If there is an existing baggage header in the request is will be updated to include the new values. All other headers are left untouched. If for some reason the request baggage header already includes Rollbar session and execution scope IDs they will be updated.

If both global and client/session instrumentation is enabled, the client/session instrumentation takes precedence over the global.

There are three ways to manage instrumentation:

1. Global instrumentation with rollbar.init().

import rollbar

rollbar.init(
    '<access_token>',
    environment='staging',
    tracing={
        "propagation": {
            "enabled_urls": [
                "http://example.com/",  # Can be an exact URL
                r"^http:\/\/example.com\/.*",  # Can be a RegEx
            ]
        }
    }
)

2. Global instrumentation with the propagation manager

In HTTPX...

from rollbar.contrib.httpx import HTTPXContextPropagationManager

HTTPXContextPropagationManager.instrument(
    enabled_urls=[r"^http:\/\/example.com\/.*"],
)

# To remove instrumentation
HTTPXContextPropagationManager.uninstrument()

In requests...

from rollbar.contrib.requests import RequestsContextPropagationManager

RequestsContextPropagationManager.instrument(
    enabled_urls=[r"^http:\/\/example.com\/.*"],
)

# To remove instrumentation
RequestsContextPropagationManager.uninstrument()

3. Individual Client/Session instrumentation with the propagation manager

In HTTPX...

import httpx
from rollbar.contrib.httpx import HTTPXContextPropagationManager

client = httpx.AsyncClient(base_url="https://example.com/")

HTTPXContextPropagationManager.instrument_client(
    client,
    enabled_urls=[r"^http:\/\/example.com\/.*"],
)

# To remove instrumentation
HTTPXContextPropagationManager.uninstrument_client(client)

In requests...

import requests
from rollbar.contrib.requests import RequestsContextPropagationManager

session = requests.Session()

RequestsContextPropagationManager.instrument_session(
    session,
    enabled_urls=[r"^http:\/\/example.com\/.*"],
)

# To remove instrumentation
RequestsContextPropagationManager.uninstrument_session(session)

Type of change

  • New feature (non-breaking change that adds functionality)

Related issues

  • SDK-578

Checklists

Development

  • Lint rules pass locally
  • The code changed/added as part of this pull request has been covered with tests
  • All tests related to the changed code pass in development

Code review

  • This pull request has a descriptive title and information useful to a reviewer. There may be a screenshot or screencast attached
  • "Ready for review" label attached to the PR and reviewers assigned
  • Issue from task tracker has a link to this pull request
  • Changes have been reviewed by at least one other engineer

@danielmorell

Copy link
Copy Markdown
Collaborator Author

@codex please review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 9fd67562fe

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@danielmorell
danielmorell requested a review from brianr August 26, 2026 12:42
@danielmorell danielmorell added this to the v1.5.0 milestone Aug 26, 2026
@waltjones
waltjones self-requested a review August 26, 2026 17:17
Comment thread rollbar/contrib/httpx/__init__.py Outdated
# A client's explicit configuration overrides the global one.
should_inject = should_inject and client not in cls._clients
if should_inject:
cls._inject_propagation_headers(state, args, kwargs)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one of a few places where it would be good to have a try/except to prevent a possible uncaught exception in the host application's execution.

Comment thread rollbar/contrib/httpx/__init__.py Outdated
# A client's explicit configuration overrides the global one.
should_inject = should_inject and client not in cls._clients
if should_inject:
cls._inject_propagation_headers(state, args, kwargs)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try/except also makes sense here.

Comment thread rollbar/contrib/requests/__init__.py Outdated
should_inject = should_inject and session not in cls._sessions

if should_inject:
cls._inject_propagation_headers(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try/except also makes sense here.

@waltjones waltjones left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

@danielmorell

Copy link
Copy Markdown
Collaborator Author

Thanks for the review and feedback on the try/except @waltjones. I have added those changes and also test coverage for them.

@danielmorell
danielmorell merged commit 35a9d89 into master Aug 27, 2026
50 checks passed
@danielmorell
danielmorell deleted the added/session-id-forwarding branch August 27, 2026 12:10
@waltjones

Copy link
Copy Markdown
Contributor

@danielmorell thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants