Skip to content

Add timestamp in the received invocation logs #1378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion azure_functions_worker/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from asyncio import BaseEventLoop
from logging import LogRecord
from typing import List, Optional
from datetime import datetime

import grpc

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

async def _handle__invocation_request(self, request):
invocation_time = datetime.utcnow()
invoc_request = request.invocation_request
invocation_id = invoc_request.invocation_id
function_id = invoc_request.function_id
Expand All @@ -452,7 +454,8 @@ async def _handle__invocation_request(self, request):
f'function ID: {function_id}',
f'function name: {fi.name}',
f'invocation ID: {invocation_id}',
f'function type: {"async" if fi.is_async else "sync"}'
f'function type: {"async" if fi.is_async else "sync"}',
f'timestamp (UTC): {invocation_time}'
]
if not fi.is_async:
function_invocation_logs.append(
Expand Down
127 changes: 76 additions & 51 deletions tests/unittests/test_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,15 +304,20 @@ async def test_sync_invocation_request_log(self):
await self._check_if_function_is_ok(host)
)

mock_logger.info.assert_any_call(
'Received FunctionInvocationRequest, '
f'request ID: {request_id}, '
f'function ID: {func_id}, '
f'function name: {func_name}, '
f'invocation ID: {invoke_id}, '
'function type: sync, '
f'sync threadpool max workers: {self._default_workers}'
)
logs, _ = mock_logger.info.call_args
self.assertRegex(logs[0],
'Received FunctionInvocationRequest, '
f'request ID: {request_id}, '
f'function ID: {func_id}, '
f'function name: {func_name}, '
f'invocation ID: {invoke_id}, '
'function type: sync, '
r'timestamp \(UTC\): '
r'(\d{4}-\d{2}-\d{2} '
r'\d{2}:\d{2}:\d{2}.\d{6}), '
'sync threadpool max workers: '
f'{self._default_workers}'
)

async def test_async_invocation_request_log(self):
with patch('azure_functions_worker.dispatcher.logger') as mock_logger:
Expand All @@ -323,14 +328,18 @@ async def test_async_invocation_request_log(self):
await self._check_if_async_function_is_ok(host)
)

mock_logger.info.assert_any_call(
'Received FunctionInvocationRequest, '
f'request ID: {request_id}, '
f'function ID: {func_id}, '
f'function name: {func_name}, '
f'invocation ID: {invoke_id}, '
'function type: async'
)
logs, _ = mock_logger.info.call_args
self.assertRegex(logs[0],
'Received FunctionInvocationRequest, '
f'request ID: {request_id}, '
f'function ID: {func_id}, '
f'function name: {func_name}, '
f'invocation ID: {invoke_id}, '
'function type: async, '
r'timestamp \(UTC\): '
r'(\d{4}-\d{2}-\d{2} '
r'\d{2}:\d{2}:\d{2}.\d{6})'
)

async def test_sync_invocation_request_log_threads(self):
os.environ.update({PYTHON_THREADPOOL_THREAD_COUNT: '5'})
Expand All @@ -342,15 +351,19 @@ async def test_sync_invocation_request_log_threads(self):
await self._check_if_function_is_ok(host)
)

mock_logger.info.assert_any_call(
'Received FunctionInvocationRequest, '
f'request ID: {request_id}, '
f'function ID: {func_id}, '
f'function name: {func_name}, '
f'invocation ID: {invoke_id}, '
'function type: sync, '
'sync threadpool max workers: 5'
)
logs, _ = mock_logger.info.call_args
self.assertRegex(logs[0],
'Received FunctionInvocationRequest, '
f'request ID: {request_id}, '
f'function ID: {func_id}, '
f'function name: {func_name}, '
f'invocation ID: {invoke_id}, '
'function type: sync, '
r'timestamp \(UTC\): '
r'(\d{4}-\d{2}-\d{2} '
r'\d{2}:\d{2}:\d{2}.\d{6}), '
'sync threadpool max workers: 5'
)

async def test_async_invocation_request_log_threads(self):
os.environ.update({PYTHON_THREADPOOL_THREAD_COUNT: '4'})
Expand All @@ -362,14 +375,18 @@ async def test_async_invocation_request_log_threads(self):
await self._check_if_async_function_is_ok(host)
)

mock_logger.info.assert_any_call(
'Received FunctionInvocationRequest, '
f'request ID: {request_id}, '
f'function ID: {func_id}, '
f'function name: {func_name}, '
f'invocation ID: {invoke_id}, '
'function type: async'
)
logs, _ = mock_logger.info.call_args
self.assertRegex(logs[0],
'Received FunctionInvocationRequest, '
f'request ID: {request_id}, '
f'function ID: {func_id}, '
f'function name: {func_name}, '
f'invocation ID: {invoke_id}, '
'function type: async, '
r'timestamp \(UTC\): '
r'(\d{4}-\d{2}-\d{2} '
r'\d{2}:\d{2}:\d{2}.\d{6})'
)

async def test_sync_invocation_request_log_in_placeholder_threads(self):
with patch('azure_functions_worker.dispatcher.logger') as mock_logger:
Expand All @@ -383,15 +400,19 @@ async def test_sync_invocation_request_log_in_placeholder_threads(self):
await self._check_if_function_is_ok(host)
)

mock_logger.info.assert_any_call(
'Received FunctionInvocationRequest, '
f'request ID: {request_id}, '
f'function ID: {func_id}, '
f'function name: {func_name}, '
f'invocation ID: {invoke_id}, '
'function type: sync, '
'sync threadpool max workers: 5'
)
logs, _ = mock_logger.info.call_args
self.assertRegex(logs[0],
'Received FunctionInvocationRequest, '
f'request ID: {request_id}, '
f'function ID: {func_id}, '
f'function name: {func_name}, '
f'invocation ID: {invoke_id}, '
'function type: sync, '
r'timestamp \(UTC\): '
r'(\d{4}-\d{2}-\d{2} '
r'\d{2}:\d{2}:\d{2}.\d{6}), '
'sync threadpool max workers: 5'
)

async def test_async_invocation_request_log_in_placeholder_threads(self):
with patch('azure_functions_worker.dispatcher.logger') as mock_logger:
Expand All @@ -405,14 +426,18 @@ async def test_async_invocation_request_log_in_placeholder_threads(self):
await self._check_if_async_function_is_ok(host)
)

mock_logger.info.assert_any_call(
'Received FunctionInvocationRequest, '
f'request ID: {request_id}, '
f'function ID: {func_id}, '
f'function name: {func_name}, '
f'invocation ID: {invoke_id}, '
'function type: async'
)
logs, _ = mock_logger.info.call_args
self.assertRegex(logs[0],
'Received FunctionInvocationRequest, '
f'request ID: {request_id}, '
f'function ID: {func_id}, '
f'function name: {func_name}, '
f'invocation ID: {invoke_id}, '
'function type: async, '
r'timestamp \(UTC\): '
r'(\d{4}-\d{2}-\d{2} '
r'\d{2}:\d{2}:\d{2}.\d{6})'
)

async def _assert_workers_threadpool(self, ctrl, host,
expected_worker_count):
Expand Down