You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
defobfuscate_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])
52
48
53
-
return handler(event, context)
49
+
return handler(event, context)
54
50
55
-
@obfuscate_sensitive_data(fields=["email"])
56
-
def lambda_handler(event, context):
57
-
...
58
-
```
51
+
@obfuscate_sensitive_data(fields=["email"])
52
+
deflambda_handler(event, context):
53
+
...
54
+
```
59
55
60
56
## Tracing middleware execution
61
57
62
58
If you are making use of [Tracer](../core/tracer.md), you can trace the execution of your middleware to ease operations.
63
59
64
60
This makes use of an existing Tracer instance that you may have initialized anywhere in your code.
65
61
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
67
64
68
-
```python hl_lines="3"
69
-
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
65
+
@lambda_handler_decorator(trace_execution=True)
66
+
defmy_middleware(handler, event, context):
67
+
return handler(event, context)
70
68
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
+
deflambda_handler(event, context):
71
+
...
72
+
```
79
73
80
74
When executed, your middleware name will [appear in AWS X-Ray Trace details as](../core/tracer.md)`## middleware_name`.
81
75
82
76
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.
83
77
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
89
81
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
+
defmiddleware_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
+
```
97
89
98
90
## Tips
99
91
100
92
* Use `trace_execution` to quickly understand the performance impact of your middlewares, and reduce or merge tasks when necessary
101
93
* When nesting multiple middlewares, always return the handler with event and context, or response
102
94
* 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)
103
95
* 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.
0 commit comments