|
| 1 | +import abc |
| 2 | +import numbers |
| 3 | +import traceback |
| 4 | +from contextlib import contextmanager |
| 5 | +from typing import Any, Generator, List, Optional, Sequence, Union |
| 6 | + |
| 7 | + |
| 8 | +class BaseSegment(abc.ABC): |
| 9 | + """Holds common properties and methods on segment and subsegment.""" |
| 10 | + |
| 11 | + @abc.abstractmethod |
| 12 | + def close(self, end_time: Optional[int] = None): |
| 13 | + """Close the trace entity by setting `end_time` |
| 14 | + and flip the in progress flag to False. |
| 15 | +
|
| 16 | + Parameters |
| 17 | + ---------- |
| 18 | + end_time: int |
| 19 | + Time in epoch seconds, by default current time will be used. |
| 20 | + """ |
| 21 | + |
| 22 | + @abc.abstractmethod |
| 23 | + def add_subsegment(self, subsegment: Any): |
| 24 | + """Add input subsegment as a child subsegment.""" |
| 25 | + |
| 26 | + @abc.abstractmethod |
| 27 | + def remove_subsegment(self, subsegment: Any): |
| 28 | + """Remove input subsegment from child subsegments.""" |
| 29 | + |
| 30 | + @abc.abstractmethod |
| 31 | + def put_annotation(self, key: str, value: Union[str, numbers.Number, bool]) -> None: |
| 32 | + """Annotate segment or subsegment with a key-value pair. |
| 33 | +
|
| 34 | + Note: Annotations will be indexed for later search query. |
| 35 | +
|
| 36 | + Parameters |
| 37 | + ---------- |
| 38 | + key: str |
| 39 | + Metadata key |
| 40 | + value: Union[str, numbers.Number, bool] |
| 41 | + Annotation value |
| 42 | + """ |
| 43 | + |
| 44 | + @abc.abstractmethod |
| 45 | + def put_metadata(self, key: str, value: Any, namespace: str = "default") -> None: |
| 46 | + """Add metadata to segment or subsegment. Metadata is not indexed |
| 47 | + but can be later retrieved by BatchGetTraces API. |
| 48 | +
|
| 49 | + Parameters |
| 50 | + ---------- |
| 51 | + key: str |
| 52 | + Metadata key |
| 53 | + value: Any |
| 54 | + Any object that can be serialized into a JSON string |
| 55 | + namespace: Set[str] |
| 56 | + Metadata namespace, by default 'default' |
| 57 | + """ |
| 58 | + |
| 59 | + @abc.abstractmethod |
| 60 | + def add_exception(self, exception: BaseException, stack: List[traceback.StackSummary], remote: bool = False): |
| 61 | + """Add an exception to trace entities. |
| 62 | +
|
| 63 | + Parameters |
| 64 | + ---------- |
| 65 | + exception: Exception |
| 66 | + Caught exception |
| 67 | + stack: List[traceback.StackSummary] |
| 68 | + List of traceback summaries |
| 69 | +
|
| 70 | + Output from `traceback.extract_stack()`. |
| 71 | + remote: bool |
| 72 | + Whether it's a client error (False) or downstream service error (True), by default False |
| 73 | + """ |
| 74 | + |
| 75 | + |
| 76 | +class BaseProvider(abc.ABC): |
| 77 | + @abc.abstractmethod |
| 78 | + @contextmanager |
| 79 | + def in_subsegment(self, name=None, **kwargs) -> Generator[BaseSegment, None, None]: |
| 80 | + """Return a subsegment context manger. |
| 81 | +
|
| 82 | + Parameters |
| 83 | + ---------- |
| 84 | + name: str |
| 85 | + Subsegment name |
| 86 | + kwargs: Optional[dict] |
| 87 | + Optional parameters to be propagated to segment |
| 88 | + """ |
| 89 | + |
| 90 | + @abc.abstractmethod |
| 91 | + @contextmanager |
| 92 | + def in_subsegment_async(self, name=None, **kwargs) -> Generator[BaseSegment, None, None]: |
| 93 | + """Return a subsegment async context manger. |
| 94 | +
|
| 95 | + Parameters |
| 96 | + ---------- |
| 97 | + name: str |
| 98 | + Subsegment name |
| 99 | + kwargs: Optional[dict] |
| 100 | + Optional parameters to be propagated to segment |
| 101 | + """ |
| 102 | + |
| 103 | + @abc.abstractmethod |
| 104 | + def put_annotation(self, key: str, value: Union[str, numbers.Number, bool]) -> None: |
| 105 | + """Annotate current active trace entity with a key-value pair. |
| 106 | +
|
| 107 | + Note: Annotations will be indexed for later search query. |
| 108 | +
|
| 109 | + Parameters |
| 110 | + ---------- |
| 111 | + key: str |
| 112 | + Metadata key |
| 113 | + value: Union[str, numbers.Number, bool] |
| 114 | + Annotation value |
| 115 | + """ |
| 116 | + |
| 117 | + @abc.abstractmethod |
| 118 | + def put_metadata(self, key: str, value: Any, namespace: str = "default") -> None: |
| 119 | + """Add metadata to the current active trace entity. |
| 120 | +
|
| 121 | + Note: Metadata is not indexed but can be later retrieved by BatchGetTraces API. |
| 122 | +
|
| 123 | + Parameters |
| 124 | + ---------- |
| 125 | + key: str |
| 126 | + Metadata key |
| 127 | + value: Any |
| 128 | + Any object that can be serialized into a JSON string |
| 129 | + namespace: Set[str] |
| 130 | + Metadata namespace, by default 'default' |
| 131 | + """ |
| 132 | + |
| 133 | + @abc.abstractmethod |
| 134 | + def patch(self, modules: Sequence[str]) -> None: |
| 135 | + """Instrument a set of supported libraries |
| 136 | +
|
| 137 | + Parameters |
| 138 | + ---------- |
| 139 | + modules: Set[str] |
| 140 | + Set of modules to be patched |
| 141 | + """ |
| 142 | + |
| 143 | + @abc.abstractmethod |
| 144 | + def patch_all(self) -> None: |
| 145 | + """Instrument all supported libraries""" |
0 commit comments