|
17 | 17 | from collections import OrderedDict |
18 | 18 | from collections.abc import MutableMapping |
19 | 19 | from typing import Mapping, Optional, Sequence, Tuple, Union |
20 | | -import django |
21 | | -from django.core.handlers.wsgi import WSGIRequest |
22 | 20 | from opentelemetry.util import types |
23 | 21 |
|
| 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 | + |
24 | 30 | # bytes are accepted as a user supplied value for attributes but |
25 | 31 | # decoded to strings internally. |
26 | 32 | _VALID_ATTR_VALUE_TYPES = (bool, str, bytes, int, float) |
|
34 | 40 | str, |
35 | 41 | Sequence, |
36 | 42 | Mapping, |
37 | | - WSGIRequest, |
38 | | -) |
| 43 | +) + ((WSGIRequest,) if _HAS_DJANGO else ()) |
39 | 44 |
|
40 | 45 |
|
41 | 46 | _logger = logging.getLogger(__name__) |
@@ -130,7 +135,7 @@ def _clean_extended_attribute_value( |
130 | 135 | value = value[:max_len] |
131 | 136 | return value |
132 | 137 |
|
133 | | - if isinstance(value, WSGIRequest): |
| 138 | + if _HAS_DJANGO and WSGIRequest is not None and isinstance(value, WSGIRequest): |
134 | 139 | wsgi_data = { |
135 | 140 | "method": getattr(value, "method", None), |
136 | 141 | "path": getattr(value, "path", None), |
|
0 commit comments