Skip to content

refactor(tracing): add from __future__ import annotations #4943

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 3 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions aws_lambda_powertools/tracing/base.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
from __future__ import annotations

import abc
import numbers
import traceback
from contextlib import contextmanager
from typing import Any, Generator, List, Optional, Sequence, Union
from typing import TYPE_CHECKING, Any, Generator, Sequence

if TYPE_CHECKING:
import numbers
import traceback


class BaseSegment(abc.ABC):
"""Holds common properties and methods on segment and subsegment."""

@abc.abstractmethod
def close(self, end_time: Optional[int] = None):
def close(self, end_time: int | None = None):
"""Close the trace entity by setting `end_time`
and flip the in progress flag to False.

Expand All @@ -28,7 +32,7 @@ def remove_subsegment(self, subsegment: Any):
"""Remove input subsegment from child subsegments."""

@abc.abstractmethod
def put_annotation(self, key: str, value: Union[str, numbers.Number, bool]) -> None:
def put_annotation(self, key: str, value: str | numbers.Number | bool) -> None:
"""Annotate segment or subsegment with a key-value pair.

Note: Annotations will be indexed for later search query.
Expand All @@ -37,7 +41,7 @@ def put_annotation(self, key: str, value: Union[str, numbers.Number, bool]) -> N
----------
key: str
Metadata key
value: Union[str, numbers.Number, bool]
value: str | numbers.Number | bool
Annotation value
"""

Expand All @@ -52,19 +56,19 @@ def put_metadata(self, key: str, value: Any, namespace: str = "default") -> None
Metadata key
value: Any
Any object that can be serialized into a JSON string
namespace: Set[str]
namespace: set[str]
Metadata namespace, by default 'default'
"""

@abc.abstractmethod
def add_exception(self, exception: BaseException, stack: List[traceback.StackSummary], remote: bool = False):
def add_exception(self, exception: BaseException, stack: list[traceback.StackSummary], remote: bool = False):
"""Add an exception to trace entities.

Parameters
----------
exception: Exception
Caught exception
stack: List[traceback.StackSummary]
stack: list[traceback.StackSummary]
List of traceback summaries

Output from `traceback.extract_stack()`.
Expand All @@ -83,7 +87,7 @@ def in_subsegment(self, name=None, **kwargs) -> Generator[BaseSegment, None, Non
----------
name: str
Subsegment name
kwargs: Optional[dict]
kwargs: dict | None
Optional parameters to be propagated to segment
"""

Expand All @@ -96,12 +100,12 @@ def in_subsegment_async(self, name=None, **kwargs) -> Generator[BaseSegment, Non
----------
name: str
Subsegment name
kwargs: Optional[dict]
kwargs: dict | None
Optional parameters to be propagated to segment
"""

@abc.abstractmethod
def put_annotation(self, key: str, value: Union[str, numbers.Number, bool]) -> None:
def put_annotation(self, key: str, value: str | numbers.Number | bool) -> None:
"""Annotate current active trace entity with a key-value pair.

Note: Annotations will be indexed for later search query.
Expand All @@ -110,7 +114,7 @@ def put_annotation(self, key: str, value: Union[str, numbers.Number, bool]) -> N
----------
key: str
Metadata key
value: Union[str, numbers.Number, bool]
value: str | numbers.Number | bool
Annotation value
"""

Expand All @@ -126,7 +130,7 @@ def put_metadata(self, key: str, value: Any, namespace: str = "default") -> None
Metadata key
value: Any
Any object that can be serialized into a JSON string
namespace: Set[str]
namespace: set[str]
Metadata namespace, by default 'default'
"""

Expand All @@ -136,7 +140,7 @@ def patch(self, modules: Sequence[str]) -> None:

Parameters
----------
modules: Set[str]
modules: set[str]
Set of modules to be patched
"""

Expand Down
Loading
Loading