Skip to content

{EventHubs} az eventhubs --help: Args-guided AAZ command tree loading for eventhubs module#33008

Closed
ReaNAiveD wants to merge 1 commit intoAzure:devfrom
ReaNAiveD:optimize-eventhubs-aaz-loading
Closed

{EventHubs} az eventhubs --help: Args-guided AAZ command tree loading for eventhubs module#33008
ReaNAiveD wants to merge 1 commit intoAzure:devfrom
ReaNAiveD:optimize-eventhubs-aaz-loading

Conversation

@ReaNAiveD
Copy link
Copy Markdown
Member

Related command

az eventhubs --help

Description

Introduces an optimized AAZ command loader (load_aaz_command_table_optimized) that navigates the package tree guided by CLI args instead of importing all command modules eagerly. Applied to the eventhubs module as a pilot.

What changed

  • New loader in _command.py: load_aaz_command_table_optimized uses pkgutil.iter_modules() to match CLI args against subpackages/command modules, loading only the targeted subtree. Helper functions _load_aaz_by_pkg, _get_pkg_children, _load_first_command support the navigation.
  • Gutted __init__.py files: Removed wildcard imports (from ._create import *) from 26 eventhubs AAZ __init__.py files to prevent cascade imports.
  • Direct module imports: Updated all operations files to import from specific modules (e.g., ._update import Update) instead of package-level imports.
  • Config toggle: az config set eventhubs.optimized_loading=false disables the optimization (falls back to full load). Warning shown only for eventhubs commands.
  • Tests: 17 new unit tests covering both original and optimized loaders.

Performance — Hyperfine (10 runs, 3 warmups)

Command dev optimized Improvement
az eventhubs --help 1,489 ms ± 86 1,217 ms ± 15 -272 ms (18%)
az eventhubs namespace --help 1,580 ms ± 99 1,276 ms ± 12 -304 ms (19%)
az eventhubs namespace create --help 1,702 ms ± 85 1,401 ms ± 43 -301 ms (18%)
az eventhubs eventhub --help 1,426 ms ± 53 1,229 ms ± 28 -197 ms (14%)

Performance — VizTracer Breakdown (after warmup)

az eventhubs --help

Function dev optimized Δ
AAZ loader (load_aaz_command_table) 73.6 ms 36.3 ms -37.3 ms (51%)
EventhubCommandsLoader.load_command_table 290.5 ms 262.9 ms -27.6 ms (10%)

az eventhubs namespace create --help

Function dev optimized Δ
AAZ loader (load_aaz_command_table) 320.6 ms 16.4 ms -304.2 ms (95%)
EventhubCommandsLoader.load_command_table 695.0 ms 233.4 ms -461.6 ms (66%)

Risk & Rollback

Users can disable the optimization without code changes:

az config set eventhubs.optimized_loading=false

The fallback uses the same optimized loader with args=None (full load), ensuring compatibility with the gutted __init__.py files.

A warning message is added.
image

Testing Guide

History Notes

[Component Name 1] BREAKING CHANGE: az command a: Make some customer-facing breaking change
[Component Name 2] az command b: Add some customer-facing feature


This checklist is used to make sure that common guidelines for a pull request are followed.

@ReaNAiveD ReaNAiveD self-assigned this Mar 23, 2026
@ReaNAiveD ReaNAiveD requested a review from Pan-Qi as a code owner March 23, 2026 04:20
Copilot AI review requested due to automatic review settings March 23, 2026 04:20
@azure-client-tools-bot-prd
Copy link
Copy Markdown

azure-client-tools-bot-prd bot commented Mar 23, 2026

