Skip to content

Add managed variables#1691

Open
dmontagu wants to merge 17 commits intomainfrom
managed-variables
Open

Add managed variables#1691
dmontagu wants to merge 17 commits intomainfrom
managed-variables

Conversation

@dmontagu
Copy link
Contributor

@dmontagu dmontagu commented Feb 8, 2026

Summary

Adds a managed variables feature to Logfire, enabling dynamic configuration management with feature flags, A/B testing, and targeted rollouts. Variables can be resolved from local in-memory configuration or from the remote Logfire API with background polling and SSE-based real-time updates.

This is a replacement of #1548 to reduce review noise.

Key additions

  • logfire/variables/ module — Variable class, VariableProvider ABC, local and remote providers, config models (rollout, conditions, variants)
  • Logfire.var() — creates managed variables with typed defaults, optional resolve functions, and Pydantic validation
  • variables_push / variables_validate / variables_push_types — CLI-friendly methods for syncing variable definitions to a provider
  • Targeting & rollout — deterministic variant selection via targeting keys (user ID, trace ID), weighted rollouts, condition-based overrides (attribute matching, regex, presence checks)
  • Context managers — variable.override(), variable.use_variant(), targeting_context() for scoped variable behavior
  • Integration with logfire.configure() — VariablesOptions dataclass, provider lifecycle tied to config/shutdown

Unresolved review comments from #1548 to revisit

These are comments that were noted but not actioned yet in this PR:

  1. Verb/naming inconsistency — Alex noted the mismatch between "get" vs "pull" and "sync" vs "push" in method names. Worth a naming review pass.
  2. Method names sound generic — Alex suggested the variables_ prefix approach (already done) but also noted names could still be confused with logfire.configure options.
  3. VariantKey/VariableName as NewTypes — Alex asked why not. David responded concerns about runtime overhead and deepening Pydantic dependency; changed to Pydantic models per Marcelo's suggestion. May revisit.
  4. logfire/variables/config.py unused code — Alex asked "is this used?" about some config code (around line 473). Worth auditing.
  5. Stale docs reference — Alex noted docs/reference/advanced/managed-variables.md:875 references a method that
    doesn't seem to exist. Docs may need a cleanup pass.
  6. Provider start lifecycle — Devin flagged that start(None) in _initialize() followed by start(logfire_instance) in configure() is fragile. Currently works because _logfire is set before the _started early-return, but the two-phase start is worth reviewing.
  7. PEP 747 (TypeForm) — Viicos suggested we could drop the sequence-of-types overload once PEP 747 lands, allowing type: str | int directly.
  8. TypeAdapter deferred build — Viicos suggested using defer_build config for variables. David opted for eager build for now to avoid forward-ref issues; can revisit if build time is a problem.

dmontagu and others added 13 commits February 5, 2026 11:09
Add comprehensive tests covering uncovered lines across the variables
module: is_resolve_function edge cases, variant deserialization errors,
remote provider change detection, force refresh, variant diffing,
variable type operations, push_variable_types, and more. Add pragmas
for genuinely untestable code paths (fork handler, unreachable fallback).
Covers: resolve function default without type, duplicate variable name,
on_config_change with unknown variable, and multiple keyword-only params.
Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Add a `timeout` field to `RemoteVariablesConfig` (default `(10, 10)`) so
that both the polling thread and the blocking first-resolve path use a
bounded `Session.get` call. Also propagate `timeout_millis` through
`VariableProvider.shutdown()` so thread joins respect the shutdown budget.
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Feb 8, 2026

Deploying logfire-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: b9dd937
Status: ✅  Deploy successful!
Preview URL: https://4e219578.logfire-docs.pages.dev
Branch Preview URL: https://managed-variables.logfire-docs.pages.dev

View logs

…sion optional

- Remove `enabled` field from VariableConfig (disabled variables are now modeled by pointing labels to code_default)
- Add `code_default` handling in `follow_ref` - returns (None, None) to trigger code default fallthrough
- Make `LabelRef.version` optional (None for code_default and label-to-label refs)
- Update `_validate_labels` to skip code_default refs in label validation
- Update tests to use code_default pattern instead of enabled=False
- Remove `enabled` field from VariableConfig reference table
- Document `code_default` as a LabelRef target
- Note that LabelRef.version is optional
- Add explanations for when to use each ref target type
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