Skip to content

Commit 37ae639

Browse files
committed
fix(docs): Extract middleware examples
Changes: - Extract code examples - Run isort and black - Update line highlights - Add make task Related to: - aws-powertools#1064
1 parent b577366 commit 37ae639

File tree

6 files changed

+69
-47
lines changed

6 files changed

+69
-47
lines changed

Diff for: Makefile

+7
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,10 @@ changelog:
9090

9191
mypy:
9292
poetry run mypy --pretty aws_lambda_powertools
93+
94+
format-examples:
95+
poetry run isort docs/examples
96+
poetry run black docs/examples/*/*/*.py
97+
98+
lint-examples:
99+
poetry run python3 -m py_compile docs/examples/*/*/*.py
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
2+
3+
4+
@lambda_handler_decorator
5+
def middleware_before_after(handler, event, context):
6+
# logic_before_handler_execution()
7+
response = handler(event, context)
8+
# logic_after_handler_execution()
9+
return response
10+
11+
12+
@middleware_before_after
13+
def lambda_handler(event, context):
14+
...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from aws_lambda_powertools import Tracer
2+
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
3+
4+
5+
@lambda_handler_decorator(trace_execution=True)
6+
def middleware_name(handler, event, context):
7+
# tracer = Tracer() # Takes a copy of an existing tracer instance
8+
# tracer.add_annotation...
9+
# tracer.add_metadata...
10+
return handler(event, context)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
2+
3+
4+
@lambda_handler_decorator(trace_execution=True)
5+
def my_middleware(handler, event, context):
6+
return handler(event, context)
7+
8+
9+
@my_middleware
10+
def lambda_handler(event, context):
11+
...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import List
2+
3+
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
4+
5+
6+
@lambda_handler_decorator
7+
def obfuscate_sensitive_data(handler, event, context, fields: List = None):
8+
# Obfuscate email before calling Lambda handler
9+
if fields:
10+
for field in fields:
11+
if field in event:
12+
event[field] = obfuscate(event[field])
13+
14+
return handler(event, context)
15+
16+
17+
@obfuscate_sensitive_data(fields=["email"])
18+
def lambda_handler(event, context):
19+
...

Diff for: docs/utilities/middleware_factory.md

+8-47
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,16 @@ You can create your own middleware using `lambda_handler_decorator`. The decorat
1818
* **event** - Lambda function invocation event
1919
* **context** - Lambda function context object
2020

21-
```python hl_lines="3-4 10" title="Creating your own middleware for before/after logic"
22-
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
23-
24-
@lambda_handler_decorator
25-
def middleware_before_after(handler, event, context):
26-
# logic_before_handler_execution()
27-
response = handler(event, context)
28-
# logic_after_handler_execution()
29-
return response
30-
31-
@middleware_before_after
32-
def lambda_handler(event, context):
33-
...
21+
```python hl_lines="4-5 12" title="Creating your own middleware for before/after logic"
22+
--8<-- "docs/examples/utilities/middleware_factory/middleware_no_params.py"
3423
```
3524

3625
## Middleware with params
3726

3827
You can also have your own keyword arguments after the mandatory arguments.
3928

40-
```python hl_lines="2 12" title="Accepting arbitrary keyword arguments"
41-
@lambda_handler_decorator
42-
def obfuscate_sensitive_data(handler, event, context, fields: List = None):
43-
# Obfuscate email before calling Lambda handler
44-
if fields:
45-
for field in fields:
46-
if field in event:
47-
event[field] = obfuscate(event[field])
48-
49-
return handler(event, context)
50-
51-
@obfuscate_sensitive_data(fields=["email"])
52-
def lambda_handler(event, context):
53-
...
29+
```python hl_lines="7 17" title="Accepting arbitrary keyword arguments"
30+
--8<-- "docs/examples/utilities/middleware_factory/middleware_with_params.py"
5431
```
5532

5633
## Tracing middleware execution
@@ -59,32 +36,16 @@ If you are making use of [Tracer](../core/tracer.md), you can trace the executio
5936

6037
This makes use of an existing Tracer instance that you may have initialized anywhere in your code.
6138

62-
```python hl_lines="3" title="Tracing custom middlewares with Tracer"
63-
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
64-
65-
@lambda_handler_decorator(trace_execution=True)
66-
def my_middleware(handler, event, context):
67-
return handler(event, context)
68-
69-
@my_middleware
70-
def lambda_handler(event, context):
71-
...
39+
```python hl_lines="4" title="Tracing custom middlewares with Tracer"
40+
--8<-- "docs/examples/utilities/middleware_factory/middleware_trace_execution.py"
7241
```
7342

7443
When executed, your middleware name will [appear in AWS X-Ray Trace details as](../core/tracer.md) `## middleware_name`.
7544

7645
For advanced use cases, you can instantiate [Tracer](../core/tracer.md) inside your middleware, and add annotations as well as metadata for additional operational insights.
7746

78-
```python hl_lines="6-8" title="Add custom tracing insights before/after in your middlware"
79-
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
80-
from aws_lambda_powertools import Tracer
81-
82-
@lambda_handler_decorator(trace_execution=True)
83-
def middleware_name(handler, event, context):
84-
# tracer = Tracer() # Takes a copy of an existing tracer instance
85-
# tracer.add_annotation...
86-
# tracer.add_metadata...
87-
return handler(event, context)
47+
```python hl_lines="7-9" title="Add custom tracing insights before/after in your middlware"
48+
--8<-- "docs/examples/utilities/middleware_factory/middleware_trace_custom.py"
8849
```
8950

9051
## Tips

0 commit comments

Comments
 (0)