2
2
import uuid
3
3
import random
4
4
from datetime import datetime
5
+ import socket
5
6
6
7
from sentry_sdk ._compat import string_types , text_type , iteritems
7
8
from sentry_sdk .utils import (
17
18
from sentry_sdk .integrations import setup_integrations
18
19
from sentry_sdk .utils import ContextVar
19
20
20
- MYPY = False
21
+ from sentry_sdk ._types import MYPY
22
+
21
23
if MYPY :
22
24
from typing import Any
23
25
from typing import Callable
24
26
from typing import Dict
25
27
from typing import Optional
26
28
27
29
from sentry_sdk .scope import Scope
28
- from sentry_sdk .utils import Event , Hint
30
+ from sentry_sdk ._types import Event , Hint
29
31
30
32
31
33
_client_init_debug = ContextVar ("client_init_debug" )
@@ -58,6 +60,9 @@ def _get_options(*args, **kwargs):
58
60
if rv ["environment" ] is None :
59
61
rv ["environment" ] = os .environ .get ("SENTRY_ENVIRONMENT" )
60
62
63
+ if rv ["server_name" ] is None and hasattr (socket , "gethostname" ):
64
+ rv ["server_name" ] = socket .gethostname ()
65
+
61
66
return rv # type: ignore
62
67
63
68
@@ -206,17 +211,20 @@ def _should_capture(
206
211
207
212
return True
208
213
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]
211
221
"""Captures an event.
212
222
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.
217
226
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.
220
228
"""
221
229
if self .transport is None :
222
230
return None
@@ -233,26 +241,33 @@ def capture_event(self, event, hint=None, scope=None):
233
241
self .transport .capture_event (event )
234
242
return rv
235
243
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
238
250
"""
239
251
Close the client and shut down the transport. Arguments have the same
240
- semantics as `self .flush() `.
252
+ semantics as :py:meth:`Client .flush`.
241
253
"""
242
254
if self .transport is not None :
243
255
self .flush (timeout = timeout , callback = callback )
244
256
self .transport .kill ()
245
257
self .transport = None
246
258
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
249
265
"""
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.
252
269
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.
256
271
"""
257
272
if self .transport is not None :
258
273
if timeout is None :
@@ -268,7 +283,8 @@ def __exit__(self, exc_type, exc_value, tb):
268
283
self .close ()
269
284
270
285
271
- MYPY = False
286
+ from sentry_sdk ._types import MYPY
287
+
272
288
if MYPY :
273
289
# Make mypy, PyCharm and other static analyzers think `get_options` is a
274
290
# type to have nicer autocompletion for params.
0 commit comments