Skip to content

Commit 844d85b

Browse files
committed
address ruben comments
1 parent dede79c commit 844d85b

File tree

5 files changed

+66
-37
lines changed

5 files changed

+66
-37
lines changed
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +0,0 @@
1-
from .datadog.dd_tracer import DDTraceProvider
2-
from .otel.otel_tracer import OtelProvider
3-
from .xray.xray_tracer import XrayProvider
4-
5-
__all__ = ["DDTraceProvider", "OtelProvider", "XrayProvider"]

aws_lambda_powertools/tracing/provider/base.py

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,105 @@
11
import abc
2-
import numbers
32
from contextlib import asynccontextmanager, contextmanager
4-
from typing import AsyncGenerator, Generator, Sequence, Union
3+
from typing import Any, AsyncGenerator, Generator, Sequence
54

65

76
class BaseSpan(abc.ABC):
8-
"""Holds common properties and methods on span."""
7+
"""A span represents a unit of work or operation within a trace.
8+
Spans are the building blocks of Traces."""
99

1010
@abc.abstractmethod
11-
def set_attribute(self, key: str, value: Union[str, numbers.Number, bool], **kwargs) -> None:
12-
"""set attribute for span with a key-value pair.
11+
def set_attribute(self, key: str, value: Any, **kwargs) -> None:
12+
"""Set an attribute for a span with a key-value pair.
1313
1414
Parameters
1515
----------
1616
key: str
1717
Attribute key
18-
value: Union[str, numbers.Number, bool]
18+
value: Any
1919
Attribute value
20+
kwargs: Optional[dict]
21+
Optional parameters
2022
"""
2123

2224
@abc.abstractmethod
2325
def record_exception(self, exception: BaseException, **kwargs):
24-
"""Add an exception to trace entities.
26+
"""Records an exception to this Span.
2527
2628
Parameters
2729
----------
2830
exception: Exception
29-
Caught exception
30-
Output from `traceback.extract_stack()`.
31+
Caught exception during the exectution of this Span
32+
kwargs: Optional[dict]
33+
Optional parameters
3134
"""
3235

3336

3437
class BaseProvider(abc.ABC):
38+
"""BaseProvider is an abstract base class that defines the expected behavior for tracing providers
39+
used by Tracer. Inheriting classes must implement this interface to be compatible with Tracer.
40+
"""
41+
3542
@abc.abstractmethod
3643
@contextmanager
3744
def trace(self, name: str, **kwargs) -> Generator[BaseSpan, None, None]:
38-
"""Return a span context manger.
45+
"""Context manager for creating a new span and set it
46+
as the current span in this tracer's context.
47+
48+
Exiting the context manager will call the span's end method,
49+
as well as return the current span to its previous value by
50+
returning to the previous context.
3951
4052
Parameters
4153
----------
4254
name: str
4355
Span name
4456
kwargs: Optional[dict]
45-
Optional parameters to be propagated to span
57+
Optional parameters to be propagated to the span
4658
"""
4759

4860
@abc.abstractmethod
4961
@asynccontextmanager
5062
def trace_async(self, name: str, **kwargs) -> AsyncGenerator[BaseSpan, None]:
51-
"""Return a async span context manger.
63+
"""Async Context manager for creating a new span and set it
64+
as the current span in this tracer's context.
65+
66+
Exiting the context manager will call the span's end method,
67+
as well as return the current span to its previous value by
68+
returning to the previous context.
5269
5370
Parameters
5471
----------
5572
name: str
5673
Span name
5774
kwargs: Optional[dict]
58-
Optional parameters to be propagated to span
75+
Optional parameters to be propagated to the span
5976
"""
6077

