-
Notifications
You must be signed in to change notification settings - Fork 418
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(azure_functions): add azure functions integration (#11474)
This PR adds an integration for tracing the [azure-functions](https://pypi.org/project/azure-functions/) package. ### Additional Notes: - This change only supports the [v2 programming model](https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-python?tabs=get-started%2Casgi%2Capplication-level&pivots=python-mode-decorators). If there are enough requests for the v1 programming model we can add tracing in a future PR - This change only supports tracing [Http triggers](https://github.com/Azure/azure-functions-python-library/blob/dd4fac4db0ff4ca3cd01d314a0ddf280aa59813e/azure/functions/decorators/function_app.py#L462). Tracing for other triggers will be added in future PRs - Azure Functions package currently supports Python versions `3.7` to `3.11` (no `3.12` support at the moment) - Builds off the integration work started by @gord02 in #9726 - Dockerfile changes to testrunner made in #11617 and #11609 * `mariadb` install was broken in the testrunner image * [azure-functions-core-tools](https://github.com/Azure/azure-functions-core-tools) package must be installed on the test runner for tests to work - Version pinned to [4.0.6280](https://github.com/Azure/azure-functions-core-tools/releases/tag/4.0.6280) due to some issues with the most recent versions - Package only supported on `linux/amd64` architecture ## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
- Loading branch information
1 parent
de9bc48
commit beb87f6
Showing
23 changed files
with
589 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# | ||
# This file is autogenerated by pip-compile with Python 3.8 | ||
# by the following command: | ||
# | ||
# pip-compile --allow-unsafe --no-annotate .riot/requirements/1337ee3.in | ||
# | ||
attrs==24.2.0 | ||
azure-functions==1.21.3 | ||
certifi==2024.8.30 | ||
charset-normalizer==3.4.0 | ||
coverage[toml]==7.6.1 | ||
exceptiongroup==1.2.2 | ||
hypothesis==6.45.0 | ||
idna==3.10 | ||
iniconfig==2.0.0 | ||
mock==5.1.0 | ||
opentracing==2.4.0 | ||
packaging==24.2 | ||
pluggy==1.5.0 | ||
pytest==8.3.3 | ||
pytest-cov==5.0.0 | ||
pytest-mock==3.14.0 | ||
requests==2.32.3 | ||
sortedcontainers==2.4.0 | ||
tomli==2.1.0 | ||
urllib3==2.2.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# | ||
# This file is autogenerated by pip-compile with Python 3.11 | ||
# by the following command: | ||
# | ||
# pip-compile --allow-unsafe --no-annotate .riot/requirements/14b54db.in | ||
# | ||
attrs==24.2.0 | ||
azure-functions==1.21.3 | ||
certifi==2024.8.30 | ||
charset-normalizer==3.4.0 | ||
coverage[toml]==7.6.8 | ||
hypothesis==6.45.0 | ||
idna==3.10 | ||
iniconfig==2.0.0 | ||
mock==5.1.0 | ||
opentracing==2.4.0 | ||
packaging==24.2 | ||
pluggy==1.5.0 | ||
pytest==8.3.3 | ||
pytest-cov==6.0.0 | ||
pytest-mock==3.14.0 | ||
requests==2.32.3 | ||
sortedcontainers==2.4.0 | ||
urllib3==2.2.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# | ||
# This file is autogenerated by pip-compile with Python 3.10 | ||
# by the following command: | ||
# | ||
# pip-compile --allow-unsafe --no-annotate .riot/requirements/1e62aea.in | ||
# | ||
attrs==24.2.0 | ||
azure-functions==1.21.3 | ||
certifi==2024.8.30 | ||
charset-normalizer==3.4.0 | ||
coverage[toml]==7.6.8 | ||
exceptiongroup==1.2.2 | ||
hypothesis==6.45.0 | ||
idna==3.10 | ||
iniconfig==2.0.0 | ||
mock==5.1.0 | ||
opentracing==2.4.0 | ||
packaging==24.2 | ||
pluggy==1.5.0 | ||
pytest==8.3.3 | ||
pytest-cov==6.0.0 | ||
pytest-mock==3.14.0 | ||
requests==2.32.3 | ||
sortedcontainers==2.4.0 | ||
tomli==2.1.0 | ||
urllib3==2.2.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# | ||
# This file is autogenerated by pip-compile with Python 3.7 | ||
# by the following command: | ||
# | ||
# pip-compile --allow-unsafe --config=pyproject.toml --no-annotate --resolver=backtracking .riot/requirements/73109d5.in | ||
# | ||
attrs==24.2.0 | ||
azure-functions==1.21.3 | ||
certifi==2024.8.30 | ||
charset-normalizer==3.4.0 | ||
coverage[toml]==7.2.7 | ||
exceptiongroup==1.2.2 | ||
hypothesis==6.45.0 | ||
idna==3.10 | ||
importlib-metadata==6.7.0 | ||
iniconfig==2.0.0 | ||
mock==5.1.0 | ||
opentracing==2.4.0 | ||
packaging==24.0 | ||
pluggy==1.2.0 | ||
pytest==7.4.4 | ||
pytest-cov==4.1.0 | ||
pytest-mock==3.11.1 | ||
requests==2.31.0 | ||
sortedcontainers==2.4.0 | ||
tomli==2.0.1 | ||
typing-extensions==4.7.1 | ||
urllib3==2.0.7 | ||
zipp==3.15.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# | ||
# This file is autogenerated by pip-compile with Python 3.9 | ||
# by the following command: | ||
# | ||
# pip-compile --allow-unsafe --no-annotate .riot/requirements/c2420c2.in | ||
# | ||
attrs==24.2.0 | ||
azure-functions==1.21.3 | ||
certifi==2024.8.30 | ||
charset-normalizer==3.4.0 | ||
coverage[toml]==7.6.8 | ||
exceptiongroup==1.2.2 | ||
hypothesis==6.45.0 | ||
idna==3.10 | ||
iniconfig==2.0.0 | ||
mock==5.1.0 | ||
opentracing==2.4.0 | ||
packaging==24.2 | ||
pluggy==1.5.0 | ||
pytest==8.3.3 | ||
pytest-cov==6.0.0 | ||
pytest-mock==3.14.0 | ||
requests==2.32.3 | ||
sortedcontainers==2.4.0 | ||
tomli==2.1.0 | ||
urllib3==2.2.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
""" | ||
The azure_functions integration traces all http requests to your Azure Function app. | ||
Enabling | ||
~~~~~~~~ | ||
Use :func:`patch()<ddtrace.patch>` to manually enable the integration:: | ||
from ddtrace import patch | ||
patch(azure_functions=True) | ||
Global Configuration | ||
~~~~~~~~~~~~~~~~~~~~ | ||
.. py:data:: ddtrace.config.azure_functions["service"] | ||
The service name reported by default for azure_functions instances. | ||
This option can also be set with the ``DD_SERVICE`` environment | ||
variable. | ||
Default: ``"azure_functions"`` | ||
""" | ||
|
||
from ddtrace.internal.utils.importlib import require_modules | ||
|
||
|
||
required_modules = ["azure.functions"] | ||
|
||
with require_modules(required_modules) as missing_modules: | ||
if not missing_modules: | ||
# Required to allow users to import from `ddtrace.contrib.azure_functions.patch` directly | ||
import warnings as _w | ||
|
||
with _w.catch_warnings(): | ||
_w.simplefilter("ignore", DeprecationWarning) | ||
from . import patch as _ # noqa: F401, I001 | ||
|
||
# Expose public methods | ||
from ddtrace.contrib.internal.azure_functions.patch import get_version | ||
from ddtrace.contrib.internal.azure_functions.patch import patch | ||
from ddtrace.contrib.internal.azure_functions.patch import unpatch | ||
|
||
__all__ = ["patch", "unpatch", "get_version"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from ddtrace.contrib.internal.azure_functions.patch import * # noqa: F403 | ||
from ddtrace.internal.utils.deprecations import DDTraceDeprecationWarning | ||
from ddtrace.vendor.debtcollector import deprecate | ||
|
||
|
||
def __getattr__(name): | ||
deprecate( | ||
("%s.%s is deprecated" % (__name__, name)), | ||
category=DDTraceDeprecationWarning, | ||
) | ||
|
||
if name in globals(): | ||
return globals()[name] | ||
raise AttributeError("%s has no attribute %s", __name__, name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import azure.functions as azure_functions | ||
from wrapt import wrap_function_wrapper as _w | ||
|
||
from ddtrace import config | ||
from ddtrace.contrib.trace_utils import int_service | ||
from ddtrace.contrib.trace_utils import unwrap as _u | ||
from ddtrace.ext import SpanTypes | ||
from ddtrace.internal import core | ||
from ddtrace.internal.schema import schematize_cloud_faas_operation | ||
from ddtrace.internal.schema import schematize_service_name | ||
from ddtrace.pin import Pin | ||
|
||
|
||
config._add( | ||
"azure_functions", | ||
{ | ||
"_default_service": schematize_service_name("azure_functions"), | ||
}, | ||
) | ||
|
||
|
||
def get_version(): | ||
# type: () -> str | ||
return getattr(azure_functions, "__version__", "") | ||
|
||
|
||
def patch(): | ||
""" | ||
Patch `azure.functions` module for tracing | ||
""" | ||
# Check to see if we have patched azure.functions yet or not | ||
if getattr(azure_functions, "_datadog_patch", False): | ||
return | ||
azure_functions._datadog_patch = True | ||
|
||
Pin().onto(azure_functions.FunctionApp) | ||
_w("azure.functions", "FunctionApp.route", _patched_route) | ||
|
||
|
||
def _patched_route(wrapped, instance, args, kwargs): | ||
trigger = "Http" | ||
|
||
pin = Pin.get_from(instance) | ||
if not pin or not pin.enabled(): | ||
return wrapped(*args, **kwargs) | ||
|
||
def _wrapper(func): | ||
function_name = func.__name__ | ||
|
||
def wrap_function(req: azure_functions.HttpRequest, context: azure_functions.Context): | ||
operation_name = schematize_cloud_faas_operation( | ||
"azure.functions.invoke", cloud_provider="azure", cloud_service="functions" | ||
) | ||
with core.context_with_data( | ||
"azure.functions.patched_route_request", | ||
span_name=operation_name, | ||
pin=pin, | ||
service=int_service(pin, config.azure_functions), | ||
span_type=SpanTypes.SERVERLESS, | ||
) as ctx, ctx.span: | ||
ctx.set_item("req_span", ctx.span) | ||
core.dispatch("azure.functions.request_call_modifier", (ctx, config.azure_functions, req)) | ||
res = None | ||
try: | ||
res = func(req) | ||
return res | ||
finally: | ||
core.dispatch( | ||
"azure.functions.start_response", (ctx, config.azure_functions, res, function_name, trigger) | ||
) | ||
|
||
# Needed to correctly display function name when running 'func start' locally | ||
wrap_function.__name__ = function_name | ||
|
||
return wrapped(*args, **kwargs)(wrap_function) | ||
|
||
return _wrapper | ||
|
||
|
||
def unpatch(): | ||
if not getattr(azure_functions, "_datadog_patch", False): | ||
return | ||
azure_functions._datadog_patch = False | ||
|
||
_u(azure_functions.FunctionApp, "route") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
releasenotes/notes/feat-add-azure-functions-integration-108911bfe1e5f081.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
features: | ||
- | | ||
azure_functions: This introduces support for Azure Functions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.