2
2
import json
3
3
import logging
4
4
import warnings
5
- from typing import Any , Callable , Dict , Optional
5
+ from typing import Any , Callable , Dict , Optional , Union , cast
6
6
7
+ from ..shared .types import AnyCallableT
7
8
from .base import MetricManager , MetricUnit
8
9
from .metric import single_metric
9
10
@@ -87,7 +88,7 @@ def __init__(self, service: Optional[str] = None, namespace: Optional[str] = Non
87
88
service = self .service ,
88
89
)
89
90
90
- def set_default_dimensions (self , ** dimensions ):
91
+ def set_default_dimensions (self , ** dimensions ) -> None :
91
92
"""Persist dimensions across Lambda invocations
92
93
93
94
Parameters
@@ -113,10 +114,10 @@ def lambda_handler():
113
114
114
115
self .default_dimensions .update (** dimensions )
115
116
116
- def clear_default_dimensions (self ):
117
+ def clear_default_dimensions (self ) -> None :
117
118
self .default_dimensions .clear ()
118
119
119
- def clear_metrics (self ):
120
+ def clear_metrics (self ) -> None :
120
121
logger .debug ("Clearing out existing metric set from memory" )
121
122
self .metric_set .clear ()
122
123
self .dimension_set .clear ()
@@ -125,11 +126,11 @@ def clear_metrics(self):
125
126
126
127
def log_metrics (
127
128
self ,
128
- lambda_handler : Optional [Callable [[Any , Any ], Any ]] = None ,
129
+ lambda_handler : Union [Callable [[Dict , Any ] , Any ], Optional [ Callable [[ Dict , Any , Optional [ Dict ]], Any ] ]] = None ,
129
130
capture_cold_start_metric : bool = False ,
130
131
raise_on_empty_metrics : bool = False ,
131
132
default_dimensions : Optional [Dict [str , str ]] = None ,
132
- ):
133
+ ) -> AnyCallableT :
133
134
"""Decorator to serialize and publish metrics at the end of a function execution.
134
135
135
136
Be aware that the log_metrics **does call* the decorated function (e.g. lambda_handler).
@@ -169,11 +170,14 @@ def handler(event, context):
169
170
# Return a partial function with args filled
170
171
if lambda_handler is None :
171
172
logger .debug ("Decorator called with parameters" )
172
- return functools .partial (
173
- self .log_metrics ,
174
- capture_cold_start_metric = capture_cold_start_metric ,
175
- raise_on_empty_metrics = raise_on_empty_metrics ,
176
- default_dimensions = default_dimensions ,
173
+ return cast (
174
+ AnyCallableT ,
175
+ functools .partial (
176
+ self .log_metrics ,
177
+ capture_cold_start_metric = capture_cold_start_metric ,
178
+ raise_on_empty_metrics = raise_on_empty_metrics ,
179
+ default_dimensions = default_dimensions ,
180
+ ),
177
181
)
178
182
179
183
@functools .wraps (lambda_handler )
@@ -194,9 +198,9 @@ def decorate(event, context):
194
198
195
199
return response
196
200
197
- return decorate
201
+ return cast ( AnyCallableT , decorate )
198
202
199
- def __add_cold_start_metric (self , context : Any ):
203
+ def __add_cold_start_metric (self , context : Any ) -> None :
200
204
"""Add cold start metric and function_name dimension
201
205
202
206
Parameters
0 commit comments