Skip to content

Commit 136b977

Browse files
committed
docs(middleware): update single code blocks to title
1 parent 33680e4 commit 136b977

File tree

1 file changed

+44
-62
lines changed

1 file changed

+44
-62
lines changed

docs/utilities/middleware_factory.md

Lines changed: 44 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -18,96 +18,78 @@ 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-
=== "app.py"
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
2223

23-
```python hl_lines="3-4 10"
24-
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
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
2530

26-
@lambda_handler_decorator
27-
def middleware_before_after(handler, event, context):
28-
# logic_before_handler_execution()
29-
response = handler(event, context)
30-
# logic_after_handler_execution()
31-
return response
32-
33-
@middleware_before_after
34-
def lambda_handler(event, context):
35-
...
36-
```
31+
@middleware_before_after
32+
def lambda_handler(event, context):
33+
...
34+
```
3735

3836
## Middleware with params
3937

4038
You can also have your own keyword arguments after the mandatory arguments.
4139

42-
=== "app.py"
43-
44-
```python hl_lines="2 12"
45-
@lambda_handler_decorator
46-
def obfuscate_sensitive_data(handler, event, context, fields: List = None):
47-
# Obfuscate email before calling Lambda handler
48-
if fields:
49-
for field in fields:
50-
if field in event:
51-
event[field] = obfuscate(event[field])
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])
5248

53-
return handler(event, context)
49+
return handler(event, context)
5450

55-
@obfuscate_sensitive_data(fields=["email"])
56-
def lambda_handler(event, context):
57-
...
58-
```
51+
@obfuscate_sensitive_data(fields=["email"])
52+
def lambda_handler(event, context):
53+
...
54+
```
5955

6056
## Tracing middleware execution
6157

6258
If you are making use of [Tracer](../core/tracer.md), you can trace the execution of your middleware to ease operations.
6359

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

66-
=== "trace_middleware_execution.py"
62+
```python hl_lines="3" title="Tracing custom middlewares with Tracer"
63+
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
6764

68-
```python hl_lines="3"
69-
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
65+
@lambda_handler_decorator(trace_execution=True)
66+
def my_middleware(handler, event, context):
67+
return handler(event, context)
7068

71-
@lambda_handler_decorator(trace_execution=True)
72-
def my_middleware(handler, event, context):
73-
return handler(event, context)
74-
75-
@my_middleware
76-
def lambda_handler(event, context):
77-
...
78-
```
69+
@my_middleware
70+
def lambda_handler(event, context):
71+
...
72+
```
7973

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

8276
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.
8377

84-
=== "app.py"
85-
86-
```python hl_lines="6-8"
87-
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
88-
from aws_lambda_powertools import Tracer
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
8981

90-
@lambda_handler_decorator(trace_execution=True)
91-
def middleware_name(handler, event, context):
92-
tracer = Tracer() # Takes a copy of an existing tracer instance
93-
tracer.add_annotation...
94-
tracer.add_metadata...
95-
return handler(event, context)
96-
```
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)
88+
```
9789

9890
## Tips
9991

10092
* Use `trace_execution` to quickly understand the performance impact of your middlewares, and reduce or merge tasks when necessary
10193
* When nesting multiple middlewares, always return the handler with event and context, or response
10294
* Keep in mind [Python decorators execution order](https://realpython.com/primer-on-python-decorators/#nesting-decorators){target="_blank"}. Lambda handler is actually called once (top-down)
10395
* Async middlewares are not supported
104-
105-
## Testing your code
106-
107-
When unit testing middlewares with `trace_execution` option enabled, use `POWERTOOLS_TRACE_DISABLED` env var to safely disable Tracer.
108-
109-
=== "shell"
110-
111-
```bash
112-
POWERTOOLS_TRACE_DISABLED=1 python -m pytest
113-
```

0 commit comments

Comments
 (0)