Skip to content

Commit 8a64427

Browse files
nagarajRPoojarinagaraj-poojarialexmojaki
authored
Check empty tokens before making connection attempt (#1110)
Co-authored-by: nagaraj-poojari <[email protected]> Co-authored-by: Alex Hall <[email protected]>
1 parent 0465f5e commit 8a64427

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

logfire/_internal/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ def add_span_processor(span_processor: SpanProcessor) -> None:
880880

881881
# try loading credentials (and thus token) from file if a token is not already available
882882
# this takes the lowest priority, behind the token passed to `configure` and the environment variable
883-
if self.token is None:
883+
if not self.token:
884884
credentials = LogfireCredentials.load_creds_file(self.data_dir)
885885

886886
# if we still don't have a token, try initializing a new project and writing a new creds file
@@ -896,7 +896,7 @@ def add_span_processor(span_processor: SpanProcessor) -> None:
896896
self.token = credentials.token
897897
self.advanced.base_url = self.advanced.base_url or credentials.logfire_api_url
898898

899-
if self.token is not None:
899+
if self.token:
900900

901901
def check_token():
902902
assert self.token is not None

logfire/_internal/config_params.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def load_param(self, name: str, runtime: Any = None) -> Any:
160160
Returns:
161161
The value of the parameter.
162162
"""
163-
if runtime is not None:
163+
if runtime is not None and runtime != '':
164164
return runtime
165165

166166
param = CONFIG_PARAMS[name]

tests/test_configure.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,6 +1328,15 @@ def test_send_to_logfire_if_token_present_empty_via_env_var() -> None:
13281328
assert len(requests_mocker.request_history) == 0
13291329

13301330

1331+
def test_send_to_logfire_if_token_present_empty_via_arg() -> None:
1332+
with ExitStack() as stack:
1333+
stack.enter_context(mock.patch('logfire._internal.config.Confirm.ask', side_effect=RuntimeError))
1334+
requests_mocker = stack.enter_context(requests_mock.Mocker())
1335+
configure(token='', send_to_logfire='if-token-present', console=False)
1336+
wait_for_check_token_thread()
1337+
assert len(requests_mocker.request_history) == 0
1338+
1339+
13311340
def wait_for_check_token_thread():
13321341
for thread in threading.enumerate():
13331342
if thread.name == 'check_logfire_token': # pragma: no cover
@@ -1350,6 +1359,22 @@ def test_send_to_logfire_if_token_present_not_empty(capsys: pytest.CaptureFixtur
13501359
del os.environ['LOGFIRE_TOKEN']
13511360

13521361

1362+
def test_send_to_logfire_if_token_present_not_empty_via_env_with_empty_arg(capsys: pytest.CaptureFixture[str]) -> None:
1363+
os.environ['LOGFIRE_TOKEN'] = 'non_empty_token'
1364+
try:
1365+
with requests_mock.Mocker() as request_mocker:
1366+
request_mocker.get(
1367+
'https://logfire-us.pydantic.dev/v1/info',
1368+
json={'project_name': 'myproject', 'project_url': 'fake_project_url'},
1369+
)
1370+
configure(token='', send_to_logfire='if-token-present')
1371+
wait_for_check_token_thread()
1372+
assert len(request_mocker.request_history) == 1
1373+
assert capsys.readouterr().err == 'Logfire project URL: fake_project_url\n'
1374+
finally:
1375+
del os.environ['LOGFIRE_TOKEN']
1376+
1377+
13531378
def test_send_to_logfire_if_token_present_in_logfire_dir(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None:
13541379
creds_file = tmp_path / 'logfire_credentials.json'
13551380
creds_file.write_text(

0 commit comments

Comments
 (0)