Skip to content

perf(telemetry): remove call to importlib.metadata from get_module_distribution_versions #13278

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

Merged
merged 7 commits into from
Apr 28, 2025

Conversation

taegyunkim
Copy link
Contributor

@taegyunkim taegyunkim commented Apr 25, 2025

Screenshot 2025-04-25 at 3 14 50 PM

TelemetryWriter periodically reports imported dependencies via calling get_module_distribution_versions() when new modules are imported. Typically when application is loaded up for the first time, the TelemetryWriter would notice new imports and then it calls with new module names. The function unnecessarily invoked importlib.metadata and replaced the usage with cached distribution name to version mapping.

See below for benchmark results.

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

Copy link
Contributor

github-actions bot commented Apr 25, 2025

CODEOWNERS have been resolved as:

releasenotes/notes/perf-telemetry-d9881d20f22013f7.yaml                 @DataDog/apm-python
ddtrace/internal/debug.py                                               @DataDog/apm-core-python
ddtrace/internal/packages.py                                            @DataDog/apm-core-python
tests/internal/test_packages.py                                         @DataDog/apm-core-python

Copy link
Contributor

github-actions bot commented Apr 25, 2025

Bootstrap import analysis

Comparison of import times between this PR and base.

Summary

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

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

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

Import time breakdown

The following import paths have shrunk:

ddtrace.auto 1.987 ms (0.86%)
ddtrace.bootstrap.sitecustomize 1.315 ms (0.57%)
ddtrace.bootstrap.preload 1.315 ms (0.57%)
ddtrace.internal.products 1.315 ms (0.57%)
ddtrace.internal.remoteconfig.client 0.633 ms (0.27%)
ddtrace 0.672 ms (0.29%)

@pr-commenter
Copy link

pr-commenter bot commented Apr 25, 2025

Benchmarks

Benchmark execution time: 2025-04-25 20:32:57

Comparing candidate commit 58ec39a in PR branch taegyunkim/get_module_distribution_versions with baseline commit ea64efc in branch main.

Found 12 performance improvements and 0 performance regressions! Performance is the same for 459 metrics, 7 unstable metrics.

scenario:packagesupdateimporteddependencies-import_many

  • 🟩 execution_time [-27.287ms; -27.173ms] or [-99.639%; -99.221%]

scenario:packagesupdateimporteddependencies-import_many_cached

  • 🟩 execution_time [-27.649µs; -26.625µs] or [-18.732%; -18.038%]

scenario:packagesupdateimporteddependencies-import_many_stdlib

  • 🟩 execution_time [-719.768ms; -716.565ms] or [-100.121%; -99.675%]

scenario:packagesupdateimporteddependencies-import_many_stdlib_cached

  • 🟩 execution_time [-721.828ms; -718.239ms] or [-100.226%; -99.727%]

scenario:packagesupdateimporteddependencies-import_many_unknown

  • 🟩 execution_time [-30.004ms; -29.874ms] or [-97.517%; -97.093%]

scenario:packagesupdateimporteddependencies-import_many_unknown_cached

  • 🟩 execution_time [-249.124µs; -234.716µs] or [-24.140%; -22.744%]

scenario:packagesupdateimporteddependencies-import_one

  • 🟩 execution_time [-11.347ms; -11.285ms] or [-100.101%; -99.551%]

scenario:packagesupdateimporteddependencies-import_one_cache

  • 🟩 execution_time [-716.962ns; -640.755ns] or [-10.267%; -9.176%]

scenario:packagesupdateimporteddependencies-import_one_stdlib

  • 🟩 execution_time [-10.385ms; -10.320ms] or [-100.132%; -99.508%]

scenario:packagesupdateimporteddependencies-import_one_stdlib_cache

  • 🟩 execution_time [-649.793ns; -579.081ns] or [-9.411%; -8.387%]

scenario:packagesupdateimporteddependencies-import_one_unknown

  • 🟩 execution_time [-19.777ms; -19.677ms] or [-100.026%; -99.519%]

scenario:packagesupdateimporteddependencies-import_one_unknown_cache

  • 🟩 execution_time [-714.874ns; -663.700ns] or [-10.285%; -9.549%]

@taegyunkim taegyunkim changed the title perf(telemetry): try invoking importlib.metadata only when needed perf(telemetry): remove call to importlib.metadata from get_module_distribution_versions Apr 25, 2025
@taegyunkim taegyunkim marked this pull request as ready for review April 25, 2025 19:29
@taegyunkim taegyunkim requested review from a team as code owners April 25, 2025 19:29
@taegyunkim taegyunkim enabled auto-merge (squash) April 25, 2025 20:34
@taegyunkim taegyunkim merged commit 6354fb6 into main Apr 28, 2025
698 of 701 checks passed
@taegyunkim taegyunkim deleted the taegyunkim/get_module_distribution_versions branch April 28, 2025 16:48
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