Python : Logs are not coming in App Insight for async function using eventloop
See original GitHub issueI am using azure function for one of our python function. We are using eventloop as described in document.
Below is sample of __init__.py
from typing import List
import logging
import json
import asyncio
import azure.functions as func
from .somepackage import SomeClass
async def main(events: List[func.EventHubEvent]): # pragma: no cover
"""
Starter function for the azure functions
@param events:
@return:
"""
loop = asyncio.get_event_loop()
tasks = []
logging.info("Got %s events" % len(events))
for idx, event in enumerate(events):
event_body = json.loads(event.get_body().decode('utf8'))
properties = event.metadata['PropertiesArray'][idx]
logging.info("Got Event : %s with properties %s" % (event_body, properties))
try:
tasks.append(
asyncio.create_task(wrapper_function(loop, event_body, properties)))
except Exception as e:
logging.exception("Error occurred while processing surface event trigger")
if tasks:
result, _ = await asyncio.wait(tasks)
logging.info("Function : event processing done")
async def wrapper_function(event_loop, event_body, properties):
some_obj = SomeClass()
result = await event_loop.run_in_executor(None, some_obj.run, event_body, properties)
return result
and this is content of somepackage.py file,
import logging
class SomeClass(object):
def __init__(self):
logging.info("Object created")
def run(self, event_body, props):
logging.info("event body is %s" % event_body)
logging.info("Properties are %s" % props)
The logging statements from run function are not coming to app insight which is connected to our azure functions. If I use normal function instead of async eventloop it push the logs to app insight.
Please let me know if more details required on this.
Thanks
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:10 (5 by maintainers)
Top Results From Across the Web
python aio libray logs and async method logs not showing in ...
With this all aio client logs and my own logs are getting logged in app ... async mehtod from main() by explicitly creating...
Read more >Request logging to Application insight does not get through ...
I have a python/fastapi app on Azure function that I tried for days to connect with Application insights with the recommended opencensus ...
Read more >Python Asyncio: The Complete Guide
Asyncio allows us to use asynchronous programming with coroutine-based ... The event loop is the core of every asyncio application.
Read more >Async for Data Scientists — Don't Block the Event Loop
Just to be clear, this story is not another async tutorial. ... We begin our event loop blocking test with two functions:.
Read more >Python Concurrency: Making sense of asyncio - Educative.io
There is no such problem with asynchronous tasks. ... The iterator exposes __next__() method in Python 3 and next() in Python 2.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@amitnanaware thanks for raising this issue! We have investigated and root caused the problem. Essentially the host checks if invocation_id field (id generated by the host to identify each call to your function in function app.) is present in generated LogRecord object which wraps your log message, if yes then only it will log messages to AppInsights. For the missing logs you can’t find in AppInsights is because invocation_id field is missing in LogRecord objects of your log messages.
Currently for the case if a function is a single threaded blocking function, invocation id is only set to thread local variable(_invocation_id_local) in worker managed executor’s threads. (https://github.com/Azure/azure-functions-python-worker/blob/dev/azure_functions_worker/dispatcher.py#L644-L648). We do not set invocation_id to customer managed executor’s threads, and even trying to set it, that _invocation_id_local variable is not accessible outside worker module and is not considered good practice.
(more for details at https://github.com/Azure/azure-functions-python-worker/blob/dev/azure_functions_worker/dispatcher.py#L742-L754 and https://github.com/Azure/azure-functions-host/blob/dev/src/WebJobs.Script.Grpc/Channel/GrpcWorkerChannel.cs#L696-L732).
Unfortunately sorry, there is no quick mitigation at current moment. We are tracking it internally and will continue investigating/working on good solutions as soon as possible.
@YunchuWang do you know if this issue has been fixed or is still outstanding? I’m seeing similar symptoms (logs from an async function in an Azure Function have no effect, logs on the main thread show up in Application Insights as expected)