Sentry Integration¤
Sentry.io is a platform for error-tracking and performance monitoring. Sentry's Python SDK enables automatic reporting of errors, exceptions and identifies performance issues.
Tip
For the quick start with Sentry.io, you can read the official documentation.
Sentry sends events (errors or transactions) to sentry.io together with additional event data (such as timestamps, server names, web browser names, stack traces, etc) and attachments (config or log files that are related to an error event). Similar events are grouped into issues. Every event has its fingerprint that Sentry uses to group events together. You can also add custom tags to each event and then filter on them. To create a trail of events that happened prior to an issue, Sentry uses breadcrumbs, which are very similar to logs, but can record more rich structured data.
Transactions are used for performance monitoring. A transaction represents the operation you want to measure or track, like a page load, page navigation, or asynchronous task. Transaction events are grouped by the transaction name. Moreover, you can monitor child tasks within a single transaction by creating child spans.
Configuration¤
When the Sentry Service is integrated to the ASAB microservice, it can be configured to send events to Sentry.io workspace.
After you create a new project in Sentry.io, DSN (data source name) is generated. You can either set the environment variable or fulfill DSN in the configuration.
You can set DSN in the configuration directly:
You can provide DSN as environment variable (which is safer, in general) in a .env
file.
Then use this variable in docker-compose.yaml
file.
my-asab-service:
image: my.asab.based.microservice
...
environment:
- SENTRY_DSN=${SENTRY_DSN}
In the configuration file, [sentry]
section may be empty, but it has to be there.
Other options available for sentry:
[sentry]
environment=production_hogwarts # will be visible as a tag 'environment'
[sentry:logging]
breadcrumbs=info # logging level for capturing breadcrumbs
events=notice # logging level for capturing events
Tip
If the application is properly containerized, other tags for Sentry.io are created automatically (using Manifest), such as:
appclass
, release
, server_name
, service_id
, instance_id
, node_id
, site_id
.
Integration¤
Sentry service is dependent on Python sentry_sdk
library.
import asab
class MyApplication(asab.Application):
def __init__(self):
super().__init__()
if "sentry" in asab.Config.sections():
import asab.sentry as asab_sentry
self.SentryService = asab_sentry.SentryService(self)
After the service is initialized:
- all uncaught exceptions are sent as events
- all logging messages with priority
ERROR
or higher are sent as events, messages with priorityINFO
or higher are sent as breadcrumbs
Capturing errors¤
As mentioned above, uncaught exceptions and errors are sent automatically to Sentry.
To capture caught exception and send it as an event, use capture_exception()
method.
To capture a custom message and send it as an event, use capture_message()
method.
For grouping issues together and filtering, you can add custom tags. Tags are set only within the current scope (method, class, module).
def my_function():
sentry_service.set_tag("method", "my_function")
sentry_service.set_tags({"foo": "bar", "fooz": "buzz"})
Info
Tag names and values cannot be arbitrary strings.
Tag keys can have a maximum length of 32 characters and can contain only letters (a-zA-Z), numbers (0-9), underscores (_), periods (.), colons (:), and dashes (-).
Tag values can have a maximum length of 200 characters and they cannot contain the newline (\n) character.
If you try to add a tag with invalid format, it won't be set and error message will be displayed.
Performance monitoring¤
To create new transaction for performance monitoring, use the context manager transaction
:
To create a child span, use the context manager span
:
with sentry_svc.transaction("speed test", "multiple tasks"):
prepare_task1()
with sentry_svc.span("task", "task1"):
task1()
prepare_task2()
with sentry_svc.span("task", "task2"):
task2()
finalize()
Reference¤
asab.sentry.SentryService
¤
Bases: Service
Service for Sentry SDK integration.
Sentry is an error tracking and performance monitoring platform.
When the service is initialized and data_source_name
is set,
all unhandled exceptions, error log messages are sent as Events to sentry.io,
together with lines of code where the error happened, structured data and values of variables.
Configuration:
Examples:
class MyApp(asab.Application):
async def initialize(self):
if "sentry" in asab.Config.sections():
import asab.sentry
self.SentryService = asab.sentry.SentryService(self)
Source code in asab/sentry/service.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
|
capture_exception(error=None, scope=None, **scope_args)
¤
Capture caught exception and send it to Sentry.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
error
|
str
|
Error message that will be sent. If not specified, the one currently held in |
None
|
Examples:
Source code in asab/sentry/service.py
capture_message(message, level=None, scope=None, **scope_args)
¤
Send textual information to Sentry.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
Text message that will be sent. |
required |
Source code in asab/sentry/service.py
set_context(section_name, context)
¤
Set custom context within the current scope.
Context data are related to the current user and the environment. They are useful when you have all the data available at a single point in time, not well suited to data that is collected over time.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
section_name
|
str
|
A unique name for the context. |
required |
context
|
dict
|
Key-value paired context dictionary. |
required |
Examples:
Note
We prefer using logging messages to setting custom context.
Source code in asab/sentry/service.py
set_tag(key, value)
¤
Add custom tag to the current scope.
Tags are key-value string pairs that are both indexed and searchable. They can help you quickly both access related events and view the tag distribution for a set of events.
Tag is set only for the current scope (function, method, class, module).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
Tag key. Tag keys can have a maximum length of 32 characters and can contain only letters (a-zA-Z), numbers (0-9), underscores (_), periods (.), colons (:), and dashes (-). |
required |
value
|
Union[str, int]
|
Tag value. Tag values can have a maximum length of 200 characters and they cannot contain the newline ( |
required |
Source code in asab/sentry/service.py
set_tags(tags_dict)
¤
Add multiple custom tags to the current scope.
Tags are key-value string pairs that are both indexed and searchable. They can help you quickly both access related events and view the tag distribution for a set of events.
Tags are set only for the current scope (function, method, class, module).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tags_dict
|
dict
|
Dictionary of tag keys and values. |
required |
Source code in asab/sentry/service.py
span(operation, description)
¤
Create a child span within custom transaction.
This method is used as a context manager.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
operation
|
str
|
Displayed span operation name that cannot be filtered, e.g., 'task'. |
required |
description
|
str
|
Displayed span name that can be filtered. |
required |
Returns:
Name | Type | Description |
---|---|---|
Span |
A context manager that measures time operation of the task inside. |
Examples:
with sentry_svc.transaction("speed test", "multiple tasks"):
prepare_task1()
with sentry_svc.span("task", "task1"):
task1()
prepare_task2()
with sentry_svc.span("task", "task2"):
task2()
finalize()
Source code in asab/sentry/service.py
transaction(span_operation, span_name)
¤
Start a new transaction.
Transactions are used for performance monitoring.
This method is used as a context manager.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
span_operation
|
str
|
Displayed span operation name that cannot be filtered, e.g., 'task'. |
required |
span_name
|
str
|
Displayed span name that can be filtered. |
required |
Returns:
Name | Type | Description |
---|---|---|
Transaction |
A context manager that measures time operation of the task inside. |
Examples: