Skip to content

Commit 5bdbd24

Browse files
mitsuhikountitaker
authored andcommitted
ref: Documentation improvements and removed some unused code (#51)
* ref: Make atexit print to stderr * ref: Kill most of minimal and update docstrings * ref: Documentation improvements and removed some unused code
1 parent 0f68f48 commit 5bdbd24

File tree

8 files changed

+158
-121
lines changed

8 files changed

+158
-121
lines changed

sentry_sdk/api.py

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1+
from contextlib import contextmanager
2+
13
from .hub import Hub
4+
from .scope import Scope
25
from .utils import EventHint
36
from .client import Client, get_options
47
from .integrations import setup_integrations
58

69

10+
__all__ = ["Hub", "Scope", "Client", "EventHint"]
11+
12+
13+
def public(f):
14+
__all__.append(f.__name__)
15+
return f
16+
17+
718
class _InitGuard(object):
819
def __init__(self, client):
920
self._client = client
@@ -25,8 +36,12 @@ def _init_on_hub(hub, args, kwargs):
2536
return _InitGuard(client)
2637

2738

39+
@public
2840
def init(*args, **kwargs):
29-
"""Initializes the SDK and optionally integrations."""
41+
"""Initializes the SDK and optionally integrations.
42+
43+
This takes the same arguments as the client constructor.
44+
"""
3045
return _init_on_hub(Hub.main, args, kwargs)
3146

3247

@@ -37,8 +52,62 @@ def _init_on_current(*args, **kwargs):
3752
return _init_on_hub(Hub.current, args, kwargs)
3853

3954

40-
from . import minimal as sentry_minimal
41-
from .minimal import * # noqa
55+
@public
56+
def capture_event(event, hint=None):
57+
"""Alias for `Hub.current.capture_event`"""
58+
hub = Hub.current
59+
if hub is not None:
60+
return hub.capture_event(event, hint)
61+
62+
63+
@public
64+
def capture_message(message, level=None):
65+
"""Alias for `Hub.current.capture_message`"""
66+
hub = Hub.current
67+
if hub is not None:
68+
return hub.capture_message(message, level)
69+
70+
71+
@public
72+
def capture_exception(error=None):
73+
"""Alias for `Hub.current.capture_exception`"""
74+
hub = Hub.current
75+
if hub is not None:
76+
return hub.capture_exception(error)
77+
78+
79+
@public
80+
def add_breadcrumb(*args, **kwargs):
81+
"""Alias for `Hub.current.add_breadcrumb`"""
82+
hub = Hub.current
83+
if hub is not None:
84+
return hub.add_breadcrumb(*args, **kwargs)
85+
86+
87+
@public
88+
def configure_scope(callback=None):
89+
"""Alias for `Hub.current.configure_scope`"""
90+
hub = Hub.current
91+
if hub is not None:
92+
return hub.configure_scope(callback)
93+
elif callback is None:
94+
95+
@contextmanager
96+
def inner():
97+
yield Scope()
98+
99+
return inner()
100+
101+
102+
@public
103+
def get_current_hub():
104+
"""Alias for `Hub.current`"""
105+
return Hub.current
106+
42107

43-
__all__ = ["Hub", "Scope", "Client", "EventHint", "init"]
44-
__all__ += sentry_minimal.__all__
108+
@public
109+
def last_event_id():
110+
"""Alias for `Hub.last_event_id`"""
111+
hub = Hub.current
112+
if hub is not None:
113+
return hub.last_event_id()

sentry_sdk/client.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ def get_options(*args, **kwargs):
4040

4141

4242
class Client(object):
43+
"""The client is internally responsible for capturing the events and
44+
forwarding them to sentry through the configured transport. It takes
45+
the client options as keyword arguments and optionally the DSN as first
46+
argument.
47+
"""
48+
4349
def __init__(self, *args, **kwargs):
4450
self.options = options = get_options(*args, **kwargs)
4551
self.transport = make_transport(options)
@@ -54,7 +60,7 @@ def __init__(self, *args, **kwargs):
5460

5561
@property
5662
def dsn(self):
57-
"""Returns the configured dsn."""
63+
"""Returns the configured DSN as string."""
5864
return self.options["dsn"]
5965

6066
def _prepare_event(self, event, hint, scope):
@@ -128,7 +134,15 @@ def _should_capture(self, event, hint=None, scope=None):
128134
return True
129135

130136
def capture_event(self, event, hint=None, scope=None):
131-
"""Captures an event."""
137+
"""Captures an event.
138+
139+
This takes the ready made event and an optoinal hint and scope. The
140+
hint is internally used to further customize the representation of the
141+
error. For more information see `EventHint`.
142+
143+
If the transport is not set nothing happens, otherwise the return
144+
value of this function will be the ID of the captured event.
145+
"""
132146
if self.transport is None:
133147
return
134148
rv = event.get("event_id")
@@ -143,6 +157,11 @@ def capture_event(self, event, hint=None, scope=None):
143157
def close(self, timeout=None, shutdown_callback=None):
144158
"""Closes the client which shuts down the transport in an
145159
orderly manner.
160+
161+
The `shutdown_callback` is invoked with two arguments: the number of
162+
pending events and the configured shutdown timeout. For instance the
163+
default atexit integration will use this to render out a message on
164+
stderr.
146165
"""
147166
if self.transport is not None:
148167
if timeout is None:

sentry_sdk/hub.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def _should_send_default_pii():
3535
class HubMeta(type):
3636
@property
3737
def current(self):
38+
"""Returns the current instance of the hub."""
3839
rv = _local.get(None)
3940
if rv is None:
4041
rv = Hub(GLOBAL_HUB)
@@ -43,6 +44,7 @@ def current(self):
4344

4445
@property
4546
def main(self):
47+
"""Returns the main instance of the hub."""
4648
return GLOBAL_HUB
4749

4850

@@ -68,6 +70,13 @@ def __exit__(self, exc_type, exc_value, tb):
6870

6971

7072
class Hub(with_metaclass(HubMeta)):
73+
"""The hub wraps the concurrency management of the SDK. Each thread has
74+
its own hub but the hub might transfer with the flow of execution if
75+
context vars are available.
76+
77+
If the hub is used with a with statement it's temporarily activated.
78+
"""
79+
7180
def __init__(self, client_or_hub=None, scope=None):
7281
if isinstance(client_or_hub, Hub):
7382
hub = client_or_hub
@@ -96,7 +105,7 @@ def run(self, callback):
96105
with statement can be used on the hub directly.
97106
"""
98107
with self:
99-
callback()
108+
return callback()
100109

101110
@property
102111
def client(self):

sentry_sdk/integrations/atexit.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import absolute_import
22

33
import os
4+
import sys
45
import atexit
56

67
from sentry_sdk.hub import Hub
@@ -9,9 +10,13 @@
910

1011

1112
def default_shutdown_callback(pending, timeout):
12-
print("Sentry is attempting to send %i pending error messages" % pending)
13-
print("Waiting up to %s seconds" % timeout)
14-
print("Press Ctrl-%s to quit" % (os.name == "nt" and "Break" or "C"))
13+
def echo(msg):
14+
sys.stderr.write(msg + "\n")
15+
16+
echo("Sentry is attempting to send %i pending error messages" % pending)
17+
echo("Waiting up to %s seconds" % timeout)
18+
echo("Press Ctrl-%s to quit" % (os.name == "nt" and "Break" or "C"))
19+
sys.stderr.flush()
1520

1621

1722
class AtexitIntegration(Integration):

sentry_sdk/minimal.py

Lines changed: 0 additions & 96 deletions
This file was deleted.

0 commit comments

Comments
 (0)