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