transport.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. from abc import ABC, abstractmethod
  2. import io
  3. import os
  4. import gzip
  5. import socket
  6. import ssl
  7. import time
  8. import warnings
  9. from datetime import datetime, timedelta, timezone
  10. from collections import defaultdict
  11. from urllib.request import getproxies
  12. try:
  13. import brotli # type: ignore
  14. except ImportError:
  15. brotli = None
  16. import urllib3
  17. import certifi
  18. import sentry_sdk
  19. from sentry_sdk.consts import EndpointType
  20. from sentry_sdk.utils import Dsn, logger, capture_internal_exceptions
  21. from sentry_sdk.worker import BackgroundWorker
  22. from sentry_sdk.envelope import Envelope, Item, PayloadRef
  23. from typing import TYPE_CHECKING, cast, List, Dict
  24. if TYPE_CHECKING:
  25. from typing import Any
  26. from typing import Callable
  27. from typing import DefaultDict
  28. from typing import Iterable
  29. from typing import Mapping
  30. from typing import Optional
  31. from typing import Self
  32. from typing import Tuple
  33. from typing import Type
  34. from typing import Union
  35. from urllib3.poolmanager import PoolManager
  36. from urllib3.poolmanager import ProxyManager
  37. from sentry_sdk._types import Event, EventDataCategory
  38. KEEP_ALIVE_SOCKET_OPTIONS = []
  39. for option in [
  40. (socket.SOL_SOCKET, lambda: getattr(socket, "SO_KEEPALIVE"), 1), # noqa: B009
  41. (socket.SOL_TCP, lambda: getattr(socket, "TCP_KEEPIDLE"), 45), # noqa: B009
  42. (socket.SOL_TCP, lambda: getattr(socket, "TCP_KEEPINTVL"), 10), # noqa: B009
  43. (socket.SOL_TCP, lambda: getattr(socket, "TCP_KEEPCNT"), 6), # noqa: B009
  44. ]:
  45. try:
  46. KEEP_ALIVE_SOCKET_OPTIONS.append((option[0], option[1](), option[2]))
  47. except AttributeError:
  48. # a specific option might not be available on specific systems,
  49. # e.g. TCP_KEEPIDLE doesn't exist on macOS
  50. pass
  51. class Transport(ABC):
  52. """Baseclass for all transports.
  53. A transport is used to send an event to sentry.
  54. """
  55. parsed_dsn: "Optional[Dsn]" = None
  56. def __init__(self: "Self", options: "Optional[Dict[str, Any]]" = None) -> None:
  57. self.options = options
  58. if options and options["dsn"] is not None and options["dsn"]:
  59. self.parsed_dsn = Dsn(options["dsn"], options.get("org_id"))
  60. else:
  61. self.parsed_dsn = None
  62. def capture_event(self: "Self", event: "Event") -> None:
  63. """
  64. DEPRECATED: Please use capture_envelope instead.
  65. This gets invoked with the event dictionary when an event should
  66. be sent to sentry.
  67. """
  68. warnings.warn(
  69. "capture_event is deprecated, please use capture_envelope instead!",
  70. DeprecationWarning,
  71. stacklevel=2,
  72. )
  73. envelope = Envelope()
  74. envelope.add_event(event)
  75. self.capture_envelope(envelope)
  76. @abstractmethod
  77. def capture_envelope(self: "Self", envelope: "Envelope") -> None:
  78. """
  79. Send an envelope to Sentry.
  80. Envelopes are a data container format that can hold any type of data
  81. submitted to Sentry. We use it to send all event data (including errors,
  82. transactions, crons check-ins, etc.) to Sentry.
  83. """
  84. pass
  85. def flush(
  86. self: "Self",
  87. timeout: float,
  88. callback: "Optional[Any]" = None,
  89. ) -> None:
  90. """
  91. Wait `timeout` seconds for the current events to be sent out.
  92. The default implementation is a no-op, since this method may only be relevant to some transports.
  93. Subclasses should override this method if necessary.
  94. """
  95. return None
  96. def kill(self: "Self") -> None:
  97. """
  98. Forcefully kills the transport.
  99. The default implementation is a no-op, since this method may only be relevant to some transports.
  100. Subclasses should override this method if necessary.
  101. """
  102. return None
  103. def record_lost_event(
  104. self,
  105. reason: str,
  106. data_category: "Optional[EventDataCategory]" = None,
  107. item: "Optional[Item]" = None,
  108. *,
  109. quantity: int = 1,
  110. ) -> None:
  111. """This increments a counter for event loss by reason and
  112. data category by the given positive-int quantity (default 1).
  113. If an item is provided, the data category and quantity are
  114. extracted from the item, and the values passed for
  115. data_category and quantity are ignored.
  116. When recording a lost transaction via data_category="transaction",
  117. the calling code should also record the lost spans via this method.
  118. When recording lost spans, `quantity` should be set to the number
  119. of contained spans, plus one for the transaction itself. When
  120. passing an Item containing a transaction via the `item` parameter,
  121. this method automatically records the lost spans.
  122. """
  123. return None
  124. def is_healthy(self: "Self") -> bool:
  125. return True
  126. def _parse_rate_limits(
  127. header: str, now: "Optional[datetime]" = None
  128. ) -> "Iterable[Tuple[Optional[EventDataCategory], datetime]]":
  129. if now is None:
  130. now = datetime.now(timezone.utc)
  131. for limit in header.split(","):
  132. try:
  133. parameters = limit.strip().split(":")
  134. retry_after_val, categories = parameters[:2]
  135. retry_after = now + timedelta(seconds=int(retry_after_val))
  136. for category in categories and categories.split(";") or (None,):
  137. yield category, retry_after # type: ignore
  138. except (LookupError, ValueError):
  139. continue
  140. class BaseHttpTransport(Transport):
  141. """The base HTTP transport."""
  142. TIMEOUT = 30 # seconds
  143. def __init__(self: "Self", options: "Dict[str, Any]") -> None:
  144. from sentry_sdk.consts import VERSION
  145. Transport.__init__(self, options)
  146. assert self.parsed_dsn is not None
  147. self.options: "Dict[str, Any]" = options
  148. self._worker = BackgroundWorker(queue_size=options["transport_queue_size"])
  149. self._auth = self.parsed_dsn.to_auth("sentry.python/%s" % VERSION)
  150. self._disabled_until: "Dict[Optional[EventDataCategory], datetime]" = {}
  151. # We only use this Retry() class for the `get_retry_after` method it exposes
  152. self._retry = urllib3.util.Retry()
  153. self._discarded_events: "DefaultDict[Tuple[EventDataCategory, str], int]" = (
  154. defaultdict(int)
  155. )
  156. self._last_client_report_sent = time.time()
  157. self._pool = self._make_pool()
  158. # Backwards compatibility for deprecated `self.hub_class` attribute
  159. self._hub_cls = sentry_sdk.Hub
  160. experiments = options.get("_experiments", {})
  161. compression_level = experiments.get(
  162. "transport_compression_level",
  163. experiments.get("transport_zlib_compression_level"),
  164. )
  165. compression_algo = experiments.get(
  166. "transport_compression_algo",
  167. (
  168. "gzip"
  169. # if only compression level is set, assume gzip for backwards compatibility
  170. # if we don't have brotli available, fallback to gzip
  171. if compression_level is not None or brotli is None
  172. else "br"
  173. ),
  174. )
  175. if compression_algo == "br" and brotli is None:
  176. logger.warning(
  177. "You asked for brotli compression without the Brotli module, falling back to gzip -9"
  178. )
  179. compression_algo = "gzip"
  180. compression_level = None
  181. if compression_algo not in ("br", "gzip"):
  182. logger.warning(
  183. "Unknown compression algo %s, disabling compression", compression_algo
  184. )
  185. self._compression_level = 0
  186. self._compression_algo = None
  187. else:
  188. self._compression_algo = compression_algo
  189. if compression_level is not None:
  190. self._compression_level = compression_level
  191. elif self._compression_algo == "gzip":
  192. self._compression_level = 9
  193. elif self._compression_algo == "br":
  194. self._compression_level = 4
  195. def record_lost_event(
  196. self,
  197. reason: str,
  198. data_category: "Optional[EventDataCategory]" = None,
  199. item: "Optional[Item]" = None,
  200. *,
  201. quantity: int = 1,
  202. ) -> None:
  203. if not self.options["send_client_reports"]:
  204. return
  205. if item is not None:
  206. data_category = item.data_category
  207. quantity = 1 # If an item is provided, we always count it as 1 (except for attachments, handled below).
  208. if data_category == "transaction":
  209. # Also record the lost spans
  210. event = item.get_transaction_event() or {}
  211. # +1 for the transaction itself
  212. span_count = (
  213. len(cast(List[Dict[str, object]], event.get("spans") or [])) + 1
  214. )
  215. self.record_lost_event(reason, "span", quantity=span_count)
  216. elif data_category == "log_item" and item:
  217. # Also record size of lost logs in bytes
  218. bytes_size = len(item.get_bytes())
  219. self.record_lost_event(reason, "log_byte", quantity=bytes_size)
  220. elif data_category == "attachment":
  221. # quantity of 0 is actually 1 as we do not want to count
  222. # empty attachments as actually empty.
  223. quantity = len(item.get_bytes()) or 1
  224. elif data_category is None:
  225. raise TypeError("data category not provided")
  226. self._discarded_events[data_category, reason] += quantity
  227. def _get_header_value(
  228. self: "Self", response: "Any", header: str
  229. ) -> "Optional[str]":
  230. return response.headers.get(header)
  231. def _update_rate_limits(
  232. self: "Self", response: "Union[urllib3.BaseHTTPResponse, httpcore.Response]"
  233. ) -> None:
  234. # new sentries with more rate limit insights. We honor this header
  235. # no matter of the status code to update our internal rate limits.
  236. header = self._get_header_value(response, "x-sentry-rate-limits")
  237. if header:
  238. logger.warning("Rate-limited via x-sentry-rate-limits")
  239. self._disabled_until.update(_parse_rate_limits(header))
  240. # old sentries only communicate global rate limit hits via the
  241. # retry-after header on 429. This header can also be emitted on new
  242. # sentries if a proxy in front wants to globally slow things down.
  243. elif response.status == 429:
  244. logger.warning("Rate-limited via 429")
  245. retry_after_value = self._get_header_value(response, "Retry-After")
  246. retry_after = (
  247. self._retry.parse_retry_after(retry_after_value)
  248. if retry_after_value is not None
  249. else None
  250. ) or 60
  251. self._disabled_until[None] = datetime.now(timezone.utc) + timedelta(
  252. seconds=retry_after
  253. )
  254. def _send_request(
  255. self: "Self",
  256. body: bytes,
  257. headers: "Dict[str, str]",
  258. endpoint_type: "EndpointType" = EndpointType.ENVELOPE,
  259. envelope: "Optional[Envelope]" = None,
  260. ) -> None:
  261. def record_loss(reason: str) -> None:
  262. if envelope is None:
  263. self.record_lost_event(reason, data_category="error")
  264. else:
  265. for item in envelope.items:
  266. self.record_lost_event(reason, item=item)
  267. headers.update(
  268. {
  269. "User-Agent": str(self._auth.client),
  270. "X-Sentry-Auth": str(self._auth.to_header()),
  271. }
  272. )
  273. try:
  274. response = self._request(
  275. "POST",
  276. endpoint_type,
  277. body,
  278. headers,
  279. )
  280. except Exception:
  281. self.on_dropped_event("network")
  282. record_loss("network_error")
  283. raise
  284. try:
  285. self._update_rate_limits(response)
  286. if response.status == 429:
  287. # if we hit a 429. Something was rate limited but we already
  288. # acted on this in `self._update_rate_limits`. Note that we
  289. # do not want to record event loss here as we will have recorded
  290. # an outcome in relay already.
  291. self.on_dropped_event("status_429")
  292. pass
  293. elif response.status >= 300 or response.status < 200:
  294. logger.error(
  295. "Unexpected status code: %s (body: %s)",
  296. response.status,
  297. getattr(response, "data", getattr(response, "content", None)),
  298. )
  299. self.on_dropped_event("status_{}".format(response.status))
  300. record_loss("network_error")
  301. finally:
  302. response.close()
  303. def on_dropped_event(self: "Self", _reason: str) -> None:
  304. return None
  305. def _fetch_pending_client_report(
  306. self: "Self", force: bool = False, interval: int = 60
  307. ) -> "Optional[Item]":
  308. if not self.options["send_client_reports"]:
  309. return None
  310. if not (force or self._last_client_report_sent < time.time() - interval):
  311. return None
  312. discarded_events = self._discarded_events
  313. self._discarded_events = defaultdict(int)
  314. self._last_client_report_sent = time.time()
  315. if not discarded_events:
  316. return None
  317. return Item(
  318. PayloadRef(
  319. json={
  320. "timestamp": time.time(),
  321. "discarded_events": [
  322. {"reason": reason, "category": category, "quantity": quantity}
  323. for (
  324. (category, reason),
  325. quantity,
  326. ) in discarded_events.items()
  327. ],
  328. }
  329. ),
  330. type="client_report",
  331. )
  332. def _flush_client_reports(self: "Self", force: bool = False) -> None:
  333. client_report = self._fetch_pending_client_report(force=force, interval=60)
  334. if client_report is not None:
  335. self.capture_envelope(Envelope(items=[client_report]))
  336. def _check_disabled(self, category: str) -> bool:
  337. def _disabled(bucket: "Any") -> bool:
  338. ts = self._disabled_until.get(bucket)
  339. return ts is not None and ts > datetime.now(timezone.utc)
  340. return _disabled(category) or _disabled(None)
  341. def _is_rate_limited(self: "Self") -> bool:
  342. return any(
  343. ts > datetime.now(timezone.utc) for ts in self._disabled_until.values()
  344. )
  345. def _is_worker_full(self: "Self") -> bool:
  346. return self._worker.full()
  347. def is_healthy(self: "Self") -> bool:
  348. return not (self._is_worker_full() or self._is_rate_limited())
  349. def _send_envelope(self: "Self", envelope: "Envelope") -> None:
  350. # remove all items from the envelope which are over quota
  351. new_items = []
  352. for item in envelope.items:
  353. if self._check_disabled(item.data_category):
  354. if item.data_category in ("transaction", "error", "default", "statsd"):
  355. self.on_dropped_event("self_rate_limits")
  356. self.record_lost_event("ratelimit_backoff", item=item)
  357. else:
  358. new_items.append(item)
  359. # Since we're modifying the envelope here make a copy so that others
  360. # that hold references do not see their envelope modified.
  361. envelope = Envelope(headers=envelope.headers, items=new_items)
  362. if not envelope.items:
  363. return None
  364. # since we're already in the business of sending out an envelope here
  365. # check if we have one pending for the stats session envelopes so we
  366. # can attach it to this enveloped scheduled for sending. This will
  367. # currently typically attach the client report to the most recent
  368. # session update.
  369. client_report_item = self._fetch_pending_client_report(interval=30)
  370. if client_report_item is not None:
  371. envelope.items.append(client_report_item)
  372. content_encoding, body = self._serialize_envelope(envelope)
  373. assert self.parsed_dsn is not None
  374. logger.debug(
  375. "Sending envelope [%s] project:%s host:%s",
  376. envelope.description,
  377. self.parsed_dsn.project_id,
  378. self.parsed_dsn.host,
  379. )
  380. headers = {
  381. "Content-Type": "application/x-sentry-envelope",
  382. }
  383. if content_encoding:
  384. headers["Content-Encoding"] = content_encoding
  385. self._send_request(
  386. body.getvalue(),
  387. headers=headers,
  388. endpoint_type=EndpointType.ENVELOPE,
  389. envelope=envelope,
  390. )
  391. return None
  392. def _serialize_envelope(
  393. self: "Self", envelope: "Envelope"
  394. ) -> "tuple[Optional[str], io.BytesIO]":
  395. content_encoding = None
  396. body = io.BytesIO()
  397. if self._compression_level == 0 or self._compression_algo is None:
  398. envelope.serialize_into(body)
  399. else:
  400. content_encoding = self._compression_algo
  401. if self._compression_algo == "br" and brotli is not None:
  402. body.write(
  403. brotli.compress(
  404. envelope.serialize(), quality=self._compression_level
  405. )
  406. )
  407. else: # assume gzip as we sanitize the algo value in init
  408. with gzip.GzipFile(
  409. fileobj=body, mode="w", compresslevel=self._compression_level
  410. ) as f:
  411. envelope.serialize_into(f)
  412. return content_encoding, body
  413. def _get_pool_options(self: "Self") -> "Dict[str, Any]":
  414. raise NotImplementedError()
  415. def _in_no_proxy(self: "Self", parsed_dsn: "Dsn") -> bool:
  416. no_proxy = getproxies().get("no")
  417. if not no_proxy:
  418. return False
  419. for host in no_proxy.split(","):
  420. host = host.strip()
  421. if parsed_dsn.host.endswith(host) or parsed_dsn.netloc.endswith(host):
  422. return True
  423. return False
  424. def _make_pool(
  425. self: "Self",
  426. ) -> "Union[PoolManager, ProxyManager, httpcore.SOCKSProxy, httpcore.HTTPProxy, httpcore.ConnectionPool]":
  427. raise NotImplementedError()
  428. def _request(
  429. self: "Self",
  430. method: str,
  431. endpoint_type: "EndpointType",
  432. body: "Any",
  433. headers: "Mapping[str, str]",
  434. ) -> "Union[urllib3.BaseHTTPResponse, httpcore.Response]":
  435. raise NotImplementedError()
  436. def capture_envelope(
  437. self,
  438. envelope: "Envelope",
  439. ) -> None:
  440. def send_envelope_wrapper() -> None:
  441. with capture_internal_exceptions():
  442. self._send_envelope(envelope)
  443. self._flush_client_reports()
  444. if not self._worker.submit(send_envelope_wrapper):
  445. self.on_dropped_event("full_queue")
  446. for item in envelope.items:
  447. self.record_lost_event("queue_overflow", item=item)
  448. def flush(
  449. self: "Self",
  450. timeout: float,
  451. callback: "Optional[Callable[[int, float], None]]" = None,
  452. ) -> None:
  453. logger.debug("Flushing HTTP transport")
  454. if timeout > 0:
  455. self._worker.submit(lambda: self._flush_client_reports(force=True))
  456. self._worker.flush(timeout, callback)
  457. def kill(self: "Self") -> None:
  458. logger.debug("Killing HTTP transport")
  459. self._worker.kill()
  460. @staticmethod
  461. def _warn_hub_cls() -> None:
  462. """Convenience method to warn users about the deprecation of the `hub_cls` attribute."""
  463. warnings.warn(
  464. "The `hub_cls` attribute is deprecated and will be removed in a future release.",
  465. DeprecationWarning,
  466. stacklevel=3,
  467. )
  468. @property
  469. def hub_cls(self: "Self") -> "type[sentry_sdk.Hub]":
  470. """DEPRECATED: This attribute is deprecated and will be removed in a future release."""
  471. HttpTransport._warn_hub_cls()
  472. return self._hub_cls
  473. @hub_cls.setter
  474. def hub_cls(self: "Self", value: "type[sentry_sdk.Hub]") -> None:
  475. """DEPRECATED: This attribute is deprecated and will be removed in a future release."""
  476. HttpTransport._warn_hub_cls()
  477. self._hub_cls = value
  478. class HttpTransport(BaseHttpTransport):
  479. if TYPE_CHECKING:
  480. _pool: "Union[PoolManager, ProxyManager]"
  481. def _get_pool_options(self: "Self") -> "Dict[str, Any]":
  482. num_pools = self.options.get("_experiments", {}).get("transport_num_pools")
  483. options = {
  484. "num_pools": 2 if num_pools is None else int(num_pools),
  485. "cert_reqs": "CERT_REQUIRED",
  486. "timeout": urllib3.Timeout(total=self.TIMEOUT),
  487. }
  488. socket_options: "Optional[List[Tuple[int, int, int | bytes]]]" = None
  489. if self.options["socket_options"] is not None:
  490. socket_options = self.options["socket_options"]
  491. if self.options["keep_alive"]:
  492. if socket_options is None:
  493. socket_options = []
  494. used_options = {(o[0], o[1]) for o in socket_options}
  495. for default_option in KEEP_ALIVE_SOCKET_OPTIONS:
  496. if (default_option[0], default_option[1]) not in used_options:
  497. socket_options.append(default_option)
  498. if socket_options is not None:
  499. options["socket_options"] = socket_options
  500. options["ca_certs"] = (
  501. self.options["ca_certs"] # User-provided bundle from the SDK init
  502. or os.environ.get("SSL_CERT_FILE")
  503. or os.environ.get("REQUESTS_CA_BUNDLE")
  504. or certifi.where()
  505. )
  506. options["cert_file"] = self.options["cert_file"] or os.environ.get(
  507. "CLIENT_CERT_FILE"
  508. )
  509. options["key_file"] = self.options["key_file"] or os.environ.get(
  510. "CLIENT_KEY_FILE"
  511. )
  512. return options
  513. def _make_pool(self: "Self") -> "Union[PoolManager, ProxyManager]":
  514. if self.parsed_dsn is None:
  515. raise ValueError("Cannot create HTTP-based transport without valid DSN")
  516. proxy = None
  517. no_proxy = self._in_no_proxy(self.parsed_dsn)
  518. # try HTTPS first
  519. https_proxy = self.options["https_proxy"]
  520. if self.parsed_dsn.scheme == "https" and (https_proxy != ""):
  521. proxy = https_proxy or (not no_proxy and getproxies().get("https"))
  522. # maybe fallback to HTTP proxy
  523. http_proxy = self.options["http_proxy"]
  524. if not proxy and (http_proxy != ""):
  525. proxy = http_proxy or (not no_proxy and getproxies().get("http"))
  526. opts = self._get_pool_options()
  527. if proxy:
  528. proxy_headers = self.options["proxy_headers"]
  529. if proxy_headers:
  530. opts["proxy_headers"] = proxy_headers
  531. if proxy.startswith("socks"):
  532. use_socks_proxy = True
  533. try:
  534. # Check if PySocks dependency is available
  535. from urllib3.contrib.socks import SOCKSProxyManager
  536. except ImportError:
  537. use_socks_proxy = False
  538. logger.warning(
  539. "You have configured a SOCKS proxy (%s) but support for SOCKS proxies is not installed. Disabling proxy support. Please add `PySocks` (or `urllib3` with the `[socks]` extra) to your dependencies.",
  540. proxy,
  541. )
  542. if use_socks_proxy:
  543. return SOCKSProxyManager(proxy, **opts)
  544. else:
  545. return urllib3.PoolManager(**opts)
  546. else:
  547. return urllib3.ProxyManager(proxy, **opts)
  548. else:
  549. return urllib3.PoolManager(**opts)
  550. def _request(
  551. self: "Self",
  552. method: str,
  553. endpoint_type: "EndpointType",
  554. body: "Any",
  555. headers: "Mapping[str, str]",
  556. ) -> "urllib3.BaseHTTPResponse":
  557. return self._pool.request(
  558. method,
  559. self._auth.get_api_url(endpoint_type),
  560. body=body,
  561. headers=headers,
  562. )
  563. try:
  564. import httpcore
  565. import h2 # noqa: F401
  566. except ImportError:
  567. # Sorry, no Http2Transport for you
  568. class Http2Transport(HttpTransport):
  569. def __init__(self: "Self", options: "Dict[str, Any]") -> None:
  570. super().__init__(options)
  571. logger.warning(
  572. "You tried to use HTTP2Transport but don't have httpcore[http2] installed. Falling back to HTTPTransport."
  573. )
  574. else:
  575. class Http2Transport(BaseHttpTransport): # type: ignore
  576. """The HTTP2 transport based on httpcore."""
  577. TIMEOUT = 15
  578. if TYPE_CHECKING:
  579. _pool: """Union[
  580. httpcore.SOCKSProxy, httpcore.HTTPProxy, httpcore.ConnectionPool
  581. ]"""
  582. def _get_header_value(
  583. self: "Self", response: "httpcore.Response", header: str
  584. ) -> "Optional[str]":
  585. return next(
  586. (
  587. val.decode("ascii")
  588. for key, val in response.headers
  589. if key.decode("ascii").lower() == header
  590. ),
  591. None,
  592. )
  593. def _request(
  594. self: "Self",
  595. method: str,
  596. endpoint_type: "EndpointType",
  597. body: "Any",
  598. headers: "Mapping[str, str]",
  599. ) -> "httpcore.Response":
  600. response = self._pool.request(
  601. method,
  602. self._auth.get_api_url(endpoint_type),
  603. content=body,
  604. headers=headers, # type: ignore
  605. extensions={
  606. "timeout": {
  607. "pool": self.TIMEOUT,
  608. "connect": self.TIMEOUT,
  609. "write": self.TIMEOUT,
  610. "read": self.TIMEOUT,
  611. }
  612. },
  613. )
  614. return response
  615. def _get_pool_options(self: "Self") -> "Dict[str, Any]":
  616. options: "Dict[str, Any]" = {
  617. "http2": self.parsed_dsn is not None
  618. and self.parsed_dsn.scheme == "https",
  619. "retries": 3,
  620. }
  621. socket_options = (
  622. self.options["socket_options"]
  623. if self.options["socket_options"] is not None
  624. else []
  625. )
  626. used_options = {(o[0], o[1]) for o in socket_options}
  627. for default_option in KEEP_ALIVE_SOCKET_OPTIONS:
  628. if (default_option[0], default_option[1]) not in used_options:
  629. socket_options.append(default_option)
  630. options["socket_options"] = socket_options
  631. ssl_context = ssl.create_default_context()
  632. ssl_context.load_verify_locations(
  633. self.options["ca_certs"] # User-provided bundle from the SDK init
  634. or os.environ.get("SSL_CERT_FILE")
  635. or os.environ.get("REQUESTS_CA_BUNDLE")
  636. or certifi.where()
  637. )
  638. cert_file = self.options["cert_file"] or os.environ.get("CLIENT_CERT_FILE")
  639. key_file = self.options["key_file"] or os.environ.get("CLIENT_KEY_FILE")
  640. if cert_file is not None:
  641. ssl_context.load_cert_chain(cert_file, key_file)
  642. options["ssl_context"] = ssl_context
  643. return options
  644. def _make_pool(
  645. self: "Self",
  646. ) -> "Union[httpcore.SOCKSProxy, httpcore.HTTPProxy, httpcore.ConnectionPool]":
  647. if self.parsed_dsn is None:
  648. raise ValueError("Cannot create HTTP-based transport without valid DSN")
  649. proxy = None
  650. no_proxy = self._in_no_proxy(self.parsed_dsn)
  651. # try HTTPS first
  652. https_proxy = self.options["https_proxy"]
  653. if self.parsed_dsn.scheme == "https" and (https_proxy != ""):
  654. proxy = https_proxy or (not no_proxy and getproxies().get("https"))
  655. # maybe fallback to HTTP proxy
  656. http_proxy = self.options["http_proxy"]
  657. if not proxy and (http_proxy != ""):
  658. proxy = http_proxy or (not no_proxy and getproxies().get("http"))
  659. opts = self._get_pool_options()
  660. if proxy:
  661. proxy_headers = self.options["proxy_headers"]
  662. if proxy_headers:
  663. opts["proxy_headers"] = proxy_headers
  664. if proxy.startswith("socks"):
  665. try:
  666. if "socket_options" in opts:
  667. socket_options = opts.pop("socket_options")
  668. if socket_options:
  669. logger.warning(
  670. "You have defined socket_options but using a SOCKS proxy which doesn't support these. We'll ignore socket_options."
  671. )
  672. return httpcore.SOCKSProxy(proxy_url=proxy, **opts)
  673. except RuntimeError:
  674. logger.warning(
  675. "You have configured a SOCKS proxy (%s) but support for SOCKS proxies is not installed. Disabling proxy support.",
  676. proxy,
  677. )
  678. else:
  679. return httpcore.HTTPProxy(proxy_url=proxy, **opts)
  680. return httpcore.ConnectionPool(**opts)
  681. class _FunctionTransport(Transport):
  682. """
  683. DEPRECATED: Users wishing to provide a custom transport should subclass
  684. the Transport class, rather than providing a function.
  685. """
  686. def __init__(
  687. self,
  688. func: "Callable[[Event], None]",
  689. ) -> None:
  690. Transport.__init__(self)
  691. self._func = func
  692. def capture_event(
  693. self,
  694. event: "Event",
  695. ) -> None:
  696. self._func(event)
  697. return None
  698. def capture_envelope(self, envelope: "Envelope") -> None:
  699. # Since function transports expect to be called with an event, we need
  700. # to iterate over the envelope and call the function for each event, via
  701. # the deprecated capture_event method.
  702. event = envelope.get_event()
  703. if event is not None:
  704. self.capture_event(event)
  705. def make_transport(options: "Dict[str, Any]") -> "Optional[Transport]":
  706. ref_transport = options["transport"]
  707. use_http2_transport = options.get("_experiments", {}).get("transport_http2", False)
  708. # By default, we use the http transport class
  709. transport_cls: "Type[Transport]" = (
  710. Http2Transport if use_http2_transport else HttpTransport
  711. )
  712. if isinstance(ref_transport, Transport):
  713. return ref_transport
  714. elif isinstance(ref_transport, type) and issubclass(ref_transport, Transport):
  715. transport_cls = ref_transport
  716. elif callable(ref_transport):
  717. warnings.warn(
  718. "Function transports are deprecated and will be removed in a future release."
  719. "Please provide a Transport instance or subclass, instead.",
  720. DeprecationWarning,
  721. stacklevel=2,
  722. )
  723. return _FunctionTransport(ref_transport)
  724. # if a transport class is given only instantiate it if the dsn is not
  725. # empty or None
  726. if options["dsn"]:
  727. return transport_cls(options)
  728. return None