You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: develop-docs/backend/api/design.mdx
+39-41Lines changed: 39 additions & 41 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,8 +31,8 @@ Use the following guidelines for naming resources and their collections:
31
31
32
32
-**Do** use lowercase and hyphenated collection names, e.g. `commit-files`.
33
33
-**Do** use plural collection names. Avoid using uncountable words because the user can't know whether the GET returns one item or a list.
34
-
-**Do** use `snake_case` for path parameters. e.g. `tags/\{tag_name}/`.
35
-
-**Do** consistently shorten parameters that are excessively long when the term will unambiguous. e.g. `organization` -> `org`.
34
+
-**Do** use `snake_case` for path parameters. e.g. `tags/{tag_name}/`.
35
+
-**Do** consistently shorten parameters that are excessively long when the term is unambiguous. e.g. `organization` -> `org`.
36
36
37
37
Standard path parameters that should be shortened in routes:
38
38
@@ -42,8 +42,8 @@ Standard path parameters that should be shortened in routes:
42
42
43
43
Information in Sentry is typically constrained by tenants. That is, almost all information is scoped to an organization. All endpoints which query customer data **must** be scoped to an organization:
44
44
45
-
-**Do** prefix resource organizations collections with `organizations/\{org}`.
46
-
-**Do** prefix resource project collections with `projects/\{org}/\{project}`.
45
+
-**Do** prefix organization resource collections with `/organizations/{org}/`.
46
+
-**Do** prefix project resource collections with `/projects/{org}/{project}/`.
47
47
-**Do not** expose endpoints which require `org` as a query parameter (it should always be a path parameter).
48
48
49
49
Knowing when to choose which constraint to couple an endpoint to will be based on the purpose of an endpoint. For example, if an endpoint is only ever going to be used to query data for a single project, it should be prefixed with `/projects/{org}/{project}/things`. If an endpoint would need to exist to query multiple projects (which is common with cross-project queries), you likely should expose it as `/organizations/{org}/things`, and expose a query param to filter on the project(s).
@@ -57,34 +57,34 @@ Exceptions to these rules include:
57
57
58
58
**Do not** exceed three levels of resource nesting.
59
59
60
-
Nesting resources such as `/organizations/\{org}/projects/`, is **preferred** over flattened resources like `/0/projects/`. This improves readability and exposes a natural understanding of resource hierarchy and relationships. However, nesting can make URLs too long and hard to use. Sentry uses 3-level nesting as a hybrid solution.
60
+
Nesting resources such as `/organizations/{org}/projects/`, is **preferred** over flattened resources like `/0/projects/`. This improves readability and exposes a natural understanding of resource hierarchy and relationships. However, nesting can make URLs too long and hard to use. Sentry uses 3-level nesting as a hybrid solution.
61
61
62
62
Here are some possible urls for values with this resource hierarchy: organization -> project -> tag -> value:
Hierarchy here does not necessarily mean that one collection belongs to a parent collection, it simply implies a relationship. For example:
69
69
70
-
-`projects/\{project_identifier}/teams/` refers to the **teams** that have been added to specific project
71
-
-`teams/\{team_identifier}/projects/` refers to the **projects** a specific team has been added to
70
+
-`/projects/{project_identifier}/teams/` refers to the **teams** that have been added to specific project
71
+
-`/teams/{team_identifier}/projects/` refers to the **projects** a specific team has been added to
72
72
73
73
## Parameter Design
74
74
75
75
-**Do** use `camelCase` for query params and request body params. e.g. `/foo/?userId=123`.
76
-
-**Do** use `camelCase` for all response attributes. e.g. `\{userId: "123"}`.
76
+
-**Do** use `camelCase` for all response attributes. e.g. `{userId: "123"}`.
77
77
78
78
For consistency, we also try to re-use well known parameters across endpoints.
79
79
80
-
-**Do** use `sortBy` for sorting. e.g. `sortBy=-dateCreated`.
81
-
-**Do** use `orderBy` for ordering. e.g. `orderBy=asc` or `orderBy=desc`.
82
-
-**Do** use `limit` for limiting the number of results returned. e.g. `limit=10`.
80
+
-**Do** use `sortBy` for sorting. e.g. `?sortBy=-dateCreated`.
81
+
-**Do** use `orderBy` for ordering. e.g. `?orderBy=asc` or `?orderBy=desc`.
82
+
-**Do** use `limit` for limiting the number of results returned. e.g. `?limit=10`.
83
83
-**Do** use `cursor` for pagination.
84
84
85
85
### Resource Identifiers
86
86
87
-
Identifiers exist both within the route (`/organizations/\{organization}/projects/`) as well as within other parameters such as query strings (`organization=123`) and request bodies (`\{organization: "123"}`).
87
+
Identifiers exist both within the route (`/organizations/{organization}/projects/`) as well as within other parameters such as query strings (`?organization=123`) and request bodies (`{organization: "123"}`).
88
88
89
89
The most important concern here is to ensure that a single identifier is exposed to key to resources. For example, it is preferred to use `organization` and accept both `organization_id` and `organization_slug` as valid identifiers.
90
90
@@ -119,24 +119,24 @@ POST /resources/{id}
119
119
120
120
### Batch Operations
121
121
122
-
Resources can get complicated when you need to expose batch operations vs single resource operations. For batch operations it it is preferred to expose them as a `POST` request on the collection when possible.
122
+
Resources can get complicated when you need to expose batch operations vs single resource operations. For batch operations it is preferred to expose them as a `POST` request on the collection when possible.
123
123
124
124
Let's say for example we have an endpoint that mutates an issue:
125
125
126
126
```
127
-
POST /api/0/organizations/:org/issues/:issue/
127
+
POST /api/0/organizations/{org}/issues/{issue}/
128
128
```
129
129
130
130
When designing a batch interface, we simply expose it on the collection instead of the individual resource:
131
131
132
132
```
133
-
POST /api/0/organizations/:org/issues/
133
+
POST /api/0/organizations/{org}/issues/
134
134
```
135
135
136
136
You may also need to expose selectors on batch resources, which can be done through normal request parameters:
137
137
138
138
```
139
-
POST /api/0/organizations/:org/issues/
139
+
POST /api/0/organizations/{org}/issues/
140
140
{
141
141
"issues": [1, 2, 3]
142
142
}
@@ -166,7 +166,7 @@ Here are some examples of how to use standard methods to represent complex tasks
166
166
167
167
**Retrieve statistics for a resource**
168
168
169
-
The best approach here is to encoded it as an attribute in the resource:
169
+
The best approach here is to encode it as an attribute in the resource:
170
170
171
171
```
172
172
GET /api/0/projects/{project}/
@@ -182,7 +182,7 @@ In some cases this will be returned as part of an HTTP header, specifically for
182
182
183
183
Order and filtering should happen as part of list api query parameters. Here's a [good read](https://www.moesif.com/blog/technical/api-design/REST-API-Design-Filtering-Sorting-and-Pagination/).
184
184
185
-
-**Do** rely on `orderBy` and `sortBy`. e.g. `/api/0/issues/\{issue_id}/events?orderBy=-date`
185
+
-**Do** rely on `orderBy` and `sortBy`. e.g. `/api/0/issues/{issue_id}/events?orderBy=-date`
186
186
-**Do not** create dedicated routes for these behaviors.
187
187
188
188
## Responses
@@ -191,13 +191,13 @@ Each response object returned from an API should be a serialized version of the
191
191
192
192
Some guidelines around the shape of responses:
193
193
194
-
-**Do** use `camelCase` for all response attributes. e.g. `\{numCount: "123"}`.
195
-
-**Do** return a responses as a named resource (e.g. `\{"user": \{"id": "123"}}`).
196
-
-**Do** indicate collections using plural nouns (e.g. `\{"users": []}`).
194
+
-**Do** use `camelCase` for all response attributes. e.g. `{"numCount": "123"}`.
195
+
-**Do** return a responses as a named resource (e.g. `{"user": {"id": "123"}}`).
196
+
-**Do** indicate collections using plural nouns (e.g. `{"users": []}`).
197
197
-**Do not** return custom objects. **Do** use a `Serializer` to serialize the resource.
198
198
-**Do** return the smallest amount of data necessary to represent the resource.
199
199
200
-
Additionally because JavaScript is a primary consumer, be mindful of the restrictions are things like numbers. Generally speaking:
200
+
Additionally because JavaScript is a primary consumer, be mindful of the restrictions on things like numbers. Generally speaking:
201
201
202
202
-**Do** return resource identifiers (even numbers) as strings.
203
203
-**Do** return decimals as strings.
@@ -222,7 +222,7 @@ Whereas our guidelines state it should be nested:
222
222
GET /api/0/projects/{project}/
223
223
{
224
224
"project": {
225
-
"id": 5,
225
+
"id": "5",
226
226
"name": "foo",
227
227
...
228
228
}
@@ -273,13 +273,13 @@ GET /api/0/projects/{project}/teams
273
273
[
274
274
{
275
275
"id": 1,
276
-
"name": "Team 1",
277
-
"slug": "team1",
276
+
"name": "Team 1",
277
+
"slug": "team1",
278
278
},
279
-
{
279
+
{
280
280
"id": 2,
281
-
"name": "Team 2",
282
-
"slug": "team2",
281
+
"name": "Team 2",
282
+
"slug": "team2",
283
283
}
284
284
]
285
285
@@ -297,17 +297,11 @@ GET /api/0/projects/{project}/
297
297
"id": 5,
298
298
"name": "foo",
299
299
"stats": {
300
-
"24h": [
301
-
[
302
-
1629064800,
303
-
27
304
-
],
305
-
[
306
-
1629068400,
307
-
24
308
-
],
309
-
...
310
-
]
300
+
"24h": [
301
+
[1629064800, 27],
302
+
[1629068400, 24],
303
+
...
304
+
]
311
305
}
312
306
}
313
307
```
@@ -330,7 +324,9 @@ This is typically only needed if the endpoint is already public and we do not wa
330
324
>> APIs often need to provide collections of data, most commonly in the `List` standard method. However, collections can be arbitrarily sized, and tend to grow over time, increasing lookup time as well as the size of the responses being sent over the wire. This is why it's important for collections to be paginated.
331
325
332
326
Paginating responses is a [standard practice for APIs](https://google.aip.dev/158), which Sentry follows.
327
+
333
328
We've seen an example of a `List` endpoint above; these endpoints have two tell-tale signs:
329
+
334
330
```json
335
331
GET /api/0/projects/{project}/teams
336
332
[
@@ -347,12 +343,14 @@ GET /api/0/projects/{project}/teams
347
343
]
348
344
349
345
```
346
+
350
347
1. The endpoint returns an array, or multiple, objects instead of just one.
351
348
2. The endpoint can sometimes end in a plural (s), but more importantly, it does __not__ end in an identifier (`*_slug`, or `*_id`).
352
349
353
350
To paginate a response at Sentry, you can leverage the [`self.paginate`](https://github.com/getsentry/sentry/blob/24.2.0/src/sentry/api/base.py#L463-L476) method as part of your endpoint.
354
351
`self.paginate` is the standardized way we paginate at Sentry, and it helps us with unification of logging and monitoring.
355
352
You can find multiple [examples of this](https://github.com/getsentry/sentry/blob/24.2.0/src/sentry/api/endpoints/api_applications.py#L22-L33) in the code base. They'll look something like:
Copy file name to clipboardExpand all lines: develop-docs/backend/application-domains/database-migrations/index.mdx
+4-3Lines changed: 4 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -135,11 +135,12 @@ generally prefer that, since it averages the load out over a longer period of ti
135
135
136
136
### Indexes
137
137
138
-
We prefer to create indexes on large existing tables with `CREATE INDEX CONCURRENTLY`. Our migration framework will do this automatically when creating
139
-
a new index. Note that when `CONCURRENTLY` is used we can't run the migration in a transaction, so it's important to use `atomic = False` to run these.
138
+
These are automatically handled by the migration framework, you can just create them on the Django model and generate the migration.
140
139
141
140
When adding indexes to large tables you should use a `is_post_deployment` migration as creating the index could take longer than the migration statement timeout of 5s.
142
141
142
+
Note: These are created using `CONCURRENTLY`, so it's important to not set `atomic = True` for migrations that contain indexes. This is disabled by default.
143
+
143
144
### Deleting columns
144
145
145
146
This is complicated due to our deploy process. When we deploy, we run migrations, and then push out the application code, which takes a while. This means that if we just delete a column or model, then code in sentry will be looking for those columns/tables and erroring until the deploy completes. In some cases, this can mean Sentry is hard down until the deploy is finished.
@@ -419,7 +420,7 @@ This second PR will contain only the migration and related boilerplate.
419
420
420
421
### Foreign Keys
421
422
422
-
Creating foreign keys is mostly fine, but for some large/busy tables like `Project`, `Group`it can cause problems due to difficulties in acquiring a lock. You can still create a Django level foreign key though, without creating a database constraint. To do so, set `db_constraint=False` when defining the key.
423
+
Creating foreign keys is mostly fine, but for some large/busy tables like `Project` and `Group`the migration can fail due to difficulties in acquiring a lock on the table. The general procedure here is to retry the migration until it goes through - The migration framework adds a lock acquisition timeout, so it's safe to do this.
Copy file name to clipboardExpand all lines: develop-docs/development-infrastructure/devservices.mdx
+44-5Lines changed: 44 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -84,23 +84,62 @@ devservices up --mode symbolicator
84
84
85
85
## Running a dependency locally
86
86
87
-
If you want to run a dependency locally rather than as a container, you can do so by toggling the runtime to LOCAL.
87
+
You can run a dependency locally instead of as a container by toggling its runtime via the `devservices toggle` command.
88
88
89
-
For example, if you are running Sentry and want to use your local Snuba, you can do the following:
89
+
### Toggle to local runtime
90
+
91
+
For example, if you're running Sentry and want to run Snuba **locally** from source, you can toggle Snuba to the local runtime like this:
92
+
93
+
```shell
94
+
devservices toggle snuba
95
+
```
96
+
97
+
or you can specify the runtime explicitly:
90
98
91
99
```shell
92
100
devservices toggle snuba LOCAL
93
101
```
94
102
95
-
This will tell devservices to not bring up snuba and its dependencies, allowing you to run snuba locally instead.
103
+
This tells devservices to treat Snuba as a service that should be started alongside dependent services like Sentry. Dependencies of Snuba, such as Redis, Kafka, and Clickhouse, will still run as containers.
104
+
105
+
<Alert title="Note">
106
+
Toggling to LOCAL does not start the dev server for that service. You'll need to start that manually.
107
+
</Alert>
108
+
109
+
### What happens when you toggle
110
+
111
+
If Sentry is already running, toggling Snuba to LOCAL will:
112
+
1. Stop Snuba's containers (unless shared by another service)
113
+
2. Start Snuba's non-local dependencies (Redis, Kafka, Clickhouse) as containers
114
+
115
+
If Sentry is not yet running, the next time `devservices up` is run, Snuba (and any other services you toggle to LOCAL) will be automatically started as local services alongside Sentry.
116
+
117
+
If you want to bring up Snuba, or other local runtime dependencies in a non-default mode, you can:
118
+
1. Tell devservices to not bring them up automatically by passing the `--exclude-local` flag to `devservices up`.
119
+
2. Bring that mode up manually via `devservices up --mode <mode>` since modes are additive.
120
+
3. Bring that service down and back up again with the new mode.
121
+
122
+
### Bringing down services with local runtimes
123
+
124
+
When stopping services with `devservices down`, the default behavior is to stop all services and their dependencies, including dependencies with local runtimes.
125
+
126
+
If you don't want dependencies with local runtimes to be brought down when bringing down a dependent service, you can pass the `--exclude-local` flag to `devservices down`. This tells devservices to only stop dependencies that are not running with local runtimes.
127
+
128
+
### Toggle back to containerized runtime
129
+
130
+
To switch a service back to running in a container, you can do the following:
131
+
132
+
```shell
133
+
devservices toggle snuba
134
+
```
96
135
97
-
To toggle the runtime back to using the container, you can do the following:
136
+
Or explicitly specify the runtime:
98
137
99
138
```shell
100
139
devservices toggle snuba CONTAINERIZED
101
140
```
102
141
103
-
If you don't provide a runtime when toggling, it will toggle to the opposite of the current runtime.
142
+
Replace `snuba` with the name of the service you want to toggle.
104
143
105
144
## Migrating data from the deprecated `sentry devservices`
When using `render()`, an in-memory router is used, which will react to navigations with `useNavigate()` or interations with `Link` components. If your component relies on the URL, you can define the initial state in `initialRouterConfig`. You can access the current router state by referencing the returned `router` class, as well as navigate programmatically with `router.navigate()`.
0 commit comments