Azure / azure-functions-python-worker

Python worker for Azure Functions.
http://aka.ms/azurefunctions
MIT License
331 stars 100 forks source link

feat: OpenTelemetry support #1469

Closed gavin-aguiar closed 2 months ago

gavin-aguiar commented 2 months ago

Description

Added support for opentelemetry.

The worker does not take a dependency on opentelemetry but checks if the function app has opentelemetry. If it finds the libraries, it will setup the Otel configs needed.

Otel is configured separately for sync and async. For async functions, we can directly call configure_opentelemetry but for sync functions this method has to be called inside the ThreadPool executor which is used to run sync functions. This is because OpenTelemetry is thread specific and will only update the Span on the thread it is configured

Sample function app.

import azure.functions as func 
import requests
import logging
from azure.monitor.opentelemetry import configure_azure_monitor

configure_azure_monitor()

app = func.FunctionApp()

@app.route('call_bing', auth_level=func.AuthLevel.ANONYMOUS)
def call_bing(req: func.HttpRequest) -> func.HttpResponse:
    logging.info(f'Logging to app insights...')
    res = requests.get('https://www.bing.com')
    return func.HttpResponse(f"Bing.com: status_code = {res.status_code}", status_code=res.status_code)

Fixes #


PR information

Quality of Code and Contribution Guidelines

codecov-commenter commented 2 months ago

Codecov Report

Attention: Patch coverage is 45.16129% with 17 lines in your changes are missing coverage. Please review.

Project coverage is 46.16%. Comparing base (330783f) to head (ba14b63). Report is 4 commits behind head on dev.

:exclamation: Current head ba14b63 differs from pull request most recent head c170240. Consider uploading reports for the commit c170240 to get more accurate results

Files Patch % Lines
azure_functions_worker/dispatcher.py 43.33% 16 Missing and 1 partial :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## dev #1469 +/- ## =========================================== - Coverage 84.64% 46.16% -38.49% =========================================== Files 35 36 +1 Lines 2052 2307 +255 Branches 387 433 +46 =========================================== - Hits 1737 1065 -672 - Misses 232 1157 +925 - Partials 83 85 +2 ``` | [Flag](https://app.codecov.io/gh/Azure/azure-functions-python-worker/pull/1469/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Azure) | Coverage Δ | | |---|---|---| | [unittests](https://app.codecov.io/gh/Azure/azure-functions-python-worker/pull/1469/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Azure) | `46.16% <45.16%> (-38.44%)` | :arrow_down: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Azure#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.