-
Notifications
You must be signed in to change notification settings - Fork 15
feat: propagate context through workflow execution #158
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
407f467
feat: propagate context through workflow execution
timl3136 70c24a7
type check fix
timl3136 cc9d6b0
fix some missing propagators
timl3136 0ca5e36
address gitar comment
timl3136 fb2cf98
lint
timl3136 2d4ca3b
Merge branch 'main' into better-context
timl3136 041a5e4
Merge branch 'main' into better-context
timl3136 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| """Internal helpers for converting and propagating Cadence headers.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Iterator, Mapping, Sequence | ||
| from contextlib import ExitStack, contextmanager | ||
| from typing import Protocol | ||
|
|
||
| from cadence.api.v1.common_pb2 import Header, Payload | ||
| from cadence.context import ContextPropagator | ||
|
|
||
|
|
||
| class _HeaderCarrier(Protocol): | ||
| """Proto message that carries a Cadence ``Header`` field.""" | ||
|
|
||
| header: Header | ||
|
|
||
|
|
||
| def header_to_dict(header: Header) -> dict[str, bytes]: | ||
| """Return a detached copy of a proto header's byte payloads.""" | ||
| return {key: bytes(payload.data) for key, payload in header.fields.items()} | ||
|
|
||
|
|
||
| def header_from_dict(headers: Mapping[str, bytes]) -> Header | None: | ||
| """Build a Header, omitting it entirely when no values were injected.""" | ||
| if not headers: | ||
| return None | ||
| return Header(fields={key: Payload(data=value) for key, value in headers.items()}) | ||
|
|
||
|
|
||
| def inject_headers(propagators: Sequence[ContextPropagator]) -> dict[str, bytes]: | ||
| """Inject ordered propagators, with later propagators winning duplicate keys.""" | ||
| headers: dict[str, bytes] = {} | ||
| for propagator in propagators: | ||
| headers.update(propagator.inject()) | ||
| return headers | ||
|
|
||
|
|
||
| def set_header(attrs: _HeaderCarrier, propagators: Sequence[ContextPropagator]) -> None: | ||
| """Attach injected context to ``attrs``, leaving the field unset when empty.""" | ||
| set_header_from_dict(attrs, inject_headers(propagators)) | ||
|
|
||
|
|
||
| def set_header_from_dict(attrs: _HeaderCarrier, headers: Mapping[str, bytes]) -> None: | ||
| """Attach ``headers`` to ``attrs``, leaving the field unset when empty.""" | ||
| header = header_from_dict(headers) | ||
| if header is not None: | ||
| attrs.header.CopyFrom(header) | ||
|
|
||
|
|
||
| def validate_propagators(propagators: Sequence[ContextPropagator]) -> None: | ||
| """Fail fast when a propagator does not implement the protocol.""" | ||
| for index, propagator in enumerate(propagators): | ||
| if not callable(getattr(propagator, "inject", None)): | ||
| raise TypeError( | ||
| f"context_propagators[{index}] is missing a callable inject() method" | ||
| ) | ||
| if not callable(getattr(propagator, "extract", None)): | ||
| raise TypeError( | ||
| f"context_propagators[{index}] is missing a callable extract() method" | ||
| ) | ||
|
|
||
|
|
||
| @contextmanager | ||
| def extract_headers( | ||
| propagators: Sequence[ContextPropagator], headers: Mapping[str, bytes] | ||
| ) -> Iterator[None]: | ||
| """Activate propagators in order and reliably unwind partial extraction.""" | ||
| with ExitStack() as stack: | ||
| for propagator in propagators: | ||
| stack.enter_context(propagator.extract(headers)) | ||
| yield | ||
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.
Uh oh!
There was an error while loading. Please reload this page.