azure-functions-python-worker icon indicating copy to clipboard operation
azure-functions-python-worker copied to clipboard

[Bug] Late OpenTelemetry context propagation in _handle__invocation_request

Open MatteoMeil opened this issue 11 months ago • 0 comments

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
  2. consequently, fi_context initialization 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) which happens after first log is emitted at line 617

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

azure-functions
azure-monitor-opentelemetry

Where are you facing this problem?

Local - Core Tools

MatteoMeil avatar Jan 07 '25 17:01 MatteoMeil