6178
@abc.abstractmethod
62-
def set_attribute(self, key: str, value: Union[str, numbers.Number, bool], **kwargs) -> None:
63-
"""set attribute on current active trace entity with a key-value pair.
79+
def set_attribute(self, key: str, value: Any, **kwargs) -> None:
80+
"""set attribute on current active span with a key-value pair.
6481
6582
Parameters
6683
----------
6784
key: str
6885
attribute key
69-
value: Union[str, numbers.Number, bool]
86+
value: Any
7087
attribute value
88+
kwargs: Optional[dict]
89+
Optional parameters to be propagated to the span
7190
"""
7291

7392
@abc.abstractmethod
7493
def patch(self, modules: Sequence[str]) -> None:
75-
"""Instrument a set of supported libraries
94+
"""Instrument a set of given libraries if supported by provider
95+
See specific provider for more detail
96+
97+
Exmaple
98+
-------
99+
tracer = Tracer(service="payment")
100+
libraries = (['aioboto3',mysql])
101+
# provider.patch will be called by tracer.patch
102+
tracer.patch(libraries)
76103
77104
Parameters
78105
----------

aws_lambda_powertools/tracing/provider/otel/otel_tracer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class OtelSpan(BaseSpan):
1515
def __init__(self, otel_span=otel_trace.Span):
1616
self.otel_span = otel_span
1717

18-
def set_attribute(self, key: str, value: str | float | bool, **kwargs) -> None: # type: ignore[override]
18+
def set_attribute(self, key: str, value: str | float | bool, **kwargs) -> None:
1919
if not isinstance(value, (str, bool, int, float)):
2020
# convert value to str if value is not a supported structur
2121
value = str(value)
@@ -69,7 +69,7 @@ async def trace_async(self, name: str, **kwargs) -> AsyncGenerator[OtelSpan, Non
6969

7070
in_subsegment_async = trace_async
7171

72-
def set_attribute(self, key: str, value: str | float | bool, **kwargs) -> None: # type: ignore[override]
72+
def set_attribute(self, key: str, value: str | float | bool, **kwargs) -> None:
7373
active_span = otel_trace.get_current_span()
7474
active_span.set_attribute(key=key, value=value)
7575

aws_lambda_powertools/tracing/provider/xray/xray_tracer.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from contextlib import asynccontextmanager, contextmanager
44
from numbers import Number
5-
from typing import Any, AsyncGenerator, Generator, Sequence
5+
from typing import Any, AsyncGenerator, Generator, Sequence, Union
66

77
from ....shared import constants
88
from ....shared.lazy_import import LazyLoader
@@ -21,7 +21,7 @@ def __init__(self, subsegment):
2121
self.add_exception = self.subsegment.add_exception
2222
self.close = self.subsegment.close
2323

24-
def set_attribute(self, key: str, value: str | Number | bool, **kwargs) -> None:
24+
def set_attribute(self, key: str, value: Any, **kwargs) -> None:
2525
if kwargs.get("namespace", "") != "":
2626
self.put_metadata(key=key, value=value, namespace=kwargs["namespace"])
2727
else:
@@ -42,21 +42,23 @@ def __init__(self, xray_recorder=None):
4242

4343
@contextmanager
4444
def trace(self, name: str, **kwargs) -> Generator[XraySpan, None, None]:
45-
with self.in_subsegment(name=name) as sub_segment:
45+
with self.in_subsegment(name=name, **kwargs) as sub_segment:
4646
yield XraySpan(subsegment=sub_segment)
4747

4848
@asynccontextmanager
4949
async def trace_async(self, name: str, **kwargs) -> AsyncGenerator[XraySpan, None]:
50-
async with self.in_subsegment_async(name=name) as subsegment:
50+
async with self.in_subsegment_async(name=name, **kwargs) as subsegment:
5151
yield XraySpan(subsegment=subsegment)
5252

53-
def set_attribute(self, key: str, value: str | Number | bool, **kwargs) -> None:
54-
if kwargs.get("namespace", "") != "":
55-
self.put_metadata(key=key, value=value, namespace=kwargs["namespace"])
56-
else:
53+
def set_attribute(self, key: str, value: Any, **kwargs) -> None:
54+
# for x_ray, put annotation support str, Number, bool
55+
# we use put_metadata if any unsupported values are provided
56+
if isinstance(value, (str, Number, bool)):
5757
self.put_annotation(key=key, value=value)
58+
else:
59+
self.put_metadata(key=key, value=value, namespace=kwargs["namespace"])
5860

59-
def put_annotation(self, key: str, value: str | Number | bool) -> None:
61+
def put_annotation(self, key: str, value: Union[str, Number, bool]) -> None:
6062
return self.recorder.put_annotation(key=key, value=value)
6163

6264
def put_metadata(self, key: str, value: Any, namespace: str = "default") -> None:

aws_lambda_powertools/tracing/tracer.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
)
1616
from aws_lambda_powertools.shared.lazy_import import LazyLoader
1717
from aws_lambda_powertools.shared.types import AnyCallableT
18-
from aws_lambda_powertools.tracing.provider import XrayProvider
1918
from aws_lambda_powertools.tracing.provider.base import BaseProvider, BaseSpan
19+
from aws_lambda_powertools.tracing.provider.xray.xray_tracer import XrayProvider
2020

2121
is_cold_start = True
2222
logger = logging.getLogger(__name__)
@@ -178,15 +178,17 @@ def __init__(
178178
if self._is_xray_provider():
179179
self._disable_xray_trace_batching()
180180

181-
def set_attribute(self, key: str, value: Union[str, numbers.Number, bool]):
181+
def set_attribute(self, key: str, value: Any, **kwargs):
182182
"""Set attribute on current active trace entity with a key-value pair.
183183
184184
Parameters
185185
----------
186186
key : str
187187
attribute key
188-
value : Union[str, numbers.Number, bool]
188+
value : Any
189189
Value for attribute
190+
kwargs: Optional[dict]
191+
Optional parameters to be passed to provider.set_attributes
190192
191193
Example
192194
-------
@@ -201,7 +203,10 @@ def set_attribute(self, key: str, value: Union[str, numbers.Number, bool]):
201203

202204
logger.debug(f"setting attribute on key '{key}' with '{value}'")
203205

204-
self.provider.set_attribute(key=key, value=value)
206+
namespace = kwargs.get("namespace") or self.service
207+
kwargs.update({"namespace": namespace})
208+
209+
self.provider.set_attribute(key=key, value=value, **kwargs)
205210

206211
def put_annotation(self, key: str, value: Union[str, numbers.Number, bool]):
207212
"""Adds annotation to existing segment or subsegment

0 commit comments

Comments
 (0)