Skip to content

Commit 407fc34

Browse files
authored
[Sync Master] 0.7.0 release (#22)
* docs: add pypi badge * fix: add missing single_metric example; test var name * chore: pypi monthly download badge * chore: fix github badge typo * feat: add docs to CI * fix: CI attempt 2 * fix: CI attempt 3 * fix: CI attempt 3 * fix: CI attempt 4 * chore: clean up CI workflows * Decorator factory Feat: Create your own middleware (#17) * feat(utils): add decorator factory * improv: use partial to reduce complexity * improv: add error handling * chore: type hint * docs: include pypi downloads badge * feat: opt in to trace each middleware that runs * improv: add initial util tests * improv: test explicit and implicit trace_execution * improv: test decorator with params * chore: linting * docs: include utilities * improv: correct tests, dec_factory only for func * improv: make util name more explicit * improv: doc trace_execution, fix casting * docs: add limitations, improve syntax * docs: use new docs syntax * fix: remove middleware decorator from libs * feat: build docs in CI * chore: linting * fix: CI python-version type * chore: remove docs CI * chore: kick CI * chore: include build badge master branch * chore: refactor naming * fix: rearrange tracing tests * improv(tracer): toggle default auto patching * feat(tracer): retrieve registered class instance * fix(Makefile): make cov target more explicit * improv(Register): support multiple classes reg. * improv(Register): inject class methods correctly * docs: add how to reutilize Tracer * improv(tracer): test auto patch method * improv: address nicolas feedback * improv: update example to reflect middleware feat * fix: metric dimension in root blob * chore: version bump Co-authored-by: heitorlessa <[email protected]> Co-authored-by: heitorlessa <[email protected]>
1 parent a9fc2bd commit 407fc34

File tree

18 files changed

+919
-383
lines changed

18 files changed

+919
-383
lines changed

Diff for: .github/workflows/pythonpackage.yml renamed to .github/workflows/python.yml

+6-10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ on:
1717
jobs:
1818
build:
1919
runs-on: ubuntu-latest
20+
defaults:
21+
run:
22+
working-directory: ./python/
2023
strategy:
2124
max-parallel: 4
2225
matrix:
@@ -28,15 +31,8 @@ jobs:
2831
with:
2932
python-version: ${{ matrix.python-version }}
3033
- name: Install dependencies
31-
run: |
32-
python -m pip install --upgrade pip
33-
make dev
34-
working-directory: ./python/
34+
run: make dev
3535
- name: Formatting and Linting
36-
run: |
37-
make lint
38-
working-directory: ./python/
36+
run: make lint
3937
- name: Test with pytest
40-
run: |
41-
make test
42-
working-directory: ./python/
38+
run: make test

Diff for: .github/workflows/python_docs.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Powertools Python Docs
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- master
7+
paths:
8+
- "python/**"
9+
push:
10+
branches:
11+
- master
12+
paths:
13+
- "python/**"
14+
15+
jobs:
16+
docs:
17+
runs-on: ubuntu-latest
18+
defaults:
19+
run:
20+
working-directory: ./python/
21+
steps:
22+
- uses: actions/checkout@v1
23+
- name: Set up Python
24+
uses: actions/setup-python@v1
25+
with:
26+
python-version: "3.8"
27+
- name: Install dependencies
28+
run: make dev
29+
- name: build docs
30+
run: make docs
31+
- name: deploy docs
32+
uses: peaceiris/actions-gh-pages@v3
33+
with:
34+
github_token: ${{ secrets.GITHUB_TOKEN }}
35+
publish_dir: python/docs/aws_lambda_powertools/

Diff for: python/HISTORY.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# HISTORY
22

3+
## April 20th, 2020
4+
5+
**0.7.0**
6+
7+
* Introduces Middleware Factory to build your own middleware
8+
* Fixes Metrics dimensions not being included correctly in EMF
9+
310
## April 9th, 2020
411

512
**0.6.3**

Diff for: python/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ target:
33
@$(MAKE) pr
44

55
dev:
6-
pip install --upgrade poetry
6+
pip install --upgrade pip poetry
77
poetry install
88

99
format:
@@ -17,7 +17,7 @@ lint: format
1717
test:
1818
poetry run pytest -vvv
1919

20-
test-html:
20+
coverage-html:
2121
poetry run pytest --cov-report html
2222

2323
pr: lint test

Diff for: python/README.md

+118-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Lambda Powertools
22

3-
![PackageStatus](https://img.shields.io/static/v1?label=status&message=beta&color=blueviolet?style=flat-square) ![PythonSupport](https://img.shields.io/static/v1?label=python&message=3.6%20|%203.7|%203.8&color=blue?style=flat-square&logo=python)
3+
![PackageStatus](https://img.shields.io/static/v1?label=status&message=beta&color=blueviolet?style=flat-square) ![PythonSupport](https://img.shields.io/static/v1?label=python&message=3.6%20|%203.7|%203.8&color=blue?style=flat-square&logo=python) ![PyPI version](https://badge.fury.io/py/aws-lambda-powertools.svg) ![PyPi monthly downloads](https://img.shields.io/pypi/dm/aws-lambda-powertools) ![Build](https://github.com/awslabs/aws-lambda-powertools/workflows/Powertools%20Python/badge.svg?branch=master)
44

55
A suite of utilities for AWS Lambda Functions that makes tracing with AWS X-Ray, structured logging and creating custom metrics asynchronously easier - Currently available for Python only and compatible with Python >=3.6.
66

@@ -32,12 +32,20 @@ A suite of utilities for AWS Lambda Functions that makes tracing with AWS X-Ray,
3232
* Validate against common metric definitions mistakes (metric unit, values, max dimensions, max metrics, etc)
3333
* No stack, custom resource, data collection needed — Metrics are created async by CloudWatch EMF
3434

35+
**Bring your own middleware**
36+
37+
* Utility to easily create your own middleware
38+
* Run logic before, after, and handle exceptions
39+
* Receive lambda handler, event, context
40+
* Optionally create sub-segment for each custom middleware
41+
3542
**Environment variables** used across suite of utilities
3643

3744
Environment variable | Description | Default | Utility
3845
------------------------------------------------- | --------------------------------------------------------------------------------- | --------------------------------------------------------------------------------- | -------------------------------------------------
3946
POWERTOOLS_SERVICE_NAME | Sets service name used for tracing namespace, metrics dimensions and structured logging | "service_undefined" | all
4047
POWERTOOLS_TRACE_DISABLED | Disables tracing | "false" | tracing
48+
POWERTOOLS_TRACE_MIDDLEWARES | Creates sub-segment for each middleware created by lambda_handler_decorator | "false" | middleware_factory
4149
POWERTOOLS_LOGGER_LOG_EVENT | Logs incoming event | "false" | logging
4250
POWERTOOLS_LOGGER_SAMPLE_RATE | Debug log sampling | 0 | logging
4351
POWERTOOLS_METRICS_NAMESPACE | Metrics namespace | None | metrics
@@ -85,6 +93,23 @@ def handler(event, context)
8593
...
8694
```
8795

96+
**Fetching a pre-configured tracer anywhere**
97+
98+
```python
99+
# handler.py
100+
from aws_lambda_powertools.tracing import Tracer
101+
tracer = Tracer(service="payment")
102+
103+
@tracer.capture_lambda_handler
104+
def handler(event, context)
105+
charge_id = event.get('charge_id')
106+
payment = collect_payment(charge_id)
107+
...
108+
109+
# another_file.py
110+
from aws_lambda_powertools.tracing import Tracer
111+
tracer = Tracer(auto_patch=False) # new instance using existing configuration with auto patching overriden
112+
```
88113

89114
### Logging
90115

@@ -154,7 +179,7 @@ def handler(event, context)
154179
}
155180
```
156181

157-
#### Custom Metrics async
182+
### Custom Metrics async
158183

159184
> **NOTE** `log_metric` will be removed once it's GA.
160185
@@ -204,6 +229,97 @@ with single_metric(name="ColdStart", unit=MetricUnit.Count, value=1) as metric:
204229
metric.add_dimension(name="function_context", value="$LATEST")
205230
```
206231

232+
233+
### Utilities
234+
235+
#### Bring your own middleware
236+
237+
This feature allows you to create your own middleware as a decorator with ease by following a simple signature.
238+
239+
* Accept 3 mandatory args - `handler, event, context`
240+
* Always return the handler with event/context or response if executed
241+
- Supports nested middleware/decorators use case
242+
243+
**Middleware with no params**
244+
245+
```python
246+
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
247+
248+
@lambda_handler_decorator
249+
def middleware_name(handler, event, context):
250+
return handler(event, context)
251+
252+
@lambda_handler_decorator
253+
def middleware_before_after(handler, event, context):
254+
logic_before_handler_execution()
255+
response = handler(event, context)
256+
logic_after_handler_execution()
257+
return response
258+
259+
260+
# middleware_name will wrap Lambda handler
261+
# and simply return the handler as we're not pre/post-processing anything
262+
# then middleware_before_after will wrap middleware_name
263+
# run some code before/after calling the handler returned by middleware_name
264+
# This way, lambda_handler is only actually called once (top-down)
265+
@middleware_before_after # This will run last
266+
@middleware_name # This will run first
267+
def lambda_handler(event, context):
268+
return True
269+
```
270+
271+
**Middleware with params**
272+
273+
```python
274+
@lambda_handler_decorator
275+
def obfuscate_sensitive_data(handler, event, context, fields=None):
276+
# Obfuscate email before calling Lambda handler
277+
if fields:
278+
for field in fields:
279+
field = event.get(field, "")
280+
event[field] = obfuscate_pii(field)
281+
282+
return handler(event, context)
283+
284+
@obfuscate_sensitive_data(fields=["email"])
285+
def lambda_handler(event, context):
286+
return True
287+
```
288+
289+
**Optionally trace middleware execution**
290+
291+
This makes use of an existing Tracer instance that you may have initialized anywhere in your code, otherwise it'll initialize one using default options and provider (X-Ray).
292+
293+
```python
294+
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
295+
296+
@lambda_handler_decorator(trace_execution=True)
297+
def middleware_name(handler, event, context):
298+
return handler(event, context)
299+
300+
@middleware_name
301+
def lambda_handler(event, context):
302+
return True
303+
```
304+
305+
Optionally, you can enrich the final trace with additional annotations and metadata by retrieving a copy of the Tracer used.
306+
307+
```python
308+
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
309+
from aws_lambda_powertools.tracing import Tracer
310+
311+
@lambda_handler_decorator(trace_execution=True)
312+
def middleware_name(handler, event, context):
313+
tracer = Tracer() # Takes a copy of an existing tracer instance
314+
tracer.add_anotation...
315+
tracer.metadata...
316+
return handler(event, context)
317+
318+
@middleware_name
319+
def lambda_handler(event, context):
320+
return True
321+
```
322+
207323
## Beta
208324

209325
> **[Progress towards GA](https://github.com/awslabs/aws-lambda-powertools/projects/1)**

0 commit comments

Comments
 (0)