Skip to content

fix: honor context.thread_local_storage invocation id in proxy worker logging - #1943

Open
alexcastgonz (pcbeingused333) wants to merge 2 commits into
Azure:devfrom
pcbeingused333:fix/proxy-thread-local-storage-invocation-id
Open

alexcastgonz (pcbeingused333) wants to merge 2 commits into
Azure:devfrom
pcbeingused333:fix/proxy-thread-local-storage-invocation-id

Conversation

@pcbeingused333

Copy link
Copy Markdown

Description

Fixes #1860

The documented pattern for correlating logs from customer-created threads is to assign context.thread_local_storage.invocation_id inside the thread. context.thread_local_storage is the runtime's own threading.local (bindings.context._invocation_id_local), but proxy_worker.dispatcher.get_current_invocation_id() only checked the asyncio task, the runtime ContextVar, the thread registry and the proxy's separate _invocation_id_local. In an async handler that offloads work with asyncio.to_thread, none of those hold the ID, so the log record had no invocation ID and was dropped from correlation.

get_current_invocation_id() now also consults the loaded runtime's bindings.context._invocation_id_local before falling back to the proxy's own local. Existing lookup order is unchanged, so the native path that already sets the ContextVar is unaffected; the lookup resolves the module from the loaded runtime package, so it works for both azure_functions_runtime and azure_functions_runtime_v1.

Testing

Added test_get_current_invocation_id_runtime_thread_local_storage, which assigns the ID on a stand-in runtime _invocation_id_local from inside asyncio.to_thread and expects get_current_invocation_id() to return it. It fails without the change and passes with it; the rest of tests/unittest_proxy/test_dispatcher.py passes (40 passed).


Pull Request Checklist

Host-Worker Contract

  • Does this PR impact the host-worker contract (e.g., gRPC messages, shared interfaces)?
    • If no, please explain why: only the proxy worker's internal invocation-ID lookup for log records changes; no gRPC messages or shared interfaces are touched.

Worker Execution Logic

  • Does this PR affect worker execution logic (e.g., function invocation, bindings, lifecycle)?

Logging correlation only; invocation, bindings and lifecycle are unchanged.

Python Version Coverage

  • Does this change apply to all supported worker models?
  • If no, please explain why: the regression is in proxy_worker (Python 3.13-3.14). azure_functions_worker (<= 3.12) already correlates these logs, and r2p2 (>= 3.15) already consults the runtime's bindings.context._invocation_id_local in bridge.py; this change brings proxy_worker in line with it.

Programming Model Compatibility (proxy_worker and r2p2)

  • Does this change apply to both:
    • V1 programming model (runtimes/v1)?
    • V2 programming model (runtimes/v2)?
  • Explanation (if limited to one model): the lookup resolves bindings.context._invocation_id_local from whichever runtime package is loaded, so it covers both.

AI assistance: this change and its regression test were prepared using Claude Code.

… logging

Customer threads (e.g. asyncio.to_thread) that follow the documented
context.thread_local_storage.invocation_id pattern were not correlated
because the proxy only consulted its own thread-local.
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant