Skip to content

Commit

Permalink
add unit tests for azure_functions integration
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanpharvey committed Nov 26, 2024
1 parent 856e062 commit cdecdaa
Show file tree
Hide file tree
Showing 18 changed files with 371 additions and 10 deletions.
7 changes: 7 additions & 0 deletions .circleci/config.templ.yml
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,13 @@ jobs:
pattern: 'aws_lambda'
snapshot: true

azure_functions:
<<: *machine_executor
steps:
- run_test:
pattern: 'azure_functions'
snapshot: true

datastreams:
<<: *contrib_job_small
steps:
Expand Down
26 changes: 26 additions & 0 deletions .riot/requirements/1337ee3.txt
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
24 changes: 24 additions & 0 deletions .riot/requirements/14b54db.txt
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
26 changes: 26 additions & 0 deletions .riot/requirements/1e62aea.txt
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
29 changes: 29 additions & 0 deletions .riot/requirements/73109d5.txt
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
26 changes: 26 additions & 0 deletions .riot/requirements/c2420c2.txt
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
26 changes: 26 additions & 0 deletions ddtrace/contrib/azure_functions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
"""
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


Expand Down
20 changes: 10 additions & 10 deletions ddtrace/contrib/internal/azure_functions/patch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import azure.functions
import azure.functions as azure_functions
from wrapt import wrap_function_wrapper as _w

from ddtrace import config
Expand Down Expand Up @@ -28,7 +28,7 @@

def get_version():
# type: () -> str
return getattr(azure.functions, "__version__", "")
return getattr(azure_functions, "__version__", "")


def _set_span_meta(span, req, res, function_name, path, trigger):
Expand All @@ -52,11 +52,11 @@ 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):
if getattr(azure_functions, "_datadog_patch", False):
return
azure.functions._datadog_patch = True
azure_functions._datadog_patch = True

Pin().onto(azure.functions.FunctionApp)
Pin().onto(azure_functions.FunctionApp)
_w("azure.functions", "FunctionApp.route", _patched_route)


Expand All @@ -71,8 +71,8 @@ def _wrapper(func):
function_name = func.__name__

def wrap_function(
req: azure.functions.HttpRequest, context: azure.functions.Context
) -> azure.functions.HttpResponse:
req: azure_functions.HttpRequest, context: azure_functions.Context
) -> azure_functions.HttpResponse:
parsed_url = parse.urlparse(req.url)
path = parsed_url.path
resource = f"{req.method} {path}"
Expand Down Expand Up @@ -105,8 +105,8 @@ def wrap_function(


def unpatch():
if not getattr(azure.functions, "_datadog_patch", False):
if not getattr(azure_functions, "_datadog_patch", False):
return
azure.functions._datadog_patch = False
azure_functions._datadog_patch = False

_u(azure.functions.FunctionApp, "route")
_u(azure_functions.FunctionApp, "route")
10 changes: 10 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ RUN \
gdb \
git \
gnupg \
gpg \
jq \
libbz2-dev \
libenchant-dev \
Expand All @@ -58,6 +59,15 @@ RUN \
# For running datadog-ci CLI with npx
&& apt-get install -y --no-install-recommends nodejs npm \
&& npm install -g @datadog/datadog-ci \
# Install azure-functions-core-tools-4, only supported on amd64 architecture for Linux
# https://github.com/Azure/azure-functions-core-tools/issues/3112
&& if [ "$TARGETARCH" = "amd64" ]; \
then \
curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg \
&& mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg \
&& echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-debian-buster-prod buster main" > /etc/apt/sources.list.d/dotnetdev.list \
&& apt-get update && apt-get install -y --no-install-recommends azure-functions-core-tools-4=4.0.6280-1; \
fi \
# Install Mariadb last because mariadb_repo_setup currently breaks apt
&& wget https://downloads.mariadb.com/MariaDB/mariadb_repo_setup \
&& chmod +x mariadb_repo_setup \
Expand Down
9 changes: 9 additions & 0 deletions riotfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2805,6 +2805,15 @@ def select_pys(min_version: str = MIN_PYTHON_VERSION, max_version: str = MAX_PYT
"envier": "==0.5.2",
},
),
Venv(
name="azure_functions",
command="pytest {cmdargs} tests/contrib/azure_functions",
pys=select_pys(min_version="3.7", max_version="3.11"),
pkgs={
"azure.functions": latest,
"requests": latest,
},
),
Venv(
name="sourcecode",
command="pytest {cmdargs} tests/sourcecode",
Expand Down
13 changes: 13 additions & 0 deletions tests/.suitespec.json
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,10 @@
],
"codeowners": [
"ddtrace/internal/codeowners.py"
],
"azure_functions": [
"ddtrace/contrib/azure_functions/*",
"ddtrace/contrib/internal/azure_functions/*"
]
},
"suites": {
Expand Down Expand Up @@ -1523,6 +1527,15 @@
"@core",
"@runtime",
"tests/runtime/*"
],
"azure_functions": [
"@bootstrap",
"@core",
"@contrib",
"@tracing",
"@azure_functions",
"tests/contrib/azure_functions/*",
"tests/snapshots/tests.contrib.azure_functions.*"
]
}
}
Empty file.
14 changes: 14 additions & 0 deletions tests/contrib/azure_functions/azure_function_app/function_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from ddtrace import patch


patch(azure_functions=True)

import azure.functions as func # noqa: E402


app = func.FunctionApp()


@app.route(route="httptest", auth_level=func.AuthLevel.ANONYMOUS)
def http_test(req: func.HttpRequest) -> func.HttpResponse:
return func.HttpResponse("Hello Datadog!")
15 changes: 15 additions & 0 deletions tests/contrib/azure_functions/azure_function_app/host.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[4.*, 5.0.0)"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "python",
"AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
"AzureWebJobsStorage": ""
}
}
Loading

0 comments on commit cdecdaa

Please sign in to comment.