Azure / Azure/azure-functions-python-worker

[Bug] context.thread_local_storage.invocation_id does not correlate thread logs in Application Insights on Python 3.13

Offen
#1,860 0 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen
bug python
Vorherrschende Sprache
Python
Sterne
357
Forks
116
Ø Merge
32 Min.
Gemergte PRs (30 T.)
1

Beschreibung

### 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

1. Deploy Azure Function App with Python 3.13, Azure Functions Runtime v4, Dedicated plan
2. Use the code below (async HTTP trigger + asyncio.to_thread)
3. Trigger the function via HTTP
4. Check Application Insights traces — background thread log is missing
5. Redeploy the same code with Python 3.12 — background thread log appears correctly

### Relevant code being tried

```shell
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

```shell

```

### requirements.txt file

```shell
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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Beginne damit, den async HTTP-Trigger mit asyncio.to_thread unter Python 3.12 und 3.13 zu reproduzieren. Untersuche anschließend den invocation_id_cv-Workaround in azure_functions_runtime.utils.executor und die Verwendung von context.thread_local_storage.invocation_id. Erledigt ist es, wenn Logs aus Hintergrund-Threads in Application Insights mit derselben InvocationId unter Python 3.13 über eine unterstützte API erscheinen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
azure, python
Bereich
backend, observability-sre
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.