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 14 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
94 changes: 51 additions & 43 deletions logfire/_internal/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,65 +801,73 @@ 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:
show_project_link = self.console and self.console.show_project_link
if self.send_to_logfire:
credentials: LogfireCredentials | None = None
show_project_link: bool = self.console and self.console.show_project_link or False

# try loading credentials (and thus token) from file if a token is not already available
# this takes the lowest priority, behind the token passed to `configure` and the environment variable
if self.token is None:
if (credentials := LogfireCredentials.load_creds_file(self.data_dir)) is None: # pragma: no branch
credentials = LogfireCredentials.load_creds_file(self.data_dir)

# if we still don't have a token, try initializing a new project and writing a new creds file
# note, we only do this if `send_to_logfire` is explicitly `True`
sydney-runkle marked this conversation as resolved.
Show resolved Hide resolved
if self.send_to_logfire is True and 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:

if credentials is not None:
self.token = credentials.token
self.advanced.base_url = self.advanced.base_url or credentials.logfire_api_url

if self.token is not None:

def check_token():
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()
validated_credentials = self._initialize_credentials_from_token(self.token)
if show_project_link and validated_credentials is not None:
validated_credentials.print_token_summary()

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

headers = {'User-Agent': f'logfire/{VERSION}', 'Authorization': self.token}
session = OTLPExporterHttpSession(max_body_size=OTLP_MAX_BODY_SIZE)
session.headers.update(headers)
span_exporter = OTLPSpanExporter(
endpoint=urljoin(self.advanced.base_url, '/v1/traces'),
session=session,
compression=Compression.Gzip,
)
span_exporter = RetryFewerSpansSpanExporter(span_exporter)
span_exporter = FallbackSpanExporter(
span_exporter, FileSpanExporter(self.data_dir / DEFAULT_FALLBACK_FILE_NAME, warn=True)
)
span_exporter = RemovePendingSpansExporter(span_exporter)
schedule_delay_millis = _get_int_from_env(OTEL_BSP_SCHEDULE_DELAY) or 500
add_span_processor(BatchSpanProcessor(span_exporter, schedule_delay_millis=schedule_delay_millis))

if metric_readers is not None:
metric_readers += [
PeriodicExportingMetricReader(
QuietMetricExporter(
OTLPMetricExporter(
endpoint=urljoin(self.advanced.base_url, '/v1/metrics'),
headers=headers,
session=session,
compression=Compression.Gzip,
# I'm pretty sure that this line here is redundant,
# and that passing it to the QuietMetricExporter is what matters
# because the PeriodicExportingMetricReader will read it from there.
headers = {'User-Agent': f'logfire/{VERSION}', 'Authorization': self.token}
session = OTLPExporterHttpSession(max_body_size=OTLP_MAX_BODY_SIZE)
session.headers.update(headers)
span_exporter = OTLPSpanExporter(
endpoint=urljoin(self.advanced.base_url, '/v1/traces'),
session=session,
compression=Compression.Gzip,
)
span_exporter = RetryFewerSpansSpanExporter(span_exporter)
span_exporter = FallbackSpanExporter(
span_exporter, FileSpanExporter(self.data_dir / DEFAULT_FALLBACK_FILE_NAME, warn=True)
)
span_exporter = RemovePendingSpansExporter(span_exporter)
schedule_delay_millis = _get_int_from_env(OTEL_BSP_SCHEDULE_DELAY) or 500
add_span_processor(BatchSpanProcessor(span_exporter, schedule_delay_millis=schedule_delay_millis))

if metric_readers is not None:
metric_readers += [
PeriodicExportingMetricReader(
QuietMetricExporter(
OTLPMetricExporter(
endpoint=urljoin(self.advanced.base_url, '/v1/metrics'),
headers=headers,
session=session,
compression=Compression.Gzip,
# I'm pretty sure that this line here is redundant,
# and that passing it to the QuietMetricExporter is what matters
# because the PeriodicExportingMetricReader will read it from there.
preferred_temporality=METRICS_PREFERRED_TEMPORALITY,
),
preferred_temporality=METRICS_PREFERRED_TEMPORALITY,
),
preferred_temporality=METRICS_PREFERRED_TEMPORALITY,
)
)
)
]
]

if processors_with_pending_spans:
tracer_provider.add_span_processor(
Expand Down
43 changes: 43 additions & 0 deletions tests/test_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,10 @@ def test_initialize_project_use_existing_project_no_projects(tmp_dir_cwd: Path,
request_mocker.get(
'https://logfire-api.pydantic.dev/v1/organizations/', json=[{'organization_name': 'fake_org'}]
)
request_mocker.get(
'https://logfire-api.pydantic.dev/v1/info',
json={'project_name': 'myproject', 'project_url': 'fake_project_url'},
)
create_project_response = {
'json': {
'project_name': 'myproject',
Expand Down Expand Up @@ -934,6 +938,10 @@ def test_initialize_project_use_existing_project(tmp_dir_cwd: Path, tmp_path: Pa
'https://logfire-api.pydantic.dev/v1/projects/',
json=[{'organization_name': 'fake_org', 'project_name': 'fake_project'}],
)
request_mocker.get(
'https://logfire-api.pydantic.dev/v1/info',
json={'project_name': 'myproject', 'project_url': 'fake_project_url'},
)
create_project_response = {
'json': {
'project_name': 'myproject',
Expand Down Expand Up @@ -986,6 +994,10 @@ def test_initialize_project_not_using_existing_project(
request_mocker.get(
'https://logfire-api.pydantic.dev/v1/organizations/', json=[{'organization_name': 'fake_org'}]
)
request_mocker.get(
'https://logfire-api.pydantic.dev/v1/info',
json={'project_name': 'myproject', 'project_url': 'fake_project_url'},
)
request_mocker.get(
'https://logfire-api.pydantic.dev/v1/projects/',
json=[{'organization_name': 'fake_org', 'project_name': 'fake_project'}],
Expand Down Expand Up @@ -1078,6 +1090,10 @@ def test_initialize_project_create_project(tmp_dir_cwd: Path, tmp_path: Path, ca
request_mocker.get(
'https://logfire-api.pydantic.dev/v1/organizations/', json=[{'organization_name': 'fake_org'}]
)
request_mocker.get(
'https://logfire-api.pydantic.dev/v1/info',
json={'project_name': 'myproject', 'project_url': 'fake_project_url'},
)

create_existing_project_request_json = {
'project_name': 'existingprojectname',
Expand Down Expand Up @@ -1186,6 +1202,10 @@ def test_initialize_project_create_project_default_organization(tmp_dir_cwd: Pat
'https://logfire-api.pydantic.dev/v1/organizations/',
json=[{'organization_name': 'fake_org'}, {'organization_name': 'fake_org1'}],
)
request_mocker.get(
'https://logfire-api.pydantic.dev/v1/info',
json={'project_name': 'myproject', 'project_url': 'fake_project_url'},
)
request_mocker.get(
'https://logfire-api.pydantic.dev/v1/account/me',
json={'default_organization': {'organization_name': 'fake_org1'}},
Expand Down Expand Up @@ -1297,6 +1317,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
Loading