Skip to content

Commit 9a51e15

Browse files
add logfire.logfire_info() (#826)
Co-authored-by: Alex Hall <[email protected]>
1 parent bb7913b commit 9a51e15

File tree

11 files changed

+54
-56
lines changed

11 files changed

+54
-56
lines changed

.github/ISSUE_TEMPLATE/bug.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@ body:
2020
2121
**Don't worry if you can't run this command or don't have this information, we'll help you if we can.**
2222
23-
Please run the following command and copy the output below:
23+
Please run the following command in your terminal:
2424
2525
```bash
2626
logfire info
2727
```
2828
29+
Or in python run:
30+
31+
```python
32+
import logfire; print(logfire.logfire_info())
33+
```
34+
2935
render: TOML

logfire-api/logfire_api/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,7 @@ def __init__(self, *args, **kwargs) -> None: ...
231231

232232
class LogfireLoggingHandler:
233233
def __init__(self, *args, **kwargs) -> None: ...
234+
235+
def logfire_info() -> str:
236+
"""Show versions of logfire, OS and related packages."""
237+
return 'logfire_info() is not implement by logfire-api'

logfire-api/logfire_api/__init__.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from ._internal.auto_trace import AutoTraceModule as AutoTraceModule
22
from ._internal.auto_trace.rewrite_ast import no_auto_trace as no_auto_trace
3+
from ._internal.cli import logfire_info as logfire_info
34
from ._internal.config import AdvancedOptions as AdvancedOptions, CodeSource as CodeSource, ConsoleOptions as ConsoleOptions, MetricsOptions as MetricsOptions, PydanticPlugin as PydanticPlugin, configure as configure
45
from ._internal.constants import LevelName as LevelName
56
from ._internal.main import Logfire as Logfire, LogfireSpan as LogfireSpan
@@ -12,7 +13,7 @@ from .version import VERSION as VERSION
1213
from logfire.sampling import SamplingOptions as SamplingOptions
1314
from typing import Any
1415

15-
__all__ = ['Logfire', 'LogfireSpan', 'LevelName', 'AdvancedOptions', 'ConsoleOptions', 'CodeSource', 'PydanticPlugin', 'configure', 'span', 'instrument', 'log', 'trace', 'debug', 'notice', 'info', 'warn', 'warning', 'error', 'exception', 'fatal', 'force_flush', 'log_slow_async_callbacks', 'install_auto_tracing', 'instrument_asgi', 'instrument_wsgi', 'instrument_pydantic', 'instrument_fastapi', 'instrument_openai', 'instrument_anthropic', 'instrument_asyncpg', 'instrument_httpx', 'instrument_celery', 'instrument_requests', 'instrument_psycopg', 'instrument_django', 'instrument_flask', 'instrument_starlette', 'instrument_aiohttp_client', 'instrument_sqlalchemy', 'instrument_sqlite3', 'instrument_aws_lambda', 'instrument_redis', 'instrument_pymongo', 'instrument_mysql', 'instrument_system_metrics', 'AutoTraceModule', 'with_tags', 'with_settings', 'suppress_scopes', 'shutdown', 'no_auto_trace', 'ScrubMatch', 'ScrubbingOptions', 'VERSION', 'add_non_user_code_prefix', 'suppress_instrumentation', 'StructlogProcessor', 'LogfireLoggingHandler', 'loguru_handler', 'SamplingOptions', 'MetricsOptions']
16+
__all__ = ['Logfire', 'LogfireSpan', 'LevelName', 'AdvancedOptions', 'ConsoleOptions', 'CodeSource', 'PydanticPlugin', 'configure', 'span', 'instrument', 'log', 'trace', 'debug', 'notice', 'info', 'warn', 'warning', 'error', 'exception', 'fatal', 'force_flush', 'log_slow_async_callbacks', 'install_auto_tracing', 'instrument_asgi', 'instrument_wsgi', 'instrument_pydantic', 'instrument_fastapi', 'instrument_openai', 'instrument_anthropic', 'instrument_asyncpg', 'instrument_httpx', 'instrument_celery', 'instrument_requests', 'instrument_psycopg', 'instrument_django', 'instrument_flask', 'instrument_starlette', 'instrument_aiohttp_client', 'instrument_sqlalchemy', 'instrument_sqlite3', 'instrument_aws_lambda', 'instrument_redis', 'instrument_pymongo', 'instrument_mysql', 'instrument_system_metrics', 'AutoTraceModule', 'with_tags', 'with_settings', 'suppress_scopes', 'shutdown', 'no_auto_trace', 'ScrubMatch', 'ScrubbingOptions', 'VERSION', 'add_non_user_code_prefix', 'suppress_instrumentation', 'StructlogProcessor', 'LogfireLoggingHandler', 'loguru_handler', 'SamplingOptions', 'MetricsOptions', 'logfire_info']
1617

1718
DEFAULT_LOGFIRE_INSTANCE = Logfire()
1819
span = DEFAULT_LOGFIRE_INSTANCE.span

logfire-api/logfire_api/_internal/cli.pyi

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,9 @@
11
import argparse
2-
from ..version import VERSION as VERSION
3-
from .auth import DEFAULT_FILE as DEFAULT_FILE, DefaultFile as DefaultFile, HOME_LOGFIRE as HOME_LOGFIRE, is_logged_in as is_logged_in, poll_for_token as poll_for_token, request_device_code as request_device_code
4-
from .config import LogfireCredentials as LogfireCredentials
5-
from .config_params import ParamManager as ParamManager
6-
from .constants import LOGFIRE_BASE_URL as LOGFIRE_BASE_URL
7-
from .tracer import SDKTracerProvider as SDKTracerProvider
8-
from .utils import read_toml_file as read_toml_file
9-
from _typeshed import Incomplete
10-
from logfire.exceptions import LogfireConfigError as LogfireConfigError
11-
from logfire.propagate import ContextCarrier as ContextCarrier, get_context as get_context
122
from typing import Any, Sequence
133

14-
BASE_OTEL_INTEGRATION_URL: str
15-
BASE_DOCS_URL: str
16-
INTEGRATIONS_DOCS_URL: Incomplete
17-
LOGFIRE_LOG_FILE: Incomplete
18-
file_handler: Incomplete
19-
logger: Incomplete
4+
__all__ = ['main', 'logfire_info']
205

21-
def version_callback() -> None:
22-
"""Show the version and exit."""
23-
def parse_whoami(args: argparse.Namespace) -> None:
24-
"""Show user authenticated username and the URL to your Logfire project."""
25-
def parse_clean(args: argparse.Namespace) -> None:
26-
"""Remove the contents of the Logfire data directory."""
27-
28-
STANDARD_LIBRARY_PACKAGES: Incomplete
29-
OTEL_PACKAGES: set[str]
30-
OTEL_PACKAGE_LINK: Incomplete
31-
32-
def parse_inspect(args: argparse.Namespace) -> None:
33-
"""Inspect installed packages and recommend packages that might be useful."""
34-
def parse_auth(args: argparse.Namespace) -> None:
35-
"""Authenticate with Logfire.
36-
37-
This will authenticate your machine with Logfire and store the credentials.
38-
"""
39-
def parse_list_projects(args: argparse.Namespace) -> None:
40-
"""List user projects."""
41-
def parse_create_new_project(args: argparse.Namespace) -> None:
42-
"""Create a new project."""
43-
def parse_use_project(args: argparse.Namespace) -> None:
44-
"""Use an existing project."""
45-
def parse_info(_args: argparse.Namespace) -> None:
6+
def logfire_info() -> str:
467
"""Show versions of logfire, OS and related packages."""
478

489
class SplitArgs(argparse.Action):

logfire-api/logfire_api/_internal/config.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ from .metrics import ProxyMeterProvider as ProxyMeterProvider
1616
from .scrubbing import BaseScrubber as BaseScrubber, NOOP_SCRUBBER as NOOP_SCRUBBER, Scrubber as Scrubber, ScrubbingOptions as ScrubbingOptions
1717
from .stack_info import warn_at_user_stacklevel as warn_at_user_stacklevel
1818
from .tracer import OPEN_SPANS as OPEN_SPANS, PendingSpanProcessor as PendingSpanProcessor, ProxyTracerProvider as ProxyTracerProvider
19-
from .utils import SeededRandomIdGenerator as SeededRandomIdGenerator, UnexpectedResponse as UnexpectedResponse, ensure_data_dir_exists as ensure_data_dir_exists, handle_internal_errors as handle_internal_errors, read_toml_file as read_toml_file, suppress_instrumentation as suppress_instrumentation
19+
from .utils import SeededRandomIdGenerator as SeededRandomIdGenerator, UnexpectedResponse as UnexpectedResponse, ensure_data_dir_exists as ensure_data_dir_exists, handle_internal_errors as handle_internal_errors, platform_is_emscripten as platform_is_emscripten, read_toml_file as read_toml_file, suppress_instrumentation as suppress_instrumentation
2020
from _typeshed import Incomplete
2121
from dataclasses import dataclass
2222
from logfire.exceptions import LogfireConfigError as LogfireConfigError

logfire-api/logfire_api/_internal/exporters/otlp.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import requests
22
from ..stack_info import STACK_INFO_KEYS as STACK_INFO_KEYS
3-
from ..utils import logger as logger, truncate_string as truncate_string
3+
from ..utils import logger as logger, platform_is_emscripten as platform_is_emscripten, truncate_string as truncate_string
44
from .wrapper import WrapperSpanExporter as WrapperSpanExporter
55
from _typeshed import Incomplete
66
from functools import cached_property

logfire-api/logfire_api/_internal/stack_info.pyi

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,24 @@ from typing import TypedDict
55

66
StackInfo = TypedDict('StackInfo', {'code.filepath': str, 'code.lineno': int, 'code.function': str}, total=False)
77
STACK_INFO_KEYS: Incomplete
8-
SITE_PACKAGES_DIR: Incomplete
9-
PYTHON_LIB_DIR: Incomplete
10-
LOGFIRE_DIR: Incomplete
11-
NON_USER_CODE_PREFIXES: Incomplete
8+
NON_USER_CODE_PREFIXES: tuple[str, ...]
129

10+
def add_non_user_code_prefix(path: str | Path) -> None:
11+
"""Add a path to the list of prefixes that are considered non-user code.
12+
13+
This prevents the stack info from including frames from the given path.
14+
15+
This is for advanced users and shouldn't often be needed.
16+
By default, the following prefixes are already included:
17+
18+
- The standard library
19+
- site-packages (specifically wherever opentelemetry is installed)
20+
- The logfire package
21+
22+
This function is useful if you're writing a library that uses logfire and you want to exclude your library's frames.
23+
Since site-packages is already included, this is already the case by default for users of your library.
24+
But this is useful when testing your library since it's not installed in site-packages.
25+
"""
1326
def get_filepath_attribute(file: str) -> StackInfo: ...
1427
def get_code_object_info(code: CodeType) -> StackInfo: ...
1528
def get_stack_info_from_frame(frame: FrameType) -> StackInfo: ...
@@ -40,8 +53,3 @@ def is_user_code(code: CodeType) -> bool:
4053
On the other hand, generator expressions and lambdas might be called far away from where they are defined.
4154
"""
4255
def warn_at_user_stacklevel(msg: str, category: type[Warning]): ...
43-
def add_non_user_code_prefix(path: str | Path) -> None:
44-
"""Add a path to the list of prefixes that are considered non-user code.
45-
46-
This prevents the stack info from including frames from the given path.
47-
"""

logfire-api/logfire_api/_internal/utils.pyi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,9 @@ class SeededRandomIdGenerator(IdGenerator):
108108
def __post_init__(self) -> None: ...
109109
def generate_span_id(self) -> int: ...
110110
def generate_trace_id(self) -> int: ...
111+
112+
def platform_is_emscripten() -> bool:
113+
"""Return True if the platform is Emscripten, e.g. Pyodide.
114+
115+
Threads cannot be created on Emscripten, so we need to avoid any code that creates threads.
116+
"""

logfire/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from ._internal.auto_trace import AutoTraceModule
1010
from ._internal.auto_trace.rewrite_ast import no_auto_trace
11+
from ._internal.cli import logfire_info
1112
from ._internal.config import AdvancedOptions, CodeSource, ConsoleOptions, MetricsOptions, PydanticPlugin, configure
1213
from ._internal.constants import LevelName
1314
from ._internal.main import Logfire, LogfireSpan
@@ -150,4 +151,5 @@ def loguru_handler() -> Any:
150151
'loguru_handler',
151152
'SamplingOptions',
152153
'MetricsOptions',
154+
'logfire_info',
153155
)

logfire/_internal/cli.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
logging.basicConfig(handlers=[file_handler], level=logging.DEBUG)
4444

4545
logger = logging.getLogger(__name__)
46+
__all__ = 'main', 'logfire_info'
4647

4748

4849
def version_callback() -> None:
@@ -318,7 +319,7 @@ def parse_use_project(args: argparse.Namespace) -> None:
318319
)
319320

320321

321-
def parse_info(_args: argparse.Namespace) -> None:
322+
def logfire_info() -> str:
322323
"""Show versions of logfire, OS and related packages."""
323324
import importlib.metadata as importlib_metadata
324325

@@ -363,7 +364,12 @@ def parse_info(_args: argparse.Namespace) -> None:
363364
'[related_packages]',
364365
*(f'{name}="{version}"' for _, name, version in sorted(related_packages)),
365366
)
366-
sys.stderr.writelines('\n'.join(toml_lines) + '\n')
367+
return '\n'.join(toml_lines) + '\n'
368+
369+
370+
def parse_info(_args: argparse.Namespace) -> None:
371+
"""Show versions of logfire, OS and related packages."""
372+
sys.stderr.writelines(logfire_info())
367373

368374

369375
def _pretty_table(header: list[str], rows: list[list[str]]):

0 commit comments

Comments
 (0)