Azure / Azure/azure-functions-python-worker

[Bug] Late OpenTelemetry context propagation in _handle__invocation_request

Aperta
#1,626 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
area:python-functions bug python
Lingua principale
Python
Stelle
357
Fork
116
Merge medio
32m
PR unite (30g)
1

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
azure, python
Ambito
backend, observability-sre
Tipo di issue
Bug
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
55/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.