Skip to content

Move managed variable registry onto config#2034

Open
alexmojaki wants to merge 5 commits into
mainfrom
codex/variables-config-registry
Open

Move managed variable registry onto config#2034
alexmojaki wants to merge 5 commits into
mainfrom
codex/variables-config-registry

Conversation

@alexmojaki

Copy link
Copy Markdown
Contributor

Summary

  • Store the managed-variable registry on LogfireConfig instead of each Logfire wrapper.
  • Keep Logfire.var() / template_var() as the public API while making with_settings() siblings share registered variables.
  • Cover sibling registry visibility, duplicate-name rejection, shared clearing, and code-default composition across siblings.

Tests

  • uv run pytest tests/test_push_variables.py tests/test_variable_composition.py tests/test_variable_templates.py
  • uv run pyright logfire/_internal/config.py logfire/_internal/main.py logfire/variables/variable.py tests/test_push_variables.py tests/test_variable_composition.py

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Open in Devin Review

@coderabbitai

coderabbitai Bot commented Jun 18, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Variable storage for Logfire is moved from per-instance state to LogfireConfig. LogfireConfig.__init__ gains a _variables dict typed to hold Variable[Any] and TemplateVariable[Any, Any] instances. Logfire._variables is converted from an instance dict to a @property that returns self._config._variables, making all sibling instances created via with_settings() share the same registry. Docstrings on variables_clear, variables_get, variables_push, variables_validate, and variables_build_config are updated to clarify config-scoped storage and include variables from with_settings() siblings. Three new unit tests verify shared visibility, duplicate-name conflicts, and variables_clear propagation across siblings. One integration test confirms cross-sibling composition resolves referenced variables.

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title directly and clearly summarizes the main change: moving the variable registry from Logfire instances to LogfireConfig.
Description check ✅ Passed The description is closely related to the changeset, covering the core objective, implementation approach, and test coverage details.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/variables-config-registry

Warning

Review ran into problems

🔥 Problems

Linked repositories: Couldn't analyze pydantic/logfire - clone failed: Clone operation failed: Stream initialization permanently failed: Ping-pong health check failed


Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
logfire/_internal/main.py (1)

2786-2788: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Update the remaining default-variable docs to config scope.

These methods call the now config-scoped variables_get(), so their default includes variables from with_settings() siblings too, not just variables registered on this wrapper instance.

Suggested wording update
-            variables: Variable instances to push. If None, all variables
-                registered with this Logfire instance will be pushed.
+            variables: Variable instances to push. If None, all variables
+                registered with this Logfire instance's config will be pushed.
...
-            variables: Variable instances to validate. If None, all variables
-                registered with this Logfire instance will be validated.
+            variables: Variable instances to validate. If None, all variables
+                registered with this Logfire instance's config will be validated.
...
-            variables: Variable instances to include. If None, uses all registered variables.
+            variables: Variable instances to include. If None, uses all variables registered with this config.

Also applies to: 2895-2897, 2998-3000

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@logfire/_internal/main.py` around lines 2786 - 2788, Update the docstring
documentation for the default behavior of the variables parameter across three
methods (locations at lines 2786-2788, 2895-2897, and 2998-3000) in the file.
The current documentation states that when variables is None, all variables
registered with the Logfire instance will be pushed, but since these methods now
call the config-scoped variables_get() method, the documentation should reflect
that the default includes variables from with_settings() siblings as well, not
just variables registered on the specific wrapper instance. Update each
docstring to clarify the expanded scope of variable inclusion when None is
passed.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/test_push_variables.py`:
- Around line 1290-1325: The tests
test_with_settings_duplicate_variable_names_conflict and
test_variables_clear_clears_with_settings_siblings use the global Logfire()
which accesses GLOBAL_CONFIG, causing variables registered in one test to
persist into subsequent tests and create order-dependent failures. Fix this by
either using a fresh isolated config fixture for each test instead of the shared
global Logfire instance, or by adding a teardown/cleanup step that calls
variables_clear() after each test to ensure the shared registry is reset between
test runs.

---

