[Bug] context.thread_local_storage.invocation_id does not correlate thread logs in Application Insights on Python 3.13
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 45/100
- Tipo de issue
- Error
- Claridad
- Bastante claro
- Estado de actividad
- Tranquilo
- Stack tecnológico
- azure, python
- Área
- backend, observability-sre
Línea de trabajo
Comienza reproduciendo el disparador HTTP async con asyncio.to_thread en Python 3.12 y 3.13; después, inspecciona el workaround de invocation_id_cv en azure_functions_runtime.utils.executor y el uso de context.thread_local_storage.invocation_id. Se considera terminado cuando los logs de los hilos en segundo plano aparecen en Application Insights con el mismo InvocationId en Python 3.13 mediante una API compatible.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Expected Behavior
According to the docs, setting context.thread_local_storage.invocation_id
inside a background thread enables log correlation for that thread:
https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-python#log-custom-telemetry
After calling:
context.thread_local_storage.invocation_id = context.invocation_id
...all logging.info() calls from that thread should appear in Application
Insights correlated to the function invocation (same InvocationId).
Actual Behavior
On Python 3.13, [THREAD] logs are completely absent from Application Insights.
[MAIN] logs appear correctly. The identical code works on Python 3.12.
Python 3.12 ✅
"Logging from main handler" → appears, InvocationId=37b57bfd-...
"Logging from a background thread" → appears, InvocationId=37b57bfd-...
Python 3.13 ❌
"Logging from main handler" → appears, InvocationId=48b27079-...
"Logging from a background thread" → ABSENT from App Insights entirely
Steps to Reproduce
- Deploy Azure Function App with Python 3.13, Azure Functions Runtime v4, Dedicated plan
- Use the code below (async HTTP trigger + asyncio.to_thread)
- Trigger the function via HTTP
- Check Application Insights traces — background thread log is missing
- Redeploy the same code with Python 3.12 — background thread log appears correctly
Relevant code being tried
import azure.functions as func
import logging
import asyncio
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
def work(context: func.Context):
context.thread_local_storage.invocation_id = context.invocation_id
logging.info("Logging from a background thread")
@app.route(route="test")
async def test_async(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
await asyncio.to_thread(work, context)
logging.info("Logging from main handler")
return func.HttpResponse("ok")
Relevant log output
requirements.txt file
azure-functions
Where are you facing this problem?
Production Environment (explain below)
Function app name
No response
Additional Information
Root cause: azure-functions-runtime (used on Python 3.13) has its own separate
threading.local object for log routing. context.thread_local_storage points to
azure-functions-worker's threading.local — a different object that the new
runtime's logging proxy never reads.
Workaround (confirmed working on Python 3.13):
from azure_functions_runtime.utils.executor import invocation_id_cv
def work():
logging.info("Logging from a background thread")
@app.route(route="test")
async def test_async(req, context):
invocation_id_cv.set(context.invocation_id) # copied by asyncio.to_thread
await asyncio.to_thread(work)
asyncio.to_thread copies ContextVars automatically, so the proxy finds the
invocation_id correctly.
Note: invocation_id_cv is an internal API — a public API for this use case
would be appreciated.
- Lenguaje dominante
- Python
- Estrellas
- 357
- Forks
- 117
- Merge medio
- 13 d 12 h
- PR fusionados (30 d)
- 2
Preparar el entorno
Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de Azure/azure-functions-python-worker
-
[Bug] ASGI cookie conversion serializes an absent Domain attribute and breaks __Host- cookiesPosiblemente ocupada @tharun06 la tomó hace 23 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
-
Dificultad 5/5 Más de una semana Aptitud para principiantes 28/100
Azure/azure-functions-python-worker#1908 · 2 comentarios ·
-
Dificultad 4/5 3-5 días Aptitud para principiantes 55/100
Azure/azure-functions-python-worker#1906 · 1 comentario ·
-
Dificultad 5/5 Más de una semana Aptitud para principiantes 35/100
-
[Python 3.14 proxy worker] Missing sys.modules["__main__"] breaks multiprocessing spawnPosiblemente ocupada Un pull request vinculado a esta issue está abierto o ya se fusionó. Abierto
Dificultad 4/5 3-5 días Aptitud para principiantes 55/100
Todos los issues de Azure/azure-functions-python-worker
Issues similares
-
customer-reported question
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
Azure/azure-sdk-for-python#49548 · 1 comentario · 1 reacción ·
Los mantenedores suelen responder en 1 día
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 88/100
btclib-org/btclib-wallet#409 ·
Los mantenedores suelen responder en 1 día
-
Phase 2 PSA Capability & Career Profile
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
bcgov/digital-journeys#2310 ·
-
bug good first issue
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
fastmachinelearning/hls4ml#1582 ·
Los mantenedores suelen responder en 3 días
-
Sort modelsAbiertoenhancement good first issue
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
Los mantenedores suelen responder en 1 día