Skip to content

Commit 0337cb7

Browse files
authored
Add timestamp in the received invocation logs (#1378)
* adding timestamp to invocation req logs * fixing / adding tests * fixing tests for 3.7 * consolidate tests
1 parent f269c09 commit 0337cb7

File tree

2 files changed

+80
-52
lines changed

2 files changed

+80
-52
lines changed

azure_functions_worker/dispatcher.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from asyncio import BaseEventLoop
1717
from logging import LogRecord
1818
from typing import List, Optional
19+
from datetime import datetime
1920

2021
import grpc
2122

@@ -431,6 +432,7 @@ async def _handle__function_load_request(self, request):
431432
exception=self._serialize_exception(ex))))
432433

433434
async def _handle__invocation_request(self, request):
435+
invocation_time = datetime.utcnow()
434436
invoc_request = request.invocation_request
435437
invocation_id = invoc_request.invocation_id
436438
function_id = invoc_request.function_id
@@ -452,7 +454,8 @@ async def _handle__invocation_request(self, request):
452454
f'function ID: {function_id}',
453455
f'function name: {fi.name}',
454456
f'invocation ID: {invocation_id}',
455-
f'function type: {"async" if fi.is_async else "sync"}'
457+
f'function type: {"async" if fi.is_async else "sync"}',
458+
f'timestamp (UTC): {invocation_time}'
456459
]
457460
if not fi.is_async:
458461
function_invocation_logs.append(

tests/unittests/test_dispatcher.py

Lines changed: 76 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -304,15 +304,20 @@ async def test_sync_invocation_request_log(self):
304304
await self._check_if_function_is_ok(host)
305305
)
306306

307-
mock_logger.info.assert_any_call(
308-
'Received FunctionInvocationRequest, '
309-
f'request ID: {request_id}, '
310-
f'function ID: {func_id}, '
311-
f'function name: {func_name}, '
312-
f'invocation ID: {invoke_id}, '
313-
'function type: sync, '
314-
f'sync threadpool max workers: {self._default_workers}'
315-
)
307+
logs, _ = mock_logger.info.call_args
308+
self.assertRegex(logs[0],
309+
'Received FunctionInvocationRequest, '
310+
f'request ID: {request_id}, '
311+
f'function ID: {func_id}, '
312+
f'function name: {func_name}, '
313+
f'invocation ID: {invoke_id}, '
314+
'function type: sync, '
315+
r'timestamp \(UTC\): '
316+
r'(\d{4}-\d{2}-\d{2} '
317+
r'\d{2}:\d{2}:\d{2}.\d{6}), '
318+
'sync threadpool max workers: '
319+
f'{self._default_workers}'
320+
)
316321

317322
async def test_async_invocation_request_log(self):
318323
with patch('azure_functions_worker.dispatcher.logger') as mock_logger:
@@ -323,14 +328,18 @@ async def test_async_invocation_request_log(self):
323328
await self._check_if_async_function_is_ok(host)
324329
)
325330

326-
mock_logger.info.assert_any_call(
327-
'Received FunctionInvocationRequest, '
328-
f'request ID: {request_id}, '
329-
f'function ID: {func_id}, '
330-
f'function name: {func_name}, '
331-
f'invocation ID: {invoke_id}, '
332-
'function type: async'
333-
)
331+
logs, _ = mock_logger.info.call_args
332+
self.assertRegex(logs[0],
333+
'Received FunctionInvocationRequest, '
334+
f'request ID: {request_id}, '
335+
f'function ID: {func_id}, '
336+
f'function name: {func_name}, '
337+
f'invocation ID: {invoke_id}, '
338+
'function type: async, '
339+
r'timestamp \(UTC\): '
340+
r'(\d{4}-\d{2}-\d{2} '
341+
r'\d{2}:\d{2}:\d{2}.\d{6})'
342+
)
334343

335344
async def test_sync_invocation_request_log_threads(self):
336345
os.environ.update({PYTHON_THREADPOOL_THREAD_COUNT: '5'})
@@ -342,15 +351,19 @@ async def test_sync_invocation_request_log_threads(self):
342351
await self._check_if_function_is_ok(host)
343352
)
344353

