Skip to content

Commit e3fddaf

Browse files
committed
improv: update docs, linting
1 parent 63a8ea9 commit e3fddaf

File tree

5 files changed

+62
-16
lines changed

5 files changed

+62
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
class MetricUnitError(Exception):
2+
"""When metric unit is not supported by CloudWatch"""
3+
24
pass
35

46

57
class SchemaValidationError(Exception):
8+
"""When serialization fail schema validation"""
9+
610
pass
711

812

913
class MetricValueError(Exception):
14+
"""When metric value isn't a valid number"""
15+
1016
pass
1117

1218

1319
class UniqueNamespaceError(Exception):
20+
"""When an additional namespace is set"""
21+
1422
pass
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
class MiddlewareInvalidArgumentError(Exception):
2+
"""When middleware receives non keyword=arguments"""
3+
24
pass

python/aws_lambda_powertools/tracing/base.py

+29-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
from __future__ import annotations
22

33
import abc
4-
import copy
5-
import functools
64
import logging
7-
import os
8-
from typing import Any, Callable, List
5+
from typing import Any, List
96

107
import aws_xray_sdk
118
import aws_xray_sdk.core
@@ -20,7 +17,14 @@ class TracerProvider(metaclass=abc.ABCMeta):
2017
Providers should be initialized independently. This
2118
allows providers to control their config/initialization,
2219
and only pass a class instance to
23-
```aws_lambda_powertools.tracing.tracer.Tracer```.
20+
`aws_lambda_powertools.tracing.tracer.Tracer`.
21+
22+
It also allows custom providers to keep lean while Tracer provide:
23+
24+
* a simplified UX
25+
* decorators for Lambda handler and methods
26+
* auto-patching, patch all modules by default or a subset
27+
* disabling all tracing operations with a single parameter or env var
2428
2529
Trace providers should implement the following methods:
2630
@@ -32,10 +36,17 @@ class TracerProvider(metaclass=abc.ABCMeta):
3236
* **disable_tracing_provider**
3337
3438
These methods will be called by
35-
```aws_lambda_powertools.tracing.tracer.Tracer```.
36-
See ```aws_lambda_powertools.tracing.base.XrayProvider```
39+
`aws_lambda_powertools.tracing.tracer.Tracer` -
40+
See `aws_lambda_powertools.tracing.base.XrayProvider`
3741
for a reference implementation.
3842
43+
`aws_lambda_powertools.tracing.tracer.Tracer` decorators
44+
for Lambda and methods use the following provider methods:
45+
46+
* create_subsegment
47+
* put_metadata
48+
* end_subsegment
49+
3950
Example
4051
-------
4152
**Client using a custom tracing provider**
@@ -112,6 +123,17 @@ def disable_tracing_provider(self):
112123

113124

114125
class XrayProvider(TracerProvider):
126+
"""X-Ray Tracer provider
127+
128+
It implements all basic ``aws_lambda_powertools.tracing.base.TracerProvider` methods,
129+
and automatically annotates cold start on first subsegment created.
130+
131+
Parameters
132+
----------
133+
client : aws_xray_sdk.core.xray_recorder
134+
X-Ray recorder client
135+
"""
136+
115137
def __init__(self, client: aws_xray_sdk.core.xray_recorder = aws_xray_sdk.core.xray_recorder):
116138
self.client = client
117139

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
class InvalidTracerProviderError(Exception):
2+
"""When given provider doesn't implement `aws_lambda_powertools.tracing.base.TracerProvider`"""
3+
24
pass
35

6+
47
class TracerProviderNotInitializedError(Exception):
8+
"""When given provider isn't initialized/bound"""
9+
510
pass

python/aws_lambda_powertools/tracing/tracer.py

+18-9
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
import logging
55
import os
66
from distutils.util import strtobool
7-
from typing import Any, Callable, Dict, List
7+
from typing import Any, Callable, Dict, Generic, List, TypeVar
88

99
from .base import TracerProvider, XrayProvider
1010
from .exceptions import InvalidTracerProviderError, TracerProviderNotInitializedError
1111

12+
subsegment = TypeVar("subsegment")
1213
is_cold_start = True
1314
logger = logging.getLogger(__name__)
1415

@@ -27,6 +28,9 @@ class Tracer:
2728
is useful when you are using your own middlewares and want to utilize an existing Tracer.
2829
Make sure to set `auto_patch=False` in subsequent Tracer instances to avoid double patching.
2930
31+
Tracer supports custom providers that implement
32+
`aws_lambda_powertools.tracing.base.TracerProvider`.
33+
3034
Environment variables
3135
---------------------
3236
POWERTOOLS_TRACE_DISABLED : str
@@ -115,6 +119,13 @@ def handler(event: dict, context: Any) -> Dict:
115119
Tracer
116120
Tracer instance with imported modules patched
117121
122+
Raises
123+
------
124+
InvalidTracerProviderError
125+
When given provider doesn't implement `aws_lambda_powertools.tracing.base.TracerProvider`
126+
TracerProviderNotInitializedError
127+
When given provider isn't initialized/bound
128+
118129
Limitations
119130
-----------
120131
* Async handler and methods not supported
@@ -152,7 +163,7 @@ def __init__(
152163
if self.auto_patch:
153164
self.patch(modules=patch_modules)
154165

155-
def create_subsegment(self, name: str):
166+
def create_subsegment(self, name: str) -> Generic[subsegment]:
156167
"""Creates subsegment/span with a given name
157168
158169
It also assumes Tracer would be instantiated statically so that cold starts are captured.
@@ -168,11 +179,10 @@ def create_subsegment(self, name: str):
168179
169180
self.create_subsegment(name="a meaningful name")
170181
171-
# FIXME - Return Subsegment Any type
172182
Returns
173183
-------
174-
models.subsegment
175-
AWS X-Ray Subsegment
184+
subsegment
185+
Trace provider subsegment
176186
"""
177187
# Will no longer be needed once #155 is resolved
178188
# https://github.com/aws/aws-xray-sdk-python/issues/155
@@ -204,7 +214,7 @@ def put_annotation(self, key: str, value: Any):
204214
----------
205215
key : str
206216
Annotation key (e.g. PaymentStatus)
207-
value : Any
217+
value : any
208218
Value for annotation (e.g. "CONFIRMED")
209219
"""
210220
# Will no longer be needed once #155 is resolved
@@ -216,14 +226,14 @@ def put_annotation(self, key: str, value: Any):
216226
logger.debug(f"Annotating on key '{key}'' with '{value}''")
217227
self.provider.put_annotation(key=key, value=value)
218228

219-
def put_metadata(self, key: str, value: object, namespace: str = None):
229+
def put_metadata(self, key: str, value: Any, namespace: str = None):
220230
"""Adds metadata to existing segment or subsegment
221231
222232
Parameters
223233
----------
224234
key : str
225235
Metadata key
226-
value : object
236+
value : any
227237
Value for metadata
228238
namespace : str, optional
229239
Namespace that metadata will lie under, by default None
@@ -285,7 +295,6 @@ def handler(event, context)
285295
@functools.wraps(lambda_handler)
286296
def decorate(event, context):
287297
self.create_subsegment(name=f"## {lambda_handler.__name__}")
288-
289298
try:
290299
logger.debug("Calling lambda handler")
291300
response = lambda_handler(event, context)

0 commit comments

Comments
 (0)