️✔️AzureCLI-FullTest
️✔️acr
️✔️latest
️✔️3.12
️✔️3.13
️✔️acs
️✔️latest
️✔️3.12
️✔️3.13
️✔️advisor
️✔️latest
️✔️3.12
️✔️3.13
️✔️ams
️✔️latest
️✔️3.12
️✔️3.13
️✔️apim
️✔️latest
️✔️3.12
️✔️3.13
️✔️appconfig
️✔️latest
️✔️3.12
️✔️3.13
️✔️appservice
️✔️latest
️✔️3.12
️✔️3.13
️✔️aro
️✔️latest
️✔️3.12
️✔️3.13
️✔️backup
️✔️latest
️✔️3.12
️✔️3.13
️✔️batch
️✔️latest
️✔️3.12
️✔️3.13
️✔️batchai
️✔️latest
️✔️3.12
️✔️3.13
️✔️billing
️✔️latest
️✔️3.12
️✔️3.13
️✔️botservice
️✔️latest
️✔️3.12
️✔️3.13
️✔️cdn
️✔️latest
️✔️3.12
️✔️3.13
️✔️cloud
️✔️latest
️✔️3.12
️✔️3.13
️✔️cognitiveservices
️✔️latest
️✔️3.12
️✔️3.13
️✔️compute_recommender
️✔️latest
️✔️3.12
️✔️3.13
️✔️computefleet
️✔️latest
️✔️3.12
️✔️3.13
️✔️config
️✔️latest
️✔️3.12
️✔️3.13
️✔️configure
️✔️latest
️✔️3.12
️✔️3.13
️✔️consumption
️✔️latest
️✔️3.12
️✔️3.13
️✔️container
️✔️latest
️✔️3.12
️✔️3.13
️✔️containerapp
️✔️latest
️✔️3.12
️✔️3.13
️✔️core
️✔️latest
️✔️3.12
️✔️3.13
️✔️cosmosdb
️✔️latest
️✔️3.12
️✔️3.13
️✔️databoxedge
️✔️latest
️✔️3.12
️✔️3.13
️✔️dls
️✔️latest
️✔️3.12
️✔️3.13
️✔️dms
️✔️latest
️✔️3.12
️✔️3.13
️✔️eventgrid
️✔️latest
️✔️3.12
️✔️3.13
️✔️eventhubs
️✔️latest
️✔️3.12
️✔️3.13
️✔️feedback
️✔️latest
️✔️3.12
️✔️3.13
️✔️find
️✔️latest
️✔️3.12
️✔️3.13
️✔️hdinsight
️✔️latest
️✔️3.12
️✔️3.13
️✔️identity
️✔️latest
️✔️3.12
️✔️3.13
️✔️iot
️✔️latest
️✔️3.12
️✔️3.13
️✔️keyvault
️✔️latest
️✔️3.12
️✔️3.13
️✔️lab
️✔️latest
️✔️3.12
️✔️3.13
️✔️managedservices
️✔️latest
️✔️3.12
️✔️3.13
️✔️maps
️✔️latest
️✔️3.12
️✔️3.13
️✔️marketplaceordering
️✔️latest
️✔️3.12
️✔️3.13
️✔️monitor
️✔️latest
️✔️3.12
️✔️3.13
️✔️mysql
️✔️latest
️✔️3.12
️✔️3.13
️✔️netappfiles
️✔️latest
️✔️3.12
️✔️3.13
️✔️network
️✔️latest
️✔️3.12
️✔️3.13
️✔️policyinsights
️✔️latest
️✔️3.12
️✔️3.13
️✔️postgresql
️✔️latest
️✔️3.12
️✔️3.13
️✔️privatedns
️✔️latest
️✔️3.12
️✔️3.13
️✔️profile
️✔️latest
️✔️3.12
️✔️3.13
️✔️rdbms
️✔️latest
️✔️3.12
️✔️3.13
️✔️redis
️✔️latest
️✔️3.12
️✔️3.13
️✔️relay
️✔️latest
️✔️3.12
️✔️3.13
️✔️resource
️✔️latest
️✔️3.12
️✔️3.13
️✔️role
️✔️latest
️✔️3.12
️✔️3.13
️✔️search
️✔️latest
️✔️3.12
️✔️3.13
️✔️security
️✔️latest
️✔️3.12
️✔️3.13
️✔️servicebus
️✔️latest
️✔️3.12
️✔️3.13
️✔️serviceconnector
️✔️latest
️✔️3.12
️✔️3.13
️✔️servicefabric
️✔️latest
️✔️3.12
️✔️3.13
️✔️signalr
️✔️latest
️✔️3.12
️✔️3.13
️✔️sql
️✔️latest
️✔️3.12
️✔️3.13
️✔️sqlvm
️✔️latest
️✔️3.12
️✔️3.13
️✔️storage
️✔️latest
️✔️3.12
️✔️3.13
️✔️synapse
️✔️latest
️✔️3.12
️✔️3.13
️✔️telemetry
️✔️latest
️✔️3.12
️✔️3.13
️✔️util
️✔️latest
️✔️3.12
️✔️3.13
️✔️vm
️✔️latest
️✔️3.12
️✔️3.13

@azure-client-tools-bot-prd
Copy link
Copy Markdown

Hi @ReaNAiveD,
Since the current milestone time is less than 7 days, this pr will be reviewed in the next milestone.

@azure-client-tools-bot-prd
Copy link
Copy Markdown

azure-client-tools-bot-prd bot commented Mar 23, 2026

️✔️AzureCLI-BreakingChangeTest
️✔️Non Breaking Changes

@yonzhan
Copy link
Copy Markdown
Collaborator

yonzhan commented Mar 23, 2026

