22import uuid
33import random
44from datetime import datetime
5+ import socket
56
67from sentry_sdk ._compat import string_types , text_type , iteritems
78from sentry_sdk .utils import (
1718from sentry_sdk .integrations import setup_integrations
1819from sentry_sdk .utils import ContextVar
1920
20- MYPY = False
21+ from sentry_sdk ._types import MYPY
22+
2123if MYPY :
2224 from typing import Any
2325 from typing import Callable
2426 from typing import Dict
2527 from typing import Optional
2628
2729 from sentry_sdk .scope import Scope
28- from sentry_sdk .utils import Event , Hint
30+ from sentry_sdk ._types import Event , Hint
2931
3032
3133_client_init_debug = ContextVar ("client_init_debug" )
@@ -58,6 +60,9 @@ def _get_options(*args, **kwargs):
5860 if rv ["environment" ] is None :
5961 rv ["environment" ] = os .environ .get ("SENTRY_ENVIRONMENT" )
6062
63+ if rv ["server_name" ] is None and hasattr (socket , "gethostname" ):
64+ rv ["server_name" ] = socket .gethostname ()
65+
6166 return rv # type: ignore
6267
6368
@@ -206,17 +211,20 @@ def _should_capture(
206211
207212 return True
208213
209- def capture_event (self , event , hint = None , scope = None ):
210- # type: (Dict[str, Any], Optional[Any], Optional[Scope]) -> Optional[str]
214+ def capture_event (
215+ self ,
216+ event , # type: Event
217+ hint = None , # type: Optional[Hint]
218+ scope = None , # type: Optional[Scope]
219+ ):
220+ # type: (...) -> Optional[str]
211221 """Captures an event.
212222
213- This takes the ready made event and an optional hint and scope. The
214- hint is internally used to further customize the representation of the
215- error. When provided it's a dictionary of optional information such
216- as exception info.
223+ :param event: A ready-made event that can be directly sent to Sentry.
224+
225+ :param hint: Contains metadata about the event that can be read from `before_send`, such as the original exception object or a HTTP request object.
217226
218- If the transport is not set nothing happens, otherwise the return
219- value of this function will be the ID of the captured event.
227+ :returns: An event ID. May be `None` if there is no DSN set or of if the SDK decided to discard the event for other reasons. In such situations setting `debug=True` on `init()` may help.
220228 """
221229 if self .transport is None :
222230 return None
@@ -233,26 +241,33 @@ def capture_event(self, event, hint=None, scope=None):
233241 self .transport .capture_event (event )
234242 return rv
235243
236- def close (self , timeout = None , callback = None ):
237- # type: (Optional[float], Optional[Callable[[int, float], None]]) -> None
244+ def close (
245+ self ,
246+ timeout = None , # type: Optional[float]
247+ callback = None , # type: Optional[Callable[[int, float], None]]
248+ ):
249+ # type: (...) -> None
238250 """
239251 Close the client and shut down the transport. Arguments have the same
240- semantics as `self .flush() `.
252+ semantics as :py:meth:`Client .flush`.
241253 """
242254 if self .transport is not None :
243255 self .flush (timeout = timeout , callback = callback )
244256 self .transport .kill ()
245257 self .transport = None
246258
247- def flush (self , timeout = None , callback = None ):
248- # type: (Optional[float], Optional[Callable[[int, float], None]]) -> None
259+ def flush (
260+ self ,
261+ timeout = None , # type: Optional[float]
262+ callback = None , # type: Optional[Callable[[int, float], None]]
263+ ):
264+ # type: (...) -> None
249265 """
250- Wait `timeout` seconds for the current events to be sent. If no
251- `timeout` is provided, the `shutdown_timeout` option value is used.
266+ Wait for the current events to be sent.
267+
268+ :param timeout: Wait for at most `timeout` seconds. If no `timeout` is provided, the `shutdown_timeout` option value is used.
252269
253- The `callback` is invoked with two arguments: the number of pending
254- events and the configured timeout. For instance the default atexit
255- integration will use this to render out a message on stderr.
270+ :param callback: Is invoked with the number of pending events and the configured timeout.
256271 """
257272 if self .transport is not None :
258273 if timeout is None :
@@ -268,7 +283,8 @@ def __exit__(self, exc_type, exc_value, tb):
268283 self .close ()
269284
270285
271- MYPY = False
286+ from sentry_sdk ._types import MYPY
287+
272288if MYPY :
273289 # Make mypy, PyCharm and other static analyzers think `get_options` is a
274290 # type to have nicer autocompletion for params.
0 commit comments