Skip to content

feat(azure_servicebus): add azure servicebus integration and function trigger distributed tracing #13691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 38 commits into
base: main
Choose a base branch
from

Conversation

duncanpharvey
Copy link
Contributor

@duncanpharvey duncanpharvey commented Jun 17, 2025

This PR adds an integration for tracing the producer of the azure-servicebus package and adds support for distributed tracing for Azure Functions consuming the service bus messages.

Instrumentation details:

  • Both azure.servicebus and azure.servicebus.aio (async package) are patched
  • Methods patched
    • ServiceBusSender.send_messages
    • ServiceBusSender.schedule_messages
  • Trace context injected into application_properties of each service bus message passed to above methods
    • Handles single ServiceBusMessage or AmqpAnnotatedMessage in addition to a list of ServiceBusMessage or AmqpAnnotatedMessage
    • Will handle trace context propagation of ServiceBusMessageBatch in a future PR
  • Trace context extracted from application_properties in Azure Function service bus consumer
  • Operation: azure.servicebus.produce
  • Azure Service Bus span attributes
    • component: azure_servicebus
    • span.kind: producer
    • messaging.destination.system: servicebus
    • messaging.destination.name: <queue/topic name>
    • azure.servicebus.fully_qualified_namespace: <host name for the Service Bus namespace>
      • Maps to peer.service if enabled

Additional Notes:

Checklist

  • 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
  • The change includes or references documentation updates if necessary
  • Backport labels are set (if applicable)

Reviewer Checklist

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

@duncanpharvey duncanpharvey requested review from a team as code owners June 17, 2025 15:02
@duncanpharvey duncanpharvey requested review from a team as code owners June 17, 2025 15:02
Copy link
Contributor

github-actions bot commented Jun 17, 2025

CODEOWNERS have been resolved as:

.riot/requirements/10f86d9.txt                                          @DataDog/apm-python
.riot/requirements/1170953.txt                                          @DataDog/apm-python
.riot/requirements/156842b.txt                                          @DataDog/apm-python
.riot/requirements/1670b58.txt                                          @DataDog/apm-python
.riot/requirements/16ae446.txt                                          @DataDog/apm-python
.riot/requirements/1826cd3.txt                                          @DataDog/apm-python
.riot/requirements/18a650f.txt                                          @DataDog/apm-python
.riot/requirements/1d35b33.txt                                          @DataDog/apm-python
.riot/requirements/1e1c2b7.txt                                          @DataDog/apm-python
.riot/requirements/3f34788.txt                                          @DataDog/apm-python
.riot/requirements/5b956b7.txt                                          @DataDog/apm-python
.riot/requirements/6c15ef8.txt                                          @DataDog/apm-python
.riot/requirements/803d32f.txt                                          @DataDog/apm-python
.riot/requirements/a1026b8.txt                                          @DataDog/apm-python
.riot/requirements/ade49fb.txt                                          @DataDog/apm-python
.riot/requirements/b43c003.txt                                          @DataDog/apm-python
ddtrace/contrib/_azure_servicebus.py                                    @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/internal/azure_servicebus/patch.py                      @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/internal/azure_servicebus/utils.py                      @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/ext/azure_servicebus.py                                         @DataDog/apm-core-python @DataDog/apm-idm-python
releasenotes/notes/feat-add-azure-servicebus-integration-4034143c46abe1c6.yaml  @DataDog/apm-python
tests/contrib/azure_servicebus/__init__.py                              @DataDog/apm-core-python @DataDog/apm-idm-python
tests/contrib/azure_servicebus/test_azure_servicebus_patch.py           @DataDog/apm-core-python @DataDog/apm-idm-python
tests/contrib/azure_servicebus/test_azure_servicebus_snapshot.py        @DataDog/apm-core-python @DataDog/apm-idm-python
tests/snapshots/tests.contrib.azure_functions.test_azure_functions_snapshot.test_service_bus_distributed_tracing[disabled].json  @DataDog/apm-python
tests/snapshots/tests.contrib.azure_functions.test_azure_functions_snapshot.test_service_bus_distributed_tracing[enabled].json  @DataDog/apm-python
tests/snapshots/tests.contrib.azure_servicebus.test_azure_servicebus_snapshot.test_schedule_messages.json  @DataDog/apm-python
tests/snapshots/tests.contrib.azure_servicebus.test_azure_servicebus_snapshot.test_schedule_messages_async.json  @DataDog/apm-python
tests/snapshots/tests.contrib.azure_servicebus.test_azure_servicebus_snapshot.test_send_messages[default_config].json  @DataDog/apm-python
tests/snapshots/tests.contrib.azure_servicebus.test_azure_servicebus_snapshot.test_send_messages[distributed_tracing_disabled].json  @DataDog/apm-python
tests/snapshots/tests.contrib.azure_servicebus.test_azure_servicebus_snapshot.test_send_messages[peer_service_enabled].json  @DataDog/apm-python
tests/snapshots/tests.contrib.azure_servicebus.test_azure_servicebus_snapshot.test_send_messages_async.json  @DataDog/apm-python
.gitlab/services.yml                                                    @DataDog/python-guild @DataDog/apm-core-python
ddtrace/_monkey.py                                                      @DataDog/apm-core-python
ddtrace/_trace/trace_handlers.py                                        @DataDog/apm-sdk-api-python
ddtrace/contrib/_azure_functions.py                                     @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/integration_registry/registry.yaml                      @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/internal/azure_functions/patch.py                       @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/settings/_config.py                                             @DataDog/apm-core-python
ddtrace/settings/peer_service.py                                        @DataDog/apm-core-python
docker-compose.yml                                                      @DataDog/apm-core-python
riotfile.py                                                             @DataDog/apm-python
supported_versions_output.json                                          @DataDog/apm-core-python
supported_versions_table.csv                                            @DataDog/apm-core-python
tests/contrib/azure_functions/azure_function_app/function_app.py        @DataDog/apm-core-python @DataDog/apm-idm-python
tests/contrib/azure_functions/azure_function_app/local.settings.json    @DataDog/apm-core-python @DataDog/apm-idm-python
tests/contrib/azure_functions/test_azure_functions_patch.py             @DataDog/apm-core-python @DataDog/apm-idm-python
tests/contrib/azure_functions/test_azure_functions_snapshot.py          @DataDog/apm-core-python @DataDog/apm-idm-python
tests/contrib/patch.py                                                  @DataDog/apm-core-python @DataDog/apm-idm-python
tests/contrib/suitespec.yml                                             @DataDog/apm-core-python @DataDog/apm-idm-python
tests/snapshots/tests.contrib.azure_functions.test_azure_functions_snapshot.test_http_get_distributed_tracing[disabled].json  @DataDog/apm-python
tests/snapshots/tests.contrib.azure_functions.test_azure_functions_snapshot.test_http_get_distributed_tracing[enabled].json  @DataDog/apm-python
.riot/requirements/1241599.txt                                          @DataDog/apm-python
.riot/requirements/1ac22db.txt                                          @DataDog/apm-python
.riot/requirements/9b11151.txt                                          @DataDog/apm-python
.riot/requirements/e7ef4bc.txt                                          @DataDog/apm-python

