Azure / Azure/azure-functions-python-worker

[Bug] Late OpenTelemetry context propagation in _handle__invocation_request

Offen
#1,626 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
area:python-functions bug python
Vorherrschende Sprache
Python
Sterne
357
Forks
116
Ø Merge
32 Min.
Gemergte PRs (30 T.)
1

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

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.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
azure, python
Bereich
backend, observability-sre
Issue-Typ
Bug
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
55/100

Neue Issues direkt in Ihr Postfach

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