Skip to content

fix(telemetry): break helpers->core import cycle so metrics plugins register#1180

Open
ajbozarth wants to merge 1 commit into
generative-computing:mainfrom
ajbozarth:fix/helpers-import-cycle
Open

fix(telemetry): break helpers->core import cycle so metrics plugins register#1180
ajbozarth wants to merge 1 commit into
generative-computing:mainfrom
ajbozarth:fix/helpers-import-cycle

Conversation

@ajbozarth
Copy link
Copy Markdown
Contributor

Issue

Fixes #1079

Description

mellea/helpers loaded mellea.core at module level, creating a cycle when mellea.core.utils -> mellea.telemetry -> metrics' bottom-of-module registration -> mellea.helpers ran during core init. The except ImportError swallowed the cycle as "plugin framework is not installed", so MELLEA_METRICS_ENABLED=true registered zero plugins in production.

  • helpers/async_helpers.py and openai_compatible_helpers.py: move core imports under TYPE_CHECKING or into function bodies.
  • telemetry/metrics.py: replace except ImportError with explicit _HAS_PLUGIN_FRAMEWORK check; move registration block below __all__.

The lazy-init migration originally scoped in #1079 is declined in favor of eager-parity with the upcoming tracing plugin design.

Testing

  • Tests added to the respective file if code was changed
  • New code has 100% coverage if code was added
  • Ensure existing tests and github automation passes (a maintainer will kick off the github automation when the rest of the PR is populated)

Attribution

  • AI coding assistants used

Adding a new component, requirement, sampling strategy, or tool?

If your PR adds or modifies one of the types below, check the matching box. A checklist of type-specific review items will be posted as a comment.

  • Component
  • Requirement
  • Sampling Strategy
  • Tool

NOTE: Please ensure you have an issue that has been acknowledged by a core contributor and routed you to open a pull request against this repository. Otherwise, please open an issue before continuing with this pull request.

…egister

mellea/helpers loaded mellea.core at module level, creating a cycle when
mellea.core.utils -> mellea.telemetry -> metrics' bottom-of-module
registration -> mellea.helpers ran during core init. The except
ImportError swallowed the cycle as "plugin framework is not installed",
so MELLEA_METRICS_ENABLED=true registered zero plugins in production.

Move helpers' core imports under TYPE_CHECKING or into function bodies,
and replace the except ImportError with an explicit _HAS_PLUGIN_FRAMEWORK
check so future cycle-shape regressions surface loudly.

Closes generative-computing#1079.

Assisted-by: Claude Code
Signed-off-by: Alex Bozarth <ajbozart@us.ibm.com>
@ajbozarth ajbozarth requested a review from a team as a code owner June 1, 2026 21:49
@ajbozarth ajbozarth self-assigned this Jun 1, 2026
@ajbozarth ajbozarth requested review from markstur and planetf1 June 1, 2026 21:49
@github-actions github-actions Bot added the bug Something isn't working label Jun 1, 2026
@ajbozarth
Copy link
Copy Markdown
Contributor Author

So while wrapping up work on phase 1 of #444 I discovered a chicken and egg issue with registering plugins that makes fully lazy init not work. In addition I found that this bug also affected the tracing work. So I decided to quickly open this bug fix since it was more severe than I thought (essentially metrics has been non-functional since I switched it to use hooks). The remaining items in #1079 are now won't fix

Copy link
Copy Markdown
Contributor

@planetf1 planetf1 left a comment

Choose a reason for hiding this comment

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

LGTM — reproduced on the parent (0 plugins), confirmed all 8 register here.

Two non-blocking follow-ups:

  • metrics_plugins.py:18 imports from mellea.core.base import GenerationMetadata at module level — the same cycle this PR fixes, safe only because core/__init__.py imports .base before .utils. If that order ever changes the cycle reopens, now silently (since this PR removes the loud failure). Worth a follow-up to make plugin registration lazy and drop the dependency on import order.
  • A regression test in test/package/test_dependency_isolation.py (already runs isolated interpreters) asserting the plugin count is non-zero with metrics enabled would help — the existing plugin tests use mocks, so they couldn't catch this failure mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor: migrate metrics to lazy init pattern (and fix auto-register circular-import bug)

2 participants