Skip to content

Commit

Permalink
Improve error raised when opentelemetry.instrumentation.django is not…
Browse files Browse the repository at this point in the history
… installed (#231)

Co-authored-by: Alex Hall <[email protected]>
  • Loading branch information
deepakdinesh1123 and alexmojaki authored Jun 3, 2024
1 parent f600862 commit 7c217f0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
9 changes: 8 additions & 1 deletion logfire/_internal/integrations/django.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
from typing import Any

from opentelemetry.instrumentation.django import DjangoInstrumentor
try:
from opentelemetry.instrumentation.django import DjangoInstrumentor
except ModuleNotFoundError:
raise RuntimeError(
'`logfire.instrument_django()` requires the `opentelemetry-instrumentation-django` package.\n'
'You can install this with:\n'
" pip install 'logfire[django]'"
)


def instrument_django(**kwargs: Any):
Expand Down
18 changes: 18 additions & 0 deletions tests/otel_integrations/test_django.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import importlib
from unittest import mock

import pytest
from django.http import HttpResponse
from django.test import Client
from inline_snapshot import snapshot

import logfire
import logfire._internal
import logfire._internal.integrations
import logfire._internal.integrations.django
from logfire.testing import TestExporter


Expand Down Expand Up @@ -115,3 +122,14 @@ def test_no_matching_route(client: Client, exporter: TestExporter):
}
]
)


def test_missing_opentelemetry_dependency() -> None:
with mock.patch.dict('sys.modules', {'opentelemetry.instrumentation.django': None}):
with pytest.raises(RuntimeError) as exc_info:
importlib.reload(logfire._internal.integrations.django)
assert str(exc_info.value) == snapshot("""\
`logfire.instrument_django()` requires the `opentelemetry-instrumentation-django` package.
You can install this with:
pip install 'logfire[django]'\
""")

0 comments on commit 7c217f0

Please sign in to comment.