Skip to content

Commit

Permalink
Merge branch 'main' into add-gauge-metric
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex authored May 8, 2024
2 parents 5836318 + cd9b659 commit a67d6f7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
19 changes: 17 additions & 2 deletions logfire/_internal/config_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from dataclasses import dataclass
from functools import cached_property, lru_cache
from pathlib import Path
from typing import Any, Literal, Set, TypeVar
from typing import Any, Callable, Literal, Set, TypeVar

from opentelemetry.sdk.environment_variables import OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_SERVICE_NAME
from typing_extensions import get_args, get_origin
Expand Down Expand Up @@ -47,10 +47,23 @@ class ConfigParam:
"""Type of the parameter."""


@dataclass
class _DefaultCallback:
"""A default value that is computed at runtime.
A good example is when we want to check if we are running under pytest and set a default value based on that.
"""

callback: Callable[[], Any]


_send_to_logfire_default = _DefaultCallback(lambda: 'PYTEST_CURRENT_TEST' not in os.environ)
"""When running under pytest, don't send spans to Logfire by default."""

# fmt: off
BASE_URL = ConfigParam(env_vars=['LOGFIRE_BASE_URL', OTEL_EXPORTER_OTLP_ENDPOINT], allow_file_config=True, default=LOGFIRE_BASE_URL)
"""Use to set the base URL of the Logfire backend."""
SEND_TO_LOGFIRE = ConfigParam(env_vars=['LOGFIRE_SEND_TO_LOGFIRE'], allow_file_config=True, default=True, tp=bool)
SEND_TO_LOGFIRE = ConfigParam(env_vars=['LOGFIRE_SEND_TO_LOGFIRE'], allow_file_config=True, default=_send_to_logfire_default, tp=bool)
"""Whether to send spans to Logfire."""
TOKEN = ConfigParam(env_vars=['LOGFIRE_TOKEN'])
"""Token for the Logfire API."""
Expand Down Expand Up @@ -161,6 +174,8 @@ def load_param(self, name: str, runtime: Any = None) -> Any:
if value is not None:
return self._cast(value, name, param.tp)

if isinstance(param.default, _DefaultCallback):
return self._cast(param.default.callback(), name, param.tp)
return self._cast(param.default, name, param.tp)

@cached_property
Expand Down
22 changes: 16 additions & 6 deletions tests/test_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ def default_span_processor(exporter: SpanExporter) -> SimpleSpanProcessor:
with request_mocker:
data_dir = Path(tmp_path) / 'logfire_data'
logfire.configure(
send_to_logfire=True,
data_dir=data_dir,
token='abc',
default_span_processor=default_span_processor,
Expand Down Expand Up @@ -836,7 +837,7 @@ def test_initialize_project_use_existing_project_no_projects(tmp_dir_cwd: Path,
}
request_mocker.post('https://logfire-api.pydantic.dev/v1/projects/fake_org', [create_project_response])

logfire.configure()
logfire.configure(send_to_logfire=True)

assert confirm_mock.mock_calls == [
call('The project will be created in the organization "fake_org". Continue?', default=True),
Expand Down Expand Up @@ -871,7 +872,7 @@ def test_initialize_project_use_existing_project(tmp_dir_cwd: Path, tmp_path: Pa
[create_project_response],
)

logfire.configure()
logfire.configure(send_to_logfire=True)

assert confirm_mock.mock_calls == [
call('Do you want to use one of your existing projects? ', default=True),
Expand Down Expand Up @@ -928,7 +929,7 @@ def test_initialize_project_not_using_existing_project(
[create_project_response],
)

logfire.configure()
logfire.configure(send_to_logfire=True)

assert confirm_mock.mock_calls == [
call('Do you want to use one of your existing projects? ', default=True),
Expand Down Expand Up @@ -968,7 +969,7 @@ def test_initialize_project_not_confirming_organization(tmp_path: Path) -> None:
)

with pytest.raises(SystemExit):
logfire.configure(data_dir=tmp_path)
logfire.configure(data_dir=tmp_path, send_to_logfire=True)

assert confirm_mock.mock_calls == [
call('Do you want to use one of your existing projects? ', default=True),
Expand Down Expand Up @@ -1045,7 +1046,7 @@ def test_initialize_project_create_project(tmp_dir_cwd: Path, tmp_path: Path, ca
],
)

logfire.configure()
logfire.configure(send_to_logfire=True)

for request in request_mocker.request_history:
assert request.headers['Authorization'] == 'fake_user_token'
Expand Down Expand Up @@ -1128,7 +1129,7 @@ def test_initialize_project_create_project_default_organization(tmp_dir_cwd: Pat
[create_project_response],
)

logfire.configure()
logfire.configure(send_to_logfire=True)

assert prompt_mock.mock_calls == [
call(
Expand Down Expand Up @@ -1278,3 +1279,12 @@ def test_initialize_credentials_from_token_unhealthy():
def test_configure_twice_no_warning(caplog: LogCaptureFixture):
logfire.configure(send_to_logfire=False)
assert not caplog.messages


def test_send_to_logfire_under_pytest():
"""
Test that the `send_to_logfire` parameter is set to False when running under pytest.
"""
assert 'PYTEST_CURRENT_TEST' in os.environ
logfire.configure()
assert GLOBAL_CONFIG.send_to_logfire is False

0 comments on commit a67d6f7

Please sign in to comment.