|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import inspect |
| 4 | +import os |
4 | 5 | import re |
5 | 6 | import sys |
| 7 | +import warnings |
6 | 8 | from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor |
7 | 9 | from contextlib import asynccontextmanager, contextmanager |
8 | 10 | from dataclasses import dataclass |
@@ -3476,3 +3478,70 @@ def test_min_level(exporter: TestExporter, config_kwargs: dict[str, Any]) -> Non |
3476 | 3478 | assert [span['name'] for span in exporter.exported_spans_as_dict()] == snapshot( |
3477 | 3479 | ['warning span', 'notice message', 'warn message', 'default span'] |
3478 | 3480 | ) |
| 3481 | + |
| 3482 | + |
| 3483 | +def test_warn_if_not_initialized(): |
| 3484 | + """Test that warnings are properly issued when logfire is not initialized.""" |
| 3485 | + |
| 3486 | + config = LogfireConfig() |
| 3487 | + |
| 3488 | + with pytest.warns(LogfireNotConfiguredWarning) as warnings_list: |
| 3489 | + config.warn_if_not_initialized('Test message') |
| 3490 | + |
| 3491 | + assert str(warnings_list[0].message) == ( |
| 3492 | + 'Test message until `logfire.configure()` has been called. ' |
| 3493 | + 'Set the environment variable LOGFIRE_IGNORE_NO_CONFIG=1 or add ignore_no_config=true in pyproject.toml to suppress this warning.' |
| 3494 | + ) |
| 3495 | + |
| 3496 | + with patch.dict(os.environ, {'LOGFIRE_IGNORE_NO_CONFIG': '1'}): |
| 3497 | + config.warn_if_not_initialized('Should not warn with env var') |
| 3498 | + |
| 3499 | + with patch.dict(os.environ, {'LOGFIRE_IGNORE_NO_CONFIG': '0'}): |
| 3500 | + with pytest.warns(LogfireNotConfiguredWarning) as warnings_list: |
| 3501 | + config.warn_if_not_initialized('Should warn with env var 0') |
| 3502 | + |
| 3503 | + assert str(warnings_list[0].message) == ( |
| 3504 | + 'Should warn with env var 0 until `logfire.configure()` has been called. ' |
| 3505 | + 'Set the environment variable LOGFIRE_IGNORE_NO_CONFIG=1 or add ignore_no_config=true in pyproject.toml to suppress this warning.' |
| 3506 | + ) |
| 3507 | + |
| 3508 | + with patch.dict(os.environ, {'LOGFIRE_IGNORE_NO_CONFIG': ''}): |
| 3509 | + with pytest.warns(LogfireNotConfiguredWarning) as warnings_list: |
| 3510 | + config.warn_if_not_initialized('Should warn with empty env var') |
| 3511 | + |
| 3512 | + assert str(warnings_list[0].message) == ( |
| 3513 | + 'Should warn with empty env var until `logfire.configure()` has been called. ' |
| 3514 | + 'Set the environment variable LOGFIRE_IGNORE_NO_CONFIG=1 or add ignore_no_config=true in pyproject.toml to suppress this warning.' |
| 3515 | + ) |
| 3516 | + assert len(warnings_list) == 1 |
| 3517 | + |
| 3518 | + |
| 3519 | +def test_warn_if_not_initialized_with_file_config(): |
| 3520 | + """Test that warnings are suppressed when ignore_no_config is set in config file.""" |
| 3521 | + import tempfile |
| 3522 | + from pathlib import Path |
| 3523 | + |
| 3524 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 3525 | + config_dir = Path(temp_dir) |
| 3526 | + pyproject_toml = config_dir / 'pyproject.toml' |
| 3527 | + pyproject_toml.write_text('[tool.logfire]\nignore_no_config = true\n') |
| 3528 | + |
| 3529 | + config = LogfireConfig(config_dir=config_dir) |
| 3530 | + |
| 3531 | + with warnings.catch_warnings(record=True) as warning_list: |
| 3532 | + warnings.simplefilter('always') |
| 3533 | + config.warn_if_not_initialized('Should not warn due to config file') |
| 3534 | + |
| 3535 | + logfire_warnings = [w for w in warning_list if issubclass(w.category, LogfireNotConfiguredWarning)] |
| 3536 | + assert len(logfire_warnings) == 0 |
| 3537 | + |
| 3538 | + |
| 3539 | +def test_warn_if_not_initialized_category(): |
| 3540 | + """Test that the warning has the correct category.""" |
| 3541 | + config = LogfireConfig() |
| 3542 | + |
| 3543 | + with pytest.warns(LogfireNotConfiguredWarning) as warnings_list: |
| 3544 | + config.warn_if_not_initialized('Test message') |
| 3545 | + |
| 3546 | + assert warnings_list[0].category == LogfireNotConfiguredWarning |
| 3547 | + assert issubclass(LogfireNotConfiguredWarning, UserWarning) |
0 commit comments