-
Notifications
You must be signed in to change notification settings - Fork 138
Added error correlation headers. #491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
danielmorell
wants to merge
8
commits into
master
Choose a base branch
from
added/cross-project-error-correlation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0da67fc
Added error correlation headers.
danielmorell b544d5a
Removed call to removed Django HttpRequest method.
danielmorell c535a01
Fixed Django HttpRequest baggage test.
danielmorell ba197da
Moved session baggage header processing to ASGI middleware.
danielmorell 5b7bc09
Exported header parse method, and make sure not to overwrite existing…
danielmorell 1c8d88e
Fixed comment location.
danielmorell 063a24a
Generate execution.scope.id if missing not session.id.
danielmorell 7686970
Explicitly set session data in Starlette middleware.
danielmorell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| from typing import TypedDict | ||
|
|
||
|
|
||
| class Attribute(TypedDict): | ||
| """ | ||
| Represents the `data.attributes` field in the payload, which is used to store session, execution scope information, | ||
| and other key-value pairs. | ||
| """ | ||
| key: str | ||
| value: str |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import random | ||
| import threading | ||
| from contextvars import ContextVar | ||
|
|
||
| from rollbar.lib.payload import Attribute | ||
|
|
||
| _context_session: ContextVar[list[Attribute]|None] = ContextVar('rollbar-session', default=None) | ||
| _thread_session: threading.local = threading.local() | ||
|
|
||
|
|
||
| def set_current_session(headers: dict[str, str]) -> None: | ||
| """ | ||
| Set current session data. | ||
|
|
||
| The session data should be a dictionary with string keys and string values. | ||
| """ | ||
| session_data = parse_session_request_baggage_headers(headers) | ||
| _context_session.set(session_data) | ||
| _thread_session.data = session_data | ||
|
|
||
|
|
||
| def get_current_session() -> list[Attribute]: | ||
| """ | ||
| Return current session data. | ||
|
|
||
| Do NOT modify the returned session data. | ||
| """ | ||
| session_data = _context_session.get() | ||
| if session_data is not None: | ||
| return session_data | ||
|
|
||
| # Fallback to thread local storage for non-async contexts. | ||
| return getattr(_thread_session, 'data', None) or [] | ||
|
|
||
|
|
||
| def reset_current_session() -> None: | ||
| """ | ||
| Reset current session data. | ||
| """ | ||
| _context_session.set(None) | ||
| _thread_session.data = None | ||
|
|
||
|
|
||
| def parse_session_request_baggage_headers(headers: dict) -> list[Attribute]: | ||
| """ | ||
| Parse the 'baggage' header from the request headers to extract session information. If the 'baggage' header is not | ||
| present or does not contain the expected keys, a new execution scope ID will be generated and returned as part of | ||
| the session attributes. | ||
| """ | ||
| if not headers: | ||
| return _build_new_scope_attributes() | ||
|
|
||
| baggage_header = None | ||
|
|
||
| # Make sure to handle case-insensitive header keys. | ||
| for key in headers.keys(): | ||
| if key.lower() == 'baggage': | ||
| baggage_header = headers[key] | ||
| break | ||
|
|
||
| if not baggage_header: | ||
| return _build_new_scope_attributes() | ||
|
|
||
| baggage_items = baggage_header.split(',') | ||
| baggage_data = [] | ||
| has_scope_id = False | ||
| for item in baggage_items: | ||
| if '=' not in item: | ||
| continue | ||
| key, value = item.split('=', 1) | ||
| key = key.strip() | ||
| if key == 'rollbar.session.id': | ||
| baggage_data.append({'key': 'session_id', 'value': value.strip()}) | ||
| if key == 'rollbar.execution.scope.id': | ||
| has_scope_id = True | ||
| baggage_data.append({'key': 'execution_scope_id', 'value': value.strip()}) | ||
|
|
||
| if not baggage_data: | ||
| return _build_new_scope_attributes() | ||
|
|
||
| # Always ensure we have an execution scope ID, even if the baggage header is present but doesn't contain it. | ||
| if not has_scope_id: | ||
| baggage_data.extend(_build_new_scope_attributes()) | ||
|
|
||
| return baggage_data | ||
|
|
||
|
|
||
| def _build_new_scope_attributes() -> list[Attribute]: | ||
| """ | ||
| Generates a new value for the `rollbar.execution.scope.id` attribute. | ||
| """ | ||
| new_id = _new_scope_id() | ||
| if new_id is None: | ||
| return [] | ||
| return [{'key': 'execution_scope_id', 'value': new_id}] | ||
|
|
||
|
|
||
| def _new_scope_id() -> str | None: | ||
| """ | ||
| Generate a new random ID with 128 bits of randomness, formatted as a 32-character hexadecimal string. To be used as | ||
| an execution scope ID. | ||
| """ | ||
| try: | ||
| # Generate a random integer with exactly 128 random bits | ||
| num = random.getrandbits(128) | ||
| except Exception as e: | ||
| return None | ||
| return format(num, "032x") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm also seeing the same problem as with flask, where if you don't pass baggage headers, the import os
import rollbar
from fastapi import FastAPI
from rollbar.contrib.fastapi import add_to
rollbar.init(
access_token=os.getenv("ROLLBAR_TOKEN", "POST_SERVER_ITEM_ACCESS_TOKEN"),
environment=os.getenv("ROLLBAR_ENV", "development"),
)
app = FastAPI(title="rollbar-fastapi-test")
add_to(app)
@app.get("/boom_nested")
async def boom_nested():
from nested_error import raise_error
rollbar.report_message("Error") # has a different request.execution.scope.id
raise_error() # raises exc that has a different request.execution.scope.id |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One last comment here - we should move this out of the core session code, and into the middleware. The reason for this is that we want a single
rollbar.execution.scope.idfor the lifespan of a request, and in the future, forwarded to other outgoing requests.If the originating request does'nt have the
rollbar.execution.scope.id, then this will be a unique value for every rollbar message sent as the request is processed, which doesn't give us much of a signal.If this scope id was built once, in the middleware when a request is received and saved to thread / async storage, then we will get that signal.