Azure / Azure/azure-functions-python-worker

[Bug] Late OpenTelemetry context propagation in _handle__invocation_request

オープン
#1,626 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る
area:python-functions bug python
主要言語
Python
スター
357
フォーク
116
平均マージ
32分
マージ済み PR(30日)
1

説明

### Expected Behavior

Context should be propagated as soon as possible to make all logs generated by function invocation have a valid _Operation Id_ in Application Insights.
Thus:
1. `self.configure_opentelemetry(fi_context)` should be executed in `_handle__invocation_request` right before first log is emitted at [line 617](https://github.com/Azure/azure-functions-python-worker/tree/ed26bd064067caa390dc6d32b84112f1dd0a4ff9/azure_functions_worker/dispatcher.py#L617)
2. consequently, [`fi_context` initialization](https://github.com/Azure/azure-functions-python-worker/blob/ed26bd064067caa390dc6d32b84112f1dd0a4ff9/azure_functions_worker/dispatcher.py#L651) should be moved up, ideally at line 602

### Actual Behavior

If OpenTelemetry is available and initialized, first log emitted by `_handle__invocation_request` is not properly recorded as part of function workflow, i.e.: in Application Insights has a blank _Operation Id_.
This is because of late context propagation (at [line 666](https://github.com/Azure/azure-functions-python-worker/blob/ed26bd064067caa390dc6d32b84112f1dd0a4ff9/azure_functions_worker/dispatcher.py#L666)) which happens after first log is emitted at [line 617](https://github.com/Azure/azure-functions-python-worker/tree/ed26bd064067caa390dc6d32b84112f1dd0a4ff9/azure_functions_worker/dispatcher.py#L617)

### Steps to Reproduce

1. Create a new Azure Function
2. Create a new trigger and write code to emit some logs
3. In `local.settings.json` add `PYTHON_ENABLE_OPENTELEMETRY=1` and `APPLICATIONINSIGHTS_CONNECTION_STRING=your-connection-string`
4. Run the trigger created at step 2.
5. Check in Application Insights that first log emitted by function invocation (i.e.: `Received function invocation request ...`) has a blank `operation_id`

### requirements.txt file

```shell
azure-functions
azure-monitor-opentelemetry
```

### Where are you facing this problem?

Local - Core Tools

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

Inspect azure_functions_worker/dispatcher.py and _handle__invocation_request around lines 602, 617, 651, and 666. Move fi_context initialization and configure_opentelemetry before the first log, then reproduce with PYTHON_ENABLE_OPENTELEMETRY=1 and Application Insights configured. Done means the first “Received function invocation request ...” log has a valid operation_id.

索引モデルが issue の本文から書いたものです。

評価

技術スタック
azure, python
領域
backend, observability-sre
issue の種類
バグ
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
55/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。