Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check .logfire for creds to respect 'if-token-present' setting #561

Merged
merged 20 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions logfire/_internal/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,16 +801,21 @@ def add_span_processor(span_processor: SpanProcessor) -> None:
if isinstance(self.metrics, MetricsOptions):
metric_readers = list(self.metrics.additional_readers)

creds_from_file = LogfireCredentials.load_creds_file(self.data_dir)
sydney-runkle marked this conversation as resolved.
Show resolved Hide resolved
# token from creds file takes the lowest priority
if self.token is None and creds_from_file is not None:
sydney-runkle marked this conversation as resolved.
Show resolved Hide resolved
self.token = creds_from_file.token
sydney-runkle marked this conversation as resolved.
Show resolved Hide resolved
self.advanced.base_url = self.advanced.base_url or creds_from_file.logfire_api_url

if (self.send_to_logfire == 'if-token-present' and self.token is not None) or self.send_to_logfire is True:
show_project_link = self.console and self.console.show_project_link

if self.token is None:
if (credentials := LogfireCredentials.load_creds_file(self.data_dir)) is None: # pragma: no branch
credentials = LogfireCredentials.initialize_project(
logfire_api_url=self.advanced.base_url,
session=requests.Session(),
)
credentials.write_creds_file(self.data_dir)
credentials = LogfireCredentials.initialize_project(
logfire_api_url=self.advanced.base_url,
session=requests.Session(),
)
credentials.write_creds_file(self.data_dir)
self.token = credentials.token
self.advanced.base_url = self.advanced.base_url or credentials.logfire_api_url
if show_project_link: # pragma: no branch
Expand Down
23 changes: 23 additions & 0 deletions tests/test_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,29 @@ def test_send_to_logfire_if_token_present_not_empty(capsys: pytest.CaptureFixtur
del os.environ['LOGFIRE_TOKEN']


def test_send_to_logfire_if_token_present_in_logfire_dir(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None:
creds_file = tmp_path / 'logfire_credentials.json'
creds_file.write_text(
"""
{
"token": "foobar",
"project_name": "myproject",
"project_url": "http://dash.localhost:8000/",
"logfire_api_url": "http://dash.localhost:8000/"
}
"""
)
with requests_mock.Mocker() as request_mocker:
request_mocker.get(
'https://logfire-api.pydantic.dev/v1/info',
sydney-runkle marked this conversation as resolved.
Show resolved Hide resolved
json={'project_name': 'myproject', 'project_url': 'http://dash.localhost:8000/'},
)
configure(send_to_logfire='if-token-present', data_dir=tmp_path)
alexmojaki marked this conversation as resolved.
Show resolved Hide resolved
wait_for_check_token_thread()
assert len(request_mocker.request_history) == 1
assert capsys.readouterr().err == 'Logfire project URL: http://dash.localhost:8000/\n'


def test_load_creds_file_invalid_json_content(tmp_path: Path):
creds_file = tmp_path / 'logfire_credentials.json'
creds_file.write_text('invalid-data')
Expand Down