Skip to content

Commit 980229f

Browse files
committed
Fix context usage.
1 parent 0fa224b commit 980229f

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

lambda_local/context.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def __init__(self, timeout_in_seconds,
3030
self.identity = identity
3131
self.client_context = client_context
3232

33+
self._timeout_in_seconds = timeout_in_seconds
3334
self._duration = timedelta(seconds=timeout_in_seconds)
3435

3536
def get_remaining_time_in_millis(self):

lambda_local/main.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,56 +31,64 @@
3131
EXITCODE_ERR = 1
3232

3333

34-
def call(func, event, timeout, environment_variables={}, arn_string="", version_name="", library=None):
34+
class ContextFilter(logging.Filter):
35+
def __init__(self, context):
36+
super(ContextFilter, self).__init__()
37+
self.context = context
38+
39+
def filter(self, record):
40+
record.aws_request_id = self.context.aws_request_id
41+
return True
42+
43+
44+
def call(func, event, context, environment_variables={}):
3545
export_variables(environment_variables)
36-
e = json.loads(event)
37-
c = context.Context(timeout, arn_string, version_name)
38-
if library is not None:
39-
load_lib(library)
40-
request_id = uuid.uuid4()
4146

42-
return _runner(request_id, e, c, func)
47+
return _runner(func, event, context)
4348

4449

4550
def run(args):
4651
# set env vars if path to json file was given
4752
set_environment_variables(args.environment_variables)
4853

4954
e = event.read_event(args.event)
50-
c = context.Context(args.timeout, args.arn_string, args.version_name)
55+
c = context.Context(
56+
args.timeout,
57+
invoked_function_arn=args.arn_string,
58+
function_version=args.version_name)
5159
if args.library is not None:
5260
load_lib(args.library)
53-
request_id = uuid.uuid4()
54-
func = load(request_id, args.file, args.function)
55-
56-
(result, err_type) = _runner(request_id, e, c, func)
61+
func = load(c.aws_request_id, args.file, args.function)
62+
63+
(result, err_type) = _runner(func, e, c)
5764

5865
if err_type is not None:
5966
sys.exit(EXITCODE_ERR)
6067

6168

62-
def _runner(request_id, event, context, func):
69+
def _runner(func, event, context):
6370
logger = logging.getLogger()
71+
log_filter = ContextFilter(context)
72+
logger.addFilter(log_filter)
73+
6474
result = None
6575

6676
logger.info("Event: {}".format(event))
67-
68-
logger.info("START RequestId: {}".format(request_id))
77+
logger.info("START RequestId: {}".format(context.aws_request_id))
6978

7079
start_time = timeit.default_timer()
7180
result, err_type = execute(func, event, context)
7281
end_time = timeit.default_timer()
7382

74-
logger.info("END RequestId: {}".format(request_id))
75-
83+
logger.info("END RequestId: {}".format(context.aws_request_id))
7684
if type(result) is TimeoutException:
7785
logger.error("RESULT:\n{}".format(result))
7886
else:
7987
logger.info("RESULT:\n{}".format(result))
8088

8189
duration = "{0:.2f} ms".format((end_time - start_time) * 1000)
8290
logger.info("REPORT RequestId: {}\tDuration: {}".format(
83-
request_id, duration))
91+
context.aws_request_id, duration))
8492

8593
return (result, err_type)
8694

@@ -105,7 +113,7 @@ def execute(func, event, context):
105113
err_type = None
106114

107115
try:
108-
with time_limit(context.timeout):
116+
with time_limit(context._timeout_in_seconds):
109117
result = func(event, context.activate())
110118
except TimeoutException as err:
111119
result = err

0 commit comments

Comments
 (0)