Thank you for your contribution! We will review the pull request and get back to you soon.

@github-actions
Copy link
Copy Markdown

The git hooks are available for azure-cli and azure-cli-extensions repos. They could help you run required checks before creating the PR.

Please sync the latest code with latest dev branch (for azure-cli) or main branch (for azure-cli-extensions).
After that please run the following commands to enable git hooks:

pip install azdev --upgrade
azdev setup -c <your azure-cli repo path> -r <your azure-cli-extensions repo path>

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces an args-guided AAZ command loader to avoid eager importing of the full AAZ package tree, and pilots it in the eventhubs command module to improve az eventhubs --help (and related help) performance.

Changes:

  • Add load_aaz_command_table_optimized in azure-cli-core to traverse AAZ packages based on CLI args and load only the relevant subtree (or full-load when requested).
  • Update eventhubs to use the optimized loader with a config toggle, and refactor AAZ packages/operations to avoid package-level wildcard imports.
  • Adjust eventhubs operations to import AAZ command classes from concrete modules (e.g., ._update) instead of relying on __init__.py re-exports.

Reviewed changes

Copilot reviewed 33 out of 33 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/azure-cli/azure/cli/command_modules/eventhubs/init.py Switch eventhubs to the optimized AAZ loader; add config toggle + warning.
src/azure-cli-core/azure/cli/core/aaz/_command.py Implement the new args-guided optimized AAZ loader and helpers.
src/azure-cli-core/azure/cli/core/aaz/init.py Export load_aaz_command_table_optimized from the AAZ public surface.
src/azure-cli/azure/cli/command_modules/eventhubs/operations/network_rule_set.py Update AAZ imports to direct _update / _show modules.
src/azure-cli/azure/cli/command_modules/eventhubs/operations/namespace_custom.py Update AAZ imports to direct _create / _update / _show / _delete modules.
src/azure-cli/azure/cli/command_modules/eventhubs/operations/event_hub_entity.py Update AAZ imports to direct _update / _create modules.
src/azure-cli/azure/cli/command_modules/eventhubs/operations/app_group_custom_file.py Update AAZ imports to direct _create / _update / _show modules.
src/azure-cli/azure/cli/command_modules/eventhubs/aaz/latest/eventhubs/init.py Remove wildcard imports to prevent cascade imports.
src/azure-cli/azure/cli/command_modules/eventhubs/aaz/latest/eventhubs/cluster/init.py Remove wildcard imports to prevent cascade imports.
src/azure-cli/azure/cli/command_modules/eventhubs/aaz/latest/eventhubs/cluster/namespace/init.py Remove wildcard imports to prevent cascade imports.
src/azure-cli/azure/cli/command_modules/eventhubs/aaz/latest/eventhubs/eventhub/init.py Remove wildcard imports to prevent cascade imports.
src/azure-cli/azure/cli/command_modules/eventhubs/aaz/latest/eventhubs/eventhub/authorization_rule/init.py Remove wildcard imports to prevent cascade imports.
src/azure-cli/azure/cli/command_modules/eventhubs/aaz/latest/eventhubs/eventhub/authorization_rule/keys/init.py Remove wildcard imports to prevent cascade imports.
src/azure-cli/azure/cli/command_modules/eventhubs/aaz/latest/eventhubs/eventhub/consumer_group/init.py Remove wildcard imports to prevent cascade imports.
src/azure-cli/azure/cli/command_modules/eventhubs/aaz/latest/eventhubs/georecovery_alias/init.py Remove wildcard imports to prevent cascade imports.
src/azure-cli/azure/cli/command_modules/eventhubs/aaz/latest/eventhubs/georecovery_alias/authorization_rule/init.py Remove wildcard imports to prevent cascade imports.
src/azure-cli/azure/cli/command_modules/eventhubs/aaz/latest/eventhubs/georecovery_alias/authorization_rule/keys/init.py Remove wildcard imports to prevent cascade imports.
src/azure-cli/azure/cli/command_modules/eventhubs/aaz/latest/eventhubs/namespace/init.py Remove wildcard imports to prevent cascade imports.
src/azure-cli/azure/cli/command_modules/eventhubs/aaz/latest/eventhubs/namespace/application_group/init.py Remove wildcard imports to prevent cascade imports.
src/azure-cli/azure/cli/command_modules/eventhubs/aaz/latest/eventhubs/namespace/authorization_rule/init.py Remove wildcard imports to prevent cascade imports.
src/azure-cli/azure/cli/command_modules/eventhubs/aaz/latest/eventhubs/namespace/authorization_rule/keys/init.py Remove wildcard imports to prevent cascade imports.
src/azure-cli/azure/cli/command_modules/eventhubs/aaz/latest/eventhubs/namespace/network_rule_set/init.py Remove wildcard imports to prevent cascade imports.
src/azure-cli/azure/cli/command_modules/eventhubs/aaz/latest/eventhubs/namespace/nsp_configuration/init.py Remove wildcard imports to prevent cascade imports.
src/azure-cli/azure/cli/command_modules/eventhubs/aaz/latest/eventhubs/namespace/private_endpoint_connection/init.py Remove wildcard imports to prevent cascade imports.
src/azure-cli/azure/cli/command_modules/eventhubs/aaz/latest/eventhubs/namespace/private_link_resource/init.py Remove wildcard imports to prevent cascade imports.
src/azure-cli/azure/cli/command_modules/eventhubs/aaz/latest/eventhubs/namespace/schema_registry/init.py Remove wildcard imports to prevent cascade imports.
src/azure-cli/azure/cli/command_modules/eventhubs/aaz/profile_2019_03_01_hybrid/eventhubs/init.py Remove wildcard imports to prevent cascade imports (hybrid profile).
src/azure-cli/azure/cli/command_modules/eventhubs/aaz/profile_2019_03_01_hybrid/eventhubs/eventhub/init.py Remove wildcard imports to prevent cascade imports (hybrid profile).
src/azure-cli/azure/cli/command_modules/eventhubs/aaz/profile_2019_03_01_hybrid/eventhubs/eventhub/authorization_rule/init.py Remove wildcard imports to prevent cascade imports (hybrid profile).
src/azure-cli/azure/cli/command_modules/eventhubs/aaz/profile_2019_03_01_hybrid/eventhubs/eventhub/authorization_rule/keys/init.py Remove wildcard imports to prevent cascade imports (hybrid profile).
src/azure-cli/azure/cli/command_modules/eventhubs/aaz/profile_2019_03_01_hybrid/eventhubs/namespace/init.py Remove wildcard imports to prevent cascade imports (hybrid profile).
src/azure-cli/azure/cli/command_modules/eventhubs/aaz/profile_2019_03_01_hybrid/eventhubs/namespace/authorization_rule/init.py Remove wildcard imports to prevent cascade imports (hybrid profile).
src/azure-cli/azure/cli/command_modules/eventhubs/aaz/profile_2019_03_01_hybrid/eventhubs/namespace/authorization_rule/keys/init.py Remove wildcard imports to prevent cascade imports (hybrid profile).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +421 to +424
"""Try to import a module by relative name, return None on failure."""
try:
return importlib.import_module(relative_name, package)
except (ModuleNotFoundError, ImportError):
Copy link

