Azure / Azure/azure-functions-python-worker

[Bug] Late OpenTelemetry context propagation in _handle__invocation_request

Đang mở
#1,626 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
area:python-functions bug python
Ngôn ngữ chính
Python
Star
357
Fork
116
Merge trung bình
32 phút
Pull request đã merge (30 ngày)
1

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
azure, python
Lĩnh vực
backend, observability-sre
Loại issue
Lỗi
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
55/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.