Skip to content

Commit 16dfdc5

Browse files
antonpirkerlizokm
andauthored
Updated backend tutorial (#10875)
Brought the Backend tutorial up to date to the new SDK api. --------- Co-authored-by: Liza Mock <[email protected]>
1 parent bf26076 commit 16dfdc5

File tree

4 files changed

+138
-99
lines changed

4 files changed

+138
-99
lines changed

docs/product/sentry-basics/integrate-backend/capturing-errors.mdx

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ If you're using your own source code, follow the instructions in [Getting Starte
1414

1515
## Unhandled Errors
1616

17-
The Sentry SDK will automatically capture and report any _unhandled error_ that happens in your application runtime without any additional configuration or explicit handling. Generally, unhandled errors are errors that aren't caught by any except (or try/catch) clause.
17+
The Sentry SDK will automatically capture and report any _unhandled error_ that occurs in your application runtime without any additional configuration or explicit handling. Generally, unhandled errors are errors that aren't caught by a `try/except` clause.
1818

19-
1. In your browser, launch the local Django app in the following endpoint to trigger an unhandled error: `http://localhost:8000/unhandled`.
19+
To trigger an unhandled error:
2020

21-
2. If you've set up an alert rule, you should be notified about the error. Otherwise, open the **Issues** page in your Sentry account.
21+
1. Run the local Django app.
2222

23-
3. Notice the unhandled exception appears in the list of issues.
23+
2. Point your browser to `http://localhost:8000/unhandled`.
24+
25+
2. If you've set up an [alert rule](/product/alerts/create-alerts/), you should be notified about the error. Otherwise, open the [**Issues**](https://sentry.io/orgredirect/organizations/:orgslug/issues/) page in your Sentry account.
26+
27+
3. You should see the unhandled exception in the list of issues.
2428

2529
![Unhandled Issue](./img/unhandled_issue.png)
2630

@@ -30,9 +34,9 @@ The Sentry SDK will automatically capture and report any _unhandled error_ that
3034

3135
5. Notice that the event:
3236

33-
- Is tagged with the `environment` and `release` options we've set in the previous tutorial and `handled:no`, marking this event as an unhandled error
34-
- Contains a suspect commit, made possible because of the commit tracking feature we enabled previously
35-
- Contains the custom breadcrumb we added through the SDK
37+
- Is tagged with the `environment` and `release` options we've set in the previous tutorial with the tag `handled:no`, marking this event as an unhandled error.
38+
- Contains a suspect commit, made possible because of the commit-tracking feature we enabled earlier.
39+
- Contains the custom breadcrumb we added through the SDK.
3640

3741
![Unhandled Breadcrumbs](./img/unhandled_issue_breadcrumbs.png)
3842

@@ -44,13 +48,23 @@ The Sentry SDK contains several methods that you can use to **explicitly** repor
4448

4549
1. Open the `views.py` file. Notice that we import `sentry_sdk` lib which contains the `capture_exception` method:
4650

47-
```python
48-
import sentry_sdk
51+
```python {filename: myapp/views.py}
52+
import sentry_sdk
4953
```
5054

5155
2. The method is used to capture the exception handled by the except clause in `HandledErrorView`:
5256

53-
![Import and Configure SDK](./img/capture_exception.png)
57+
```python {filename: myapp/views.py}
58+
class HandledErrorView(APIView):
59+
def get(self, request):
60+
...
61+
try:
62+
'2' + 2
63+
except Exception as err:
64+
sentry_sdk.capture_exception(err)
65+
66+
return Response()
67+
```
5468

5569
3. To try it out on your localhost, trigger the following endpoint: `http://localhost:8000/handled`.
5670

@@ -70,22 +84,26 @@ Typically, `capture_message` is not emitted, but there are times when a develope
7084

7185
2. You can use it anywhere within your app. In our example, we've created a dedicated view class, `CaptureMessageView`, to trigger and capture a message we want to track:
7286

73-
```Python
74-
sentry_sdk.capture_message("You caught me!")
87+
```python {filename: myapp/views.py}
88+
sentry_sdk.capture_message("You caught me!")
7589
```
7690

7791
3. To try it out on your localhost, trigger the following endpoint: `http://localhost:8000/message`.
7892

7993
4. As before, open the new issue’s detail page from the **Issues** page.
94+
<Alert>
95+
You'll need to remove the `"issue.priority is high or medium"` filter from the "Custom Search" field on the [**Issues**](https://sentry.io/orgredirect/organizations/:orgslug/issues/) page to see all the issue types in the list.
96+
</Alert>
97+
8098

8199
![Import and Configure SDK](./img/capture_message.png)
82100

83-
> By default captured messages are marked with a severity level tag `level:info`, as reflected in the tags section. However, the `capture_message` methods accept an **optional** severity level parameter.
101+
> By default, captured messages are marked with a severity level tag `level:info`, as reflected in the tags section. However, the `capture_message` methods accept an **optional** severity level parameter.
84102
85103
5. In the `views.py` file, change the `capture_message` method to:
86104

87-
```Python
88-
sentry_sdk.capture_message("You caught me!", "fatal")
105+
```python {filename: myapp/views.py}
106+
sentry_sdk.capture_message("You caught me!", "fatal")
89107
```
90108

91109
6. Save the changes and trigger the `/message` endpoint again. Changes should be applied immediately through `StateReloader`.
@@ -94,28 +112,27 @@ Typically, `capture_message` is not emitted, but there are times when a develope
94112

95113
## Enriching your Event Data
96114

97-
You can enrich your event and error data through the Sentry SDK by adding custom tags and user context attributes. In addition to providing more context to your errors, those will expand your options to search, filter, and query through your event metadata. Learn more about the advantages of enriching your data in [Event Data](/product/sentry-basics/enrich-data/).
115+
You can enrich your event and error data through the Sentry SDK by adding custom tags and user context attributes. In addition to providing more context to your errors, enriched data will expand your options to search, filter, and query through your event metadata. Learn more about the advantages of enriching your data in [Event Data](/product/sentry-basics/enrich-data/).
98116

99117
To enrich the data of the message events we've captured with `capture_message`:
100118

101119
1. In the `views.py` file, locate the line that triggers `sentry_sdk.capture_message`.
102120

103121
2. Replace that line with the following code:
104122

105-
```Python
106-
with sentry_sdk.push_scope() as scope:
107-
scope.set_tag("my-tag", "my value")
108-
scope.user = { "email" : "[email protected]" }
109-
scope.set_extra("someVariable", "some data")
123+
```python {filename: myapp/views.py}
124+
sentry_sdk.set_tag("my-tag", "my value")
125+
sentry_sdk.user = { "email" : "[email protected]" }
126+
sentry_sdk.set_extra("someVariable", "some data")
110127

111-
sentry_sdk.capture_message("You caught me!", "fatal")
128+
sentry_sdk.capture_message("You caught me!", "fatal")
112129
```
113130

114131
> We're using the `push_scope` method that allows us to send data with one specific event on a local scope. We're setting a custom tag, user context attribute (email), and extra data on the local scope to enrich the data on the message event.
115132
116133
3. Save your changes and trigger the `/message` endpoint again.
117134

118-
4. Open the issue’s detail page from the **Issues** page.
135+
4. Open the issue’s detail page from the [**Issues**](https://sentry.io/orgredirect/organizations/:orgslug/issues/) page.
119136

120137
5. Notice that:
121138

docs/product/sentry-basics/integrate-backend/configuration-options.mdx

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,84 +4,92 @@ sidebar_order: 2
44
description: "Learn how to configure releases, breadcrumbs, and environments to enhance the SDK's functionality."
55
---
66

7-
Sentry has various configuration options to help enhance the SDK functionality. The options can help provide additional data needed to debug issues even faster or help control what is sent to Sentry by filtering. Learn more in [Configuration](/platforms/python/configuration/).
7+
Sentry has various configuration options to help enhance the SDK functionality. The options can help provide additional data needed to debug issues even faster or to help control what's sent to Sentry by filtering. Learn more in [Configuration](/platforms/python/configuration/).
88

99
## Releases
1010

11-
A _release_ is a version of your code that is deployed to an environment. Configuring the release helps you figure out if there is a regression in your code, create accountability, resolve issues within Sentry, and stay up to date with your deployments. Releases need to be configured within your SDK and then managed through the [sentry-cli](/product/cli/).
11+
A _release_ is a version of your code that's deployed to an environment. Configuring the release helps you figure out if there's a regression in your code, ensure accountability, resolve issues within Sentry, and stay up-to-date with your deployments. Releases need to be configured within your SDK and then managed through the [sentry-cli](/product/cli/).
1212

1313
Sentry currently supports integrations with GitHub, Bitbucket, Azure DevOps, GitLab, and others. For a complete list of our integrations, check out our [Integrations documentation](/organization/integrations/).
1414

1515
To set up the release in this project:
1616

1717
1. Open the file `settings.py`. Notice that we add the `release` configuration option when initializing the SDK:
1818

19-
```python
20-
release=os.environ.get("VERSION"),
19+
```python {filename: myproject/settings.py}
20+
sentry_sdk.init(
21+
...
22+
release=os.environ.get("VERSION"),
23+
...
24+
)
2125
```
2226

2327
2. Open the `Makefile` you ran in the previous tutorial.
2428

2529
![Makefile](./img/makefile.png)
2630

27-
3. Notice that we're setting the release version name as an environment variable that is then used in the application's runtime. We're letting the CLI propose a release version name, but you'd probably want to apply your naming conventions:
31+
3. Notice that we're setting the release version name as an environment variable that's then used in the application's runtime. We're letting the CLI propose a release version name, but you probably want to apply your own naming conventions:
2832

29-
```bash
33+
```Makefile {filename: Makefile}
3034
VERSION=`sentry-cli releases propose-version`
3135
```
3236

33-
4. Then we create the new release for our project with the proposed or selected name.
37+
4. We then create a new release for our project with the proposed or selected name.
3438

35-
```bash
36-
> create_release:
37-
sentry-cli releases -o $(SENTRY_ORG) new -p $(SENTRY_PROJECT) $(VERSION)
39+
```Makefile {filename: Makefile}
40+
create_release:
41+
sentry-cli releases -o $(SENTRY_ORG) new -p $(SENTRY_PROJECT) $(VERSION)
3842
```
3943

4044
5. In the previous tutorial, we configured the GitHub integration and added the code repository for commit tracking. Now we can associate commits from that repository to the new release by running the command:
4145

42-
```bash
43-
> associate_commits:
44-
sentry-cli releases -o $(SENTRY_ORG) -p $(SENTRY_PROJECT) \
45-
set-commits $(VERSION) --auto
46+
```Makefile {filename: Makefile}
47+
associate_commits:
48+
sentry-cli releases -o $(SENTRY_ORG) -p $(SENTRY_PROJECT) \
49+
set-commits $(VERSION) --auto
4650
```
4751

4852
## Breadcrumbs
4953

50-
_Breadcrumbs_ are the trail of events that led up to the error. They can be quite useful when trying to reproduce an issue. Depending on the platform, the SDK will track various types of breadcrumbs by default (for backend SDKs those are DB queries, network events, logging, and others), and you can add custom breadcrumbs as well.
54+
_Breadcrumbs_ are the trail of events that led up to an error. They're very useful for reproducing an issue. Depending on the platform, the SDK will track various types of breadcrumbs by default (for backend SDKs these are DB queries, network events, logging, and others). You can also add your own custom breadcrumbs.
5155

5256
To add breadcrumbs to our app:
5357

5458
1. Open the file `myapp > view.py`.
5559

56-
2. Notice we import `add_breadcrumb` from the SDK lib.
60+
2. Notice that we import the `sentry_sdk` lib, which contains the `add_breadcrumb` method:
5761

58-
```python
59-
from sentry_sdk import add_breadcrumb
62+
```python {filename: myapp/views.py}
63+
import sentry_sdk
6064
```
6165

62-
3. We create a custom breadcrumb for each method handler in the view classes. This breadcrumb will be added to the trail of breadcrumbs associated with any error triggered through these method call flows. For instance, under `HandledErrorView:get`:
66+
3. We then create a custom breadcrumb for each method-handler in the view classes. This breadcrumb gets added to the trail of breadcrumbs associated with any error triggered through these method call flows. For instance, under `HandledErrorView.get`:
6367

64-
```python
65-
add_breadcrumb(
66-
category='URL Endpoints',
67-
message='In the handled function',
68-
level='info',
68+
```python {filename: myapp/views.py}
69+
sentry_sdk.add_breadcrumb(
70+
category='URL Endpoints',
71+
message='In the handled function',
72+
level='info',
6973
)
7074
```
7175

7276
## Environment
7377

74-
_Environment_ is a powerful configuration option that enables developers using Sentry to perform various workflows, such as filtering issues and triggering alerts, in the context of the deployment environment in which the errors occurred.
78+
_Environment_ is a powerful configuration option that enables developers using Sentry to perform various workflows, (such as filtering issues and triggering alerts) in the context of the deployment environment in which the errors occurred.
7579

7680
1. Open the `settings.py` file.
7781

7882
2. Notice that we initialize the SDK with the `environment` configuration option. Any event that's subsequently captured by the SDK will be tagged with the configured environment value.
7983

80-
```python
81-
environment:"Production"
84+
```python {filename: myproject/settings.py}
85+
sentry_sdk.init(
86+
...
87+
environment="Production"
88+
...
89+
)
8290
```
8391

84-
> Environment values are freeform strings. Neither the Sentry SDK nor [sentry.io](https://sentry.io) will not limit you to any specific value or format. In this example, we hardcoded the value. In a real-life app, the value would probably be determined dynamically through a properties file, system, or environment variable.
92+
> Environment values are freeform strings. Neither the Sentry SDK nor [sentry.io](https://sentry.io) will limit you to any specific value or format. In this example, we hardcoded the value. In a real-life app, the value would probably be determined dynamically through a properties file, system, or environment variable.
8593
8694
## Next
8795

0 commit comments

Comments
 (0)