Outside diff comments:
In `@logfire/_internal/main.py`:
- Around line 2786-2788: Update the docstring documentation for the default
behavior of the variables parameter across three methods (locations at lines
2786-2788, 2895-2897, and 2998-3000) in the file. The current documentation
states that when variables is None, all variables registered with the Logfire
instance will be pushed, but since these methods now call the config-scoped
variables_get() method, the documentation should reflect that the default
includes variables from with_settings() siblings as well, not just variables
registered on the specific wrapper instance. Update each docstring to clarify
the expanded scope of variable inclusion when None is passed.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 43c493f4-4c91-4909-b370-c7ea75f79169

📥 Commits

Reviewing files that changed from the base of the PR and between 91f5225 and 438a582.

📒 Files selected for processing (4)
  • logfire/_internal/config.py
  • logfire/_internal/main.py
  • tests/test_push_variables.py
  • tests/test_variable_composition.py
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • pydantic/pydantic (auto-detected)

Comment thread tests/test_push_variables.py Outdated
Comment thread tests/test_push_variables.py Outdated
Comment thread tests/test_push_variables.py Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
tests/test_push_variables.py (1)

1256-1283: ⚡ Quick win

Prefer user-facing API over internal Logfire() constructor.

These tests import and directly instantiate Logfire() from the internal module. Per maintainer guidance, tests should use the user-facing logfire module (preconfigured by the test framework) or call logfire.configure() rather than importing internal constructors.

While these isolated instances don't cause test pollution, using internal APIs makes tests fragile to refactoring and doesn't match actual usage patterns.

♻️ Refactor to use user-facing API
 def test_var_registers_variable() -> None:
     """Test that var() registers variables with the logfire instance."""
-    from logfire._internal.main import Logfire
-
-    lf = Logfire()
-    assert lf.variables_get() == []
+    logfire.variables_clear()
+    assert logfire.variables_get() == []

-    var1 = lf.var(name='test_var_1', default=True, type=bool)
-    assert len(lf.variables_get()) == 1
-    assert lf.variables_get()[0] is var1
+    var1 = logfire.var(name='test_var_1', default=True, type=bool)
+    assert len(logfire.variables_get()) == 1
+    assert logfire.variables_get()[0] is var1

-    var2 = lf.var(name='test_var_2', default=42, type=int)
-    assert len(lf.variables_get()) == 2
-    assert var2 in lf.variables_get()
+    var2 = logfire.var(name='test_var_2', default=42, type=int)
+    assert len(logfire.variables_get()) == 2
+    assert var2 in logfire.variables_get()
+    
+    logfire.variables_clear()

Apply the same pattern to test_get_variables_returns_all_registered.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/test_push_variables.py` around lines 1256 - 1283, Replace the direct
instantiation of the internal Logfire class in
test_get_variables_returns_all_registered with the public user-facing logfire
API. Remove the import statement from logfire._internal.main and the line lf =
Logfire(), then replace all references to the lf object with the preconfigured
logfire module that is available from the test framework. Update all calls like
lf.var() and lf.variables_get() to use logfire.var() and logfire.variables_get()
instead. Apply the same refactoring to the preceding test function that also
uses the internal Logfire constructor.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@tests/test_push_variables.py`:
- Around line 1256-1283: Replace the direct instantiation of the internal
Logfire class in test_get_variables_returns_all_registered with the public
user-facing logfire API. Remove the import statement from logfire._internal.main
and the line lf = Logfire(), then replace all references to the lf object with
the preconfigured logfire module that is available from the test framework.
Update all calls like lf.var() and lf.variables_get() to use logfire.var() and
logfire.variables_get() instead. Apply the same refactoring to the preceding
test function that also uses the internal Logfire constructor.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: e899ccbe-7b87-4176-b0b8-c54ea98c1047

📥 Commits

Reviewing files that changed from the base of the PR and between c8a62f3 and 529d4fd.

📒 Files selected for processing (2)
  • logfire/_internal/main.py
  • tests/test_push_variables.py
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • pydantic/pydantic-ai (auto-detected)
  • pydantic/pydantic (auto-detected)
🚧 Files skipped from review as they are similar to previous changes (1)
  • logfire/_internal/main.py

@alexmojaki
alexmojaki requested a review from dmontagu June 18, 2026 13:42
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.

1 participant