-
Notifications
You must be signed in to change notification settings - Fork 30
Implement v1 show_history status compatibility #245
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
6 commits
Select commit
Hold shift + click to select a range
fc3b048
Add v1 status history compatibility
andystaples 879f3b5
Use v1 trace field casing
andystaples 7c2ef97
Preserve v1 failed history fields
andystaples 387cd1e
Merge main into show-history compatibility
andystaples e882381
Document gRPC history compatibility boundary
andystaples 73d2a27
Merge branch 'main' into andystaples-fix-v1-show-history
andystaples 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
189 changes: 189 additions & 0 deletions
189
azure-functions-durable/azure/durable_functions/internal/compat/history_projection.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,189 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| import json | ||
| from dataclasses import fields, is_dataclass | ||
| from datetime import datetime, timezone | ||
| from typing import Any, cast | ||
|
|
||
| from durabletask import history, task | ||
| from durabletask.client import OrchestrationStatus | ||
|
|
||
| from .orchestration_runtime_status import from_durabletask_status | ||
|
|
||
|
|
||
| def project_history( | ||
| events: list[history.HistoryEvent], | ||
| *, | ||
| show_input: bool, | ||
| show_history_output: bool) -> list[dict[str, Any]]: | ||
| """Project Durable Task history events into the v1 status-query shape.""" | ||
| projected: list[dict[str, Any]] = [] | ||
| scheduled_tasks: dict[int, tuple[int, history.TaskScheduledEvent]] = {} | ||
| scheduled_sub_orchestrations: dict[ | ||
| int, tuple[int, history.SubOrchestrationInstanceCreatedEvent]] = {} | ||
| removed_indexes: set[int] = set() | ||
|
|
||
| for event in events: | ||
| if isinstance( | ||
| event, | ||
| (history.OrchestratorStartedEvent, | ||
| history.OrchestratorCompletedEvent)): | ||
| continue | ||
|
|
||
| item = _project_event( | ||
| event, | ||
| show_input=show_input, | ||
| show_history_output=show_history_output) | ||
| projected_index = len(projected) | ||
|
|
||
| if isinstance(event, history.TaskScheduledEvent): | ||
| scheduled_tasks[event.event_id] = (projected_index, event) | ||
| elif isinstance( | ||
| event, | ||
| (history.TaskCompletedEvent, history.TaskFailedEvent)): | ||
| _add_scheduled_event_data( | ||
| item, | ||
| event.task_scheduled_id, | ||
| scheduled_tasks, | ||
| removed_indexes, | ||
| show_input) | ||
| elif isinstance(event, history.SubOrchestrationInstanceCreatedEvent): | ||
| scheduled_sub_orchestrations[event.event_id] = ( | ||
| projected_index, event) | ||
| elif isinstance( | ||
| event, | ||
| (history.SubOrchestrationInstanceCompletedEvent, | ||
| history.SubOrchestrationInstanceFailedEvent)): | ||
| _add_scheduled_event_data( | ||
| item, | ||
| event.task_scheduled_id, | ||
| scheduled_sub_orchestrations, | ||
| removed_indexes, | ||
| show_input) | ||
|
|
||
| projected.append(item) | ||
|
|
||
| return [ | ||
| event for index, event in enumerate(projected) | ||
| if index not in removed_indexes | ||
| ] | ||
|
|
||
|
|
||
| def _project_event( | ||
| event: history.HistoryEvent, | ||
| *, | ||
| show_input: bool, | ||
| show_history_output: bool) -> dict[str, Any]: | ||
| item = { | ||
| "EventType": type(event).__name__.removesuffix("Event"), | ||
| "Timestamp": _format_datetime(event.timestamp), | ||
| } | ||
| for field in fields(event): | ||
| if field.name in {"event_id", "timestamp"}: | ||
| continue | ||
| if field.name == "input" and not show_input: | ||
| continue | ||
| if field.name in {"result", "output"} and not show_history_output: | ||
| continue | ||
|
|
||
| value = getattr(event, field.name) | ||
| if value is None: | ||
| continue | ||
| if field.name in {"result", "output"}: | ||
| value = _raw_payload(value) | ||
| item[_field_name(field.name)] = _serialize(value) | ||
|
|
||
| if isinstance( | ||
| event, | ||
| (history.TaskScheduledEvent, | ||
| history.SubOrchestrationInstanceCreatedEvent)): | ||
| item.pop("Version", None) | ||
| elif isinstance(event, history.ExecutionStartedEvent): | ||
| item["FunctionName"] = item.pop("Name") | ||
| for field_name in ( | ||
| "OrchestrationInstance", | ||
| "ParentInstance", | ||
| "Version", | ||
| "Tags"): | ||
| item.pop(field_name, None) | ||
| elif isinstance( | ||
| event, | ||
| (history.TaskCompletedEvent, | ||
| history.TaskFailedEvent, | ||
| history.SubOrchestrationInstanceCompletedEvent, | ||
| history.SubOrchestrationInstanceFailedEvent)): | ||
| item.pop("TaskScheduledId", None) | ||
| elif isinstance(event, history.ExecutionCompletedEvent): | ||
| try: | ||
| status = OrchestrationStatus(event.orchestration_status) | ||
| except ValueError: | ||
| pass | ||
| else: | ||
| item["OrchestrationStatus"] = from_durabletask_status(status).name | ||
| elif isinstance(event, history.TimerFiredEvent): | ||
| item.pop("TimerId", None) | ||
|
|
||
| return item | ||
|
|
||
|
|
||
| def _add_scheduled_event_data( | ||
| item: dict[str, Any], | ||
| scheduled_id: int, | ||
| scheduled_events: dict[int, tuple[int, Any]], | ||
| removed_indexes: set[int], | ||
| show_input: bool) -> None: | ||
| scheduled = scheduled_events.get(scheduled_id) | ||
| if scheduled is None: | ||
| return | ||
|
|
||
| scheduled_index, scheduled_event = scheduled | ||
| item["ScheduledTime"] = _format_datetime(scheduled_event.timestamp) | ||
| item["FunctionName"] = scheduled_event.name | ||
| if show_input and scheduled_event.input is not None: | ||
| item["Input"] = scheduled_event.input | ||
| removed_indexes.add(scheduled_index) | ||
|
|
||
|
|
||
| def _serialize(value: Any) -> Any: | ||
| if isinstance(value, datetime): | ||
| return _format_datetime(value) | ||
| if isinstance(value, task.FailureDetails): | ||
| result = { | ||
| "ErrorMessage": value.message, | ||
| "ErrorType": value.error_type, | ||
| } | ||
| if value.stack_trace is not None: | ||
| result["StackTrace"] = value.stack_trace | ||
| return result | ||
| if is_dataclass(value) and not isinstance(value, type): | ||
| return { | ||
| _field_name(field.name): _serialize(getattr(value, field.name)) | ||
| for field in fields(value) | ||
| if getattr(value, field.name) is not None | ||
| } | ||
| if isinstance(value, list): | ||
| return [_serialize(item) for item in cast(list[Any], value)] | ||
| if isinstance(value, dict): | ||
| mapping = cast(dict[Any, Any], value) | ||
| return {key: _serialize(item) for key, item in mapping.items()} | ||
| return value | ||
|
|
||
|
|
||
| def _field_name(name: str) -> str: | ||
| if name == "scheduled_start_timestamp": | ||
| return "ScheduledStartTime" | ||
| return "".join(part.title() for part in name.split("_")) | ||
|
andystaples marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def _raw_payload(serialized: str) -> Any: | ||
| try: | ||
| return json.loads(serialized) | ||
| except (TypeError, ValueError): | ||
| return serialized | ||
|
|
||
|
|
||
| def _format_datetime(value: datetime) -> str: | ||
| if value.tzinfo is not None: | ||
| value = value.astimezone(timezone.utc).replace(tzinfo=None) | ||
| return value.strftime("%Y-%m-%dT%H:%M:%S.%f") + "Z" | ||
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.