Azure / Azure/azure-functions-python-worker

[Bug] Late OpenTelemetry context propagation in _handle__invocation_request

Open
#1,626 0 comments 0 reactions 0 assignees View on GitHub
area:python-functions bug python
Dominant language
Python
Stars
357
Forks
116
Avg merge
32m
Merged PRs (30d)
1

Description

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.