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 3 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
18 changes: 11 additions & 7 deletions logfire/_internal/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,31 +801,35 @@ def add_span_processor(span_processor: SpanProcessor) -> None:
if isinstance(self.metrics, MetricsOptions):
metric_readers = list(self.metrics.additional_readers)

if (self.send_to_logfire == 'if-token-present' and self.token is not None) or self.send_to_logfire is True:
credentials = LogfireCredentials.load_creds_file(self.data_dir) # pragma: no branch
sydney-runkle marked this conversation as resolved.
Show resolved Hide resolved
token_present = (self.token is not None) or (credentials is not None)

if (self.send_to_logfire == 'if-token-present' and token_present) 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
if credentials is None:
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
credentials.print_token_summary()
else:

def check_token():
nonlocal credentials

assert self.token is not None
creds = self._initialize_credentials_from_token(self.token)
if show_project_link and creds is not None: # pragma: no branch
creds.print_token_summary()
credentials = credentials or self._initialize_credentials_from_token(self.token)

thread = Thread(target=check_token, name='check_logfire_token')
thread.start()

if show_project_link and credentials is not None: # pragma: no branch
credentials.print_token_summary()

headers = {'User-Agent': f'logfire/{VERSION}', 'Authorization': self.token}
session = OTLPExporterHttpSession(max_body_size=OTLP_MAX_BODY_SIZE)
session.headers.update(headers)
Expand Down
22 changes: 22 additions & 0 deletions tests/test_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,28 @@ 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
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
Loading