-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from planetlabs/sentry-monitor-latest
Adding get_latest endpoint to sentry monitoring
- Loading branch information
Showing
2 changed files
with
57 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from sentry_sdk import Hub, start_transaction | ||
import functools | ||
|
||
|
||
class SentryTransaction: | ||
''' | ||
Context manager for performance monitoring in sentry. Used by the | ||
monitor_performance decorator. You can also use this to track any | ||
block of code. Example: | ||
with sentry.SentryTransaction(op='MetisCaptureCalculator'): | ||
# tracked code here | ||
''' | ||
def __init__(self, name=None, tags=None, transaction=None, op='default'): | ||
self.op = op | ||
self.name = name | ||
self.tags = tags | ||
self.hub = Hub.current | ||
self.transaction = transaction or self.hub.scope.transaction | ||
self.span = self.hub.scope.span | ||
self.new_transaction = None | ||
|
||
def __enter__(self): | ||
if self.transaction is None: | ||
self.new_transaction = start_transaction( | ||
name=self.name, op=self.op) | ||
else: | ||
self.new_transaction = self.span.start_child( | ||
op=self.op, hub=self.hub | ||
) | ||
if self.tags: | ||
for key, value in self.tags.items(): | ||
self.new_transaction.set_tag(key, value) | ||
|
||
def __exit__(self, type, value, traceback): | ||
self.new_transaction.finish() | ||
|
||
|
||
def monitor_performance(op=None): | ||
''' | ||
Use @monitor_performance() decorator to add | ||
sentry performance monitoring to function. | ||
Use without arguments to use function name, | ||
or optionally add custom name seen in sentry | ||
''' | ||
def _monitor_performance(f): | ||
|
||
@functools.wraps(f) | ||
def wrapper(*args, **kwargs): | ||
op_name = op or f.__name__ | ||
with SentryTransaction(op=op_name): | ||
return f(*args, **kwargs) | ||
return wrapper | ||
return _monitor_performance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters