-
Notifications
You must be signed in to change notification settings - Fork 28
feat: add ReferenceContext for span reference hierarchy propagation #1708
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
jepadil23
wants to merge
3
commits into
main
Choose a base branch
from
feat/reference-hierarchy-context
base: main
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.
+674
−5
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
254 changes: 254 additions & 0 deletions
254
packages/uipath-platform/src/uipath/platform/common/_reference_context.py
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,254 @@ | ||
| """Immutable reference-hierarchy context for span propagation. | ||
|
|
||
| Follows the same design as service-common BaggageContext: | ||
| - Immutable, copy-on-write — each mutating call returns a NEW instance so | ||
| sibling spans cannot bleed context into each other. | ||
| - ContextVar-backed accessor — flows across await boundaries without | ||
| threading the value through every function signature. | ||
| - Wire format compatible with the ``ref.*`` keys in ``x-uipath-tracebaggage`` | ||
| so context parsed by service-common middleware is understood here and | ||
| vice-versa. | ||
| """ | ||
| from __future__ import annotations | ||
|
|
||
| import contextvars | ||
| import uuid | ||
| from dataclasses import dataclass | ||
| from typing import Iterator, List, Optional, Tuple | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "ReferenceEntry", | ||
| "ReferenceContext", | ||
| "ReferenceContextAccessor", | ||
| "BAGGAGE_HEADER_NAME", | ||
| "BAGGAGE_KEY_TYPE", | ||
| "BAGGAGE_KEY_ID", | ||
| "BAGGAGE_KEY_VERSION", | ||
| ] | ||
|
|
||
| BAGGAGE_HEADER_NAME = "x-uipath-tracebaggage" | ||
|
|
||
| # Key names — matches service-common ReferenceHierarchyKeys | ||
| BAGGAGE_KEY_TYPE = "ref.type" | ||
| BAGGAGE_KEY_ID = "ref.id" | ||
| BAGGAGE_KEY_VERSION = "ref.v" | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class ReferenceEntry: | ||
| """A single node in the reference hierarchy call chain.""" | ||
|
|
||
| service_type: str | ||
| reference_id: str # UUID string | ||
| version: Optional[str] = None | ||
|
|
||
|
|
||
| class ReferenceContext: | ||
| """Immutable, copy-on-write ordered list of reference entries. | ||
|
|
||
| Outermost caller first, current service appended last. | ||
| Each mutating call returns a new instance — the original is never | ||
| modified, preventing sibling spans from sharing context. | ||
|
|
||
| Usage:: | ||
|
|
||
| ctx = ReferenceContext.Empty | ||
| ctx = ctx.add("maestro", process_id, "2.1.0") | ||
| ctx = ctx.add("agent", agent_id) | ||
| token = ReferenceContextAccessor.set(ctx) | ||
| try: | ||
| ... | ||
| finally: | ||
| ReferenceContextAccessor.reset(token) | ||
| """ | ||
|
|
||
| __slots__ = ("_entries",) | ||
|
|
||
|
|
||
| def __init__(self, entries: Tuple[ReferenceEntry, ...] = ()) -> None: | ||
| self._entries: Tuple[ReferenceEntry, ...] = entries | ||
|
|
||
| @property | ||
| def entries(self) -> Tuple[ReferenceEntry, ...]: | ||
| return self._entries | ||
|
|
||
| def __len__(self) -> int: | ||
| return len(self._entries) | ||
|
|
||
| def __iter__(self) -> Iterator[ReferenceEntry]: | ||
| return iter(self._entries) | ||
|
|
||
| def __bool__(self) -> bool: | ||
| return len(self._entries) > 0 | ||
|
|
||
| def __eq__(self, other: object) -> bool: | ||
| if not isinstance(other, ReferenceContext): | ||
| return NotImplemented | ||
| return self._entries == other._entries | ||
|
|
||
| def __hash__(self) -> int: | ||
| return hash(self._entries) | ||
|
|
||
| def add( | ||
| self, | ||
| service_type: str, | ||
| reference_id: str | uuid.UUID, | ||
| version: Optional[str] = None, | ||
| ) -> "ReferenceContext": | ||
| """Returns a new context with this entry appended (copy-on-write). | ||
|
|
||
| Args: | ||
| service_type: Identifier for the calling service (e.g. ``"agent"``, | ||
| ``"maestro"``). | ||
| reference_id: UUID of the referenced entity (UUID object or string). | ||
| version: Optional version string. | ||
|
|
||
| Returns: | ||
| A new :class:`ReferenceContext` with the entry appended. | ||
| """ | ||
| if not service_type or not service_type.strip(): | ||
| raise ValueError("service_type must be a non-empty string.") | ||
| if isinstance(reference_id, uuid.UUID): | ||
| id_str = str(reference_id) | ||
| elif isinstance(reference_id, str): | ||
| id_str = reference_id | ||
| else: | ||
| raise TypeError("reference_id must be a UUID or string.") | ||
|
Comment on lines
+111
to
+116
|
||
| entry = ReferenceEntry( | ||
| service_type=service_type, | ||
| reference_id=id_str, | ||
| version=version if version and version.strip() else None, | ||
| ) | ||
| return ReferenceContext(self._entries + (entry,)) | ||
|
|
||
| def to_wire_list(self) -> List[dict]: | ||
| """Serialize to the ``referenceHierarchy`` wire format. | ||
|
|
||
| Returns: | ||
| A list of dicts suitable for JSON serialization as | ||
| ``Context.referenceHierarchy`` in the span payload. | ||
| """ | ||
| result = [] | ||
| for e in self._entries: | ||
| item: dict = { | ||
| "serviceType": e.service_type, | ||
| "referenceId": e.reference_id, | ||
| } | ||
| if e.version: | ||
| item["version"] = e.version | ||
| result.append(item) | ||
| return result | ||
|
Comment on lines
+124
to
+140
|
||
|
|
||
| @staticmethod | ||
| def from_baggage_header(header_value: Optional[str]) -> "ReferenceContext": | ||
|
Check failure on line 143 in packages/uipath-platform/src/uipath/platform/common/_reference_context.py
|
||
| """Parse ``x-uipath-tracebaggage`` header value into a ReferenceContext. | ||
|
|
||
| Only entries that carry the ``ref.*`` shape (type + valid UUID id) are | ||
| included. Malformed or plain-KV entries are silently skipped so a bad | ||
| header from an upstream service cannot crash this one. | ||
|
|
||
| Args: | ||
| header_value: Raw header string, e.g. | ||
| ``"ref.type=agent;ref.id=<uuid>;ref.v=1.0,ref.type=maestro;ref.id=<uuid>"`` | ||
|
|
||
| Returns: | ||
| Parsed :class:`ReferenceContext`, or :attr:`ReferenceContext.Empty` | ||
| if the header is absent, empty, or contains no valid ref entries. | ||
| """ | ||
| if not header_value or not header_value.strip(): | ||
| return ReferenceContext.Empty | ||
|
|
||
| entries: List[ReferenceEntry] = [] | ||
| for raw_entry in header_value.split(","): | ||
| entry_text = raw_entry.strip() | ||
| if not entry_text: | ||
| continue | ||
| props: dict[str, str] = {} | ||
| for raw_pair in entry_text.split(";"): | ||
| pair_text = raw_pair.strip() | ||
| eq = pair_text.find("=") | ||
| if eq <= 0 or eq >= len(pair_text) - 1: | ||
| continue | ||
| key = pair_text[:eq].strip() | ||
| value = pair_text[eq + 1:].strip() | ||
| if key and value: | ||
| props[key] = value | ||
|
|
||
| type_v = props.get(BAGGAGE_KEY_TYPE) | ||
| id_v = props.get(BAGGAGE_KEY_ID) | ||
| if not type_v or not id_v: | ||
| continue | ||
| try: | ||
| parsed_uuid = uuid.UUID(id_v) | ||
| except (ValueError, AttributeError): | ||
| continue | ||
| entries.append( | ||
| ReferenceEntry( | ||
| service_type=type_v, | ||
| reference_id=str(parsed_uuid), | ||
| version=props.get(BAGGAGE_KEY_VERSION) or None, | ||
| ) | ||
| ) | ||
|
|
||
| if not entries: | ||
| return ReferenceContext.Empty | ||
| return ReferenceContext(tuple(entries)) | ||
|
|
||
| def to_baggage_header_value(self) -> str: | ||
| """Serialize to ``x-uipath-tracebaggage`` header value. | ||
|
|
||
| Returns: | ||
| Comma-separated entries; each is a semicolon-separated list of | ||
| ``key=value`` pairs. Empty context returns ``""``. | ||
| """ | ||
| if not self._entries: | ||
| return "" | ||
| parts: List[str] = [] | ||
| for e in self._entries: | ||
| kv = f"{BAGGAGE_KEY_TYPE}={e.service_type};{BAGGAGE_KEY_ID}={e.reference_id}" | ||
| if e.version: | ||
| kv += f";{BAGGAGE_KEY_VERSION}={e.version}" | ||
| parts.append(kv) | ||
| return ",".join(parts) | ||
|
|
||
|
|
||
| # Assigned after class body so ReferenceContext is fully bound. | ||
| ReferenceContext.Empty = ReferenceContext() # type: ignore[attr-defined] | ||
|
|
||
|
|
||
| class ReferenceContextAccessor: | ||
| """Ambient accessor for the current :class:`ReferenceContext`. | ||
|
|
||
| Backed by :mod:`contextvars` so the value propagates across ``await`` | ||
| boundaries without being threaded through every call signature. | ||
|
Comment on lines
+219
to
+223
|
||
|
|
||
| Usage:: | ||
|
|
||
| token = ReferenceContextAccessor.set(ctx) | ||
| try: | ||
| ... # code here sees ReferenceContextAccessor.get() == ctx | ||
| finally: | ||
| ReferenceContextAccessor.reset(token) | ||
| """ | ||
|
|
||
| _current: contextvars.ContextVar[Optional[ReferenceContext]] = ( | ||
| contextvars.ContextVar("uipath_reference_context", default=None) | ||
| ) | ||
|
|
||
| @classmethod | ||
| def get(cls) -> Optional[ReferenceContext]: | ||
| """Return the current ambient context, or ``None`` if not set.""" | ||
| return cls._current.get() | ||
|
|
||
| @classmethod | ||
| def set(cls, value: Optional[ReferenceContext]) -> contextvars.Token: | ||
|
|
||
| """Set the ambient context. Returns a token for restoration. | ||
|
|
||
| Pass the token to :meth:`reset` in a ``finally`` block. | ||
| """ | ||
| return cls._current.set(value) | ||
|
|
||
| @classmethod | ||
| def reset(cls, token: contextvars.Token) -> None: | ||
|
|
||
| """Restore the ambient context to its prior value.""" | ||
| cls._current.reset(token) | ||
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.
Could we make this a SpanProcessor subclass registered via trace_manager.add_span_processor() (same pattern as LiveTrackingSpanProcessor) instead of the new register_span_start_hook list? Its on_start already fires for every span in the creating thread, so it covers auto-instrumented spans too — and it avoids adding a new public API + the import-time side effect (just register it before LiveTrackingSpanProcessor since that one upserts on start).