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.
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: |
There was a problem hiding this comment.
i feel like this should go into a helper function
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.
|
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.
| return state | ||
| if api_key is not None or app_url is not None: | ||
| return BraintrustState() | ||
| return _state |
There was a problem hiding this comment.
the api_key and app_url is a bit odd for this func. i'm worried we would make an isolated state each time causing unusual
| """ | ||
|
|
||
| state = state or _state | ||
| state = _resolve_state(state, api_key, app_url) |
There was a problem hiding this comment.
seems odd to do this _resolve_state which might cause a side effect (a new braintrust state) which might login
there's a few funcs that i wouldn't expect a login attempt:
init_dataset()
init_logger()
parent_context()
Experiment()
ReadonlyExperiment()
SpanImpl()
Dataset()
Logger()
| return lambda: _compute_logger_metadata(**captured_compute_object_metadata_args).project.id | ||
|
|
||
| def compute_project_id(): | ||
| login() |
There was a problem hiding this comment.
why should we always login?
| login() | ||
| return _compute_logger_metadata(_state, **captured_compute_object_metadata_args).project.id |
There was a problem hiding this comment.
i didn't expect _state but the state
ibolmo
left a comment
There was a problem hiding this comment.
Modified _compute_logger_metadata() to accept a required state parameter and use it for org_id and app_conn() instead of the global _state
based on the PR description that seems sensible. is there a requirement to do all of these _resolve_state and possibly create a random BraintrustState()?
if we need to, we should error if the user is not logged in
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.