345-
mock_logger.info.assert_any_call(
346-
'Received FunctionInvocationRequest, '
347-
f'request ID: {request_id}, '
348-
f'function ID: {func_id}, '
349-
f'function name: {func_name}, '
350-
f'invocation ID: {invoke_id}, '
351-
'function type: sync, '
352-
'sync threadpool max workers: 5'
353-
)
354+
logs, _ = mock_logger.info.call_args
355+
self.assertRegex(logs[0],
356+
'Received FunctionInvocationRequest, '
357+
f'request ID: {request_id}, '
358+
f'function ID: {func_id}, '
359+
f'function name: {func_name}, '
360+
f'invocation ID: {invoke_id}, '
361+
'function type: sync, '
362+
r'timestamp \(UTC\): '
363+
r'(\d{4}-\d{2}-\d{2} '
364+
r'\d{2}:\d{2}:\d{2}.\d{6}), '
365+
'sync threadpool max workers: 5'
366+
)
354367

355368
async def test_async_invocation_request_log_threads(self):
356369
os.environ.update({PYTHON_THREADPOOL_THREAD_COUNT: '4'})
@@ -362,14 +375,18 @@ async def test_async_invocation_request_log_threads(self):
362375
await self._check_if_async_function_is_ok(host)
363376
)
364377

365-
mock_logger.info.assert_any_call(
366-
'Received FunctionInvocationRequest, '
367-
f'request ID: {request_id}, '
368-
f'function ID: {func_id}, '
369-
f'function name: {func_name}, '
370-
f'invocation ID: {invoke_id}, '
371-
'function type: async'
372-
)
378+
logs, _ = mock_logger.info.call_args
379+
self.assertRegex(logs[0],
380+
'Received FunctionInvocationRequest, '
381+
f'request ID: {request_id}, '
382+
f'function ID: {func_id}, '
383+
f'function name: {func_name}, '
384+
f'invocation ID: {invoke_id}, '
385+
'function type: async, '
386+
r'timestamp \(UTC\): '
387+
r'(\d{4}-\d{2}-\d{2} '
388+
r'\d{2}:\d{2}:\d{2}.\d{6})'
389+
)
373390

374391
async def test_sync_invocation_request_log_in_placeholder_threads(self):
375392
with patch('azure_functions_worker.dispatcher.logger') as mock_logger:
@@ -383,15 +400,19 @@ async def test_sync_invocation_request_log_in_placeholder_threads(self):
383400
await self._check_if_function_is_ok(host)
384401
)
385402

386-
mock_logger.info.assert_any_call(
387-
'Received FunctionInvocationRequest, '
388-
f'request ID: {request_id}, '
389-
f'function ID: {func_id}, '
390-
f'function name: {func_name}, '
391-
f'invocation ID: {invoke_id}, '
392-
'function type: sync, '
393-
'sync threadpool max workers: 5'
394-
)
403+
logs, _ = mock_logger.info.call_args
404+
self.assertRegex(logs[0],
405+
'Received FunctionInvocationRequest, '
406+
f'request ID: {request_id}, '
407+
f'function ID: {func_id}, '
408+
f'function name: {func_name}, '
409+
f'invocation ID: {invoke_id}, '
410+
'function type: sync, '
411+
r'timestamp \(UTC\): '
412+
r'(\d{4}-\d{2}-\d{2} '
413+
r'\d{2}:\d{2}:\d{2}.\d{6}), '
414+
'sync threadpool max workers: 5'
415+
)
395416

396417
async def test_async_invocation_request_log_in_placeholder_threads(self):
397418
with patch('azure_functions_worker.dispatcher.logger') as mock_logger:
@@ -405,14 +426,18 @@ async def test_async_invocation_request_log_in_placeholder_threads(self):
405426
await self._check_if_async_function_is_ok(host)
406427
)
407428

408-
mock_logger.info.assert_any_call(
409-
'Received FunctionInvocationRequest, '
410-
f'request ID: {request_id}, '
411-
f'function ID: {func_id}, '
412-
f'function name: {func_name}, '
413-
f'invocation ID: {invoke_id}, '
414-
'function type: async'
415-
)
429+
logs, _ = mock_logger.info.call_args
430+
self.assertRegex(logs[0],
431+
'Received FunctionInvocationRequest, '
432+
f'request ID: {request_id}, '
433+
f'function ID: {func_id}, '
434+
f'function name: {func_name}, '
435+
f'invocation ID: {invoke_id}, '
436+
'function type: async, '
437+
r'timestamp \(UTC\): '
438+
r'(\d{4}-\d{2}-\d{2} '
439+
r'\d{2}:\d{2}:\d{2}.\d{6})'
440+
)
416441

417442
async def _assert_workers_threadpool(self, ctrl, host,
418443
expected_worker_count):

0 commit comments

Comments
 (0)