Copilot AI Mar 23, 2026

Choose a reason for hiding this comment

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

_try_import_module currently catches ImportError as well as ModuleNotFoundError and silently returns None. This can mask real import-time failures inside existing modules (e.g., a bug or missing dependency within a command module) and result in commands/groups silently not being registered. Consider only swallowing ModuleNotFoundError (or checking the missing module name), and letting other ImportErrors propagate or at least logging them at debug level with the exception details.

Suggested change
"""Try to import a module by relative name, return None on failure."""
try:
return importlib.import_module(relative_name, package)
except (ModuleNotFoundError, ImportError):
"""Try to import a module by relative name, return None if the module is not found."""
try:
return importlib.import_module(relative_name, package)
except ModuleNotFoundError as ex:
logger.debug("Module %s could not be imported from package %s: %s", relative_name, package, ex, exc_info=True)

Copilot uses AI. Check for mistakes.
Comment on lines +45 to +50
if use_optimized and args and args[0:1] == ['eventhubs']:
logger.warning(
"The eventhubs module is using optimized command loading for improved performance. "
"If you encounter any issues, you can disable this by running: "
"az config set %s.%s=false",
_OPTIMIZED_LOADING_CONFIG_SECTION, _OPTIMIZED_LOADING_CONFIG_KEY)
Copy link

Copilot AI Mar 23, 2026

Choose a reason for hiding this comment

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

The optimized-loading warning is emitted on every eventhubs invocation (any args starting with ['eventhubs']). This adds persistent stderr noise for normal command execution and can be disruptive for scripting/log parsing. Consider restricting it to help invocations (args contains --help/-h), emitting it only once per process, or downgrading to a debug message while the feature is enabled by default.

Copilot uses AI. Check for mistakes.
@ReaNAiveD
Copy link
Copy Markdown
Member Author

Close this experiment since the usage of this module is not enough for us to identify potential issues.

@ReaNAiveD ReaNAiveD closed this Mar 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Auto-Assign Auto assign by bot Event Hubs az eventhubs performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants