|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from contextlib import asynccontextmanager, contextmanager |
| 4 | +from numbers import Number |
| 5 | +from typing import Any, AsyncGenerator, Generator, Literal, Sequence, Union |
| 6 | + |
| 7 | +from aws_lambda_powertools.shared import constants |
| 8 | +from aws_lambda_powertools.shared.lazy_import import LazyLoader |
| 9 | +from aws_lambda_powertools.tracing.provider.base import BaseProvider, BaseSpan |
| 10 | + |
| 11 | +aws_xray_sdk = LazyLoader(constants.XRAY_SDK_MODULE, globals(), constants.XRAY_SDK_MODULE) |
| 12 | + |
| 13 | + |
| 14 | +class XraySpan(BaseSpan): |
| 15 | + def __init__(self, subsegment): |
| 16 | + self.subsegment = subsegment |
| 17 | + self.add_subsegment = self.subsegment.add_subsegment |
| 18 | + self.remove_subsegment = self.subsegment.remove_subsegment |
| 19 | + self.put_annotation = self.subsegment.put_annotation |
| 20 | + self.put_metadata = self.subsegment.put_metadata |
| 21 | + self.add_exception = self.subsegment.add_exception |
| 22 | + self.close = self.subsegment.close |
| 23 | + |
| 24 | + def set_attribute( |
| 25 | + self, |
| 26 | + key: str, |
| 27 | + value: Any, |
| 28 | + category: Literal["Annotation", "Metadata", "Auto"] = "Auto", |
| 29 | + **kwargs, |
| 30 | + ) -> None: |
| 31 | + """ |
| 32 | + Set an attribute on this span with a key-value pair. |
| 33 | +
|
| 34 | + Parameters |
| 35 | + ---------- |
| 36 | + key: str |
| 37 | + attribute key |
| 38 | + value: Any |
| 39 | + Value for attribute |
| 40 | + category: Literal["Annotation","Metadata","Auto"] = "Auto" |
| 41 | + This parameter specifies the category of attribute to set. |
| 42 | + - **"Annotation"**: Sets the attribute as an Annotation. |
| 43 | + - **"Metadata"**: Sets the attribute as Metadata. |
| 44 | + - **"Auto" (default)**: Automatically determines the attribute |
| 45 | + type based on its value. |
| 46 | +
|
| 47 | + kwargs: Optional[dict] |
| 48 | + Optional parameters to be passed to provider.set_attributes |
| 49 | + """ |
| 50 | + if category == "Annotation": |
| 51 | + self.put_annotation(key=key, value=value) |
| 52 | + return |
| 53 | + |
| 54 | + if category == "Metadata": |
| 55 | + self.put_metadata(key=key, value=value, namespace=kwargs.get("namespace", "dafault")) |
| 56 | + return |
| 57 | + |
| 58 | + # Auto |
| 59 | + if isinstance(value, (str, Number, bool)): |
| 60 | + self.put_annotation(key=key, value=value) |
| 61 | + return |
| 62 | + |
| 63 | + # Auto & not in (str, Number, bool) |
| 64 | + self.put_metadata(key=key, value=value, namespace=kwargs.get("namespace", "dafault")) |
| 65 | + |
| 66 | + def record_exception(self, exception: BaseException, **kwargs): |
| 67 | + stack = aws_xray_sdk.core.utils.stacktrace.get_stacktrace() |
| 68 | + self.add_exception(exception=exception, stack=stack) |
| 69 | + |
| 70 | + |
| 71 | +class AwsXrayProvider(BaseProvider): |
| 72 | + def __init__(self, xray_recorder=None): |
| 73 | + if not xray_recorder: |
| 74 | + from aws_xray_sdk.core import xray_recorder |
| 75 | + |
| 76 | + self.recorder = xray_recorder |
| 77 | + self.in_subsegment = self.recorder.in_subsegment |
| 78 | + self.in_subsegment_async = self.recorder.in_subsegment_async |
| 79 | + |
| 80 | + @contextmanager |
| 81 | + def trace(self, name: str, **kwargs) -> Generator[XraySpan, None, None]: |
| 82 | + with self.in_subsegment(name=name, **kwargs) as sub_segment: |
| 83 | + yield XraySpan(subsegment=sub_segment) |
| 84 | + |
| 85 | + @asynccontextmanager |
| 86 | + async def trace_async(self, name: str, **kwargs) -> AsyncGenerator[XraySpan, None]: |
| 87 | + async with self.in_subsegment_async(name=name, **kwargs) as subsegment: |
| 88 | + yield XraySpan(subsegment=subsegment) |
| 89 | + |
| 90 | + def set_attribute( |
| 91 | + self, |
| 92 | + key: str, |
| 93 | + value: Any, |
| 94 | + category: Literal["Annotation", "Metadata", "Auto"] = "Auto", |
| 95 | + **kwargs, |
| 96 | + ) -> None: |
| 97 | + """ |
| 98 | + Set an attribute on the current active span with a key-value pair. |
| 99 | +
|
| 100 | + Parameters |
| 101 | + ---------- |
| 102 | + key: str |
| 103 | + attribute key |
| 104 | + value: Any |
| 105 | + Value for attribute |
| 106 | + category: Literal["Annotation","Metadata","Auto"] = "Auto" |
| 107 | + This parameter specifies the type of attribute to set. |
| 108 | + - **"Annotation"**: Sets the attribute as an Annotation. |
| 109 | + - **"Metadata"**: Sets the attribute as Metadata. |
| 110 | + - **"Auto" (default)**: Automatically determines the attribute |
| 111 | + type based on its value. |
| 112 | +
|
| 113 | + kwargs: Optional[dict] |
| 114 | + Optional parameters to be passed to provider.set_attributes |
| 115 | + """ |
| 116 | + if category == "Annotation": |
| 117 | + self.put_annotation(key=key, value=value) |
| 118 | + return |
| 119 | + |
| 120 | + if category == "Metadata": |
| 121 | + self.put_metadata(key=key, value=value, namespace=kwargs.get("namespace", "dafault")) |
| 122 | + return |
| 123 | + |
| 124 | + # Auto |
| 125 | + if isinstance(value, (str, Number, bool)): |
| 126 | + self.put_annotation(key=key, value=value) |
| 127 | + return |
| 128 | + |
| 129 | + # Auto & not in (str, Number, bool) |
| 130 | + self.put_metadata(key=key, value=value, namespace=kwargs.get("namespace", "dafault")) |
| 131 | + |
| 132 | + def put_annotation(self, key: str, value: Union[str, Number, bool]) -> None: |
| 133 | + return self.recorder.put_annotation(key=key, value=value) |
| 134 | + |
| 135 | + def put_metadata(self, key: str, value: Any, namespace: str = "default") -> None: |
| 136 | + return self.recorder.put_metadata(key=key, value=value, namespace=namespace) |
| 137 | + |
| 138 | + def patch(self, modules: Sequence[str]) -> None: |
| 139 | + return aws_xray_sdk.core.patch(modules) |
| 140 | + |
| 141 | + def patch_all(self) -> None: |
| 142 | + return aws_xray_sdk.core.patch_all() |
0 commit comments