Skip to content

Commit 6b76a8e

Browse files
committed
Fixed import issue
1 parent 4ead19a commit 6b76a8e

File tree

1 file changed

+10
-5
lines changed
  • opentelemetry-api/src/opentelemetry/attributes

1 file changed

+10
-5
lines changed

opentelemetry-api/src/opentelemetry/attributes/__init__.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@
1717
from collections import OrderedDict
1818
from collections.abc import MutableMapping
1919
from typing import Mapping, Optional, Sequence, Tuple, Union
20-
import django
21-
from django.core.handlers.wsgi import WSGIRequest
2220
from opentelemetry.util import types
2321

22+
# Optional Django imports - only available if Django is installed
23+
try:
24+
from django.core.handlers.wsgi import WSGIRequest
25+
_HAS_DJANGO = True
26+
except ImportError:
27+
WSGIRequest = None # type: ignore
28+
_HAS_DJANGO = False
29+
2430
# bytes are accepted as a user supplied value for attributes but
2531
# decoded to strings internally.
2632
_VALID_ATTR_VALUE_TYPES = (bool, str, bytes, int, float)
@@ -34,8 +40,7 @@
3440
str,
3541
Sequence,
3642
Mapping,
37-
WSGIRequest,
38-
)
43+
) + ((WSGIRequest,) if _HAS_DJANGO else ())
3944

4045

4146
_logger = logging.getLogger(__name__)
@@ -130,7 +135,7 @@ def _clean_extended_attribute_value(
130135
value = value[:max_len]
131136
return value
132137

133-
if isinstance(value, WSGIRequest):
138+
if _HAS_DJANGO and WSGIRequest is not None and isinstance(value, WSGIRequest):
134139
wsgi_data = {
135140
"method": getattr(value, "method", None),
136141
"path": getattr(value, "path", None),

0 commit comments

Comments
 (0)