-
Notifications
You must be signed in to change notification settings - Fork 250
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
Unable to use AzureLogHandler into a GUnicorn/Flask app #957
Comments
See this comment for something that is related. It seems that gunicorn uses os.fork to spawn it's child processes, so the worker thread (in your parent process) that is responsible for exporting your telemetry is not copied. You can either try having the initialization logic ( |
Hi @lzchen, thanks for your suggestion.
|
I believe Also, can you specify exactly what you do when you are testing creating a new logger instance per process/thread and what you are seeing in your App insights telemetry to indicate that it is not working? |
Thanks @lzchen, trying to create a new logger for each pid (process or thread) has worked. Instead of having a global |
yep
I've tried with GU_THREADS_NUM=3 and GU_WORK_NUM=1 and GU_WORK_NUM=3 and GU_THREADS_NUM=1
|
Oh that is useful information. Appreciate the help. I'm glad you got it working! Is this issue resolved? |
Hi @lzchen, not sure if I can consider the issue resolved. The mileage may vary :) |
@freedev Just an FYI: Take a look at this link. It seemed like using a multiprocessing queue in the exporter worked. Could you possibly test your code using this? |
Yep, I will.
Again, yes. I remember this link comes from the comment you gave me in first instance and now, with hindsight, it seems very helpful. |
That's great. Are you able to mark the ticket submitted to Azure devops as resolved? If this is a new feature, we can continue the conversation here or in the new feature request. |
Hi both, stumbled on this as i am facing the same issue. However i noticed removing the --preload flag on my gunicorn command seems to allow logging to App Insights. @freedev have you seen this also? More info on --preload: The --preload flag tells Gunicorn to "load the app before forking the worker processes". By doing so, each worker is "given a copy of the app, already instantiated by the Master, rather than instantiating the app itself". This means the following code only executes once in the Master process: |
Describe your environment.
We want to export Python logs to Azure Monitor.
We have a simple python gunicorn/flask webapp, we are unable to execute
logger.info()
(or other log methods) and successfully send the message to Application Insights.It seems that when AzureLogHandler runs into flask/gunicorn only the main process use the handler, child thread/processes are unable to send log lines to Azure Application Insights.
For example, when http request is received by a flask/gunicorn app, should be unable to log through the handler, but nothing happens. It seems that having HTTP requests handled into child processes (threads or forks) in some way is related to this behaviour.
Current environment is running into a docker container with:
Steps to reproduce.
I have created a minimal project on github:
Change with your own Instrumentation Key (00000000-0000-0000-0000-000000000000)
run-docker.sh script will start a simple web app running into a docker instance and listening on port 5000.
Once the docker instance is started there is a first log message written successfully by main process.
After we expect that each http request that comes to flask application should be logged through the AzureLogHandler but none arrives to Application Insights.
try with:
What is the expected behavior?
We expect to be able to use AzureLogHandler and write into Application Insights for each http request received;
Each http request that comes to flask application should be logged through the AzureLogHandler.
For example: once the application is started with:
Then open Azure Portal -> Application Insights -> Monitoring - Logs
Then execute the query:
You should be able to see the line with message:
But even a message for each request handled:
Please note that messages for request handling are missing.
What is the actual behavior?
Only the log lines written by main process appears into Application Insights.
Child process/threads are unable to send log lines to Application insights.
To doublecheck try to start flask without gunicorn:
In this way I see each http request being logged successfully.
In our context not have gunicorn is not acceptable this solution.
We have tried to start the flask without gunicorn just to have the proof that AzureLogHandler does not work in a multithreaded/multiprocess environment.
Additional context.
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: