Skip to content

Commit

Permalink
Don't show urllib3 when requests is installed on logfire inspect (
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex authored Dec 30, 2024
1 parent ed1ec08 commit 9f73d99
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
7 changes: 5 additions & 2 deletions logfire/_internal/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import argparse
import functools
import importlib
import importlib.metadata
import importlib.util
import logging
import platform
Expand Down Expand Up @@ -107,7 +108,9 @@ def parse_clean(args: argparse.Namespace) -> None:

# TODO(Marcelo): Automatically check if this list should be updated.
# NOTE: List of packages from https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation.
STANDARD_LIBRARY_PACKAGES = {'urllib', 'sqlite3'}
OTEL_PACKAGES: set[str] = {
*STANDARD_LIBRARY_PACKAGES,
'aio_pika',
'aiohttp',
'aiopg',
Expand Down Expand Up @@ -136,11 +139,9 @@ def parse_clean(args: argparse.Namespace) -> None:
'remoulade',
'requests',
'sqlalchemy',
'sqlite3',
'starlette',
'tornado',
'tortoise_orm',
'urllib',
'urllib3',
}
OTEL_PACKAGE_LINK = {'aiohttp': 'aiohttp-client', 'tortoise_orm': 'tortoiseorm', 'scikit-learn': 'sklearn'}
Expand Down Expand Up @@ -171,6 +172,8 @@ def parse_inspect(args: argparse.Namespace) -> None:
# Drop packages that are dependencies of other packages.
if packages.get('starlette') and packages.get('fastapi'):
del packages['starlette']
if packages.get('urllib3') and packages.get('requests'):
del packages['urllib3']

for name, otel_package in sorted(packages.items()):
package_name = otel_package.replace('.', '-')
Expand Down
57 changes: 49 additions & 8 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import sys
import webbrowser
from contextlib import ExitStack
from importlib.machinery import ModuleSpec
from pathlib import Path
from unittest.mock import call, patch

Expand All @@ -19,7 +20,7 @@

import logfire._internal.cli
from logfire import VERSION
from logfire._internal.cli import OTEL_PACKAGES, main
from logfire._internal.cli import STANDARD_LIBRARY_PACKAGES, main
from logfire._internal.config import LogfireCredentials, sanitize_project_name
from logfire.exceptions import LogfireConfigError

Expand Down Expand Up @@ -208,18 +209,58 @@ def test_inspect(
assert capsys.readouterr().err.startswith('The following packages')


def test_inspect_drop_dependant_packages(
tmp_dir_cwd: Path, logfire_credentials: LogfireCredentials, capsys: pytest.CaptureFixture[str]
def packages_from_output(output: str) -> set[str]:
pattern = r'│\s*(\w+)\s*│\s*([\w-]+)\s*│'
matches = re.findall(pattern, output)
return {match[1] for match in matches}


@pytest.mark.parametrize(
('installed', 'should_install'),
[
(
['fastapi'],
{
'opentelemetry-instrumentation-fastapi',
'opentelemetry-instrumentation-urllib',
'opentelemetry-instrumentation-sqlite3',
},
),
(
['fastapi', 'starlette'],
{
'opentelemetry-instrumentation-fastapi',
'opentelemetry-instrumentation-urllib',
'opentelemetry-instrumentation-sqlite3',
},
),
(
['urllib3', 'requests'],
{
'opentelemetry-instrumentation-requests',
'opentelemetry-instrumentation-urllib',
'opentelemetry-instrumentation-sqlite3',
},
),
],
)
def test_inspect_with_dependencies(
tmp_dir_cwd: Path,
logfire_credentials: LogfireCredentials,
installed: list[str],
should_install: list[str],
capsys: pytest.CaptureFixture[str],
) -> None:
logfire_credentials.write_creds_file(tmp_dir_cwd / '.logfire')
with ExitStack() as stack:
find_spec = stack.enter_context(patch('importlib.util.find_spec'))
find_spec.side_effect = [True, None] * len(OTEL_PACKAGES)

def new_find_spec(name: str) -> ModuleSpec | None:
if name in STANDARD_LIBRARY_PACKAGES or name in installed:
return ModuleSpec(name, None)

with patch('importlib.util.find_spec', new=new_find_spec):
main(['inspect'])
output = capsys.readouterr().err
assert 'opentelemetry-instrumentation-fastapi' in output
assert 'opentelemetry-instrumentation-starlette' not in output
assert packages_from_output(output) == should_install


@pytest.mark.parametrize('webbrowser_error', [False, True])
Expand Down

0 comments on commit 9f73d99

Please sign in to comment.