@duncanpharvey duncanpharvey changed the title feat(azure_servicebus): add azure servicebus integration and distributed tracing with azure function service bus triggers feat(azure_servicebus): add azure servicebus integration and function trigger distributed tracing Jun 17, 2025
Copy link
Contributor

github-actions bot commented Jun 17, 2025

Bootstrap import analysis

Comparison of import times between this PR and base.

Summary

The average import time from this PR is: 276 ± 2 ms.

The average import time from base is: 278 ± 2 ms.

The import time difference between this PR and base is: -1.63 ± 0.1 ms.

Import time breakdown

The following import paths have appeared:

ddtrace.auto 0.320 ms (0.12%)
ddtrace.bootstrap.sitecustomize 0.320 ms (0.12%)
ddtrace._trace.trace_handlers 0.320 ms (0.12%)
ddtrace.ext.azure_servicebus 0.320 ms (0.12%)

The following import paths have grown:

ddtrace.auto 0.091 ms (0.03%)
ddtrace.bootstrap.sitecustomize 0.091 ms (0.03%)
ddtrace.contrib.internal.subprocess.constants 0.063 ms (0.02%)
ddtrace.contrib.internal.logging.constants 0.028 ms (0.01%)

The following import paths have shrunk:

ddtrace.auto 2.003 ms (0.73%)
ddtrace.bootstrap.sitecustomize 1.330 ms (0.48%)
ddtrace.bootstrap.preload 1.330 ms (0.48%)
ddtrace.internal.remoteconfig.client 0.628 ms (0.23%)
ddtrace 0.673 ms (0.24%)
ddtrace.internal._unpatched 0.030 ms (0.01%)
json 0.030 ms (0.01%)
json.decoder 0.030 ms (0.01%)
re 0.030 ms (0.01%)
enum 0.030 ms (0.01%)
types 0.030 ms (0.01%)

@pr-commenter
Copy link

pr-commenter bot commented Jun 17, 2025

Benchmarks

Benchmark execution time: 2025-06-18 02:20:24

Comparing candidate commit 82e0204 in PR branch duncan-harvey/azure-servicebus-integration with baseline commit 5592908 in branch main.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 561 metrics, 3 unstable metrics.

Copy link
Collaborator

@emmettbutler emmettbutler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice use of ddtrace.internal.core!

__get_version__ = get_version

def assert_module_patched(self, azure_servicebus):
pass
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replacing this with an assertion verifying that the azure.servicebus module has been patched would strengthen this test case and protect against regressions. Same for the other two functions here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great suggestion! I added some assertions to the patch tests.

Also because this integration also patches azure.servicebus.aio, a submodule of azure.servicebus, I added an additional case for test_supported_versions_function_exists. I tried changing the else to use rpartition as well but it broke the tests for pynamodb.connection.base since that submodule should resolve to pynamodb. Let me know if you have any ideas for a more general solution.

elif module_name in {"azure.servicebus.aio"}:
# handle specific submodules where the top level module name includes a dot
module_name = module_name.rpartition(".")[0]
else:
# this may be a submodule we are importing, so get the top level module name
# ie: snowflake.connector -> snowflake
module_name = module_name.split(".")[0]

Comment on lines 29 to 30
def get_version():
# type: () -> str
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def get_version():
# type: () -> str
def get_version() -> str:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion applied for both Azure Service Bus and Azure Functions

def get_version() -> str:
return getattr(azure_servicebus, "__version__", "")

def get_version() -> str:
return getattr(azure_functions, "__version__", "")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants