Open
Conversation
When passing `state=BraintrustState()` to `init_logger()`, the logger was not fully isolated because `_compute_logger_metadata()` was still using the global `_state` instead of the passed state parameter. Changes: - Modified `_compute_logger_metadata()` to accept a required `state` parameter and use it for `org_id` and `app_conn()` instead of the global `_state` - Updated `init_logger()` to pass the state to `_compute_logger_metadata()` - Updated `_span_components_to_object_id_lambda()` to explicitly call `login()` and pass `_state` when computing project IDs for deserialized span components This enables multi-tenant logging where separate loggers can be created for different organizations, each with their own isolated BraintrustState.
clutchski
reviewed
Feb 3, 2026
py/src/braintrust/logger.py
Outdated
| state = state or _state | ||
| # If no explicit state is provided but api_key or app_url is specified, | ||
| # create an isolated state to avoid conflicts with the global state | ||
| if state is None: |
Collaborator
There was a problem hiding this comment.
i feel like this should go into a helper function
Author
There was a problem hiding this comment.
Added _resolve_state to address this suggestion
Add _resolve_state in to centralize logic for choosing a BraintrustState: return an explicit state if provided, create an isolated state when api_key or app_url is passed, otherwise use the global state.
Collaborator
|
I think (a) the tests need to pass (b) we should somehow have tests verify the correct behaviour (e.g. data is sent to more than one org) vs just asserting internal detals about the logger |
Tests were failing because the _compute_logger_metadata function now takes state as the first positional argument, but the test helper init_test_logger has a mock that didn't account for this. Updated fake_compute_logger_metadata arguments to include state in order to address this.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When passing
state=BraintrustState()toinit_logger(), the logger was not fully isolated because_compute_logger_metadata()was still using the global_stateinstead of the passed state parameter.Changes:
_compute_logger_metadata()to accept a requiredstateparameter and use it fororg_idandapp_conn()instead of the global_stateinit_logger()to pass the state to_compute_logger_metadata()_span_components_to_object_id_lambda()to explicitly calllogin()and pass_statewhen computing project IDs for deserialized span componentsapi_keyorapp_urlexists thenBraintrustState()is passed implicitlyThis enables multi-tenant logging where separate loggers can be created for different organizations, each with their own isolated BraintrustState.